Skip to content

Update workflow policy to scan all branches for dangerous workflows #569 #622

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

Merged
merged 6 commits into from
Mar 4, 2025

Conversation

serb-google
Copy link
Contributor

This PR addresses #569 and introduces 2 changes:

  • Adds support for local git repos in scorecard.go, with additional functionality to switch between all branches. I opted to not replace the existing Github client as policies may want to interact directly with Github.
  • Updates workflow.go to scan all remote branches in the local git repo.

Tested locally using https://github.com/serb-google/allstar, with a branch insecure_test which contains a fake unsafe workflow rule. Verified this triggered the alert which included output Vulnerable Branch: refs/remotes/origin/insecure_test.

This is my first contribution to this repo, please review carefully :)

@serb-google serb-google requested a review from a team as a code owner January 6, 2025 01:59
Signed-off-by: Samuel Erb <[email protected]>
Signed-off-by: Samuel Erb <[email protected]>
jeffmendoza
jeffmendoza previously approved these changes Jan 17, 2025
Copy link
Member

@jeffmendoza jeffmendoza left a comment

Choose a reason for hiding this comment

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

LGTM

@jeffmendoza
Copy link
Member

@raghavkaul can you review as well? Thanks.

Copy link

@raghavkaul raghavkaul left a comment

Choose a reason for hiding this comment

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

Thanks for this push, looks good with minor feedback.

if err != nil {
return "", nil, err
}
return dir, repo, nil
}

// Function Close will close the scorecard clients. This cleans up the
// downloaded tarball.
func Close(fullRepo string) {

Choose a reason for hiding this comment

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

@jeffmendoza do we need to defer this func somewhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice catch! I went through and defer scorecard.Close... all uses of the Get function.

Copy link
Member

Choose a reason for hiding this comment

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

We have the Close here:

defer scorecard.Close(fmt.Sprintf("%s/%s", owner, repo))

Just one per repo run, so that each policy can get the clients, the first one will download the tarball, but the subsequent policies that use scorecard will not have to download the tarball again.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup, you're right. I was able to validate this running ./main -once=true with inotifywait -m -e modify,create,delete /tmp also running. Only one folder named allstar<random number> is created and then deleted per-run after removing the unnecessary closes.

Msg(msg)
return &policydef.Result{
Enabled: enabled,
Pass: true,

Choose a reason for hiding this comment

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

Suggested change
Pass: true,
Pass: false,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed!

Msg(msg)
return &policydef.Result{
Enabled: enabled,
Pass: true,

Choose a reason for hiding this comment

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

Suggested change
Pass: true,
Pass: false,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed!

Msg(msg)
return &policydef.Result{
Enabled: enabled,
Pass: true,

Choose a reason for hiding this comment

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

Suggested change
Pass: true,
Pass: false,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed!

@serb-google
Copy link
Contributor Author

Validated this still works as expected:

{"severity":"INFO","org":"serb-google","repo":"allstar","area":"Dangerous Workflow","result":false,"enabled":false,"notify":"Project is out of compliance with Dangerous Workflow policy: dangerous workflow patterns detected\n\n\t\t**Rule Description**\n\t\tDangerous workflows are GitHub Action workflows that exhibit dangerous patterns that could render them vulnerable to attack. A vulnerable workflow is susceptible to leaking repository secrets, or allowing an attacker write access using the GITHUB_TOKEN. For more information about the particular patterns that are detected, see the [OpenSSF Scorecard documentation](https://github.com/ossf/scorecard/blob/main/docs/checks.md#dangerous-workflow) on dangerous workflows. Any vulnerable branch can be exploited, so this rule will check all branches (vulnerable list below).\n\n\t\t**Remediation Steps**\n\t\tAvoid the dangerous workflow patterns. See this [post](https://securitylab.github.com/research/github-actions-preventing-pwn-requests/) for information on avoiding untrusted code checkouts. See this [document](https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions#understanding-the-risk-of-script-injections) for information on avoiding and mitigating the risk of script injections.\n\t\t**Dangerous Patterns Found**\n\n- [0]:\n\n**Additional Information**\n\tThis policy uses [OpenSSF Scorecard](https://github.com/ossf/scorecard/). You may wish to run a Scorecard scan directly on this repository for more details.\n\nVulnerable Branch: refs/remotes/origin/insecure_test","details":{"Findings":null},"time":"2025-02-28T21:36:22Z","message":"Policy run result."}

Signed-off-by: Samuel Erb <[email protected]>
@serb-google
Copy link
Contributor Author

Force-pushed to add Signed-off-by line which was missing in previous commit.

Copy link

@raghavkaul raghavkaul left a comment

Choose a reason for hiding this comment

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

New changes LGTM.

@jeffmendoza jeffmendoza merged commit bb2b83b into ossf:main Mar 4, 2025
7 checks passed
jeffmendoza pushed a commit that referenced this pull request Mar 4, 2025
Signed-off-by: Samuel Erb <[email protected]>
@jeffmendoza
Copy link
Member

Thanks!

@coheigea
Copy link
Contributor

coheigea commented Mar 6, 2025

@jeffmendoza @serb-google This PR is breaking the dangerous workflows scan for private repositories. It fails with an authentication failure on:

repo, err := git.PlainClone(dir, false, &git.CloneOptions{
		URL: "https://github.com/" + fullRepo,
	})

which can only work for public repos as it's not re-using the http config. The logs I see are:

{"severity":"ERROR","error":"authentication required: Repository not found.","time":"2025-03-06T07:02:58Z","message":"Unexpected error running policies."}

@serb-google
Copy link
Contributor Author

Hi @coheigea, I believe serb-google@57aa84f should address this, but I need to find a way to test this before sending it out for a PR.

@coheigea
Copy link
Contributor

coheigea commented Mar 7, 2025

@serb-google Hi, I get a different error message with the patch.

Before:

{"severity":"ERROR","error":"authentication required: Repository not found.","time":"2025-03-07T09:54:00Z","message":"Unexpected error running policies."}

After:

{"severity":"ERROR","error":"authentication required: invalid credentials","time":"2025-03-07T09:54:40Z","message":"Unexpected error running policies."}

@serb-google
Copy link
Contributor Author

Hi @coheigea , I was able to work through this issue and found a way to also test this. If you don't mind can you test #660 as well?

Thanks,
Sam

@coheigea
Copy link
Contributor

@serb-google I've found our scans running way slower than before on big repos with many branches after this change. What do you think about adding a boolean config whether to restrict to the default branch or not?

@serb-google
Copy link
Contributor Author

Sorry for the delay @coheigea. I created #677 which should give you enough control to specify which branches you would like checked.

Thanks,
Sam

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