Skip to content

Add new "evaluations" format support to Anchorectl parser #12425

Merged
Maffooch merged 5 commits into
DefectDojo:bugfixfrom
cosmel-dojo:sc-11051-fix-anchorectl-parser-support
May 22, 2025
Merged

Add new "evaluations" format support to Anchorectl parser #12425
Maffooch merged 5 commits into
DefectDojo:bugfixfrom
cosmel-dojo:sc-11051-fix-anchorectl-parser-support

Conversation

@cosmel-dojo

@cosmel-dojo cosmel-dojo commented May 12, 2025

Copy link
Copy Markdown
Contributor

⚠️ Note on feature completeness ⚠️

Description

This PR fixes the AnchoreCTL Policies Report parser to support both legacy and new formats generated by the AnchoreCTL tool. Users reported issues importing newer format policy reports from AnchoreCTL with the -o json flag, as the parser expected a list at the root level, but new format has an object with an evaluations array.

The changes:

  • Add detection logic to identify the new format (object with 'evaluations' array)
  • Implement transformation logic to convert new format data into a structure compatible with existing parser logic
  • Improve error handling with more descriptive messages
  • Make field extraction more robust with proper fallbacks for both formats

Test results

Extended the test suite by adding 4 new test cases that specifically target the new format:

  • test_new_format_anchore_engine_parser_has_no_finding
  • test_new_format_anchore_engine_parser_has_one_finding_and_it_is_correctly_parsed
  • test_new_format_anchore_engine_parser_has_many_findings
  • test_new_format_anchore_engine_parser_has_one_finding_and_description_has_severity

All tests pass successfully, verifying that both the legacy and new formats are properly supported.

Commands to run tests locally:

# Make sure the testing environment is running
docker compose -f docker-compose.yml -f docker-compose.override.unit_tests.yml up -d

# Run the tests
./run-unittest.sh --test-case unittests.tools.test_anchorectl_policies_parser.TestAnchoreCTLPoliciesParser

Documentation

No documentation update needed as the parser description already mentions both formats are supported.

Checklist

  • Make sure to rebase your PR against the very latest dev.
  • Features/Changes should be submitted against the dev.
  • Bugfixes should be submitted against the bugfix branch.
  • Give a meaningful name to your PR, as it may end up being used in the release notes.
  • Your code is flake8 compliant.
  • Your code is python 3.11 compliant.
  • If this is a new feature and not a bug fix, you've included the proper documentation in the docs at https://github.com/DefectDojo/django-DefectDojo/tree/dev/docs as part of this PR.
  • Model changes must include the necessary migrations in the dojo/db_migrations folder.
  • Add applicable tests to the unit tests.
  • Add the proper label to categorize your PR.

… array

This commit updates the AnchoreCTL Policies parser to support both the legacy and new format reports generated by the AnchoreCTL tool.

Changes:
- Added detection for the new format which has an object with evaluations array instead of a root-level list
- Implemented conversion logic to transform the new format into a compatible structure for parsing
- Improved error handling with more descriptive messages
- Made field extraction more robust with proper fallbacks between formats

The parser now successfully processes both:
- Legacy format (list at root level)
- New format from anchorectl policy evaluate -o json (object with evaluations array)
@dryrunsecurity

dryrunsecurity Bot commented May 12, 2025

Copy link
Copy Markdown

DryRun Security

This pull request reveals multiple security concerns, including potential information disclosure through verbose error handling, unsafe JSON parsing, root user permissions in Docker images, and two significant vulnerabilities (high and medium severity) in a test binary, which collectively suggest the need for improved security practices and vulnerability management.

