Skip to content

How to parse JSON file in code using CodeQL? #19351

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

Closed
donky16 opened this issue Apr 23, 2025 · 2 comments
Closed

How to parse JSON file in code using CodeQL? #19351

donky16 opened this issue Apr 23, 2025 · 2 comments
Labels
question Further information is requested

Comments

@donky16
Copy link

donky16 commented Apr 23, 2025

I have a Java project and its permission settings use JSON file to config. Now I can not use CodeQL to parse JSON file and find BAC issues.

@donky16 donky16 added the question Further information is requested label Apr 23, 2025
@redsun82
Copy link
Contributor

👋 @donky16 there's no official supported way of extracting JSON data into CodeQL. That said, there is a roundabout way of doing it, with many caveats.

The idea is that JSON is a subset of YAML, which we can extract, but which only certain language library packs can then analyse.

In order to force the YAML extractor to extract JSON, you can run:

codeql database init --language=yaml --source-root=. json-db
codeql database index-files --language=yaml --include-extension=.json json-db
codeql database finalize json-db

You can double check that the expected json files were extracted by looking in json-db/src.zip. You can tweak what is extracted by passing --include=<pattern> or --include=!<negated pattern> to the database index-files command.

Beware, that this data cannot be put in the same database that is used for java analysis, and must be a separate database. Then, you can use any CodeQL language library pack that understands YAML to write queries on these files. Currently the library packs that include YAML support are codeql/python-all, codeql/javascript-all and codeql/actions-all. You can then work with the JSON trees importing respectively semmle.python.Yaml, semmle.javascript.YAML or codeql.actions.ast.internal.Yaml.

@donky16
Copy link
Author

donky16 commented Apr 24, 2025

Thanks @redsun82

@donky16 donky16 closed this as completed Apr 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants