Skip to content

Commit 2668911

Browse files
committed
Fix GitOps Command Issue on Pushed Commit by Unautorized User
Issue: when an unautorized user sends GitOps comment on a pushed commit, PAC is triggering CI since access check is done only for pull_request event in verifyRepoAndUser func of controller. Solution: added a check for push event and Ops comment event type in verifyRepoAndUser func. https://issues.redhat.com/browse/SRVKP-7110 Signed-off-by: Zaki Shaikh <[email protected]>
1 parent 799386b commit 2668911

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

pkg/pipelineascode/match.go

+34
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@ is that what you want? make sure you use -n when generating the secret, eg: echo
132132
return repo, err
133133
}
134134

135+
// Verify whether the sender of the GitOps command (e.g., /test) has the appropriate permissions to
136+
// trigger CI on the repository, as any user is able to comment on a pushed commit in open-source repositories.
137+
if p.event.TriggerTarget == triggertype.Push && opscomments.IsAnyOpsEventType(p.event.EventType) {
138+
if allowed, err := p.checkAccessOrFail(ctx, repo); !allowed {
139+
return nil, err
140+
}
141+
}
142+
135143
// Check if the submitter is allowed to run this.
136144
// on push we don't need to check the policy since the user has pushed to the repo so it has access to it.
137145
// on comment we skip it for now, we are going to check later on
@@ -410,3 +418,29 @@ func (p *PacRun) createNeutralStatus(ctx context.Context) error {
410418

411419
return nil
412420
}
421+
422+
func (p *PacRun) checkAccessOrFail(ctx context.Context, repo *v1alpha1.Repository) (bool, error) {
423+
allowed, err := p.vcx.IsAllowed(ctx, p.event)
424+
if err != nil {
425+
return false, err
426+
}
427+
if allowed {
428+
return true, nil
429+
}
430+
msg := fmt.Sprintf("User %s is not allowed to trigger CI by GitOps command on this repo.", p.event.Sender)
431+
if p.event.AccountID != "" {
432+
msg = fmt.Sprintf("User: %s AccountID: %s is not allowed to trigger CI by GitOps command on this repo.", p.event.Sender, p.event.AccountID)
433+
}
434+
p.eventEmitter.EmitMessage(repo, zap.InfoLevel, "RepositoryPermissionDenied", msg)
435+
status := provider.StatusOpts{
436+
Status: CompletedStatus,
437+
Title: fmt.Sprintf("User %s is not allowed", p.event.Sender),
438+
Conclusion: failureConclusion,
439+
Text: msg,
440+
DetailsURL: p.event.URL,
441+
}
442+
if err := p.vcx.CreateStatus(ctx, p.event, status); err != nil {
443+
return false, fmt.Errorf("failed to run create status, user is not allowed to run the CI:: %w", err)
444+
}
445+
return false, nil
446+
}

0 commit comments

Comments
 (0)