GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Promote SSRF query from experimental by smowton · Pull Request #5587 · github/codeql

Conversation

@smowton

@smowton smowton commented Apr 1, 2021 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Contributes to https://github.com/github/codeql-java-team/issues/37

Evaluation: the original experimental query produced alerts from 412 projects. I took an arbitrary 100-project sample and eliminated 38 hits by (a) excluding the results of other HTTP fetches as sources, since fetching from some URL and then using the result to direct another fetch is routine and usually safe, and (b) excluding strings that occur to the right of a sanitising prefix, like "http://mysite.com/" + tainted().

Reviewing 30 (again, arbitrarily chosen) hits by hand, I found:

  • 1 FP due to concat with a variable that is in practice always safe (not readily fixable)
  • 1 FP due to substringing a URI to keep only the non-user-controlled parts (not readily fixable)
  • 2 FP due to considering integers potentially dangerous (general issue)
  • 2 FP due to using ad-hoc string replacment to sanitize (e.g. "http://myhost.com/@".replace("@", tainted()) (not readily fixable)
  • 1 FP due to admin-only endpoint (not readily fixable)
  • 1 FP due to concatenation like someServer + "/" + tainted() (fixable) (fixed)
  • 19 TP
  • 1 unsure

However it's worth noting that of the TPs, only one or two were likely to be accidents: most were deliberately allowing a user to e.g. specify an image URL to fetch, which naturally means we can use the server to DoS some third site / probe its internal network unless there are other mitigations we can't see in place.

This work suggests followups:

  • Conversion to an integer type should probably be considered sanitising for most queries that require a full character set to exploit their particular vulnerability.
  • Using StringBuilderVar and/or slightly extending StringFormatter to spot various cases of appending / concatenation was difficult. We should consider generalising these to give a general-purpose "string appended to other string" framework. I haven't extended StringBuilderVar here because that would increase the scope of queries whose results may be perturbed and need to be re-verified.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@smowton
smowton requested a review from a team April 1, 2021 15:45
@github-actions github-actions Bot added documentation Java labels Apr 1, 2021
jorgectf
jorgectf reviewed Apr 1, 2021
View reviewed changes
Comment thread java/ql/src/Security/CWE/CWE-918/RequestForgery.qll Outdated
/** A sanitizer for request forgery vulnerabilities. */
abstract class RequestForgerySanitizer extends DataFlow::Node { }

private class HostnameSanitzingPrefix extends CompileTimeConstantExpr {

@jorgectf jorgectf Apr 1, 2021

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Don't want to taint the PR discussion, but perhaps this is meant to be HostnameSan(i)tizingPrefix?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@smowton smowton Apr 6, 2021

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Thanks, fixed

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

jorgectf reacted with hooray emoji
All reactions
  • 🎉 1 reaction
@aschackmull

aschackmull commented Apr 13, 2021

Copy link
Copy Markdown
Contributor
  • Conversion to an integer type should probably be considered sanitising for most queries that require a full character set to exploit their particular vulnerability.

This sanitizer is very easy to add to a query and is already used in a few places. We might as well add it to the query here in this PR, I guess.
Sanitizer code:

node.getType() instanceof NumericType or node.getType() instanceof BooleanType

(Possibly also NumberType)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

aschackmull
aschackmull reviewed Apr 13, 2021
View reviewed changes
Comment thread java/change-notes/2021-04-06-ssrf-query.md Outdated
@@ -0,0 +1,2 @@
lgtm,codescanning
* The server-side request forgery (SSRF) query has been promoted from experimental to the main query pack. Its results will now appear by default.

@aschackmull aschackmull Apr 13, 2021

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Should we credit the original author in the change note? There was some discussion about whether or not to do this in https://github.com/github/codeql-release-notes/pull/10, but I don't know if that reached a conclusion.
I also think that we should include the query id, we tend to reference queries in change notes by name (id).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@intrigus-lgtm intrigus-lgtm Apr 13, 2021

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

The C/C++ team seems to do that:
https://github.com/github/codeql/pull/5256/files#diff-6947ef4ef4cef28d3a7a76c1aec757c8695b621922b33c4a5c00ca443c2558cd

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@aschackmull

aschackmull commented Apr 13, 2021

Copy link
Copy Markdown
Contributor

There are a couple of typos in the qhelp, which should be fixed. Also, we should get a doc review.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@smowton

smowton commented Apr 14, 2021

Copy link
Copy Markdown
Contributor Author

@aschackmull comments applied. I've simply sanitised PrimitiveType since there are no primitives that serve as a realistic SSRF vector, credited @porcupineyhairs and copyedited the qhelp.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@smowton smowton added the ready-for-doc-review This PR requires and is ready for review from the GitHub docs team. label Apr 14, 2021
@smowton

smowton commented Apr 14, 2021

Copy link
Copy Markdown
Contributor Author

@shati-patel @felicitymay note this is a promoted experimental query, so docs review should look at the whole query not just parts altered in promoting it from experimental to the main query set.

felicitymay reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

aschackmull
aschackmull reviewed Apr 14, 2021
View reviewed changes
Comment thread java/change-notes/2021-04-06-ssrf-query.md Outdated
@@ -0,0 +1,2 @@
lgtm,codescanning
* The server-side request forgery (SSRF) query has been promoted from experimental to the main query pack. Its results will now appear by default. Thanks to original experimental query author @porcupineyhairs.

@aschackmull aschackmull Apr 14, 2021

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Suggested change
* The server-side request forgery (SSRF) query has been promoted from experimental to the main query pack. Its results will now appear by default. Thanks to original experimental query author @porcupineyhairs.
* The query "Server Side Request Forgery (SSRF)" (`java/ssrf`) has been promoted from experimental to the main query pack. Its results will now appear by default. Thanks to original experimental query author @porcupineyhairs.

If it's "server-side" rather than "server side" then the query name should be updated in the query meta-data.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@smowton smowton Apr 15, 2021

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Server-side is better grammar (it's a compound noun), but sources on the internet and in our codebase are inconsistent. I've switched to the hyphenated form throughout here.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
aschackmull
aschackmull reviewed Apr 14, 2021
View reviewed changes
Comment thread java/ql/src/Security/CWE/CWE-918/RequestForgery.qll Outdated
@@ -5,7 +5,12 @@ import semmle.code.java.frameworks.spring.Spring
import semmle.code.java.frameworks.JaxWS

@aschackmull aschackmull Apr 14, 2021

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

We aim to support extensibility in the security queries, by putting a .qll in semmle.code.java.Security that includes a couple of abstract classes for the most common extensions - in this case I think we should expose sinks, additional steps, and sanitizers for customization.

To do this, I think this entire file should be split in two parts. The ad-hoc and very query specific things can move to the .ql whereas the parts that are needed to support this kind of extension should move to the importable dir.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@aschackmull aschackmull Apr 14, 2021

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

In particular, the abstract class RequestForgerySink and abstract class RequestForgerySanitizer should be importable. (And their private subclasses should likely live in the file next to those definitions).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@smowton

smowton commented Apr 15, 2021

Copy link
Copy Markdown
Contributor Author

@aschackmull all changes applied. I didn't move any code from the .qll file as everything is either a default extension point instance, or is used to implement one. However I did move the file as requested.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@smowton smowton mentioned this pull request Apr 16, 2021
Java: queries about user-controlled URLs should be sanitized by enforcing a prefix #4530
Open
felicitymay
felicitymay reviewed Apr 19, 2021
View reviewed changes

@felicitymay felicitymay left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Most of my comments highlight fairly trivial inconsistencies. Generally the text seems okay to merge.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread java/change-notes/2021-04-06-ssrf-query.md Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread java/ql/src/Security/CWE/CWE-918/RequestForgery.ql Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread java/ql/src/Security/CWE/CWE-918/RequestForgery.ql Outdated
/**
* @name Server-Side Request Forgery (SSRF)
* @description Making web requests based on unvalidated user-input
* may cause server to communicate with malicious servers.

@felicitymay felicitymay Apr 19, 2021

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Suggested change
* may cause server to communicate with malicious servers.
* may allow the server to communicate with malicious servers.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread java/ql/src/semmle/code/java/security/RequestForgery.qll Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread java/ql/src/semmle/code/java/security/RequestForgery.qll Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread java/ql/src/semmle/code/java/security/RequestForgery.qll Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread java/ql/src/semmle/code/java/security/RequestForgery.qll Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread java/ql/src/semmle/code/java/security/RequestForgery.qll Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread java/ql/src/Security/CWE/CWE-918/RequestForgery.qhelp Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread java/ql/src/Security/CWE/CWE-918/RequestForgery.qhelp Outdated
Comment on lines 16 to 18
<p>To guard against SSRF attacks, it is advisable to avoid putting user input
directly into the request URL. Instead, maintain a list of authorized
directly into a request URL. Instead, maintain a list of authorized
URLs on the server; then choose from that list based on the user input provided.</p>

@felicitymay felicitymay Apr 19, 2021

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

It won't allow me to make a suggestion 😞 This may be clearer as:

To guard against SSRF attacks, you should avoid putting user-provided input
directly into a request URL. Instead, maintain a list of authorized
URLs on the server; then choose from that list based on the input provided.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
smowton
smowton commented Apr 19, 2021
View reviewed changes
Comment thread java/ql/src/Security/CWE/CWE-918/RequestForgery.ql Resolved

Uh oh!

There was an error while loading. Please reload this page.

smowton
smowton commented Apr 19, 2021
View reviewed changes
Comment thread java/ql/src/semmle/code/java/security/RequestForgery.qll Resolved

Uh oh!

There was an error while loading. Please reload this page.

smowton
smowton commented Apr 19, 2021
View reviewed changes
Comment thread java/ql/src/semmle/code/java/security/RequestForgery.qll Resolved

Uh oh!

There was an error while loading. Please reload this page.

smowton
smowton commented Apr 19, 2021
View reviewed changes
Comment thread java/ql/src/semmle/code/java/security/RequestForgery.qll Resolved

Uh oh!

There was an error while loading. Please reload this page.

smowton
smowton commented Apr 19, 2021
View reviewed changes
Comment thread java/ql/src/semmle/code/java/security/RequestForgery.qll Resolved

Uh oh!

There was an error while loading. Please reload this page.

smowton
smowton commented Apr 19, 2021
View reviewed changes
Comment thread java/ql/src/semmle/code/java/security/RequestForgery.qll Resolved

Uh oh!

There was an error while loading. Please reload this page.

smowton
smowton commented Apr 19, 2021
View reviewed changes
Comment thread java/ql/src/semmle/code/java/security/RequestForgery.qll Resolved

Uh oh!

There was an error while loading. Please reload this page.

smowton
smowton commented Apr 19, 2021
View reviewed changes
Comment thread java/ql/src/semmle/code/java/security/RequestForgery.qll Resolved

Uh oh!

There was an error while loading. Please reload this page.

smowton
smowton commented Apr 19, 2021
View reviewed changes
Comment thread java/ql/src/semmle/code/java/security/RequestForgery.qll Resolved

Uh oh!

There was an error while loading. Please reload this page.

@smowton

smowton commented Apr 19, 2021

Copy link
Copy Markdown
Contributor Author

Thanks @felicitymay, all applied. @aschackmull this is up for review again.

felicitymay reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

intrigus-lgtm
intrigus-lgtm reviewed Apr 19, 2021
View reviewed changes
Comment thread java/ql/src/semmle/code/java/security/RequestForgery.qll Resolved

Uh oh!

There was an error while loading. Please reload this page.

intrigus-lgtm
intrigus-lgtm reviewed Apr 19, 2021
View reviewed changes
Comment thread java/ql/src/semmle/code/java/security/RequestForgery.qll Resolved

Uh oh!

There was an error while loading. Please reload this page.

intrigus-lgtm
intrigus-lgtm reviewed Apr 19, 2021
View reviewed changes
Comment thread java/ql/src/semmle/code/java/security/RequestForgery.qll Resolved

Uh oh!

There was an error while loading. Please reload this page.

intrigus-lgtm
intrigus-lgtm reviewed Apr 19, 2021
View reviewed changes
Comment thread java/ql/src/semmle/code/java/security/RequestForgery.qll Outdated
HostnameSanitizingPrefix() {
exists(
this.getStringValue()
.regexpFind(".*([?#]|[^?#:/\\\\][/\\\\]).*|[/\\\\][^/\\\\].*|^/$", 0, offset)

@intrigus-lgtm intrigus-lgtm Apr 19, 2021

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Maybe add a few examples that this regex will find?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

smowton reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
smowton
smowton commented Apr 19, 2021
View reviewed changes
Comment thread java/ql/src/semmle/code/java/security/RequestForgery.qll Resolved

Uh oh!

There was an error while loading. Please reload this page.

smowton and others added 26 commits June 17, 2021 11:41
@smowton
Java SSRF query: comment on sanitizing regex
b25e867
@smowton @intrigus-lgtm
Improve comment casing
9138d2b
Co-authored-by: intrigus-lgtm <[email protected]>
@smowton
Fix casing of change note
8d70e3d
@smowton
Update test results to account for changed model structure
1549993
(Models now have internal nodes in order to allow field flow through them)
@smowton @aschackmull
Copyedit documentation
0db5484
Co-authored-by: Anders Schack-Mulligen <[email protected]>
@smowton
Fix getAnArgUsageOffset and improve its space complexity
f388aae
Also add tests checking the output of the new function
@smowton
Tidy and remove catersian product from getUrlArgument
3167af2
@smowton
Broaden PrimitiveSanitizer to include boxed primitives and other java…
6b76f42
….lang.Numbers
@smowton
Simplify and improve AddExpr logic
5b25694
The improvement is in considering (userSupplied + "/") itself a sanitising prefix.
@smowton
Improve StringBuilder append chain tracking
55c72ce
Previously this didn't catch the case of constructors chaining directly into appends, like `StringBuilder sb = new StringBuilder("1").append("2")`
@smowton
Fix and document one-based argument indexing in StringFormat's `getAn…
0f2139f
…ArgUsageOffset`
@smowton
Convert SSRF sinks into url-open CSV sinks
49bbfc3
I also drop the previous approach of taint-tracking through various builder objects in favour of assuming that a URI set in a request-builder object is highly likely to end up requested in some way or another.

This will cause the `java/non-https-url` query to pick the new sinks up too, and fixes a Spring case that had never worked but went unnoticed until now.
@smowton
Add missing tests, add additional models revealed missing in the proc…
ee872f1
…ess, and add stubs to support them all.
@smowton
Factor request-forgery config so it can be used in an inline-expectat…
b66dcbe
…ions test
@smowton
Convert request forgery tests to inline expectations; add missing mod…
8b080a9
…els revealed by this process.
@smowton
Tidy Spring test
57ca36b
@smowton
Tidy Jax-RS test
74569ce
@smowton
Remove redundant test
08ab5f5
@smowton
Remove dead code from test
6c4a909
@smowton
Split and rename JavaNetHttp and ApacheHttp tests for consistency
cb99e17
@smowton
Rename RequestForgery.java -> SanitizationTests.java
c531b81
@smowton
Remove no-longer-needed BasicRequestLine model from InsecureBasicAuth…
7509e36
….ql; adjust test expectations accordingly
@smowton
Note incidental extra models in change note
09f2755
@smowton @aschackmull
Improve docs
1176fec
Co-authored-by: Anders Schack-Mulligen <[email protected]>
@smowton
Adjust Spring models to use erased function signatures
da1e760
@smowton
Add Jakarta WS url-open sink
11b7032
@smowton
smowton force-pushed the smowton/admin/promote-ssrf-query branch from 8d5e1ad to 11b7032 Compare June 17, 2021 10:59
@smowton

smowton commented Jun 17, 2021

Copy link
Copy Markdown
Contributor Author

@aschackmull rebased to resolve conflicts with just-merged Jax-WS models.

Actual changes:

Trivial docs fixes: 1176fec
Adapt models to use erased signatures: da1e760
Add models and tests for Jakarta WS: 11b7032

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

aschackmull
aschackmull approved these changes Jun 17, 2021
View reviewed changes
@smowton
smowton merged commit 64001cc into github:main Jun 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@aschackmull aschackmull aschackmull approved these changes
+5 more reviewers
@felicitymay felicitymay felicitymay left review comments
@atorralba atorralba atorralba left review comments
@jorgectf jorgectf jorgectf left review comments
@max-schaefer max-schaefer max-schaefer left review comments
@intrigus-lgtm intrigus-lgtm intrigus-lgtm left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

documentation Java

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

7 participants

@smowton @aschackmull @felicitymay @atorralba @jorgectf @max-schaefer @intrigus-lgtm
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.