Skip to content

[javascript] CodeQL query to detect if cookies are sent without the flag secure being set #3978

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

Merged
merged 37 commits into from
Aug 28, 2020

Conversation

dellalibera
Copy link
Contributor

Query to detect if the secure flag is set to cookies is available in java query but it is not available in javascript query.

This query detects if cookies are sent without the flag secure being set or with the flag secure being set to false.

Failing to set the secure flag on a cookie can cause it to be sent in cleartext.
This makes it easier for an attacker to intercept and read the cookie.

This query handles the cases when cookies are set with:

I added several examples showing the different cases when the cookie is sent without the flag secure being set or with the flag secure being set to false.

Any feedback is welcome.
I hope this will help.

@dellalibera dellalibera requested a review from a team as a code owner July 26, 2020 21:04
@dellalibera dellalibera changed the title CodeQL query to detect if cookies are sent without the flag secure being set [javascript] CodeQL query to detect if cookies are sent without the flag secure being set Jul 26, 2020
@@ -8,7 +8,7 @@ This makes it easier for an attacker to intercept.</p>
</overview>
<recommendation>

<p>Always set the <code>secure</code> flag to `true` on a cookie before adding it
<p>Always set the `secure` flag to `true` on a cookie before adding it
Copy link
Contributor

Choose a reason for hiding this comment

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

Why did you change this?
As far as I know, this will not work, but <code> will work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I was thinking the opposite. Thanks for the feedback. I made the change.

@esbena esbena self-assigned this Aug 3, 2020
@asgerf asgerf added the JS label Aug 3, 2020
Copy link
Contributor

@esbena esbena left a comment

Choose a reason for hiding this comment

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

Very nice. I only have some minor change requests.

Have you seen https://github.com/github/codeql/blob/71933a4d8a32d32f085f14b7b44f41d1cdcae2e4/javascript/ql/src/semmle/javascript/frameworks/CookieLibraries.qll ? It could inspire some changes to this PR, or perhaps some of the content in this PR belongs in that file (note that this file is not in "experimental", so I will set the bar a review bar a bit higher)?

One large, and optional, change to consider.
The qhelp file is a bit verbose in terms of examples. Could you remove some of them, and perhaps add them as tests instead? See #3835 (comment) for how another external contributor recently made tests.

It is a bit of a shame that result = this.getCookieOptionsArgument().getAPropertyWrite(flag).getRhs() has to be repeated so many times. This could be fixed with some additional inheritance, but that is a matter of taste.


Generally. Whenever you write exist(X x | x = getX() and ...) without ever using the x more than once, you can write exists(getX()) and ... instead.

/**
* Abstract class to represent different cases of insecure cookie settings.
*/
abstract class InsecureCookies extends DataFlow::Node {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we rename the entire InsecureCookie module and class to just Cookie, and then just let the isInsecure determine if the cookie is secure or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, no problem. I made the change.

Comment on lines 29 to 31
* Predicate that determines if a cookie is insecure.
*/
abstract predicate isInsecure();
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* Predicate that determines if a cookie is insecure.
*/
abstract predicate isInsecure();
* Holds if this cookie is secure.
*/
abstract predicate isSecure();

Optional: I think it is clearer to flip the polarity and avoid the negation in the predicate name.
Please note that this requires a change to all implementations of this predicate below.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I agree with you. I changed the polarity (I did it in one commit) of all the implementations of this predicate.

override predicate isInsecure() {
not exists(string s |
getCookieOptionsArgument().mayHaveStringValue(s) and
s.matches("%; secure%")
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm. What if the string is ...;secure...? That is: it lacks a space between ; and secure.

How about something likes.regexpMatch("(.*;)?\\s*secure.*") instead? That should allow any whitespace variation, and also permit the string to start with secure.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Again, you are right. I missed that. I made the change

@adityasharad adityasharad changed the base branch from master to main August 14, 2020 18:33
dellalibera and others added 19 commits August 16, 2020 14:13
….qll

Co-authored-by: Esben Sparre Andreasen <esbena@github.com>
….qll

Co-authored-by: Esben Sparre Andreasen <esbena@github.com>
….qll

Co-authored-by: Esben Sparre Andreasen <esbena@github.com>
….qll

Co-authored-by: Esben Sparre Andreasen <esbena@github.com>
….qll

Co-authored-by: Esben Sparre Andreasen <esbena@github.com>
….qll

Co-authored-by: Esben Sparre Andreasen <esbena@github.com>
….qll

Co-authored-by: Esben Sparre Andreasen <esbena@github.com>
….qll

Co-authored-by: Esben Sparre Andreasen <esbena@github.com>
….qll

Co-authored-by: Esben Sparre Andreasen <esbena@github.com>
….qll

Co-authored-by: Esben Sparre Andreasen <esbena@github.com>
….qll

Co-authored-by: Esben Sparre Andreasen <esbena@github.com>
….qll

Co-authored-by: Esben Sparre Andreasen <esbena@github.com>
….qll

Co-authored-by: Esben Sparre Andreasen <esbena@github.com>
….qll

Co-authored-by: Esben Sparre Andreasen <esbena@github.com>
….qll

Co-authored-by: Esben Sparre Andreasen <esbena@github.com>
….qll

Co-authored-by: Esben Sparre Andreasen <esbena@github.com>
….qll

Co-authored-by: Esben Sparre Andreasen <esbena@github.com>
….qll

Co-authored-by: Esben Sparre Andreasen <esbena@github.com>
@dellalibera
Copy link
Contributor Author

dellalibera commented Aug 16, 2020

Hi @esbena,

sorry for the late reply.
I super appreciated your feedback (I learnt a lot).

About the js-cookie library I was inspired by the code that you mentioned https://github.com/github/codeql/blob/71933a4d8a32d32f085f14b7b44f41d1cdcae2e4/javascript/ql/src/semmle/javascript/frameworks/CookieLibraries.qll.
However the existing implementation provides access only to the value of a cookie and not its options (please correct me if I am wrong).

I made all the changes that you suggested and then I also changed the polarity of the predicate isInsecure (I changed also all the implementations of this predicate).

Finally, I removed all the examples and I added them as tests, as you suggested (I hope I added in the correct folder).

Let me know if now it’s ok and if there is something else I can do.
Again, thanks for all the feedback.

@esbena
Copy link
Contributor

esbena commented Aug 24, 2020

Thanks. The new tests seem fine.
I have a few QL style comments left.
Please remember to run codeql query format before pushing, otherwise the PR checks will fail.

@dellalibera
Copy link
Contributor Author

Thanks for your reply. I formatted the code.
Let me know if there is something else I can do.

Copy link
Contributor

@esbena esbena left a comment

Choose a reason for hiding this comment

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

Oops. Forgot to attach the inline feedback. PTAL

dellalibera and others added 4 commits August 26, 2020 01:46
….qll

Co-authored-by: Esben Sparre Andreasen <esbena@github.com>
….qll

Co-authored-by: Esben Sparre Andreasen <esbena@github.com>
….qll

Co-authored-by: Esben Sparre Andreasen <esbena@github.com>
….qll

Co-authored-by: Esben Sparre Andreasen <esbena@github.com>
@dellalibera
Copy link
Contributor Author

@esbena, no problem :)
thanks for the other suggestions (I learnt some new stuff).

Let me know if there is something else I can do.

@intrigus-lgtm
Copy link
Contributor

^ Pro tip: in the "Files changed" view, you have the option of adding review suggestions to a batch and commit them all at once. (github/codeql-go#66 (comment))

Copy link
Contributor

@esbena esbena left a comment

Choose a reason for hiding this comment

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

One more thing. I stumbled upon it when I looked at the results of the query on LGTM.com.

@dellalibera
Copy link
Contributor Author

dellalibera commented Aug 26, 2020

@intrigus-lgtm thanks for the tip.

@esbena I made the changes and updated the expected output.

@esbena
Copy link
Contributor

esbena commented Aug 27, 2020

I took the liberty of pushing a formatting fix to your branch. The tests should go green shortly.

@codeql-ci codeql-ci merged commit ac94869 into github:main Aug 28, 2020
@esbena
Copy link
Contributor

esbena commented Aug 28, 2020

Thank you for yet another contribution @dellalibera.

@dellalibera
Copy link
Contributor Author

@esbena It was really a pleasure. Thank you for all the feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants