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

i/p/patterns: disallow /./ and /../ in path patterns #14774

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

olivercalder
Copy link
Member

@olivercalder olivercalder commented Nov 27, 2024

Since patterns like /foo/./bar don't match paths paths like /foo/bar, throw an error if the client tries to reply or create a rule with such a construction clearly in the pattern. That is, if the pattern contains /./ or /../, or if it ends with /. or /...

Notably, we do this on the raw pattern string, similarly to how we validate that the pattern begins with a /: we don't consider whether all alts in a leading group happen to start with /, we simply throw an error if the first character is not /.

In this case, we take a more lenient (but similarly lazy) approach: if there's a literal /./ or /../, or trailing /. or /.., then an error is thrown. As long as there's something interrupting the / or end of the pattern string (e.g. a group), we consider that fine. For example, we allow /foo/.{,bar} even though technically this renders to the variants /foo/. and /foo/.bar, where the former is undesirable.

We may reconsider this in the future, but checking whether any rendered variant contains /./ or /../ in general requires fully rendering each variant and checking each one, which we do not do at parse time, and one nearly always has at least one pattern which is capable of matching something valid (an exception being the pattern /foo/.{,}, for example). But the user must fairly deliberately shoot themself in the foot to end up in that situation.

Again, the worst case if a pattern which is "bad" in this way gets past validation is that we end up with a path pattern which is incapable of matching any paths. This is undesirable, but not problematic.

This is a successor to #14502
CC @sminez
This is tracked internally by https://warthogs.atlassian.net/browse/SNAPDENG-32063

Since patterns like /foo/./bar don't match paths paths like /foo/bar,
throw an error if the client tries to reply or create a rule with such a
construction clearly in the pattern. That is, if the pattern contains
`/./` or `/../`, or if it ends with `/.` or `/..`.

Notably, we do this on the raw pattern string, similarly to how we
validate that the pattern begins with a `/`: we don't consider whether
all alts in a leading group happen to start with `/`, we simply throw an
error if the first character is not `/`.

In this case, we take a more lenient (but similarly lazy) approach: if
there's a literal `/./` or `/../`, or trailing `/.` or `/..`, then an
error is thrown. As long as there's something interrupting the `/` or
end of the pattern string (e.g. a group), we consider that fine. For
example, we allow `/foo/.{,bar}` even though technically this renders to
the variants `/foo/.` and `/foo/.bar`, where the former is undesirable.

We may reconsider this in the future, but checking whether any rendered
variant contains `/./` or `/../` in general requires fully rendering
each variant and checking each one, which we do not do at parse time,
and one nearly always has at least one pattern which is capable of
matching something valid (an exception being the pattern `/foo/.{,}`,
for example). But the user must fairly deliberately shoot themself in
the foot to end up in that situation.

Again, the worst case if a pattern which is "bad" in this way gets past
validation is that we end up with a path pattern which is incapable of
matching any paths. This is undesirable, but not problematic.

Signed-off-by: Oliver Calder <[email protected]>
@olivercalder olivercalder added the Simple 😃 A small PR which can be reviewed quickly label Nov 27, 2024
Copy link

codecov bot commented Nov 27, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 79.03%. Comparing base (96ea7b0) to head (18556dc).
Report is 115 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #14774      +/-   ##
==========================================
+ Coverage   78.95%   79.03%   +0.08%     
==========================================
  Files        1084     1087       +3     
  Lines      146638   147944    +1306     
==========================================
+ Hits       115773   116927    +1154     
- Misses      23667    23775     +108     
- Partials     7198     7242      +44     
Flag Coverage Δ
unittests 79.03% <100.00%> (+0.08%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

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

Copy link
Contributor

@ZeyadYasser ZeyadYasser left a comment

Choose a reason for hiding this comment

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

LGTM!

interfaces/prompting/patterns/scan.go Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Simple 😃 A small PR which can be reviewed quickly
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants