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 minor crash for certain oddly formed *.js files #2128

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

PierceLBrooks
Copy link

Description

  • Source branch in your fork has meaningful name (not main)

Hello there! I recently discovered an interesting piece of apparent javascript code that I wanted to inspect further at https://www.scribd.com/document/512791356/Player - which has been attached here - and it was found that neither the https://beautifier.io/ web interface nor this python module could beautify the content without either hanging indefinitely or throwing an exception like this one below:

pbrooks@MacBook-Pro Downloads % python3 ./jsbeautify.py file://$PWD/Player.js ./Vimeo.js
Traceback (most recent call last):
  File "/Users/pbrooks/Downloads/./jsbeautify.py", line 40, in <module>
    main()
  File "/Users/pbrooks/Downloads/./jsbeautify.py", line 33, in main
    js = beauty(r["content"])
  File "/Users/pbrooks/Downloads/./jsbeautify.py", line 12, in beauty
    return jsbeautifier.beautify(content.decode())
  File "/opt/homebrew/lib/python3.10/site-packages/jsbeautifier/__init__.py", line 82, in beautify
    return b.beautify(string, opts)
  File "/opt/homebrew/lib/python3.10/site-packages/jsbeautifier/javascript/beautifier.py", line 187, in beautify
    self._tokens = Tokenizer(source_text, self._options).tokenize()
  File "/opt/homebrew/lib/python3.10/site-packages/jsbeautifier/core/tokenizer.py", line 71, in tokenize
    current = self.__get_next_token_with_comments(previous, open_token)
  File "/opt/homebrew/lib/python3.10/site-packages/jsbeautifier/core/tokenizer.py", line 93, in __get_next_token_with_comments
    current = self._get_next_token(previous, open_token)
  File "/opt/homebrew/lib/python3.10/site-packages/jsbeautifier/javascript/tokenizer.py", line 226, in _get_next_token
    token = token or self._read_regexp(c, previous_token)
  File "/opt/homebrew/lib/python3.10/site-packages/jsbeautifier/javascript/tokenizer.py", line 331, in _read_regexp
    if c == "/" and self.allowRegExOrXML(previous_token):
  File "/opt/homebrew/lib/python3.10/site-packages/jsbeautifier/javascript/tokenizer.py", line 514, in allowRegExOrXML
    and previous_token.opened.previous.type == TOKEN.RESERVED
AttributeError: 'NoneType' object has no attribute 'previous'

The beautification script I used can be located on this page... https://gist.github.com/PierceLBrooks/7a29d53f4915f847b82e41e2954ca000

Before Merge Checklist

These items can be completed after PR is created.

(Check any items that are not applicable (NA) for this PR)

  • JavaScript implementation
  • Python implementation (NA if HTML beautifier)
  • Added Tests to data file(s)
  • Added command-line option(s) (NA if
  • README.md documents new feature/option(s)

Player.txt

@bitwiseman
Copy link
Member

Could you add a test with the input that crashes without this fix?

@bitwiseman bitwiseman marked this pull request as draft July 5, 2023 19:56
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.

None yet

2 participants