Skip to content
New issue

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

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

Already on GitHub? # to your account

JS: Update test suite to use post-processed inline expectations #18670

Open
wants to merge 99 commits into
base: main
Choose a base branch
from

Conversation

asgerf
Copy link
Contributor

@asgerf asgerf commented Feb 4, 2025

Updates all of JavaScript's query tests to use post-processed inline expectations except in a few cases where it's not possible. Overall 233 query tests consisting of 712 JavaScript files have been updated.

Inline expectations are now written as // $ Alert instead of // NOT OK. Our old NOT OK-style comments were not automatically verified for consistency except in a handful of cases, so some discrepancies have crept in over the years. This PR reviews and fixes up these discrepancies and of course prevents them from being introduced in the future.

Only query tests have been updated. Library tests have to be implemented a bit differently; that's for another PR.

This PR has the following overall structure:

  • Automatically translate all OK-style comments to $-style. This step tries to capture the original intent behind the test, and does not try to adhere to actual query output.
  • Enable post-processing in .qlref files and run the test to get a list of discrepancies.
  • Fix trivial/unsurprising discrepancies. There are many alerts that simply didn't have a NOT OK comment, but where it's farily easy too see from the test that it was intended to be flagged. There are many of these and I found it's probably easiest to spot check some results from a single commit than having 100+ commits with "trivial" changes.
  • A long tail of small but non-trivial changes in individual commits, usually with a commit message to explain the rationale for the change.

In a few cases the underlying bugs were simple enough that I went ahead and fixed them on the spot.

@github-actions github-actions bot added the JS label Feb 4, 2025
@asgerf asgerf force-pushed the js/test-suite branch 4 times, most recently from 6e464f3 to 3a86b71 Compare February 7, 2025 13:26
@@ -1 +1 @@
Comments/CommentedOutCode.ql
query: Comments/CommentedOutCode.ql

Check warning

Code scanning / CodeQL

Query test without inline test expectations Warning test

Query test does not use inline test expectations.
@@ -1 +1 @@
Comments/TodoComments.ql
query: Comments/TodoComments.ql

Check warning

Code scanning / CodeQL

Query test without inline test expectations Warning test

Query test does not use inline test expectations.
@@ -1 +1 @@
JSDoc/JSDocForNonExistentParameter.ql
query: JSDoc/JSDocForNonExistentParameter.ql

Check warning

Code scanning / CodeQL

Query test without inline test expectations Warning test

Query test does not use inline test expectations.
@@ -1 +1 @@
LanguageFeatures/ConditionalComments.ql
query: LanguageFeatures/ConditionalComments.ql

Check warning

Code scanning / CodeQL

Query test without inline test expectations Warning test

Query test does not use inline test expectations.
@@ -1 +1 @@
NodeJS/UnusedDependency.ql
query: NodeJS/UnusedDependency.ql

Check warning

Code scanning / CodeQL

Query test without inline test expectations Warning test

Query test does not use inline test expectations.
@@ -1 +1 @@
Security/CWE-094/ExpressionInjection.ql
query: Security/CWE-094/ExpressionInjection.ql

Check warning

Code scanning / CodeQL

Query test without inline test expectations Warning test

Query test does not use inline test expectations.
@@ -1 +1 @@
Security/CWE-300/InsecureDependencyResolution.ql
query: Security/CWE-300/InsecureDependencyResolution.ql

Check warning

Code scanning / CodeQL

Query test without inline test expectations Warning test

Query test does not use inline test expectations.
@@ -1 +1 @@
Security/CWE-312/ActionsArtifactLeak.ql
query: Security/CWE-312/ActionsArtifactLeak.ql

Check warning

Code scanning / CodeQL

Query test without inline test expectations Warning test

Query test does not use inline test expectations.
@@ -1 +1 @@
Security/CWE-313/PasswordInConfigurationFile.ql
query: Security/CWE-313/PasswordInConfigurationFile.ql

Check warning

Code scanning / CodeQL

Query test without inline test expectations Warning test

Query test does not use inline test expectations.
@@ -1 +1 @@
Security/CWE-862/EmptyPasswordInConfigurationFile.ql
query: Security/CWE-862/EmptyPasswordInConfigurationFile.ql

Check warning

Code scanning / CodeQL

Query test without inline test expectations Warning test

Query test does not use inline test expectations.
@@ -1 +1 @@
JSDoc/BadParamTag.ql
query: JSDoc/BadParamTag.ql

Check warning

Code scanning / CodeQL

Query test without inline test expectations Warning test

Query test does not use inline test expectations.
@@ -1 +1 @@
LanguageFeatures/SyntaxError.ql
query: LanguageFeatures/SyntaxError.ql

Check warning

Code scanning / CodeQL

Query test without inline test expectations Warning test

Query test does not use inline test expectations.
@asgerf asgerf force-pushed the js/test-suite branch 2 times, most recently from a456a5b to 77a27de Compare February 27, 2025 14:11
@@ -1 +1 @@
Security/CWE-312/BuildArtifactLeak.ql
query: Security/CWE-312/BuildArtifactLeak.ql

Check warning

Code scanning / CodeQL

Query test without inline test expectations Warning test

Query test does not use inline test expectations.
@@ -1 +1 @@
LanguageFeatures/WrongExtensionJSON.ql
query: LanguageFeatures/WrongExtensionJSON.ql

Check warning

Code scanning / CodeQL

Query test without inline test expectations Warning test

Query test does not use inline test expectations.
@asgerf asgerf force-pushed the js/test-suite branch 3 times, most recently from bc599d1 to 9df0bf6 Compare February 28, 2025 12:21
Some OK-style comments had to be moved to the following line, shifting line numbers.

In selected range also included the comments themselves.

Lastly, the result sets were reordered by the CLI in some cases.
JSON does not support comments so we can't use inline expectations
The presence of a syntax error sometimes prevents us from parsing the inline comment correctly.
One of the diffs look confusing but:
Previously parameter {2,3} where flagged, now parameter {1,2} are flagged.

Note that for command injection, the SystemCommandExecution is flagged
despite the test file claiming otherwise.
This adds Alert annotations for alerts that seem intentional by the test
but has not been annotated with 'NOT OK', or the comment was in the wrong
place.

In a few cases I included 'Source' expectations to make it easier to see
what happened. Other 'Source' expectations will be added in bulk a later
commit.
These are listed in a function called 'good' but it's difficult to say in isolation whether they should be flagged or not. Accepting the changes as they seem reasonable.
We had two 'NOT OK' comments for the same alert. The alert appears on the 'pref' object above.
Using RelatedLocations to add clarity
This file was added on main while this branch was in progress. Porting the whole file in one step.
The history of updates to this test got messed up so just squashing
into one commit.

Some possible regressions have been accepted, but the query is strangely
opinionated so it's just hard to say what it ought to flag.
@asgerf asgerf marked this pull request as ready for review February 28, 2025 13:41
@asgerf asgerf requested a review from a team as a code owner February 28, 2025 13:41
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant