Skip to content

Commit 0e2a006

Browse files
fix: add package read permission for container jobs
1 parent 8458d27 commit 0e2a006

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

remediation/workflow/permissions/permissions.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ func AddJobLevelPermissions(inputYaml string, addEmptyTopLevelPermissions bool)
209209

210210
jobState := &JobState{}
211211
jobState.WorkflowEnv = workflow.Env
212+
jobState.IsContainerJob = (job.Container.Image != "")
212213
perms, err := jobState.getPermissions(job.Steps)
213214

214215
if err != nil {
@@ -369,6 +370,8 @@ type JobState struct {
369370
MissingActions []string
370371
Errors []error
371372
ActionPermissions *metadata.ActionPermissions
373+
374+
IsContainerJob bool // true if the job is running in a container
372375
}
373376

374377
func evaluateEnvironmentVariables(step metadata.Step) string {
@@ -519,6 +522,11 @@ func (jobState *JobState) getPermissionsForRunStep(step metadata.Step) ([]Permis
519522
func (jobState *JobState) getPermissions(steps []metadata.Step) ([]string, error) {
520523
permissions := []string{}
521524

525+
// If the job is a container job, we need to add packages: read permission
526+
if jobState.IsContainerJob {
527+
permissions = append(permissions, fmt.Sprintf("%s # for container job", packages_read))
528+
}
529+
522530
for _, step := range steps {
523531

524532
if step.Uses != "" { // it is an action

remediation/workflow/secureworkflow_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ func TestSecureWorkflowContainerJob(t *testing.T) {
333333
queryParams := make(map[string]string)
334334
queryParams["skipHardenRunnerForContainers"] = "true"
335335
queryParams["addProjectComment"] = "false"
336+
queryParams["addPermissions"] = "true"
336337

337338
output, err := SecureWorkflow(queryParams, string(input), &mockDynamoDBClient{})
338339

testfiles/secureworkflow/output/container-job.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ permissions:
99

1010
jobs:
1111
test:
12+
permissions:
13+
contents: read # for actions/checkout to fetch code
14+
packages: read # for container job
1215
runs-on: ubuntu-latest
1316
container:
1417
image: cgr.dev/chainguard/wolfi-base@sha256:91ed94ec4e72368a9b5113f2ffb1d8e783a91db489011a89d9fad3e3816a75ba

0 commit comments

Comments
 (0)