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

Match inline event handlers in JavaScript files #18

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

yaroslavafenkin
Copy link

Addresses #16.

Known limitation:
If JavaScript is poorly formatted may result in false positives. E.g. document. onload=function(){} will get matched.

@yaroslavafenkin
Copy link
Author

yaroslavafenkin commented Nov 21, 2024

@daniel-beck I've also noticed in Java patterns we don't account for inline handlers being inside single quotes or no quotes at all. Was that intentional by chance?

@@ -123,7 +125,7 @@ private static void visitFile(File file, Set<Match> matches) throws IOException

if (fileName.endsWith(".java")) {
final String text = readFileToString(file);
printMatches(matchRegexes(JAVA_PATTERNS, text, file));
matches.addAll(matchRegexes(JAVA_PATTERNS, text, file));
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unsure if it was done intentionally. Will revert if was.

@daniel-beck
Copy link
Owner

I've also noticed in Java patterns we don't account for inline handlers being inside single quotes or not quotes at all. Was that intentional by chance?

Could you provide a code example where that would happen?

@yaroslavafenkin
Copy link
Author

I've also noticed in Java patterns we don't account for inline handlers being inside single quotes or not quotes at all. Was that intentional by chance?

Could you provide a code example where that would happen?

https://github.com/jenkinsci/workflow-support-plugin/blob/f8d41ac42279d1811b81b0afb5e6e2e4b95843fe/src/main/java/org/jenkinsci/plugins/workflow/support/steps/input/POSTHyperlinkNote.java#L86

I can imagine that being

return " onclick='fetch(decodeURIComponent(atob('" + encodeForJavascript(url) + "')), { method: 'post', headers: crumb.wrap({})}); return false'";

or

return " onclick=fetch(decodeURIComponent(atob('" + encodeForJavascript(url) + "')), { method: 'post', headers: crumb.wrap({})}); return false";

@daniel-beck
Copy link
Owner

daniel-beck commented Nov 22, 2024

I don't see the problem. Adding those two suggested lines to the original, I get:

% java -jar target/csp-scanner.jar ~/workflow-support-plugin/                                    
== Inline Event Handler (Java)
File: /Users/danielbeck/workflow-support-plugin/src/main/java/org/jenkinsci/plugins/workflow/support/steps/input/POSTHyperlinkNote.java +
Line: 86
----
onclick=\"fetch(decodeURIComponent(atob('"
----

== Inline Event Handler (Java)
File: /Users/danielbeck/workflow-support-plugin/src/main/java/org/jenkinsci/plugins/workflow/support/steps/input/POSTHyperlinkNote.java +
Line: 87
----
onclick='fetch(decodeURIComponent(atob('"
----

== Inline Event Handler (Java)
File: /Users/danielbeck/workflow-support-plugin/src/main/java/org/jenkinsci/plugins/workflow/support/steps/input/POSTHyperlinkNote.java +
Line: 88
----
onclick=fetch(decodeURIComponent(atob('"
----

Are you misinterpreting the quote matching that accounts for all hits in a Java source file necessarily being part of a double-quoted Java string as being part of the HTML attribute match?

@yaroslavafenkin
Copy link
Author

I don't see the problem. Adding those two suggested lines to the original, I get:

% java -jar target/csp-scanner.jar ~/workflow-support-plugin/                                    
== Inline Event Handler (Java)
File: /Users/danielbeck/workflow-support-plugin/src/main/java/org/jenkinsci/plugins/workflow/support/steps/input/POSTHyperlinkNote.java +
Line: 86
----
onclick=\"fetch(decodeURIComponent(atob('"
----

== Inline Event Handler (Java)
File: /Users/danielbeck/workflow-support-plugin/src/main/java/org/jenkinsci/plugins/workflow/support/steps/input/POSTHyperlinkNote.java +
Line: 87
----
onclick='fetch(decodeURIComponent(atob('"
----

== Inline Event Handler (Java)
File: /Users/danielbeck/workflow-support-plugin/src/main/java/org/jenkinsci/plugins/workflow/support/steps/input/POSTHyperlinkNote.java +
Line: 88
----
onclick=fetch(decodeURIComponent(atob('"
----

Are you misinterpreting the quote matching that accounts for all hits in a Java source file necessarily being part of a double-quoted Java string as being part of the HTML attribute match?

Hm, I've also checked and all of the cases are matched. Then I should revisit the regex I'm adding, likely can be simplified.

@@ -36,7 +36,9 @@ public class Scanner {
// Examples indicate trying to match open-paren would be too restrictive:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval#direct_and_indirect_eval
// geval is defined in hudson-behavior.js
protected static final Map<String, Pattern> JS_PATTERNS = Map.of("(g)eval Call", Pattern.compile("\\Wg?eval\\W"));
protected static final Map<String, Pattern> JS_PATTERNS = Map.of("(g)eval Call", Pattern.compile("\\Wg?eval\\W"),
"Inline Event Handler (JavaScript)", Pattern.compile("[\\s+]" + JS_EVENT_ATTRIBUTES + ".*?((?<!\\\\)['\"]?)[_\\w]+\\s*\\(", Pattern.CASE_INSENSITIVE),
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"Inline Event Handler (JavaScript)", Pattern.compile("[\\s+]" + JS_EVENT_ATTRIBUTES + ".*?((?<!\\\\)['\"]?)[_\\w]+\\s*\\(", Pattern.CASE_INSENSITIVE),
"Inline Event Handler (JavaScript)", Pattern.compile("[\\s+]" + JS_EVENT_ATTRIBUTES + "=.*(['"]?)[$_\\w]+\\s*\\(", Pattern.CASE_INSENSITIVE),

Could simplify to this. [$_\\w]+ is not a complete regex for valid JS identifiers though. But maybe good enough?

Can also be

Suggested change
"Inline Event Handler (JavaScript)", Pattern.compile("[\\s+]" + JS_EVENT_ATTRIBUTES + ".*?((?<!\\\\)['\"]?)[_\\w]+\\s*\\(", Pattern.CASE_INSENSITIVE),
"Inline Event Handler (JavaScript)", Pattern.compile("[\\s+]" + JS_EVENT_ATTRIBUTES + "=.*(['"]?)", Pattern.CASE_INSENSITIVE),

This will match the rest of the line after the handler.

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.

2 participants