Sitelet https://web.archive.org/web/20260529151952/https://github.com/github/codeql/pull/9002
Skip to content

Java: Add OkHttp and Retrofit models#9002

Merged
atorralba merged 7 commits into
github:mainfrom
atorralba:atorralba/https-urls-improvs
May 11, 2022
Merged

Java: Add OkHttp and Retrofit models#9002
atorralba merged 7 commits into
github:mainfrom
atorralba:atorralba/https-urls-improvs

Conversation

@atorralba
Copy link
Copy Markdown
Contributor

Adds sinks of kind open-url and summaries for the libraries OkHttp and Retrofit.

Also simplifies the query java/non-https-urls to also consider sinks that are not method accesses.

@github-actions github-actions Bot added the Java label May 2, 2022
@atorralba atorralba force-pushed the atorralba/https-urls-improvs branch from 96f5e0b to 9a35aba Compare May 2, 2022 13:45
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 2, 2022

⚠️ The head of this PR and the base branch were compared for differences in the framework coverage reports. The generated reports are available in the artifacts of this workflow run. The differences will be picked up by the nightly job after the PR gets merged.

Click to show differences in coverage

java

Generated file changes for java

  • Changes to framework-coverage-java.rst:
-    Others,"``androidx.slice``, ``cn.hutool.core.codec``, ``com.esotericsoftware.kryo.io``, ``com.esotericsoftware.kryo5.io``, ``com.fasterxml.jackson.core``, ``com.fasterxml.jackson.databind``, ``com.opensymphony.xwork2.ognl``, ``com.rabbitmq.client``, ``com.unboundid.ldap.sdk``, ``com.zaxxer.hikari``, ``flexjson``, ``groovy.lang``, ``groovy.util``, ``jodd.json``, ``net.sf.saxon.s9api``, ``ognl``, ``org.apache.commons.codec``, ``org.apache.commons.jexl2``, ``org.apache.commons.jexl3``, ``org.apache.commons.logging``, ``org.apache.commons.ognl``, ``org.apache.directory.ldap.client.api``, ``org.apache.ibatis.jdbc``, ``org.apache.log4j``, ``org.apache.logging.log4j``, ``org.apache.shiro.codec``, ``org.apache.shiro.jndi``, ``org.codehaus.groovy.control``, ``org.dom4j``, ``org.hibernate``, ``org.jboss.logging``, ``org.jdbi.v3.core``, ``org.jooq``, ``org.mvel2``, ``org.scijava.log``, ``org.slf4j``, ``org.xml.sax``, ``org.xmlpull.v1``, ``play.mvc``, ``ratpack.core.form``, ``ratpack.core.handling``, ``ratpack.core.http``, ``ratpack.exec``, ``ratpack.form``, ``ratpack.func``, ``ratpack.handling``, ``ratpack.http``, ``ratpack.util``",65,347,929,,,,14,18,,
+    Others,"``androidx.slice``, ``cn.hutool.core.codec``, ``com.esotericsoftware.kryo.io``, ``com.esotericsoftware.kryo5.io``, ``com.fasterxml.jackson.core``, ``com.fasterxml.jackson.databind``, ``com.opensymphony.xwork2.ognl``, ``com.rabbitmq.client``, ``com.unboundid.ldap.sdk``, ``com.zaxxer.hikari``, ``flexjson``, ``groovy.lang``, ``groovy.util``, ``jodd.json``, ``net.sf.saxon.s9api``, ``ognl``, ``okhttp3``, ``org.apache.commons.codec``, ``org.apache.commons.jexl2``, ``org.apache.commons.jexl3``, ``org.apache.commons.logging``, ``org.apache.commons.ognl``, ``org.apache.directory.ldap.client.api``, ``org.apache.ibatis.jdbc``, ``org.apache.log4j``, ``org.apache.logging.log4j``, ``org.apache.shiro.codec``, ``org.apache.shiro.jndi``, ``org.codehaus.groovy.control``, ``org.dom4j``, ``org.hibernate``, ``org.jboss.logging``, ``org.jdbi.v3.core``, ``org.jooq``, ``org.mvel2``, ``org.scijava.log``, ``org.slf4j``, ``org.xml.sax``, ``org.xmlpull.v1``, ``play.mvc``, ``ratpack.core.form``, ``ratpack.core.handling``, ``ratpack.core.http``, ``ratpack.exec``, ``ratpack.form``, ``ratpack.func``, ``ratpack.handling``, ``ratpack.http``, ``ratpack.util``, ``retrofit2``",65,377,932,,,,14,18,,3
-    Totals,,213,6362,1441,106,6,10,107,33,1,81
+    Totals,,213,6392,1444,106,6,10,107,33,1,84
  • Changes to framework-coverage-java.csv:
+ okhttp3,2,,30,,,,,,,,,,,,,,2,,,,,,,,,,,,,5,25
+ retrofit2,1,,,,,,,,,,,,,,,,1,,,,,,,,,,,,,,

@github github deleted a comment from github-actions Bot May 2, 2022
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 2, 2022

⚠️ The head of this PR and the base branch were compared for differences in the framework coverage reports. The generated reports are available in the artifacts of this workflow run. The differences will be picked up by the nightly job after the PR gets merged.

Click to show differences in coverage

java

Generated file changes for java

  • Changes to framework-coverage-java.rst:
-    Others,"``androidx.slice``, ``cn.hutool.core.codec``, ``com.esotericsoftware.kryo.io``, ``com.esotericsoftware.kryo5.io``, ``com.fasterxml.jackson.core``, ``com.fasterxml.jackson.databind``, ``com.opensymphony.xwork2.ognl``, ``com.rabbitmq.client``, ``com.unboundid.ldap.sdk``, ``com.zaxxer.hikari``, ``flexjson``, ``groovy.lang``, ``groovy.util``, ``jodd.json``, ``net.sf.saxon.s9api``, ``ognl``, ``org.apache.commons.codec``, ``org.apache.commons.jexl2``, ``org.apache.commons.jexl3``, ``org.apache.commons.logging``, ``org.apache.commons.ognl``, ``org.apache.directory.ldap.client.api``, ``org.apache.ibatis.jdbc``, ``org.apache.log4j``, ``org.apache.logging.log4j``, ``org.apache.shiro.codec``, ``org.apache.shiro.jndi``, ``org.codehaus.groovy.control``, ``org.dom4j``, ``org.hibernate``, ``org.jboss.logging``, ``org.jdbi.v3.core``, ``org.jooq``, ``org.mvel2``, ``org.scijava.log``, ``org.slf4j``, ``org.xml.sax``, ``org.xmlpull.v1``, ``play.mvc``, ``ratpack.core.form``, ``ratpack.core.handling``, ``ratpack.core.http``, ``ratpack.exec``, ``ratpack.form``, ``ratpack.func``, ``ratpack.handling``, ``ratpack.http``, ``ratpack.util``",65,347,929,,,,14,18,,
+    Others,"``androidx.slice``, ``cn.hutool.core.codec``, ``com.esotericsoftware.kryo.io``, ``com.esotericsoftware.kryo5.io``, ``com.fasterxml.jackson.core``, ``com.fasterxml.jackson.databind``, ``com.opensymphony.xwork2.ognl``, ``com.rabbitmq.client``, ``com.unboundid.ldap.sdk``, ``com.zaxxer.hikari``, ``flexjson``, ``groovy.lang``, ``groovy.util``, ``jodd.json``, ``net.sf.saxon.s9api``, ``ognl``, ``okhttp3``, ``org.apache.commons.codec``, ``org.apache.commons.jexl2``, ``org.apache.commons.jexl3``, ``org.apache.commons.logging``, ``org.apache.commons.ognl``, ``org.apache.directory.ldap.client.api``, ``org.apache.ibatis.jdbc``, ``org.apache.log4j``, ``org.apache.logging.log4j``, ``org.apache.shiro.codec``, ``org.apache.shiro.jndi``, ``org.codehaus.groovy.control``, ``org.dom4j``, ``org.hibernate``, ``org.jboss.logging``, ``org.jdbi.v3.core``, ``org.jooq``, ``org.mvel2``, ``org.scijava.log``, ``org.slf4j``, ``org.xml.sax``, ``org.xmlpull.v1``, ``play.mvc``, ``ratpack.core.form``, ``ratpack.core.handling``, ``ratpack.core.http``, ``ratpack.exec``, ``ratpack.form``, ``ratpack.func``, ``ratpack.handling``, ``ratpack.http``, ``ratpack.util``, ``retrofit2``",65,377,932,,,,14,18,,3
-    Totals,,213,6366,1441,106,6,10,107,33,1,81
+    Totals,,213,6396,1444,106,6,10,107,33,1,84
  • Changes to framework-coverage-java.csv:
+ okhttp3,2,,30,,,,,,,,,,,,,,2,,,,,,,,,,,,,5,25
+ retrofit2,1,,,,,,,,,,,,,,,,1,,,,,,,,,,,,,,

@atorralba atorralba marked this pull request as ready for review May 2, 2022 15:46
@atorralba atorralba requested a review from a team as a code owner May 2, 2022 15:46
Comment thread java/ql/lib/semmle/code/java/frameworks/OkHttp.qll Outdated
"okhttp3;HttpUrl$Builder;false;removeAllQueryParameters;;;Argument[-1];ReturnValue;value",
"okhttp3;HttpUrl$Builder;false;removePathSegment;;;Argument[-1];ReturnValue;value",
"okhttp3;HttpUrl$Builder;false;scheme;;;Argument[-1];ReturnValue;value",
"okhttp3;HttpUrl$Builder;false;scheme;;;Argument[0];Argument[-1];taint",
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.

Comment on why only scheme contributes taint but not the other URL parts?

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.

Good point. I added that while figuring out why java/non-https-urls wasn't working on an app that used these libraries, but forgot to handle taint more generally. Let me fix that.

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.

Done here 2d3b15f.

Co-authored-by: Chris Smowton <smowton@github.com>
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 3, 2022

⚠️ The head of this PR and the base branch were compared for differences in the framework coverage reports. The generated reports are available in the artifacts of this workflow run. The differences will be picked up by the nightly job after the PR gets merged.

Click to show differences in coverage

java

Generated file changes for java

  • Changes to framework-coverage-java.rst:
-    Others,"``androidx.slice``, ``cn.hutool.core.codec``, ``com.esotericsoftware.kryo.io``, ``com.esotericsoftware.kryo5.io``, ``com.fasterxml.jackson.core``, ``com.fasterxml.jackson.databind``, ``com.opensymphony.xwork2.ognl``, ``com.rabbitmq.client``, ``com.unboundid.ldap.sdk``, ``com.zaxxer.hikari``, ``flexjson``, ``groovy.lang``, ``groovy.util``, ``jodd.json``, ``net.sf.saxon.s9api``, ``ognl``, ``org.apache.commons.codec``, ``org.apache.commons.jexl2``, ``org.apache.commons.jexl3``, ``org.apache.commons.logging``, ``org.apache.commons.ognl``, ``org.apache.directory.ldap.client.api``, ``org.apache.ibatis.jdbc``, ``org.apache.log4j``, ``org.apache.logging.log4j``, ``org.apache.shiro.codec``, ``org.apache.shiro.jndi``, ``org.codehaus.groovy.control``, ``org.dom4j``, ``org.hibernate``, ``org.jboss.logging``, ``org.jdbi.v3.core``, ``org.jooq``, ``org.mvel2``, ``org.scijava.log``, ``org.slf4j``, ``org.xml.sax``, ``org.xmlpull.v1``, ``play.mvc``, ``ratpack.core.form``, ``ratpack.core.handling``, ``ratpack.core.http``, ``ratpack.exec``, ``ratpack.form``, ``ratpack.func``, ``ratpack.handling``, ``ratpack.http``, ``ratpack.util``",65,347,929,,,,14,18,,
+    Others,"``androidx.slice``, ``cn.hutool.core.codec``, ``com.esotericsoftware.kryo.io``, ``com.esotericsoftware.kryo5.io``, ``com.fasterxml.jackson.core``, ``com.fasterxml.jackson.databind``, ``com.opensymphony.xwork2.ognl``, ``com.rabbitmq.client``, ``com.unboundid.ldap.sdk``, ``com.zaxxer.hikari``, ``flexjson``, ``groovy.lang``, ``groovy.util``, ``jodd.json``, ``net.sf.saxon.s9api``, ``ognl``, ``okhttp3``, ``org.apache.commons.codec``, ``org.apache.commons.jexl2``, ``org.apache.commons.jexl3``, ``org.apache.commons.logging``, ``org.apache.commons.ognl``, ``org.apache.directory.ldap.client.api``, ``org.apache.ibatis.jdbc``, ``org.apache.log4j``, ``org.apache.logging.log4j``, ``org.apache.shiro.codec``, ``org.apache.shiro.jndi``, ``org.codehaus.groovy.control``, ``org.dom4j``, ``org.hibernate``, ``org.jboss.logging``, ``org.jdbi.v3.core``, ``org.jooq``, ``org.mvel2``, ``org.scijava.log``, ``org.slf4j``, ``org.xml.sax``, ``org.xmlpull.v1``, ``play.mvc``, ``ratpack.core.form``, ``ratpack.core.handling``, ``ratpack.core.http``, ``ratpack.exec``, ``ratpack.form``, ``ratpack.func``, ``ratpack.handling``, ``ratpack.http``, ``ratpack.util``, ``retrofit2``",65,377,932,,,,14,18,,3
-    Totals,,213,6366,1441,106,6,10,107,33,1,81
+    Totals,,213,6396,1444,106,6,10,107,33,1,84
  • Changes to framework-coverage-java.csv:
+ okhttp3,2,,30,,,,,,,,,,,,,,2,,,,,,,,,,,,,5,25
+ retrofit2,1,,,,,,,,,,,,,,,,1,,,,,,,,,,,,,,

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 4, 2022

⚠️ The head of this PR and the base branch were compared for differences in the framework coverage reports. The generated reports are available in the artifacts of this workflow run. The differences will be picked up by the nightly job after the PR gets merged.

Click to show differences in coverage

java

Generated file changes for java

  • Changes to framework-coverage-java.rst:
-    Others,"``androidx.slice``, ``cn.hutool.core.codec``, ``com.esotericsoftware.kryo.io``, ``com.esotericsoftware.kryo5.io``, ``com.fasterxml.jackson.core``, ``com.fasterxml.jackson.databind``, ``com.opensymphony.xwork2.ognl``, ``com.rabbitmq.client``, ``com.unboundid.ldap.sdk``, ``com.zaxxer.hikari``, ``flexjson``, ``groovy.lang``, ``groovy.util``, ``jodd.json``, ``net.sf.saxon.s9api``, ``ognl``, ``org.apache.commons.codec``, ``org.apache.commons.jexl2``, ``org.apache.commons.jexl3``, ``org.apache.commons.logging``, ``org.apache.commons.ognl``, ``org.apache.directory.ldap.client.api``, ``org.apache.ibatis.jdbc``, ``org.apache.log4j``, ``org.apache.logging.log4j``, ``org.apache.shiro.codec``, ``org.apache.shiro.jndi``, ``org.codehaus.groovy.control``, ``org.dom4j``, ``org.hibernate``, ``org.jboss.logging``, ``org.jdbi.v3.core``, ``org.jooq``, ``org.mvel2``, ``org.scijava.log``, ``org.slf4j``, ``org.xml.sax``, ``org.xmlpull.v1``, ``play.mvc``, ``ratpack.core.form``, ``ratpack.core.handling``, ``ratpack.core.http``, ``ratpack.exec``, ``ratpack.form``, ``ratpack.func``, ``ratpack.handling``, ``ratpack.http``, ``ratpack.util``",65,347,929,,,,14,18,,
+    Others,"``androidx.slice``, ``cn.hutool.core.codec``, ``com.esotericsoftware.kryo.io``, ``com.esotericsoftware.kryo5.io``, ``com.fasterxml.jackson.core``, ``com.fasterxml.jackson.databind``, ``com.opensymphony.xwork2.ognl``, ``com.rabbitmq.client``, ``com.unboundid.ldap.sdk``, ``com.zaxxer.hikari``, ``flexjson``, ``groovy.lang``, ``groovy.util``, ``jodd.json``, ``net.sf.saxon.s9api``, ``ognl``, ``okhttp3``, ``org.apache.commons.codec``, ``org.apache.commons.jexl2``, ``org.apache.commons.jexl3``, ``org.apache.commons.logging``, ``org.apache.commons.ognl``, ``org.apache.directory.ldap.client.api``, ``org.apache.ibatis.jdbc``, ``org.apache.log4j``, ``org.apache.logging.log4j``, ``org.apache.shiro.codec``, ``org.apache.shiro.jndi``, ``org.codehaus.groovy.control``, ``org.dom4j``, ``org.hibernate``, ``org.jboss.logging``, ``org.jdbi.v3.core``, ``org.jooq``, ``org.mvel2``, ``org.scijava.log``, ``org.slf4j``, ``org.xml.sax``, ``org.xmlpull.v1``, ``play.mvc``, ``ratpack.core.form``, ``ratpack.core.handling``, ``ratpack.core.http``, ``ratpack.exec``, ``ratpack.form``, ``ratpack.func``, ``ratpack.handling``, ``ratpack.http``, ``ratpack.util``, ``retrofit2``",65,394,932,,,,14,18,,3
-    Totals,,213,6366,1441,106,6,10,107,33,1,81
+    Totals,,213,6413,1444,106,6,10,107,33,1,84
  • Changes to framework-coverage-java.csv:
+ okhttp3,2,,47,,,,,,,,,,,,,,2,,,,,,,,,,,,,22,25
+ retrofit2,1,,,,,,,,,,,,,,,,1,,,,,,,,,,,,,,

Copy link
Copy Markdown
Contributor

@smowton smowton left a comment

Choose a reason for hiding this comment

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

Approving as this looks plausible; one thing to check: I expect e.g. the url-injection query will distinguish calls that may affect the host or path being targeted from those that can only affect the query string or the fragment of the URL, perhaps by not modelling methods like setFragment, or perhaps by sanitising when a user-controlled input clearly has to follow a ? or #. Check if we are doing that at the moment for queries that care about URLs, and if so how do we mirror that behaviour for these libraries (by selectively removing models? By adding sanitisers?)

@atorralba
Copy link
Copy Markdown
Contributor Author

I thought about this while adding these models, and it all boils down to what a URL being (generally) tainted means for us. For instance, there could be cases in which a tainted fragment is still dangerous (e.g. the fragment of a URL being added to HTML output could still cause a XSS), so we can't discard setFragment as a general taint-preserving step.

The fact that a query expects only certain kinds of taint is context-specific and I don't think should be handled by removing general purposed taint models. I see two possible solutions:

  • Each query adds the appropriate exceptions (i.e. sanitizers) for non-interesting taint types (in this case, the sinks that are not relevant for SSRF).
  • We handle this with synthetic fields that would represent the specific kinds of taint (e.g. for URLs, we could have synthetic fields for scheme, host, port, path, query and fragment, and the SSRF query would only allow implicit reads at its sinks of scheme, host and port).

I think I like the first option because it's less convoluted and a bit more obvious. WDYT?

@smowton
Copy link
Copy Markdown
Contributor

smowton commented May 9, 2022

These functions generally speaking contributing taint and having sanitisers to exclude cases where only the fragment or query string can be tainted per-query sounds reasonable to me. I don't know whether the Java query suite does this much or at all, though I remember at least the Go queries doing this and likely Java as well, and would encourage you to make a quick survey of the current behaviour of URL-relevant queries before merging a PR that might produce inconsistent behaviour (i.e. some libraries excluding query-string taint and some not) between different ways of constructing a URL.

@atorralba
Copy link
Copy Markdown
Contributor Author

I looked into this, and it seems that Java queries assume that a tainted java.net.URL/URI object means that the hostname part is tainted. There are no specific sanitizers for URL/URI methods that set non-hostname parts (but there aren't general taint steps for these methods either). So it seems the intention has been to only taint URL/URI objects if the taint is relevant for the URL-related queries (i.e. the hostname is tainted).

This is not true however for Spring's UriBuilder/UriComponents, JaxWS's UriBuilder, and Android's Uri/Uri$Builder, for which taint is propagated generally, regardless of which part of the URI is affected.

So this PR aggravates an already existent inconsistency indeed. The lazy fix is just removing the non-hostname rows from this PR and the library models mentioned above (at the risk of losing flow on specific cases as explained in my previous comment). The involved fix is adding the missing steps to the other models (mainly java.net.URI and java.net.URL) and adding the appropriate sanitizers for all non-hostname models to the URL-related queries.

I still favor the second, more involved option, but this is starting to look like something that should be handled in a follow-up PR.

@atorralba
Copy link
Copy Markdown
Contributor Author

Another, maybe better option would be to use Flow States and require flow to go through hostname-altering steps (so an allow list approach, rather than a disallow list).

@smowton
Copy link
Copy Markdown
Contributor

smowton commented May 11, 2022

Alright, in that case I don't object to merging this and following up appropriately, probably via a team talk about what the rule ought to be.

@atorralba atorralba merged commit 43b425d into github:main May 11, 2022
@atorralba atorralba deleted the atorralba/https-urls-improvs branch May 11, 2022 08:48
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.

2 participants