Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: webhook URL matching edge cases #7981

Merged
merged 4 commits into from
Dec 17, 2021

Conversation

crenshaw-dev
Copy link
Member

This fixes two edge cases when matching git hook payloads to targetRevisions.

  1. Dots are now interpreted literally. Previously, example.com:org/some.repo would match targetRevision example.com:org/some-repo.

    The current implementation would technically allow any regex pattern in the org or repo name. But practically speaking, dots are the only control characters that show up in org, repo, and domain names.

  2. Partial matches no longer count. Previously example.com:org/a would match targetRevision example.com:org/apple.

Signed-off-by: Michael Crenshaw <michael@crenshaw.dev>
Signed-off-by: Michael Crenshaw <michael@crenshaw.dev>
@crenshaw-dev crenshaw-dev marked this pull request as draft December 17, 2021 15:54
@codecov
Copy link

codecov bot commented Dec 17, 2021

Codecov Report

Merging #7981 (2eb7f31) into master (0676b65) will increase coverage by 0.00%.
The diff coverage is 58.33%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #7981   +/-   ##
=======================================
  Coverage   41.43%   41.44%           
=======================================
  Files         173      173           
  Lines       22570    22575    +5     
=======================================
+ Hits         9353     9357    +4     
  Misses      11886    11886           
- Partials     1331     1332    +1     
Impacted Files Coverage Δ
util/webhook/webhook.go 62.62% <58.33%> (+0.45%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0676b65...2eb7f31. Read the comment docs.

Signed-off-by: Michael Crenshaw <michael@crenshaw.dev>

regexEscapedHostname := regexp.QuoteMeta(urlObj.Hostname())
regexEscapedPath := regexp.QuoteMeta(urlObj.Path[1:])
regexpStr := fmt.Sprintf(`(?i)^(http://|https://|\w+@|ssh://(\w+@)?)%s(:[0-9]+|)[:/]%s(\.git)?$`, regexEscapedHostname, regexEscapedPath)
Copy link
Member Author

Choose a reason for hiding this comment

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

The only change to the pattern (besides escaping the inputs) is to add ^ and $.


// Standard cases.
{true, "https://example.com/org/repo", "https://example.com/org/repo", "exact match should match"},
{false, "https://example.com/org/repo", "https://example.com/org/repo-2", "partial match should not match"},
Copy link
Member Author

Choose a reason for hiding this comment

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

The ^/$ change makes this pass.

Comment on lines +276 to +277
{false, "https://example.com/org/a..d", "https://example.com/org/abcd", "dots in repo names should not be treated as wildcards"},
{false, "https://an.example.com/org/repo", "https://an-example.com/org/repo", "dots in domain names should not be treated as wildcards"},
Copy link
Member Author

Choose a reason for hiding this comment

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

The QuoteMeta changes make these pass.

util/webhook/webhook.go Outdated Show resolved Hide resolved
Signed-off-by: Michael Crenshaw <michael@crenshaw.dev>
}

regexEscapedHostname := regexp.QuoteMeta(urlObj.Hostname())
regexEscapedPath := regexp.QuoteMeta(urlObj.Path[1:])
Copy link
Member

Choose a reason for hiding this comment

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

Just a question for my understanding: Why do we quote Path[1:] and not the whole Path here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good question. I think the [1:] index has been there since Jesse first committed the regex 4 years ago. My assumption has been that it's stripping the leading / from the path.

Copy link
Member

Choose a reason for hiding this comment

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

Ah right - I see that it's already been so in the original. Yeah, in the context of the complete regexp, it makes sense, because URLs could be in different formats, including scp-format (e.g. git@foo:bar/baz)

Copy link
Member

@jannfis jannfis left a comment

Choose a reason for hiding this comment

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

LGTM! Thank you!

@jannfis jannfis merged commit 914fb83 into argoproj:master Dec 17, 2021
@crenshaw-dev crenshaw-dev deleted the fix-webhook-url-edge-cases branch December 17, 2021 19:58
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.

2 participants