Skip to content

Commit ad03499

Browse files
shubham-stepsecurityvarunsh-coder
authored andcommitted
Do not set permissions for jobs with GITHUB_TOKEN in job level env
1 parent d61982f commit ad03499

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

remediation/workflow/metadata/actionmetadata.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type Step struct {
3030
type Job struct {
3131
Permissions Permissions `yaml:"permissions"`
3232
Uses string `yaml:"uses"`
33+
Env Env `yaml:"env"`
3334
// RunsOn []string `yaml:"runs-on"`
3435
Steps []Step `yaml:"steps"`
3536
}

remediation/workflow/permissions/permissions.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const errorMissingAction = "KnownIssue-4: Action %s is not in the knowledge base
3838
const errorAlreadyHasPermissions = "KnownIssue-5: Permissions were not added to the job since it already had permissions defined"
3939
const errorDockerAction = "KnownIssue-6: Action %s is a docker action which uses Github token. Docker actions that uses token are not supported"
4040
const errorReusableWorkflow = "KnownIssue-7: Action %s is a reusable workflow. Reusable workflows are not supported as of now."
41+
const errorGithubTokenInJobEnv = "KnownIssue-8: Permissions were not added to the jobs since it has GITHUB_TOKEN in job level env variable"
4142
const errorIncorrectYaml = "Unable to parse the YAML workflow file"
4243

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

82+
func githubTokenInJobLevelEnv(job metadata.Job) bool {
83+
for _, envValue := range job.Env {
84+
if strings.Contains(envValue, "secrets.GITHUB_TOKEN") || strings.Contains(envValue, "github.token") {
85+
return true
86+
}
87+
}
88+
return false
89+
}
90+
8191
func AddWorkflowLevelPermissions(inputYaml string, addProjectComment bool) (string, error) {
8292
workflow := metadata.Workflow{}
8393

@@ -177,6 +187,12 @@ func AddJobLevelPermissions(inputYaml string) (*SecureWorkflowReponse, error) {
177187
continue
178188
}
179189

190+
if githubTokenInJobLevelEnv(job) {
191+
fixWorkflowPermsReponse.HasErrors = true
192+
errors[jobName] = append(errors[jobName], errorGithubTokenInJobEnv)
193+
continue
194+
}
195+
180196
if metadata.IsCallingReusableWorkflow(job) {
181197
fixWorkflowPermsReponse.HasErrors = true
182198
errors[jobName] = append(errors[jobName], fmt.Sprintf(errorReusableWorkflow, job.Uses))
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: Job level env
2+
on:
3+
pull_request:
4+
branches: [main]
5+
6+
jobs:
7+
job-with-error:
8+
env:
9+
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
10+
runs-on: ubuntu-latest
11+
steps:
12+
13+
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4
14+
- name: some step that uses token
15+
run: |
16+
npm ci
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
KnownIssue-8: Permissions were not added to the jobs since it has GITHUB_TOKEN in job level env variable

0 commit comments

Comments
 (0)