fix(github): restrict comment editing to PAC-owned comments#2487
Conversation
Summary of ChangesHello @theakshaypant, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the GitHub integration by introducing a critical safeguard: Pipelines-as-Code will now strictly manage only its own comments. This prevents unintended alterations to comments made by other users or automated systems. The change also refines how GitHub App bot identities are determined, ensuring accurate ownership checks. Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
pkg/provider/github/parse_payload.go
Outdated
| return token, err | ||
| } | ||
|
|
||
| // Refer https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/authenticating-as-a-github-app-installation#using-an-installation-access-token-to-authenticate-as-an-app-installation |
There was a problem hiding this comment.
There is some code duplication in this method and GenerateJWT.
There was a problem hiding this comment.
Code Review
This pull request introduces an important security enhancement by restricting Pipelines-as-Code (PAC) comment editing to only those comments created by its own identity, preventing unauthorized modifications. While this is a significant security improvement, the current implementation unfortunately exacerbates an existing SSRF and credential leakage vulnerability. User-controlled HTTP headers, specifically X-GitHub-Enterprise-Host, are used to define API endpoints before webhook signature validation, which could allow an attacker to exfiltrate signed JWTs or installation tokens. It is strongly recommended to validate the host header against a trusted allow-list before use. The implementation correctly distinguishes between Personal Access Token (PAT) and GitHub App authentication and is well-tested, with a minor suggestion regarding a code comment.
pkg/provider/github/parse_payload.go
Outdated
| func (v *Provider) fetchAppSlug(ctx context.Context, applicationID int64, privateKey []byte, apiURL string) (string, error) { | ||
| parsedPK, err := jwt.ParseRSAPrivateKeyFromPEM(privateKey) | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to parse private key: %w", err) | ||
| } | ||
|
|
||
| token := jwt.NewWithClaims(jwt.SigningMethodRS256, &jwt.RegisteredClaims{ | ||
| Issuer: fmt.Sprintf("%d", applicationID), | ||
| IssuedAt: jwt.NewNumericDate(time.Now()), | ||
| ExpiresAt: jwt.NewNumericDate(time.Now().Add(5 * time.Minute)), | ||
| }) | ||
|
|
||
| tokenString, err := token.SignedString(parsedPK) | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to sign private key: %w", err) | ||
| } | ||
|
|
||
| client, _, _ := MakeClient(ctx, apiURL, tokenString) | ||
| app, _, err := client.Apps.Get(ctx, "") | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to get app info: %w", err) | ||
| } | ||
|
|
||
| return app.GetSlug(), nil | ||
| } |
There was a problem hiding this comment.
CRITICAL: SSRF and Credential Leakage. The newly introduced fetchAppSlug function uses the apiURL parameter, which is derived from the unvalidated X-GitHub-Enterprise-Host HTTP header, to define the GitHub API endpoint. This URL is used to create a GitHub client that authenticates using a JWT signed with the App's private key. An attacker can provide a malicious host in this header, causing PAC to send a signed JWT to the attacker's server. This JWT can be used by the attacker to impersonate the GitHub App and gain unauthorized access to repositories where the App is installed.
Recommendation: Validate the X-GitHub-Enterprise-Host header against an allow-list of trusted hosts or only use hosts explicitly configured in the controller's settings. Avoid making network requests to user-controlled endpoints before validating the webhook signature.
pkg/provider/github/parse_payload.go
Outdated
| return token, err | ||
| } | ||
|
|
||
| // Refer https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/authenticating-as-a-github-app-installation#using-an-installation-access-token-to-authenticate-as-an-app-installation |
There was a problem hiding this comment.
The comment is slightly misleading. This function authenticates as a GitHub App, not as an installation. The linked documentation section is about authenticating as an installation. A more accurate link would be to the documentation for authenticating as a GitHub App.
| // Refer https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/authenticating-as-a-github-app-installation#using-an-installation-access-token-to-authenticate-as-an-app-installation | |
| // Refer https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/authenticating-as-a-github-app |
a007c67 to
8928fdf
Compare
0b9008f to
088863e
Compare
| // Type must be set explicitly so the incoming webhook handler | ||
| // (adapter.detectIncoming) knows this is a direct webhook setup. | ||
| // Without it, GitProvider.Type == "" causes the controller to | ||
| // assume GitHub App mode and attempt to fetch the global | ||
| // pipelines-as-code-secret, which doesn't exist for direct webhooks. | ||
| Type: "github", |
pkg/provider/github/github.go
Outdated
| if v.pacUserLogin == "" { | ||
| if event.InstallationID > 0 { | ||
| // For Apps, get the app slug. | ||
| slug, err := v.fetchAppSlug(ctx, event.Provider.URL) |
There was a problem hiding this comment.
i think we use event.Provider.GHEURL on reconciler for GHE, which mean it would be broken.
can you double check this works there?
There was a problem hiding this comment.
I had looked at GHEURL (considering the jwt token part used GHEURL in the reference code at GetAppToken) but looking at this assignment, I felt this would work.
Will test it nevertheless.
|
|
||
| // Refer https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/authenticating-as-a-github-app-installation | ||
| func (v *Provider) fetchAppSlug(ctx context.Context, apiURL string) (string, error) { | ||
| ns := info.GetNS(ctx) |
There was a problem hiding this comment.
This is bugging that we to do this thrice, can you check if we can do this in one go without having to query the Github APP again ?
i.e
% g \\.GetAppIDAndPrivateKey
pkg/provider/github/github.go:1091: applicationID, privateKey, err := v.GetAppIDAndPrivateKey(ctx, ns, v.Run.Clients.Kube)
pkg/provider/github/app/token.go:119: applicationID, privateKey, err := gh.GetAppIDAndPrivateKey(ctx, ip.namespace, ip.run.Clients.Kube)
pkg/provider/github/parse_payload.go:55: applicationID, privateKey, err := v.GetAppIDAndPrivateKey(ctx, ns, kube)
(withouth having to do crazy refactoring)
There was a problem hiding this comment.
This change was introducing a bit of complexity and the said craziness, created a separate ticket to track it.
deded70 to
acd2a7d
Compare
Add check to ensure PAC only edits comments created by its own credentials, preventing accidental modification of comments from other users or bot accounts. For GitHub Apps, fetch bot login from app slug if needed on comment match. Jira: https://issues.redhat.com/browse/SRVKP-10857 Signed-off-by: Akshay Pant <akshay.akshaypant@gmail.com> Assisted-by: Claude Sonnet 4.5 (via Claude Code)
acd2a7d to
f49a44e
Compare
|
/test |
|
plesae make sure to make a plan for the followup |
Will be taking it up this week itself. |
📝 Description of the Change
Add check to ensure PAC only edits comments created by its own credentials, preventing accidental modification of comments from other users or bot accounts.
For GitHub Apps, fetch bot login from app slug during token generation.
👨🏻 Linked Jira
https://issues.redhat.com/browse/SRVKP-10857
🔗 Linked GitHub Issue
N/A
🚀 Type of Change
fix:)feat:)feat!:,fix!:)docs:)chore:)refactor:)enhance:)deps:)🧪 Testing Strategy
Controller logs
Comments on PR

GitHub App test - deleted pac comment for cel validation error and added a duplicate comment. PAC creates a new comment instead of editing the duplicate comment from another user.

🤖 AI Assistance
If you have used AI assistance, please provide the following details:
Which LLM was used?
Extent of AI Assistance:
Important
If the majority of the code in this PR was generated by an AI, please add a
Co-authored-bytrailer to your commit message.For example:
Co-authored-by: Gemini gemini@google.com
Co-authored-by: ChatGPT noreply@chatgpt.com
Co-authored-by: Claude noreply@anthropic.com
Co-authored-by: Cursor noreply@cursor.com
Co-authored-by: Copilot Copilot@users.noreply.github.com
**💡You can use the script
./hack/add-llm-coauthor.shto automatically addthese co-author trailers to your commits.
✅ Submitter Checklist
fix:,feat:) matches the "Type of Change" I selected above.make testandmake lintlocally to check for and fix anyissues. For an efficient workflow, I have considered installing
pre-commit and running
pre-commit installtoautomate these checks.