💭 Unconfirmed Findings (5)
Vulnerability Potential Information Disclosure through Verbose Error Handling
Description Located in dojo/tools/anchorectl_policies/parser.py, this vulnerability involves logging warnings instead of raising exceptions, which could allow partial or incomplete scan results without clear user indication, potentially leading to missed security findings.
Vulnerability Unsafe JSON Parsing with Multiple Decoding Attempts
Description Found in dojo/tools/anchorectl_policies/parser.py, this risk involves multiple JSON decoding attempts that could expose the parser to inconsistent parsing behaviors, potentially causing parsing errors or unexpected results.
Vulnerability Root User Effective Permissions
Description Discovered in multiple test JSON files (unittests/scans/anchorectl_policies/), this vulnerability involves Docker images running with root user privileges, which presents elevated security risks if the container is compromised.
Vulnerability High Severity Vulnerability in Non-OS Package
Description Located in unittests/scans/anchorectl_policies/new_format_many_violations.json, this is a HIGH severity vulnerability (CVE-2022-1234) in /usr/local/bin/testbinary with a 'stop' status, indicating a critical security concern.
Vulnerability Medium Severity Vulnerability in Non-OS Package
Description Found in unittests/scans/anchorectl_policies/new_format_many_violations.json, this is a MEDIUM severity vulnerability (GHSA-1234-abcd-5678) in /usr/local/bin/testbinary with a 'stop' status, representing a significant security risk.

All finding details can be found in the DryRun Security Dashboard.

@Maffooch Maffooch requested a review from dogboat May 12, 2025 18:01
@Maffooch Maffooch added this to the 2.46.3 milestone May 12, 2025
@valentijnscholten

Copy link
Copy Markdown
Member

Hello @cosmel-dojo, welcome to the team. Does this PR fix #12362? I think it does, but the example report in there contains a little bit more date than the samples in this PR.

@cosmel-dojo

Copy link
Copy Markdown
Contributor Author

Hello @cosmel-dojo, welcome to the team. Does this PR fix #12362? I think it does, but the example report in there contains a little bit more date than the samples in this PR.

I used the sample file the user attached, to apply the solution.

The tests I added are using the new format that the AnchoreCTL uses.

@cosmel-dojo cosmel-dojo changed the title Sc 11051 fix anchorectl parser support Fix anchorectl parser support May 15, 2025

@dogboat dogboat left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice! 🙌

@valentijnscholten

Copy link
Copy Markdown
Member

@cosmel-dojo Would it make sense to one or two lines to ./docs/content/en/connecting_your_tools/parsers/file/anchorectl_policies.md to explain the command needed to generate the report(s)? It might be very straightforward because I don't know anchore. But in general I think it's good to have an example command in the docs.

@github-actions github-actions Bot added the docs label May 16, 2025
@cosmel-dojo

Copy link
Copy Markdown
Contributor Author

@cosmel-dojo Would it make sense to one or two lines to ./docs/content/en/connecting_your_tools/parsers/file/anchorectl_policies.md to explain the command needed to generate the report(s)? It might be very straightforward because I don't know anchore. But in general I think it's good to have an example command in the docs.

Hey @valentijnscholten

I updated the Anchorectl Policies.

Comment thread docs/content/en/connecting_your_tools/parsers/file/anchorectl_policies.md Outdated
@valentijnscholten valentijnscholten changed the title Fix anchorectl parser support Add new "evaluations" format support to Anchorectl parser May 17, 2025

@mtesauro mtesauro left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Approved

@valentijnscholten valentijnscholten modified the milestones: 2.46.3, 2.47.0 May 19, 2025
@Maffooch Maffooch merged commit 5a57c1f into DefectDojo:bugfix May 22, 2025
78 checks passed
xansec pushed a commit to xansec/django-DefectDojo that referenced this pull request Jun 18, 2025
…#12425)

* Fix AnchoreCTL Policies parser to support new format with evaluations array

This commit updates the AnchoreCTL Policies parser to support both the legacy and new format reports generated by the AnchoreCTL tool.

Changes:
- Added detection for the new format which has an object with evaluations array instead of a root-level list
- Implemented conversion logic to transform the new format into a compatible structure for parsing
- Improved error handling with more descriptive messages
- Made field extraction more robust with proper fallbacks between formats

The parser now successfully processes both:
- Legacy format (list at root level)
- New format from anchorectl policy evaluate -o json (object with evaluations array)

* Added tests for the new format to verify correct parsing

* Fixed linter errors

* Update AnchoreCTL Policies Report documentation for clarity and format support

* Removed unnecessary text from anchorectl_policies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants