Skip to content

PatternSyntaxException may be thrown when looking for an environment entry or metric with a name that looks a bit like a regular expression #9730

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

Closed
wants to merge 3 commits into from

Conversation

DylianBEGO
Copy link
Contributor

Sometimes, Spring detects a string as a regex because it contains regex parts. The problem is this string is not a regex and a PatternSyntaxException is raised when pattern try to compile it.

This modification checks if the regex compilation is possible, else we consider it is just a simple string.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jul 11, 2017
@wilkinsona
Copy link
Member

Interesting. Thanks for the PR. Just to help us understand the nature of the problem, can you please share an example of an endpoint call you were making where the current behaviour is inadequate?

@wilkinsona wilkinsona added the status: waiting-for-feedback We need additional information before we can continue label Jul 11, 2017
@DylianBEGO
Copy link
Contributor Author

For example, /admin/env/* throw a PatternSyntaxException with the current method, this path is considered to be a regex while not

@wilkinsona
Copy link
Member

I get that something like * would break. I was more interested in a real-world use case for such a call. Do you have a * key in the environment?

DylianBEGO and others added 3 commits July 12, 2017 13:42
…can be compiled

* Code Format

* Catch PatternSyntaxException instead of RuntimeException

* Fix Checkstyle violations

Revert "* Catch PatternSyntaxException instead of RuntimeException"

This reverts commit 0b85e4f.

* Ctach PatternSyntaxException instead of RuntimeException

* Fix checkstyle violations

* Delete flag parameter
@DylianBEGO
Copy link
Contributor Author

No, i don't have a key like this.

I think it's more robust to not throw an exception when someone makes a mistake. For example, if we put * by mistake instead of .* or .something* or even for any other invalid regex.

What do you think ?

@wilkinsona
Copy link
Member

It's a little bit tricky. On the one hand, if you pass in something that was intended to be a regex but you mess up the syntax, a bad request response would be helpful. On the other hand, we don't want to prevent people from searching for keys that look a bit like a regular expression…

@jebeaudet
Copy link
Contributor

While I agree with the helpfulness of the bad request response, I don't see how this is possible without breaking the api. If we had a specific query param for the regex, we could then be sure of the intentions of the user and return an error in the case of an invalid regex. But with the path param syntax serving as a filter on keys with a contains() and a regex, I don't see how we could know if the user meant the key named foo*bar of the equivalent regex.

IMO, this PR is the best we can do without breaking the api, we won't support my example but at least it won't throw expressions that look like a regex but are not. We could introduce a new query param for a regex on top of the existing behaviour I guess, although I'm not a fan of this solution as I think it's cumbersome when searching (having to put a query param instead of a path param).

@wilkinsona
Copy link
Member

Thanks. In the course of some work on the Actuator for 2.0, it's become apparent that we really need to move to a query parameter for the pattern. Please see #9796 for more details on that. For 1.5.x, I agree that what you're proposing here is the best we can do.

@wilkinsona wilkinsona added priority: normal and removed status: waiting-for-feedback We need additional information before we can continue status: waiting-for-triage An issue we've not yet triaged labels Jul 19, 2017
@wilkinsona wilkinsona added this to the 1.5.5 milestone Jul 19, 2017
@wilkinsona wilkinsona added the type: bug A general bug label Jul 19, 2017
@wilkinsona wilkinsona changed the title Improve Regex Detection for endpoint PatternSyntaxException may be thrown when looking for an environment entry or metric with a name that looks a bit like a regular expression Jul 19, 2017
@jebeaudet
Copy link
Contributor

Glad to hear that! Any change to get it merged in the 1.4.x branch as well? We're not planning on updating to 1.5 in the near future and I'd like to see that fix :)

@wilkinsona
Copy link
Member

wilkinsona commented Jul 19, 2017

No, sorry. As mentioned when it was announced, 1.4.7 was the last 1.4.x release.

@jebeaudet
Copy link
Contributor

Oh, that's too bad... Thanks for answer, I've missed this detail.

wilkinsona pushed a commit that referenced this pull request Jul 21, 2017
Previously, if a name contained part of a regex but wasn't actually
a regex, a PatternSyntaxException would be thrown and the request
would fail.

This commit updates NamePatternFilter to catch PatternSyntaxException
and treat the regex-like input as a name insteead.

See gh-9730
wilkinsona added a commit that referenced this pull request Jul 21, 2017
* gh-9730:
  Polish "Handle possible regexes defensively in NamePatternFilter"
  Handle possible regexes defensively in NamePatternFilter
@wilkinsona
Copy link
Member

Thanks for the PR, @DylianBEGO. I've merged it into 1.5.x and master with some minor polishing.

dbego pushed a commit to coveord/spring-boot that referenced this pull request Sep 18, 2017
[spring-projects#9730] in MVC
endpoint 
* Bump to version 1.4.10.BUILD-COVEO

[AUTH-610]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants