Skip to content

fix(webhook): prevent duplicate repository CR on trailing slash#2683

Merged
zakisk merged 1 commit intotektoncd:mainfrom
zakisk:fix-duplicate-repo-url-bypass
Apr 14, 2026
Merged

fix(webhook): prevent duplicate repository CR on trailing slash#2683
zakisk merged 1 commit intotektoncd:mainfrom
zakisk:fix-duplicate-repo-url-bypass

Conversation

@zakisk
Copy link
Copy Markdown
Member

@zakisk zakisk commented Apr 14, 2026

The webhook admission controller's checkIfRepoExist used exact string equality to compare repository URLs. This allowed users to create a second Repository CR with the same URL by simply appending a trailing slash (e.g. https://github.com/org/repo vs https://github.com/org/repo/), bypassing the uniqueness validation.

And in matching logic we compare repository CRs by trimming suffix slash "/" which was facilitating this flaw. if there are two repository CRs with URLs https://github.com/pac/repo and https://github.com/pac/repo/ then in matching the repo with slash would be matched

  • pkg/webhook/validation.go:

    • Normalize URLs with strings.TrimSuffix before comparison in checkIfRepoExist to treat trailing slash variants as identical
  • pkg/webhook/validation_test.go:

    • Add test case for rejecting repository URL with trailing slash when a matching repo already exists
  • test/repository_webhook_test.go:

    • Add e2e test TestOthersRepositoryCreationWithTrailingSlash to verify the webhook rejects duplicate repos created via trailing slash bypass

🔗 Linked GitHub Issue

Fixes #

JIRA

https://redhat.atlassian.net/browse/SRVKP-11295

🧪 Testing Strategy

  • Unit tests
  • Integration tests
  • End-to-end tests
  • Manual testing
  • Not Applicable

🤖 AI Assistance

AI assistance can be used for various tasks, such as code generation,
documentation, or testing.

Please indicate whether you have used AI assistance
for this PR and provide details if applicable.

  • I have not used any AI assistance for this PR.
  • I have used AI assistance for this PR.

Important

Slop will be simply rejected, if you are using AI assistance you need to make sure you
understand the code generated and that it meets the project's standards. you
need at least know how to run the code and deploy it (if needed). See
startpaac to make it easy
to deploy and test your code changes.

If the majority of the code in this PR was generated by an AI, please add a Co-authored-by trailer to your commit message.
For example:

Co-authored-by: Claude noreply@anthropic.com

✅ Submitter Checklist

  • 📝 My commit messages are clear, informative, and follow the project's How to write a git commit message guide. The Gitlint linter ensures in CI it's properly validated
  • ✨ I have ensured my commit message prefix (e.g., fix:, feat:) matches the "Type of Change" I selected above.
  • ♽ I have run make test and make lint locally to check for and fix any
    issues. For an efficient workflow, I have considered installing
    pre-commit and running pre-commit install to
    automate these checks.
  • 📖 I have added or updated documentation for any user-facing changes.
  • 🧪 I have added sufficient unit tests for my code changes.
  • 🎁 I have added end-to-end tests where feasible. See README for more details.
  • 🔎 I have addressed any CI test flakiness or provided a clear reason to bypass it.
  • If adding a provider feature, I have filled in the following and updated the provider documentation:
    • GitHub App
    • GitHub Webhook
    • Gitea/Forgejo
    • GitLab
    • Bitbucket Cloud
    • Bitbucket Data Center

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request enhances the repository URL validation logic within the webhook to prevent the creation of duplicate repository resources that differ only by trailing slashes. The changes include updating the comparison logic in checkIfRepoExist and adding comprehensive unit and integration tests. Feedback suggests improving the robustness of the URL normalization by using strings.TrimRight and strings.TrimSpace to handle multiple trailing slashes and whitespace, ensuring a more secure and consistent validation process.

Comment thread pkg/webhook/validation.go Outdated
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 14, 2026

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 58.85%. Comparing base (a6e4aab) to head (31e17c1).
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2683   +/-   ##
=======================================
  Coverage   58.85%   58.85%           
=======================================
  Files         204      204           
  Lines       20149    20149           
=======================================
  Hits        11859    11859           
  Misses       7525     7525           
  Partials      765      765           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread test/repository_webhook_test.go
@chmouel
Copy link
Copy Markdown
Member

chmouel commented Apr 14, 2026

/lgtm

just fix it to trimright as the bot suggest and that would be good to merge

Copy link
Copy Markdown

@pipelines-as-code pipelines-as-code bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Congrats @zakisk your PR Has been approved 🎉

✅ Pull Request Approved

Approval Status:

  • Required Approvals: 1
  • Current Approvals: 1

👥 Reviewers Who Approved:

Reviewer Permission Level Approval Status
@chmouel admin

📝 Next Steps

  • Ensure all required checks pass
  • Comply with branch protection rules
  • Request a maintainer to merge using the /merge command (or merge it
    directly if you have repository permission).

Automated by the PAC Boussole 🧭

The webhook admission controller's checkIfRepoExist used exact
string equality to compare repository URLs. This allowed users
to create a second Repository CR with the same URL by simply
appending a trailing slash (e.g. https://github.com/org/repo
vs https://github.com/org/repo/), bypassing the uniqueness
validation.

  - pkg/webhook/validation.go:
    - Normalize URLs with strings.TrimSuffix before comparison
      in checkIfRepoExist to treat trailing slash variants as
      identical

  - pkg/webhook/validation_test.go:
    - Add test case for rejecting repository URL with trailing
      slash when a matching repo already exists

  - test/repository_webhook_test.go:
    - Add e2e test TestOthersRepositoryCreationWithTrailingSlash
      to verify the webhook rejects duplicate repos created via
      trailing slash bypass

Signed-off-by: Zaki Shaikh <zashaikh@redhat.com>
@zakisk zakisk force-pushed the fix-duplicate-repo-url-bypass branch from 537396e to 31e17c1 Compare April 14, 2026 14:37
@zakisk
Copy link
Copy Markdown
Member Author

zakisk commented Apr 14, 2026

@aThorp96 I think this PR would fix https://redhat.atlassian.net/browse/SRVKP-11295 somehow?

@chmouel
Copy link
Copy Markdown
Member

chmouel commented Apr 14, 2026

/lgtm

Copy link
Copy Markdown

@pipelines-as-code pipelines-as-code bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Congrats @zakisk your PR Has been approved 🎉

✅ Pull Request Approved

Approval Status:

  • Required Approvals: 1
  • Current Approvals: 1

👥 Reviewers Who Approved:

Reviewer Permission Level Approval Status
@chmouel admin

📝 Next Steps

  • Ensure all required checks pass
  • Comply with branch protection rules
  • Request a maintainer to merge using the /merge command (or merge it
    directly if you have repository permission).

Automated by the PAC Boussole 🧭

@zakisk zakisk merged commit c9be9d6 into tektoncd:main Apr 14, 2026
14 checks passed
@zakisk zakisk deleted the fix-duplicate-repo-url-bypass branch April 14, 2026 15:29
Comment thread pkg/webhook/validation.go
for i := len(repositories) - 1; i >= 0; i-- {
repoFromCluster := repositories[i]
if repoFromCluster.Spec.URL == repo.Spec.URL &&
if strings.TrimRight(strings.TrimSpace(repoFromCluster.Spec.URL), "/") == strings.TrimRight(strings.TrimSpace(repo.Spec.URL), "/") &&
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zakisk ah I think this is a likely culprit for the issue you linked.

We could probably make this normalization a bit more consistent using something like the below (playground link), but this should work as well I think. Though if strings.TrimSpace() is necessary then it would still be necessary in addition to path.Clean().

func normalizeUrl(repoUrl string) (string, err) {
    u, err := url.Parse(repoUrl)
    if err != nil {
        return "", err
    }
    u.Path = path.Clean(u.Path)
    return u.String(), nil
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, @aThorp96 makes sense! feel free to raise PR or you want me to do?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants