Skip to content

Commit

Permalink
Incorrect error thrown when function is in comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Bouncey committed Sep 26, 2016
1 parent d3f8757 commit d5c3731
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion client/commonFramework/detect-unsafe-code-stream.js
Expand Up @@ -7,6 +7,9 @@ window.common = (function(global) {
const detectFunctionCall = /function\s*?\(|function\s+\w+\s*?\(/gi;
const detectUnsafeJQ = /\$\s*?\(\s*?\$\s*?\)/gi;
const detectUnsafeConsoleCall = /if\s\(null\)\sconsole\.log\(1\);/gi;
const detectInComments = new RegExp(['\\/\\/.*?function.*?|',

This comment has been minimized.

Copy link
@ktajpuri

ktajpuri Sep 26, 2016

Contributor

could you please explain this regex ? I tried to figure out but seems too tough for me.

This comment has been minimized.

Copy link
@Bouncey

Bouncey Sep 26, 2016

Author Member

Sure, so I had to build this RegEx from an array of strings. This allowed me to wrap the regex onto new lines and keep within the 80 char limit.

In its proper form the regex looks like this:

const detectInComments = /\/\/.*?function.*?|\/\*[\s\w\W]*?function[\s\w\W]*?\*\//gi;

The regex is looking to match:

// anything here except newlines (zero to unlimited times) function & anything here except newlines (zero to unlimited times)

0r

/* some stuff here like !"£$:@~ or 123456 and new lines (zero to unlimited)
maybe a function here
more of this possibly )(*&^^%$£98765 (zero to unlimited) */
'\\/\\*[\\s\\w\\W]*?function',
'[\\s\\w\\W]*?\\*\\/'].join(''), 'gi');

common.detectUnsafeCode$ = function detectUnsafeCode$(code) {
const openingComments = code.match(/\/\*/gi);
Expand Down Expand Up @@ -35,7 +38,8 @@ window.common = (function(global) {

if (
code.match(/function/g) &&
!code.match(detectFunctionCall)
!code.match(detectFunctionCall) &&
!code.match(detectInComments)
) {
return Observable.throw(
new Error('SyntaxError: Unsafe or unfinished function declaration')
Expand Down

0 comments on commit d5c3731

Please sign in to comment.