Skip to content

JS: promote the js/missing-origin-verification query#8724

Merged
erik-krogh merged 11 commits into
github:mainfrom
erik-krogh:postMessage
May 9, 2022
Merged

JS: promote the js/missing-origin-verification query#8724
erik-krogh merged 11 commits into
github:mainfrom
erik-krogh:postMessage

Conversation

@erik-krogh
Copy link
Copy Markdown
Contributor

@erik-krogh erik-krogh commented Apr 12, 2022

CVE-2022-24762: TP/TN

We already treat the data from a "message" event as a RemoteFlowSource.

However, sometimes there might be a security issue related to the "message" event that we don't flag.

  • Maybe data is forwarded using a very dynamic message passing system (surprisingly common for "message" events).
  • Maybe there is no XSS/Code/Command sink, and the security issue is information disclosure (e.g. CVE-2022-24762).

For these reasons it can be a good idea to flag all "message" events that have no origin check.
I've placed it as a medium precision warning query, so it is not enabled by default.

Here are some example results.


I organized the tests in CWE-020 into subfolders because it was starting to be hard to keep track of them.
I did a drive-by addition to PostMessageEventSanitizer, such that it now recognizes inclusion-tests.

Evaluation will come later when DCA starts working again.

@erik-krogh erik-krogh added the Awaiting evaluation Do not merge yet, this PR is waiting for an evaluation to finish label Apr 12, 2022
@erik-krogh erik-krogh marked this pull request as ready for review April 12, 2022 13:40
@erik-krogh erik-krogh requested a review from a team as a code owner April 12, 2022 13:40
@erik-krogh
Copy link
Copy Markdown
Contributor Author

The evaluation looks about as expected.

No performance regression, but lots of new results.
The results look reasonable, but it's still not something I want to be enabled in the default suite.

@erik-krogh erik-krogh removed the Awaiting evaluation Do not merge yet, this PR is waiting for an evaluation to finish label Apr 20, 2022
Comment thread javascript/ql/src/Security/CWE-020/MissingOriginCheck.qhelp Outdated
result =
origin(t.continue(), handler)
.getAMethodCall([
"toString", "toLowerCase", "toUpperCase", "toLocaleLowerCase", "toLocaleUpperCase"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not relevant for this PR:

"toLocaleLowerCase", "toLocaleUpperCase"

Hmm. I would consider it a bug to use those instead of the canonical to(Lower|Upper)Case calls, but perhaps the programmers are saved by the fact that all browser origins normalizes the same way regardless of locales.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hmm. I would consider it a bug to use those instead of the canonical to(Lower|Upper)Case calls

It would definitely be weird to use the Locale variants of those methods.
I mainly included them because Copilot suggested them.

}

/** Holds if there exists a check of the .origin or .source of the postmessage `handler`. */
predicate hasOriginCheck(PostMessageHandler handler) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I expected to see various startsWith and endsWith variants here. They can be sufficient if done correctly, if they are done incorrectly, we could flag them with a separate query.

Did you see any FPs due to not recognizing such checks?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Did you see any FPs due to not recognizing such checks?

I went through the example results, and there was indeed an FP related to startsWith: https://lgtm.com/query/4833361159564412520/
I'll get StartsWith/EndsWith in there.

@erik-krogh erik-krogh requested a review from esbena April 29, 2022 13:32
esbena
esbena previously approved these changes May 3, 2022
@esbena esbena added the ready-for-doc-review This PR requires and is ready for review from the GitHub docs team. label May 3, 2022
@mchammer01
Copy link
Copy Markdown
Contributor

👋🏻 - I'll try to review this later today. Sorry for the delay, the team is understaffed this week 😢

@mchammer01 mchammer01 self-requested a review May 5, 2022 09:30
mchammer01
mchammer01 previously approved these changes May 5, 2022
Copy link
Copy Markdown
Contributor

@mchammer01 mchammer01 left a comment

Choose a reason for hiding this comment

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

@erik-krogh - this LGTM ✨
A few editorial comments/suggestions for your consideration.
I believe that you do not need to add the MITRE CWE link at the end of the References section in the qhelp file as this is done automatically behind the scenes from the tags specified in the ql file.
Approving this so I am not blocking you.

Comment thread javascript/ql/src/Security/CWE-020/MissingOriginCheck.qhelp Outdated
Comment thread javascript/ql/src/Security/CWE-020/MissingOriginCheck.qhelp Outdated
Comment thread javascript/ql/src/Security/CWE-020/MissingOriginCheck.qhelp Outdated
Comment thread javascript/ql/src/Security/CWE-020/MissingOriginCheck.qhelp Outdated
Comment thread javascript/ql/src/Security/CWE-020/MissingOriginCheck.qhelp Outdated
Comment thread javascript/ql/src/Security/CWE-020/MissingOriginCheck.qhelp Outdated
Co-authored-by: mc <42146119+mchammer01@users.noreply.github.com>
@erik-krogh erik-krogh dismissed stale reviews from mchammer01 and esbena via 2d7c7ff May 5, 2022 11:03
@erik-krogh erik-krogh requested review from esbena and mchammer01 May 5, 2022 11:04
Copy link
Copy Markdown

@github-advanced-security github-advanced-security AI left a comment

Choose a reason for hiding this comment

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

Found 13 vulnerabilities.

* An equality test on `e.origin` or `e.source` where `e` is a `postMessage` event object,
* considered as a sanitizer for `e`.
*/
private class PostMessageEventSanitizer extends AdditionalSanitizerGuardNode, DataFlow::ValueNode {

Check warning

Code scanning / CodeQL

Using implicit `this`

Use of implicit `this`.

PostMessageEventSanitizer() {
exists(string prop | prop = "origin" or prop = "source" |
astNode.getAnOperand().(PropAccess).accesses(event, prop) and

Check warning

Code scanning / CodeQL

Using implicit `this`

Use of implicit `this`.
exists(string prop | prop = "origin" or prop = "source" |
astNode.getAnOperand().(PropAccess).accesses(event, prop) and
event.mayReferToParameter(any(PostMessageEventHandler h).getEventParameter())
event.mayReferToParameter(any(PostMessageEventHandler h).getEventParameter()) and

Check warning

Code scanning / CodeQL

Using implicit `this`

Use of implicit `this`.
test.getAnOperand().flow() = read
)
or
exists(InclusionTest test | polarity = test.getPolarity() and this = test |

Check warning

Code scanning / CodeQL

Using implicit `this`

Use of implicit `this`.

override predicate sanitizes(boolean outcome, Expr e) {
outcome = astNode.getPolarity() and
outcome = polarity and

Check warning

Code scanning / CodeQL

Using implicit `this`

Use of implicit `this`.

Check warning

Code scanning / CodeQL

Using implicit `this`

Use of implicit `this`.

Check warning

Code scanning / CodeQL

Using implicit `this`

Use of implicit `this`.

Check warning

Code scanning / CodeQL

Using implicit `this`

Use of implicit `this`.

Check warning

Code scanning / CodeQL

Using implicit `this`

Use of implicit `this`.

Check warning

Code scanning / CodeQL

Using implicit `this`

Use of implicit `this`.
mchammer01
mchammer01 previously approved these changes May 5, 2022
Copy link
Copy Markdown
Contributor

@mchammer01 mchammer01 left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for addressing my comments ✨
Sorry, added a minor suggestion as there were missing words in a sentence that I seem to have missed during my first review 😞


<p>
The example is fixed below, where the origin is checked to be trusted.
It is therefore not possible for a malicious user to attack using an untrusted origin.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
It is therefore not possible for a malicious user to attack using an untrusted origin.
It is therefore not possible for a malicious user to raise an attack using an untrusted origin.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hmmm. I'm not fond the the raise an addition.
How about perform an instead?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Perform an attack is absolutely fine 👍🏻

Comment thread javascript/ql/src/Security/CWE-020/MissingOriginCheck.qhelp Outdated
@erik-krogh erik-krogh merged commit 53b26eb into github:main May 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation JS ready-for-doc-review This PR requires and is ready for review from the GitHub docs team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants