Skip to content

Conversation

@the-storm
Copy link
Contributor

Pre-submission checklist

  • I've ran the following linters locally and fixed lint errors related to the files I modified in this PR
    • black .
    • usort format .
    • flake8
  • I've installed dev dependencies pip install -r requirements-dev.txt and completed the following:
    • I've ran tests with ./scripts/run-tests.sh and made sure all tests are passing

Summary

This PR adds initial tests to SARIF output from SAPP. Currently the SARIF output has no test coverage at all. This API add a schema test that the output SARIF passes the defined schema.

Test Plan

./scripts/run-tests.sh
----------------------------------------------------------------------
Ran 111 tests in 1.719s
...

and running the specific new tests

(venv) ielsayed@ibrahims-mbp-2 ~/s/r/personal-sapp (add-sarif-schema-validation-tests) [SIGINT]> python3 -m unittest -v sapp.ui.tests.sarif_test
testSarifSchemaCheckWithIssuesNoTraces (sapp.ui.tests.sarif_test.SarifTest.testSarifSchemaCheckWithIssuesNoTraces) ... ok
testSarifSchemaCheckWithIssuesWithTraces (sapp.ui.tests.sarif_test.SarifTest.testSarifSchemaCheckWithIssuesWithTraces) ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.134s

OK

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Sep 13, 2023
callee_port=trace_frame.callee_port,
caller="",
caller_port="",
filename="",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

to conform with the schema definition


class SARIF:
version: str = "2.1.0"
schema: str = "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json" # noqa
Copy link
Contributor Author

Choose a reason for hiding this comment

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

the old schema is actually had a broken regex and it was fixed in the codeQL schema see this commit github/codeql-action@9824588

Comment on lines +143 to +144
if len(trace_tuples) == 0:
return []
Copy link
Contributor Author

Choose a reason for hiding this comment

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

to conform with the schema definition

@the-storm
Copy link
Contributor Author

I think the failing test is just a flaky test. I don't think the failure is from this PR

@the-storm
Copy link
Contributor Author

/cc @arthaud not sure if you guys missed this PR :D

import jsonschema
import requests

from sapp.sarif import SARIF
Copy link
Contributor

Choose a reason for hiding this comment

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

Below we use relative imports and here we use an absolute import, is this intentional? If not then let's try to be consisent.

output = sarif.to_json()
output = json.loads(output)
try:
response = requests.get(SARIF.schema)
Copy link
Contributor

Choose a reason for hiding this comment

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

Not a big fan of tests pulling random stuff from the internet. Maybe we could just push that file in the repository?

Comment on lines +118 to +119
output = sarif.to_json()
output = json.loads(output)
Copy link
Contributor

Choose a reason for hiding this comment

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

sarif.to_json() does a json.dumps internally, so this is doing a back-and-forth between the python representation and a string. It would be nice to avoid this, but I guess that's not a big deal for a test..

Comment on lines +219 to +229
sarif = SARIF("mariana-trench", session, set(issues))
output = sarif.to_json()
output = json.loads(output)
try:
response = requests.get(SARIF.schema)
response.raise_for_status()
schema = response.json()
jsonschema.Draft202012Validator(schema).validate(output)
except Exception as e:
print(f"Error downloading schema: {e}")
raise e
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe move that in a function, since it's used twice.

Comment on lines +208 to +217
input_return_map = {
(session, issues[0].issue_instance_id, TraceKind.POSTCONDITION): [
source_frames_query_results[0],
source_frames_query_results[1],
],
(session, issues[0].issue_instance_id, TraceKind.PRECONDITION): [
sink_frames_query_results[0],
sink_frames_query_results[1],
],
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this necessary? From my understanding, since we use self.fakes.precondition/postcondition, those should be in the database and initial_frames should find those?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants