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

Slow regexp for JSON #1080

Closed
timehat opened this issue Jan 10, 2017 · 4 comments
Closed

Slow regexp for JSON #1080

timehat opened this issue Jan 10, 2017 · 4 comments

Comments

@timehat
Copy link

timehat commented Jan 10, 2017

Happened upon an instance for the JSON highlighting we have enabled in our product where the regexp in prism was taking a very long time (long enough to act as an effective denial of service issue on our web app).

Our user posted a JSON log containing a Java stacktrace, which included several instances of the combination of \n\t with lines of the stack trace between each instance. I've reduced down the entire JSON blob and found that I can cause slowness on the Prism demo with the following snippet:

{ "a": "\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t" }

The perf hit appears to grow exponentially (see below) as the instances of the "\n\t" increase, such that this will probably just result in you getting a "tab not responding" message.

{ "a": "\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t" }

I haven't looked closely at the regex, but did find that the slowness occurred on the 2nd iteration of the /"(?:\\.|[^|"])*"(?=\s*:)/gi pattern used in tokenLoop (when set to JSON).

Unfortunately I cannot debug more into the regex itself to understand, but wanted to provide the heads up on a potential string that can cripple usages of the JSON formatter.

Exponential growth as more back slashes are added to the string:
image

@timehat
Copy link
Author

timehat commented Jan 10, 2017

After one more check, really having too many \ characters will get you into this situation:

{ "a": "\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\" }
{ "a": "\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1" }
{ "a": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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aaaaaaaaaaaaaaaaaaaaaaaaaaa\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" }

@Golmote
Copy link
Contributor

Golmote commented Jan 10, 2017

Thanks for your precise report! This should be fixed now.

@timehat
Copy link
Author

timehat commented Jan 10, 2017

Thank you for the fast turnaround. 👍

@LeaVerou
Copy link
Member

Awesome @Golmote is awesome!

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

3 participants