Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding overly permissive CORS policy detector #248

Merged
merged 3 commits into from Dec 31, 2016

Conversation

plr0man
Copy link
Member

@plr0man plr0man commented Dec 29, 2016

Detector reports overly permissive CORS policy that sets asterisk as the value of the Access-Control-Allow-Origin header, like in the following examples:
response.addHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Origin", "*");

@h3xstream
Copy link
Member

h3xstream commented Dec 29, 2016

Good idea !

@h3xstream h3xstream added the enhancement New feature or improvement to existing detector. label Dec 29, 2016
@h3xstream h3xstream self-assigned this Dec 29, 2016
@h3xstream h3xstream self-requested a review December 29, 2016 08:19
Copy link
Member

@h3xstream h3xstream left a comment

Choose a reason for hiding this comment

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

See comments for the small changes needed.
The "bonus" comment is informational.


LDC ldc = ByteCode.getPrevInstruction(location.getHandle().getPrev(), LDC.class);
if (ldc != null) {
if ("Access-Control-Allow-Origin".equals(ldc.getValue(cpg)) &&
Copy link
Member

Choose a reason for hiding this comment

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

I would also test for the same header with different case (access-control-allow-origin).
http://stackoverflow.com/a/5259004/89769

LDC ldc = ByteCode.getPrevInstruction(location.getHandle().getPrev(), LDC.class);
if (ldc != null) {
if ("Access-Control-Allow-Origin".equals(ldc.getValue(cpg)) &&
"*".equals(ByteCode.getConstantLDC(location.getHandle().getPrev(), cpg, String.class))) {
Copy link
Member

Choose a reason for hiding this comment

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

I would also test for wildcard use as sub-domain. (replace equals by contains)
http://blog.portswigger.net/2016/10/exploiting-cors-misconfigurations-for.html

Also the exact value "null" could be detected

String className = invoke.getClassName(cpg);

if (className.equals("javax.servlet.http.HttpServletResponse") &&
(methodName.equals("addHeader") || methodName.equals("setHeader"))) {
Copy link
Member

Choose a reason for hiding this comment

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

Bonus : Typically website that need to use Access-Control-Allow-Origin will analyze the Origin header.
http://blog.portswigger.net/2016/10/exploiting-cors-misconfigurations-for.html

This is not mandatory for this PR. The description will have to be guideline of how to analyse properly Origin header. I would be interested to write this one.

Copy link
Member Author

Choose a reason for hiding this comment

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

Nice! Go ahead, I'm looking forward to seeing how you approach it.

@ptamarit
Copy link
Contributor

Maybe it would make sense to move this detector in a cors sub-package.

Not sure I'd have time to do it, but similar checks specific to Spring Framework could be added (see https://docs.spring.io/spring-security/site/docs/current/reference/html/cors.html and http://docs.spring.io/spring/docs/current/spring-framework-reference/html/cors.html).

"*".equals(ByteCode.getConstantLDC(location.getHandle().getPrev(), cpg, String.class))) {
String headerValue = ByteCode.getConstantLDC(location.getHandle().getPrev(), cpg, String.class);
if ("Access-Control-Allow-Origin".equalsIgnoreCase((String)ldc.getValue(cpg)) &&
(headerValue.contains("*") || "null".equalsIgnoreCase(headerValue))) {

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed case sensitivity, header value wildcards and "null" value.

@h3xstream h3xstream merged commit 631a82d into find-sec-bugs:master Dec 31, 2016
@plr0man plr0man deleted the cors branch December 31, 2016 21:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or improvement to existing detector.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants