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

Do not set permissions for jobs with GITHUB_TOKEN in job level env #2480

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions remediation/workflow/metadata/actionmetadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Step struct {
type Job struct {
Permissions Permissions `yaml:"permissions"`
Uses string `yaml:"uses"`
Env Env `yaml:"env"`
// RunsOn []string `yaml:"runs-on"`
Steps []Step `yaml:"steps"`
}
Expand Down
16 changes: 16 additions & 0 deletions remediation/workflow/permissions/permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const errorMissingAction = "KnownIssue-4: Action %s is not in the knowledge base
const errorAlreadyHasPermissions = "KnownIssue-5: Permissions were not added to the job since it already had permissions defined"
const errorDockerAction = "KnownIssue-6: Action %s is a docker action which uses Github token. Docker actions that uses token are not supported"
const errorReusableWorkflow = "KnownIssue-7: Action %s is a reusable workflow. Reusable workflows are not supported as of now."
const errorGithubTokenInJobEnv = "KnownIssue-8: Permissions were not added to the jobs since it has GITHUB_TOKEN in job level env variable"
const errorIncorrectYaml = "Unable to parse the YAML workflow file"

// To avoid a typo while adding the permissions
Expand Down Expand Up @@ -78,6 +79,15 @@ func alreadyHasWorkflowPermissions(workflow metadata.Workflow) bool {
return workflow.Permissions.IsSet
}

func githubTokenInJobLevelEnv(job metadata.Job) bool {
for _, envValue := range job.Env {
if strings.Contains(envValue, "secrets.GITHUB_TOKEN") || strings.Contains(envValue, "github.token") {
return true
}
}
return false
}

func AddWorkflowLevelPermissions(inputYaml string, addProjectComment bool) (string, error) {
workflow := metadata.Workflow{}

Expand Down Expand Up @@ -177,6 +187,12 @@ func AddJobLevelPermissions(inputYaml string) (*SecureWorkflowReponse, error) {
continue
}

if githubTokenInJobLevelEnv(job) {
fixWorkflowPermsReponse.HasErrors = true
errors[jobName] = append(errors[jobName], errorGithubTokenInJobEnv)
continue
}

if metadata.IsCallingReusableWorkflow(job) {
fixWorkflowPermsReponse.HasErrors = true
errors[jobName] = append(errors[jobName], fmt.Sprintf(errorReusableWorkflow, job.Uses))
Expand Down
16 changes: 16 additions & 0 deletions testfiles/joblevelpermskb/input/github-token-in-job-env.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Job level env
on:
pull_request:
branches: [main]

jobs:
job-with-error:
env:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
runs-on: ubuntu-latest
steps:

- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4
- name: some step that uses token
run: |
npm ci
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
KnownIssue-8: Permissions were not added to the jobs since it has GITHUB_TOKEN in job level env variable