Skip to content

Conversation

am0o0
Copy link
Contributor

@am0o0 am0o0 commented Nov 2, 2023

No description provided.

am0o0 and others added 7 commits August 29, 2023 21:23
… results now, ConstantString is suggested as a better alternative for finding constant sources
add new additional global taint and dataflow steps
update tests of CWE-798
add a new sanitizer for `semmle.javascript.security.dataflow.HardcodedCredentialsQuery`
this.getFile()
.getLocation()
.hasLocationInfo(any(string s |
s.regexpMatch(["/.*test[.].*", "/.*demo[.].*", "/.*example[.].*", "/.*sample[.].*"])
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure if this should be added as a sanitizer on this level.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

please tell me where I should put this sanitizer because I encountered a lot of FPs before applying it so I must use it but don't know exactly where.

Copy link
Member

Choose a reason for hiding this comment

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

Let's wait for the CodeQL team to review it. But I fear if it adds a lot of FP's without this sanitizer it might not be perfectly suited as addition for this prod query.
On the other hand: excluding test files for less FP's might help to reduce FP's for the hardcoded-credentials query as a whole.

Copy link
Contributor

Choose a reason for hiding this comment

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

Two things.

  • This shouldn't be added as a sanitizer, instead you should probably add a condition in the where statement in the .ql file.
  • Use the isTestFile predicate from ClassifyFiles.qll instead.

The performance is this current sanitizer is terrible on some large projects, so this definitely cannot be merged as is.

@ghsecuritylab ghsecuritylab marked this pull request as ready for review May 30, 2024 13:52
@ghsecuritylab ghsecuritylab requested a review from a team as a code owner May 30, 2024 13:52
Copy link
Contributor

@erik-krogh erik-krogh left a comment

Choose a reason for hiding this comment

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

The idea seems OK.
I haven't looked through the models yet, but I did run a small internal evaluation on them, and that looked OK.

But first there is a problem with the sanitizer.

I'll take a closer look at the models once that's resolved.

this.getFile()
.getLocation()
.hasLocationInfo(any(string s |
s.regexpMatch(["/.*test[.].*", "/.*demo[.].*", "/.*example[.].*", "/.*sample[.].*"])
Copy link
Contributor

Choose a reason for hiding this comment

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

Two things.

  • This shouldn't be added as a sanitizer, instead you should probably add a condition in the where statement in the .ql file.
  • Use the isTestFile predicate from ClassifyFiles.qll instead.

The performance is this current sanitizer is terrible on some large projects, so this definitely cannot be merged as is.

@am0o0
Copy link
Contributor Author

am0o0 commented Jun 6, 2024

I only included the new where condition on sinks because the constant hardcoded creds can be loaded from a test or example directory. However, the sink using these hardcoded creds should not be in a test or example directory.

@erik-krogh
Copy link
Contributor

erik-krogh commented Jun 6, 2024

I only included the new where condition on sinks because the constant hardcoded creds can be loaded from a test or example directory. However, the sink using these hardcoded creds should not be in a test or example directory.

Can you try to use ClassifyFiles.qll instead? I know those heuristics probably don't match exactly what you're looking for, but those can be improved later.
(I very much suggest that you don't look into that now, but do that in a later PR if needed, just to keep the scope of this PR low).

If you don't want to do that, then an option is to move your contributions into the experimental folder.

Note: The performance issue I mentioned previously is definitely gone with the filter in the where part of the query.

@am0o0
Copy link
Contributor Author

am0o0 commented Jun 25, 2024

@erik-krogh if the changes are not good and it is better to move my changes to experimental please let me know.

Copy link
Contributor

@erik-krogh erik-krogh left a comment

Choose a reason for hiding this comment

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

@erik-krogh if the changes are not good and it is better to move my changes to experimental please let me know.

No, I think that is fine to get it in the standard suite.
But I did discuss internally, and agreed that you have some changes to make (beyond the comments below).

Somehow the isTestFile filter should only apply to the JWT keys you've added.
Probably by creating a new kind for getCredentialsKind, and then applying the filter only for that credentials-kind.

am0o0 added 6 commits July 1, 2024 11:38
…steps with test cases, update test cases and expected test results
since we want to check if a jwt related sink is in this dir or not
… from with the isTestFile predicate.

According to expected test results, with a new query, the jwt sinks of __test__/ dir have been exluded from query results.
Comment on lines 49 to 63
not isTestFile(sink.getNode().getFile()) and
updateMessageWithSourceValue(value, source.getNode(), sink.getNode())
else
// sink kind is "jwt key" and source is not constant string
if
sink.getNode().(Sink).(DefaultCredentialsSink).getKind() = "jwt key" and
not source.getNode().asExpr() instanceof ConstantString
then not isTestFile(sink.getNode().getFile()) and value = "This hard-coded value"
else
// sink kind is not "jwt key" and source is constant string
if
not sink.getNode().(Sink).(DefaultCredentialsSink).getKind() = "jwt key" and
source.getNode().asExpr() instanceof ConstantString
then updateMessageWithSourceValue(value, source.getNode(), sink.getNode())
else value = "This hard-coded value"
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you revert these changes, and then instead put the filter on the Sink class instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can you give me some tips about this? I couldn't figure out a solution.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@erik-krogh Could you help me with this, I have free time this week, so I can fix the issues quicker.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'll have a look this week. I just came back from a few weeks of vacation.

Copy link
Contributor

Choose a reason for hiding this comment

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

You can e.g. do it like this: erik-krogh@38e5c90
(I tried to push directly to your PR, but I can't do that).

There should probably also be a comment on the new characteristic predicate in DefaultCredentialsSink.

@erik-krogh
Copy link
Contributor

The autoformatter checks are failing on javascript/ql/src/Security/CWE-798/HardcodedCredentials.ql and javascript/ql/lib/semmle/javascript/security/dataflow/HardcodedCredentialsCustomizations.qll.
You can use codeql query format -i <paths-to-files> to run the autoformatter.

@erik-krogh
Copy link
Contributor

There is still javascript/ql/src/Security/CWE-798/HardcodedCredentials.ql, it's still failing the autoformatter checks.

@erik-krogh erik-krogh added the no-change-note-required This PR does not need a change note label Aug 7, 2024
Copy link
Contributor

@erik-krogh erik-krogh left a comment

Choose a reason for hiding this comment

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

There is a single debug-line it looks like you forgot to remove.
But otherwise I think we're ready to merge 🎉

|
src = n.getArgument(0) and
trg = n and
n.getLocation().getFile().getRelativePath().matches("%HardcodedCredentials.js%")
Copy link
Contributor

Choose a reason for hiding this comment

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

This line looks like some debug code you forgot to remove?

@erik-krogh erik-krogh merged commit 41506fb into github:main Aug 8, 2024
13 checks passed
@am0o0 am0o0 deleted the amammad-js-hardcodedJWTKey branch September 14, 2024 11:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
JS no-change-note-required This PR does not need a change note
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants