Skip to content

Commit

Permalink
Fix faulty shell file handling
Browse files Browse the repository at this point in the history
Parsing errors are meant to be discarded but aren't. This patch
changes the code so that the error is indeed discarded and checking
continues as intended and adds a unit test for it.

Signed-off-by: Arnaud J Le Hors <[email protected]>
  • Loading branch information
lehors committed Nov 22, 2021
1 parent 71e8698 commit 50be3da
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions checks/shell_download_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,7 @@ func validateShellFile(pathfn string, content []byte, dl checker.DetailLogger) (
if err != nil && errors.Is(err, sce.ErrorShellParsing) {
// Discard and print this particular error for now.
dl.Debug(err.Error())
err = nil
}
return r, err
}
19 changes: 19 additions & 0 deletions checks/shell_download_validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package checks
import (
"os"
"testing"

scut "github.com/ossf/scorecard/v3/utests"
)

func TestIsSupportedShellScriptFile(t *testing.T) {
Expand Down Expand Up @@ -86,3 +88,20 @@ func TestIsSupportedShellScriptFile(t *testing.T) {
})
}
}

func TestValidateShellFile(t *testing.T) {
t.Parallel()
filename := "testdata/script-invalid.sh"
var content []byte
var err error

content, err = os.ReadFile(filename)
if err != nil {
t.Errorf("cannot read file: %v", err)
}
dl := scut.TestDetailLogger{}
_, err = validateShellFile(filename, content, &dl)
if err != nil {
t.Errorf("failed to discard shell parsing error: %v", err)
}
}
17 changes: 17 additions & 0 deletions checks/testdata/script-invalid.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/sh
# Copyright 2021 Security Scorecard Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# syntax error: unexpected token 'fi'
fi

0 comments on commit 50be3da

Please sign in to comment.