Skip to content

Commit a6dd57a

Browse files
authored
Merge pull request #7 from advanced-security/sarif-patching
SARIF Patching fix
2 parents ea24236 + a2d96f4 commit a6dd57a

File tree

8 files changed

+45
-29
lines changed

8 files changed

+45
-29
lines changed

.release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: "codeql-extractor-action"
22
repository: "advanced-security/codeql-extractor-action"
3-
version: 0.0.15
3+
version: 0.0.16
44

55
ecosystems:
66
- Docs

CONTRIBUTING.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ If you have an idea for a new feature or enhancement, please open an issue on Gi
2828
3. Make your changes
2929
4. Write tests for your changes (if applicable)
3030
5. Run the tests to make sure everything is working
31+
6. Commit your changes with a clear commit message
32+
7. Push your changes to your fork
33+
8. Open a [pull request][pr] against the `main` branch of the original repository
3134

3235
### Required Tools
3336

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "codeql-extractor-action"
33
description = "GitHub Action for CodeQL Extractors"
4-
version = "0.0.15"
4+
version = "0.0.16"
55
authors = ["GeekMasher"]
66

77
license = "MIT"

README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ This action is designed to be used in conjunction with the [CodeQL][CodeQL] anal
2929

3030
```yml
3131
- name: "CodeQL Extractor Action"
32-
uses: advanced-security/codeql-extractor-action@v0.0.15
32+
uses: advanced-security/codeql-extractor-action@v0.0.16
3333
with:
3434
# Repository reference (e.g. "owner/repo", "owner/repo@ref")
3535
extractor: "advanced-security/codeql-extractor-iac"
@@ -50,8 +50,7 @@ A CodeQL extractor is a tool that extracts code from a repository and prepares i
5050
To create an extractor, you need to create a GitHub repository that contains the extractor releases as an artifact / assest in a GitHub release.
5151
The extractor should be a Tarball file that contains the compiled extractor and all other necessary files for the extractor to run.
5252
53-
54-
## Maintainers
53+
## Maintainers
5554
5655
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
5756
<!-- prettier-ignore-start -->
@@ -69,7 +68,7 @@ The extractor should be a Tarball file that contains the compiled extractor and
6968
7069
## Support
7170
72-
Please create [GitHub Issues][github-issues] if there are bugs or feature requests.
71+
Please create [GitHub Issues][github-issues] or [GitHub Discussion][github-discussions] if there are bugs or feature requests.
7372
7473
This project uses [Sematic Versioning (v2)](https://semver.org/) and with major releases, breaking changes will occur.
7574
@@ -78,13 +77,11 @@ This project uses [Sematic Versioning (v2)](https://semver.org/) and with major
7877
This project is licensed under the terms of the MIT open source license.
7978
Please refer to [MIT][license] for the full terms.
8079
81-
8280
<!-- Resoucres -->
8381
8482
[license]: ./LICENSE
8583
[github]: https://github.com/advanced-security/codeql-extractor-action
8684
[github-issues]: https://github.com/advanced-security/codeql-extractor-action/issues
8785
[github-actions]: https://github.com/advanced-security/codeql-extractor-action/actions
8886
[github-discussions]: https://github.com/advanced-security/codeql-extractor-action/discussions
89-
9087
[CodeQL]: https://codeql.github.com/

action.Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
FROM ghcr.io/advanced-security/codeql-extractor-action:v0.0.15
1+
FROM ghcr.io/advanced-security/codeql-extractor-action:v0.0.16
22

33
ENTRYPOINT [ "codeql-extractor-action" ]

src/extractors.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,38 @@ pub async fn fetch_extractor(
149149
Ok(extractor_pack)
150150
}
151151

152+
/// Update the SARIF file with the extractor information (CodeQL ${language})
153+
///
154+
/// Update only the `runs.0.tool.driver` section of the SARIF file
155+
pub fn update_sarif(path: &PathBuf, extractor: String) -> Result<()> {
156+
let sarif_content =
157+
std::fs::read_to_string(path).context(format!("Failed to read SARIF file: {:?}", path))?;
158+
let mut sarif_json: serde_json::Value = serde_json::from_str(&sarif_content)
159+
.context(format!("Failed to parse SARIF file: {:?}", path))?;
160+
161+
log::debug!("SARIF JSON :: {sarif_json:#?}");
162+
if let Some(tool) = sarif_json
163+
.get_mut("runs")
164+
.and_then(|runs| runs.get_mut(0))
165+
.and_then(|run| run.get_mut("tool"))
166+
{
167+
if let Some(driver) = tool.get_mut("driver") {
168+
driver["name"] = serde_json::Value::String(format!("CodeQL - {}", extractor));
169+
log::info!("Updated SARIF file with extractor: {extractor}");
170+
} else {
171+
log::warn!("No 'driver' field found in SARIF file");
172+
}
173+
} else {
174+
log::warn!("No 'runs' or 'tool' field found in SARIF file");
175+
}
176+
177+
let data = serde_json::to_string(&sarif_json)
178+
.context(format!("Failed to serialize SARIF JSON: {:?}", path))?;
179+
// Write the updated SARIF back to the file
180+
std::fs::write(path, data).context(format!("Failed to write SARIF file: {:?}", path))?;
181+
Ok(())
182+
}
183+
152184
/// Update the permissions for tool scripts (*.sh) and the extractor (extractor)
153185
fn update_tools_permisisons(path: &PathBuf) -> Result<()> {
154186
let tools_path = path.join("tools");

src/main.rs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use anyhow::{Context, Result};
22
use ghactions::{ActionTrait, group, groupend};
33
use ghactions_core::RepositoryReference;
4+
use ghastoolkit::codeql::database::queries::CodeQLQueries;
45
use ghastoolkit::prelude::*;
5-
use ghastoolkit::{Sarif, codeql::database::queries::CodeQLQueries};
66
use log::{debug, info};
77

88
mod action;
@@ -189,24 +189,8 @@ async fn main() -> Result<()> {
189189

190190
log::info!("Post-processing SARIF results");
191191

192-
match Sarif::try_from(sarif_path.clone()) {
193-
Ok(mut sarif) => {
194-
log::info!("Updating SARIF tool name for language: {language}");
195-
sarif.runs.iter_mut().for_each(|run| {
196-
run.tool.driver.name = format!("CodeQL - {language}");
197-
});
198-
199-
log::debug!("Writing SARIF file to {sarif_path:?}");
200-
if let Err(e) = std::fs::write(&sarif_path, serde_json::to_string(&sarif)?) {
201-
log::error!("Failed to write SARIF file: {e}");
202-
} else {
203-
log::info!("SARIF file written successfully: {sarif_path:?}");
204-
}
205-
}
206-
Err(e) => {
207-
log::error!("Failed to read and parse SARIF file: {e}");
208-
}
209-
}
192+
extractors::update_sarif(&sarif_path, extractor.display_name.clone())
193+
.context("Failed to update SARIF file with extractor information")?;
210194

211195
// Reload the database to get analysis info
212196
database.reload()?;

0 commit comments

Comments
 (0)