PrismJS / PrismJS/prism

Add try catch to unguarded JSON.parse

Open
#3,850 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
13k
Forks
1.4k
Avg merge
15h 36m
Merged PRs (30d)
3

Description

Information:

  • Prism version: [e.g. 1.14, latest from the download page, etc.]: latest
  • Plugins: [a list of plugins you are using or 'none']: none
  • Environment: [e.g. Browser, Node, Webpack]: browser

Description
Inside the code, there is this snippet:

if (!_.disableWorkerMessageHandler) {
	// In worker
	_self.addEventListener('message', function (evt) {
		var message = JSON.parse(evt.data);
		var lang = message.language;
		var code = message.code;
		var immediateClose = message.immediateClose;
        
		_self.postMessage(_.highlight(code, _.languages[lang], lang));
		if (immediateClose) {
			_self.close();
		}
	}, false);
}

And it interferes with other code that don't send JSON data. We can add try catch that is ignored?
Probably my Chrome extensions.

Adding this:

window.Prism = window.Prism || {};
Prism.disableWorkerMessageHandler = true;

Makes no difference, I keep getting JSON parse error in console when including Prism.js.

I can create a PR.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Locate the worker message handler containing JSON.parse(evt.data) and read its surrounding code and tests, if any. Reproduce the browser message case described in the issue; done means unrelated non-JSON messages no longer produce an uncaught parse error while valid Prism worker messages continue to work.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.