Skip to content

Java: CWE-1004 Query to check sensitive cookies without the HttpOnly flag set#5307

Merged
smowton merged 18 commits into
github:mainfrom
luchua-bc:java/sensitive-cookie-not-httponly
Apr 13, 2021
Merged

Java: CWE-1004 Query to check sensitive cookies without the HttpOnly flag set#5307
smowton merged 18 commits into
github:mainfrom
luchua-bc:java/sensitive-cookie-not-httponly

Conversation

@luchua-bc

Copy link
Copy Markdown
Contributor

Cross-Site Scripting (XSS) has been a popular attack for many years, which is ranked as one of the OWASP Top 10 Security Vulnerabilities 2020. Cookies holding sensitive information are vulnerable to XSS attacks if they don't have the HttpOnly flag set.

The HttpOnly flag directs compatible browsers to prevent client-side script from accessing cookies. Including the HttpOnly flag in the Set-Cookie HTTP response header for a sensitive cookie helps mitigate the risk associated with XSS where an attacker's script code attempts to read the contents of a cookie and exfiltrate information obtained.

The query detects all the following three scenarios of the Java EE framework:

  • HttpOnly not set in javax.servlet.http.Cookie
  • HttpOnly not set in javax.ws.rs.core.Cookie or jakarta.ws.rs.core.Cookie
  • HttpOnly not directly set in response.addHeader("Set-Cookie", ...) or response.setHeader("Set-Cookie", ...)

Please consider to merge the PR. Thanks.

@owen-mc owen-mc left a comment

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 think the definition of the source should be much simpler and not specify what the source flows to, as that should be dealt with later. (But it would be okay to specify what flows to the source.) And the sanitizers are far too broad, quite possibly by mistake.

Comment thread java/ql/src/experimental/Security/CWE/CWE-1004/SensitiveCookieNotHttpOnly.qhelp Outdated
Comment thread java/ql/src/experimental/Security/CWE/CWE-1004/SensitiveCookieNotHttpOnly.ql Outdated
Comment thread java/ql/src/experimental/Security/CWE/CWE-1004/SensitiveCookieNotHttpOnly.ql Outdated
Comment thread java/ql/src/experimental/Security/CWE/CWE-1004/SensitiveCookieNotHttpOnly.ql Outdated
Comment thread java/ql/src/experimental/Security/CWE/CWE-1004/SensitiveCookieNotHttpOnly.ql Outdated
Comment thread java/ql/src/experimental/Security/CWE/CWE-1004/SensitiveCookieNotHttpOnly.ql Outdated
Comment thread java/ql/src/experimental/Security/CWE/CWE-1004/SensitiveCookieNotHttpOnly.ql Outdated
Comment thread java/ql/src/experimental/Security/CWE/CWE-1004/SensitiveCookieNotHttpOnly.ql Outdated
Comment thread java/ql/src/experimental/Security/CWE/CWE-1004/SensitiveCookieNotHttpOnly.ql Outdated
Comment thread java/ql/src/experimental/Security/CWE/CWE-1004/SensitiveCookieNotHttpOnly.ql Outdated

@owen-mc owen-mc left a comment

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.

Great. The sources and sinks are looking much better. Now I have some suggestions for improving the sanitizers - by removing all of them! I think the logic naturally goes in other places.

Comment thread java/ql/src/experimental/Security/CWE/CWE-1004/SensitiveCookieNotHttpOnly.ql Outdated
Comment thread java/ql/src/experimental/Security/CWE/CWE-1004/SensitiveCookieNotHttpOnly.ql Outdated
Comment thread java/ql/src/experimental/Security/CWE/CWE-1004/SensitiveCookieNotHttpOnly.ql Outdated
Comment thread java/ql/src/experimental/Security/CWE/CWE-1004/SensitiveCookieNotHttpOnly.ql Outdated
Comment thread java/ql/src/experimental/Security/CWE/CWE-1004/SensitiveCookieNotHttpOnly.ql Outdated
@kevinbackhouse

Copy link
Copy Markdown
Contributor

@luchua-bc: We ran your query over a large number of projects on LGTM. There seem to be hundreds of projects that are getting flagged because they have cut 'n' pasted "Example 2" from this webpage. I am not an expert on CSRF, so please could you take a look at that example and confirm whether it is incorrect? Thanks, Kev.

@luchua-bc

luchua-bc commented Mar 11, 2021

Copy link
Copy Markdown
Contributor Author

Hi @kevinbackhouse,

Thanks for reviewing this PR.

Code from "Example 2" on that page creates a cookie of Spring CSRF token. CSRF tokens are quite different from other tokens such as access tokens and session tokens, and they can be used in JavaScript.

I've added one check to the query as well as a new test case to exclude CSRF tokens so that we can eliminate those FPs.

@luchua-bc

@kevinbackhouse

Copy link
Copy Markdown
Contributor

Hi @luchua-bc: thanks for the update. It eliminated a lot of false positives. Looking through the new results, I am seeing some that are like this one, where they have added "HttpOnly" as a hard-coded string. Do you think you could eliminate those false positives too?

@kevinbackhouse

Copy link
Copy Markdown
Contributor

@luchua-bc: please could contact me privately so I can share some more results? Not sure if they're true or false positives, but I would like to check them with you first. I am kevinbackhouse@github.com.

@luchua-bc

Copy link
Copy Markdown
Contributor Author

@kevinbackhouse: I've checked all the sample projects, and have modified the query to accommodate their special cases. Now there is no FPs reported.

There are four cases in total, some of which are not used that often but it's still nice to cover all of them to reduce FPs and make it a powerful query. They are:

  • Set HttpOnly in a wrapper method
  • Set HttpOnly through a method parameter instead of boolean literal
  • Not to set HttpOnly in a cookie creation method with the call setMaxAge=0, which effectively removes the cookie thus shall not be flagged
  • Set HttpOnly through String.format(...) string concatenation

Please retest and let me know if we need to test against more projects.

Thanks,
@luchua-bc

@owen-mc owen-mc left a comment

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.

Two places where you should really be using taint flow rather than trying to imitate it yourself. Also, please squash Remove unused import into Update the query to accommodate more cases, if you are comfortable with editing git history. (You will have to force-push when you next push.)

Comment thread java/ql/src/experimental/Security/CWE/CWE-1004/SensitiveCookieNotHttpOnly.ql Outdated
Comment thread java/ql/src/experimental/Security/CWE/CWE-1004/SensitiveCookieNotHttpOnly.ql Outdated
Comment thread java/ql/src/experimental/Security/CWE/CWE-1004/SensitiveCookieNotHttpOnly.ql Outdated
Comment thread java/ql/src/experimental/Security/CWE/CWE-1004/SensitiveCookieNotHttpOnly.ql Outdated
@luchua-bc luchua-bc force-pushed the java/sensitive-cookie-not-httponly branch from 5b084fb to 08c3bf2 Compare March 24, 2021 23:35

@owen-mc owen-mc left a comment

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.

That looks much better. Well done for realising you had to use TaintTracking2, which I probably should have mentioned. Just one thing to fix - making sure the source of a taint tracking configuration is constrained.

Comment thread java/ql/src/experimental/Security/CWE/CWE-1004/SensitiveCookieNotHttpOnly.ql Outdated
Comment thread java/ql/src/experimental/Security/CWE/CWE-1004/SensitiveCookieNotHttpOnly.ql Outdated
@owen-mc owen-mc dismissed their stale review March 25, 2021 10:50

The requested changes have been made

@owen-mc

owen-mc commented Mar 25, 2021

Copy link
Copy Markdown
Contributor

The checks are failing, but it's unrelated to this PR.

@owen-mc owen-mc assigned aschackmull and unassigned owen-mc Mar 25, 2021
@smowton smowton assigned smowton and unassigned aschackmull Apr 8, 2021

@smowton smowton left a comment

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.

Partial review, to be continued tomorrow. It would be helpful to give an outline of how this query works at the top of the .ql file, as it's a bit more complicated than expected from its description.

Comment thread java/ql/src/experimental/Security/CWE/CWE-1004/SensitiveCookieNotHttpOnly.ql Outdated
Comment thread java/ql/src/experimental/Security/CWE/CWE-1004/SensitiveCookieNotHttpOnly.ql Outdated
Comment thread java/ql/src/experimental/Security/CWE/CWE-1004/SensitiveCookieNotHttpOnly.ql Outdated
Comment thread java/ql/src/experimental/Security/CWE/CWE-1004/SensitiveCookieNotHttpOnly.ql Outdated
Comment thread java/ql/src/experimental/Security/CWE/CWE-1004/SensitiveCookieNotHttpOnly.ql Outdated
Comment thread java/ql/src/experimental/Security/CWE/CWE-1004/SensitiveCookieNotHttpOnly.ql Outdated
Comment thread java/ql/src/experimental/Security/CWE/CWE-1004/SensitiveCookieNotHttpOnly.ql Outdated
Comment thread java/ql/src/experimental/Security/CWE/CWE-1004/SensitiveCookieNotHttpOnly.ql Outdated
Comment thread java/ql/src/experimental/Security/CWE/CWE-1004/SensitiveCookieNotHttpOnly.ql Outdated
Comment thread java/ql/src/experimental/Security/CWE/CWE-1004/SensitiveCookieNotHttpOnly.ql Outdated

@smowton smowton left a comment

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.

One last comment: suggest re-ordering the predicates and configurations here such that we get used predicates before their users (e.g. predicate f() { ... } should come before predicate g(...) { f(...) }). This would make this a bit easier to read.

Comment thread java/ql/src/experimental/Security/CWE/CWE-1004/SensitiveCookieNotHttpOnly.ql Outdated
Comment thread java/ql/src/experimental/Security/CWE/CWE-1004/SensitiveCookieNotHttpOnly.ql Outdated
Comment thread java/ql/src/experimental/Security/CWE/CWE-1004/SensitiveCookieNotHttpOnly.ql Outdated
Comment thread java/ql/src/experimental/Security/CWE/CWE-1004/SensitiveCookieNotHttpOnly.ql Outdated
Comment thread java/ql/src/experimental/Security/CWE/CWE-1004/SensitiveCookieNotHttpOnly.ql Outdated
Comment thread java/ql/src/experimental/Security/CWE/CWE-1004/SensitiveCookieNotHttpOnly.ql Outdated
Comment thread java/ql/src/experimental/Security/CWE/CWE-1004/SensitiveCookieNotHttpOnly.ql Outdated
Comment thread java/ql/src/experimental/Security/CWE/CWE-1004/SensitiveCookieNotHttpOnly.ql Outdated
Comment thread java/ql/src/experimental/Security/CWE/CWE-1004/SensitiveCookieNotHttpOnly.ql Outdated
Comment thread java/ql/src/experimental/Security/CWE/CWE-1004/SensitiveCookieNotHttpOnly.ql Outdated
Comment thread java/ql/src/experimental/Security/CWE/CWE-1004/SensitiveCookieNotHttpOnly.ql Outdated
Comment thread java/ql/src/experimental/Security/CWE/CWE-1004/SensitiveCookieNotHttpOnly.ql Outdated
Comment thread java/ql/test/stubs/servlet-api-2.4/javax/servlet/http/Cookie.java Outdated
@smowton

smowton commented Apr 13, 2021

Copy link
Copy Markdown
Contributor

Merged with slight amendments via #5663.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants