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 — [Java] CWE-552: Unsafe url forward by haby0 · Pull Request #6240 · github/codeql

Conversation

@haby0

@haby0 haby0 commented Jul 8, 2021 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

Constructing a server-side redirect path with user input could allow an attacker to download application binaries
(including application classes or jar files) or view arbitrary files within protected directories.

Sorry, something went wrong.

Uh oh!

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

All reactions
@haby0
haby0 requested a review from a team as a code owner July 8, 2021 11:44
@github-actions github-actions Bot added documentation Java labels Jul 8, 2021
@haby0 haby0 changed the title [Java] CWE-348: Unsafe url forward [Java] CWE-552: Unsafe url forward Jul 8, 2021
This was referenced Jul 12, 2021
Java: CWE-552 Query to detect configuration file/source code exposure from unsafe request dispatcher #6251
Closed
[Java] CWE-552: Unsafe url forward github/securitylab#399
Closed
smowton
smowton reviewed Jul 13, 2021
View reviewed changes

@smowton smowton 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.

Only taken a quick look so do please check in more detail, but I think there are already queries for the Spring aspects of this. getRequestDispatcher(...) does appear to be new.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread java/ql/src/experimental/Security/CWE/CWE-552/UnsafeUrlForward.qll Outdated
}

/** A Unsafe url forward sink from spring controller method. */
private class SpringUrlForwardSink extends UnsafeUrlForwardSink {

@smowton smowton Jul 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.

Overlap with CWE-601/SpringUrlRedirect.ql?

Sorry, something went wrong.

Uh oh!

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

All reactions

@haby0 haby0 Jul 13, 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.

No overlap, no url jump will be triggered here, files will be read.

Sorry, something went wrong.

Uh oh!

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

All reactions

@haby0 haby0 Jul 13, 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.

Hello @pwntester, do you have any suggestions for this?

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread java/ql/src/experimental/Security/CWE/CWE-552/UnsafeUrlForward.qll Outdated
)
or
exists(MethodAccess ma |
ma.getMethod().hasName("setViewName") and

@smowton smowton Jul 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.

Overlap with CWE-094/SpringViewManipulation.ql?

Sorry, something went wrong.

Uh oh!

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

All reactions

@haby0 haby0 Jul 13, 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.

CWE-094/SpringViewManipulation must use the Thymeleaf template engine.

Sorry, something went wrong.

Uh oh!

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

All reactions
@haby0
haby0 dismissed a stale review via df93cd0 September 16, 2021 12:14
pwntester
pwntester suggested changes Sep 23, 2021
View reviewed changes
Comment thread java/ql/src/experimental/Security/CWE/CWE-552/UnsafeUrlForward.ql Outdated
Comment on lines +30 to +45
exists(AddExpr ae |
ae.getRightOperand() = node.asExpr() and
(
not ae.getLeftOperand().(CompileTimeConstantExpr).getStringValue().matches("/WEB-INF/%")
and
not ae.getLeftOperand().(CompileTimeConstantExpr).getStringValue() = "forward:"
)
)

@pwntester pwntester Jul 12, 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.

Needs to account for string building patterns. This has been implemented in the past (#4530)

Sorry, something went wrong.

Uh oh!

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

All reactions

@haby0 haby0 Sep 24, 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.

I made changes, please review again, thank you!

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread java/ql/src/experimental/Security/CWE/CWE-552/UnsafeUrlForward.ql Outdated
Comment on lines +23 to +44
not exists(MethodAccess ma |
ma.getMethod().getName() in ["getRequestURI", "getRequestURL", "getPathInfo"] and
ma.getMethod()
.getDeclaringType()
.getASupertype*()
.hasQualifiedName("javax.servlet.http", "HttpServletRequest")
)
}

@pwntester pwntester Sep 23, 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.

You forgot to connect ma and source. Probably you want to add source.asExpr() = ma

Sorry, something went wrong.

Uh oh!

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

All reactions
@smowton
smowton requested a review from atorralba September 24, 2021 17:51
atorralba
atorralba reviewed Sep 27, 2021
View reviewed changes

@atorralba atorralba 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.

I added some inline comments.

Also, I agree there's some overlap with SpringUrlRedirect, since that query also looks for forward: URLs, but I guess it makes sense because that would be both an open redirect and a local file include. So I think there's no need to change it in this PR, but it's something to take into account when we eventually promote all of these (altogether with the copy-pasted code from SpringUrlRedirect, SpringViewManipulation and RequestForgery).

Sorry, something went wrong.

Uh oh!

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

haby0 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Comment thread java/ql/lib/semmle/code/java/frameworks/Servlets.qll Outdated
Comment on lines +201 to +202
getNumberOfParameters() = 1 and
getParameter(0).getType() instanceof TypeString

@atorralba atorralba Sep 27, 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.

Are there overloads of this method with different parameters? If not, these two conditions aren't needed.

Sorry, something went wrong.

Uh oh!

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

All reactions

@haby0 haby0 Sep 28, 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.

Updated.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread java/ql/src/experimental/Security/CWE/CWE-552/UnsafeUrlForward.qll Outdated
/**
* A concatenate expression using the string `forward:` on the left.
*
* E.g: `"forward:" + url`

@atorralba atorralba Sep 27, 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.

Avoid contractions in QLDoc: https://github.com/github/codeql/blob/main/docs/qldoc-style-guide.md#language-requirements

Sorry, something went wrong.

Uh oh!

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

All reactions

@haby0 haby0 Sep 28, 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.

Updated.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread java/ql/src/experimental/Security/CWE/CWE-552/UnsafeUrlForward.qll Outdated
/**
* A call to `StringBuilder.append` or `StringBuffer.append` method, and the parameter value is `"forward:"`.
*
* E.g: `StringBuilder.append("forward:")`

@atorralba atorralba Sep 27, 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.

Avoid contractions in QLDoc: https://github.com/github/codeql/blob/main/docs/qldoc-style-guide.md#language-requirements

Sorry, something went wrong.

Uh oh!

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

All reactions

@haby0 haby0 Sep 28, 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.

Updated.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread java/ql/src/experimental/Security/CWE/CWE-552/UnsafeUrlForward.ql Outdated
ma.getMethod()
.getDeclaringType()
.getASupertype*()
.hasQualifiedName("javax.servlet.http", "HttpServletRequest") and

@atorralba atorralba Sep 27, 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.

you can use the HttpServletRequest class from Servlets.qll.

Sorry, something went wrong.

Uh oh!

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

All reactions

@haby0 haby0 Sep 28, 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.

Updated.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread java/ql/src/experimental/Security/CWE/CWE-552/UnsafeUrlForward.qll Outdated
)
or
exists(ClassInstanceExpr cie |
cie.getConstructedType().hasQualifiedName("org.springframework.web.servlet", "ModelAndView") and

@atorralba atorralba Sep 27, 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.

You can use the ModelAndView class from SpringWeb.qll.

Sorry, something went wrong.

Uh oh!

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

All reactions

@haby0 haby0 Sep 28, 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.

Updated.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread java/ql/src/experimental/Security/CWE/CWE-552/UnsafeUrlForward.qll Outdated
ma.getMethod().hasName("setViewName") and
ma.getMethod()
.getDeclaringType()
.hasQualifiedName("org.springframework.web.servlet", "ModelAndView") and

@atorralba atorralba Sep 27, 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.

You can use the ModelAndView class from SpringWeb.qll.

Sorry, something went wrong.

Uh oh!

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

All reactions

@haby0 haby0 Sep 28, 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.

Updated.

Sorry, something went wrong.

Uh oh!

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

All reactions
@haby0
haby0 requested a review from atorralba September 28, 2021 01:45
smowton
smowton reviewed Oct 6, 2021
View reviewed changes
Comment thread java/ql/src/experimental/Security/CWE/CWE-552/UnsafeUrlForward.qhelp Outdated
Comment on lines +20 to +21
In <code>bad1</code> method and <code>bad2</code> method and <code>bad3</code> method and
<code>bad4</code> method and <code>bad5</code> method and <code>bad6</code> method, shows an HTTP request parameter being used directly in a URL forward

@smowton smowton Oct 6, 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
In <code>bad1</code> method and <code>bad2</code> method and <code>bad3</code> method and
<code>bad4</code> method and <code>bad5</code> method and <code>bad6</code> method, shows an HTTP request parameter being used directly in a URL forward
The <code>bad</code> methods show an HTTP request parameter being used directly in a URL forward

Sorry, something went wrong.

Uh oh!

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

All reactions

@smowton smowton Oct 6, 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.

Also consider should the user manual example have so many cases? Are they all illustrating sufficiently different things, or would one or two do, with the rest kept in the tests but not the documentation?

Sorry, something went wrong.

Uh oh!

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

All reactions

@haby0 haby0 Oct 8, 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.

Updated.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread java/ql/src/experimental/Security/CWE/CWE-552/UnsafeUrlForward.qll Outdated
private Expr getQualifier(Expr e) { result = e.(MethodAccess).getQualifier() }

/**
* An extension of `StringBuilderVar` that also accounts for strings appended in StringBuilder/Buffer's constructor

@smowton smowton Oct 6, 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.

This being copy-pasted from RequestForgery.qll, let's pull it out into a library rather than duplicate

Sorry, something went wrong.

Uh oh!

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

All reactions
@haby0
haby0 requested a review from smowton October 8, 2021 04:13
haby0 and others added 7 commits October 19, 2021 12:49
@haby0 @smowton
Add CWE-552-UnsafeUrlForward
d0eec1e
@haby0 @smowton
Eliminate FP
952b34a
@haby0 @smowton
Modify Sanitizer
679652e
@haby0 @smowton
Modify the model
283376e
@haby0 @smowton
Fix
9d9a7ab
@smowton
Copyedit
8a4fa0a
@smowton
Rewrite query to use shared StringPrefixes library
057d0fb
@smowton

smowton commented Oct 19, 2021

Copy link
Copy Markdown
Contributor

Rewritten to use the shared library; will merge on green

All reactions

Sorry, something went wrong.

Uh oh!

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

smowton
smowton approved these changes Oct 19, 2021
View reviewed changes
@smowton
smowton merged commit 233a334 into github:main Oct 19, 2021
@haby0
haby0 deleted the java/UnsafeUrlForward branch October 26, 2021 06:21
@jcogs33 jcogs33 mentioned this pull request Mar 11, 2024
Java: Promote Unsafe URL Forward query from experimental #14854
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@atorralba atorralba Awaiting requested review from atorralba
2 more reviewers
@pwntester pwntester pwntester requested changes
@smowton smowton smowton approved these changes
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.

4 participants

@haby0 @smowton @pwntester @atorralba
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.