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

.json not translating Pattern correctly #191

Closed
sepatel opened this issue Mar 27, 2020 · 2 comments
Closed

.json not translating Pattern correctly #191

sepatel opened this issue Mar 27, 2020 · 2 comments

Comments

@sepatel
Copy link

sepatel commented Mar 27, 2020

Something appears to be wrong with the way that .json is converting a Pattern object. We initially had done something like
collection.find("{authz: ${user.authz.toRegex().json}")
and found our code wasn't working as expected. Not a big deal, we googled up and figured out that we were idiots since mongo doesn't support Regex() it wanted Pattern(). So we tweaked the code up to do
collection.find("{authz: ${user.authz.toRegex().toPattern().json}")
and found it still didn't work.

After some deeper diving we found that using a Document("authz", user.authz.toRegex().pattern()) was working when using the native mongo java drivers. That let to some experiments where we discovered that the .json on the pattern doesn't convert things correctly.

val pattern = Pattern.compile("System#.*R")

Example of what creates a bad conversion

println("{authz: ${pattern.json}}")

Output: {authz: "System#.*R"}

Example of what creates a good conversion

println(Document("authz", pattern).json)

Output: {"authz": {"$regex": "System#.*R", "$options": ""}}

This happens in both 3.11.0 driver (what we currently have in production) and 3.12.2 driver (we should be able to upgrade to this if needed).

@zigzago
Copy link
Member

zigzago commented Mar 28, 2020

Thanks for reporting. I'm going to fix the bug. There is a workaround. You can write:
collection.find("{authz: /System#.*R/}")

No need to compile a java Pattern or kotlin Regexp :)

@sepatel
Copy link
Author

sepatel commented Mar 29, 2020

:) If I were hardcoding this specific thing sure, but its a variable coming from data model meaning that I then have to manually escape the regex pattern to try and protect from injection attacks or simply a regex pattern that has a / or other special character in it.

I think it would be best to wait for a fix and use a properly coded fix 👍

@zigzago zigzago closed this as completed in 49079fc Apr 5, 2020
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

No branches or pull requests

2 participants