Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Only validate shell scripts supported by our parser #862

Merged
merged 3 commits into from
Aug 19, 2021

Conversation

ristomcgehee
Copy link
Contributor

  • Please check if the PR fulfills these requirements
  • What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)
    Bug fix

  • What is the current behavior? (You can also link to an open issue here)
    The pinned dependency check is failing on some repos because they contain shell scripts that our not supported by the parser we are using. For example, https://github.com/u-boot/u-boot/blob/master/scripts/ld-version.sh is an awk file that we are trying to parse as a shell script.
    Part of BUG: Parsing errors #839

  • What is the new behavior (if this is a feature change)?
    The parser we are using, https://github.com/mvdan/sh, supports parsing the following scripts: bash, sh, and mksh. For this PR, I am only parsing a file if it is using of those 3 shells. If a shebang is present in the file, I base the decision to parse entirely on it, otherwise I use the file extension.

  • Does this PR introduce a breaking change? (What changes might users need to make in their application due to this PR?)
    No

  • Other information:
    This PR makes it so that some shell scripts (dash, ksh, etc.) will no longer be checked for pinned dependencies. I think this is the most correct thing to do since our parser does not support these shells. If we don't want to lose the checking of these shells, what we could do is attempt to parse these shells, and if parsing fails, swallow the error and continue with the check. This should work ~99% of the time, and in the 1% of times where it doesn't work, the check will run without giving any errors. Let me know if you'd prefer this approach instead.

Comment on lines 518 to 520
Score: checker.MaxResultScore,
NumberOfWarn: 0,
NumberOfInfo: 1,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is because we are no longer parsing ksh:

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of ignoring the file, change the shell name in the workflow. We want to make sure this test passes with a supported shell.

@@ -49,7 +49,7 @@ var _ = Describe("E2E TEST:"+checks.CheckPinnedDependencies, func() {
expected := scut.TestReturn{
Errors: nil,
Score: checker.MinResultScore,
NumberOfWarn: 154,
NumberOfWarn: 152,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@azeemshaikh38
Copy link
Contributor

Is this PR ready for review?

@ristomcgehee
Copy link
Contributor Author

Is this PR ready for review?

Yes. Am I supposed to request reviewers or tag it with something?

@azeemshaikh38
Copy link
Contributor

Is this PR ready for review?

Yes. Am I supposed to request reviewers or tag it with something?

You can request reviewers to let them know PR is ready for review. Have added myself and @laurentsimon for now.

t.Parallel()

tests := []struct {
filename string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: add a name string field here which can be used to identify the test. Currently we use filename to identify the test (line 66).

t.Parallel()
var content []byte
var err error
if tt.filename == "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to test for empty filename here?

Copy link
Contributor Author

@ristomcgehee ristomcgehee Aug 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, yeah, we don't need that here. I copy-pasted and didn't look closely at what I copied.

} else {
content, err = ioutil.ReadFile(tt.filename)
if err != nil {
panic(fmt.Errorf("cannot read file: %w", err))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of panic, use t.Failf

}
result := isSupportedShellScriptFile(tt.filename, content)
if result != tt.expected {
t.Fail()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a failure message, showing the expected and got values. Something like: https://github.com/ossf/scorecard/blob/main/utests/utlib.go#L129

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I was unsure whether I should do that here since the only possibilities are true and false, but I do think it would be helpful.

@@ -35,6 +35,10 @@ var (
shellNames = []string{
"sh", "bash", "dash", "ksh", "mksh",
}
// supportedShells is the list of shells that are supported by mvdan.cc/sh/v3/syntax.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can declare supportedShells , and build shellNames from it. Take shellInterpreters as an example.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea.

Comment on lines 518 to 520
Score: checker.MaxResultScore,
NumberOfWarn: 0,
NumberOfInfo: 1,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of ignoring the file, change the shell name in the workflow. We want to make sure this test passes with a supported shell.

@@ -49,7 +49,7 @@ var _ = Describe("E2E TEST:"+checks.CheckPinnedDependencies, func() {
expected := scut.TestReturn{
Errors: nil,
Score: checker.MinResultScore,
NumberOfWarn: 154,
NumberOfWarn: 152,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@inferno-chromium inferno-chromium merged commit c54d77b into ossf:main Aug 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants