Repository navigation
feat(scan-image): fail scans of images with pip #74
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f6b3cdd
feat(scan-image): fail scans of images with pip
uniqueg 23e314a
fix(scan-image): count conda pip packages
uniqueg 6c02d20
fix(scan-image): address review of the pip check
uniqueg ddedf06
fix(scan-image): show pip findings as code
uniqueg 76fd80c
fix(scan-image): harden the pip check inputs
uniqueg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| #!/usr/bin/env bash | ||
| # Checks that an image doesn't contain pip. Input in the environment: | ||
| # SCAN_PACKAGES, the path of Trivy's JSON report of the image with all | ||
| # packages listed (`--list-all-pkgs`). Fails if the report lists pip as a | ||
| # Python, conda or OS package (see "pip in images" in README.md for what | ||
| # counts), and if the report can't be read; lists the packages found in the | ||
| # log and, if available, the step summary | ||
| set -euo pipefail | ||
|
|
||
| report="${SCAN_PACKAGES:-}" | ||
|
|
||
| if ! command -v jq >/dev/null; then | ||
| echo "::error title=Scan image::The pip check needs jq on the runner" | ||
| exit 1 | ||
| fi | ||
|
|
||
| # A Trivy report: results and their packages are absent, null or arrays | ||
| # (anything else could hide packages from the check below) | ||
| if [[ -z "${report}" || ! -f "${report}" ]] \ | ||
| || ! jq -e ' | ||
| type == "object" and has("ArtifactName") | ||
| and (.Results | type | IN("null", "array")) | ||
| and all(.Results[]?; | ||
| type == "object" and (.Packages | type | IN("null", "array")) | ||
| and all(.Packages[]?; type == "object"))' "${report}" \ | ||
| >/dev/null 2>&1; then | ||
|
sourcery-ai[bot] marked this conversation as resolved.
Resolved
|
||
| echo "::error title=Scan image::No package list of the image to check for pip" | ||
| exit 1 | ||
| fi | ||
|
|
||
| # One line per package; control characters (from the image's metadata) are | ||
| # removed, so that a field can't start a new log line | ||
| found="$(jq -r ' | ||
| [.Results[]? | ||
| | . as $result | ||
| | .Packages[]? | ||
| | ((.Name // "") | tostring | ascii_downcase) as $name | ||
| | select( | ||
| ($result.Class == "lang-pkgs" | ||
| and ($result.Type | IN("python-pkg", "conda-pkg")) | ||
| and $name == "pip") | ||
| or ($result.Class == "os-pkgs" | ||
| and ($name | test("^((platform-)?(py|python)[0-9.]*-)?pip$")))) | ||
| | "\(.Name) \(.Version // "?") (\(.FilePath // $result.Target // "?"))" | ||
|
sourcery-ai[bot] marked this conversation as resolved.
Resolved
|
||
| | gsub("[[:cntrl:]]"; "")] | ||
| | unique[]' "${report}")" | ||
|
|
||
| if [[ -n "${found}" ]]; then | ||
| # Strip characters that could start a workflow command, and escape `%` as | ||
| # the runner expects in a command's message | ||
| found="${found//::/}" | ||
| message="${found//%/%25}" | ||
| echo "::error title=Scan image::The image contains pip: ${message//$'\n'/, }" | ||
| if [[ -n "${GITHUB_STEP_SUMMARY:-}" ]]; then | ||
| { | ||
| echo "### The image contains pip" | ||
| echo | ||
| # As code, so that the image's metadata isn't rendered as Markdown | ||
| while IFS= read -r line; do | ||
| echo "- \`${line//\`/}\`" | ||
| done <<< "${found}" | ||
| echo | ||
| echo "Remove pip from the image (e.g., install packages with uv), or" | ||
| echo "set the input \`fail-on-pip\` of the scan action to \`false\`." | ||
| } >> "${GITHUB_STEP_SUMMARY}" | ||
| fi | ||
| exit 1 | ||
| fi | ||
| echo "No pip in the image" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| { | ||
| "SchemaVersion": 2, | ||
| "ArtifactName": "fixture-alpine-py3-pip", | ||
| "ArtifactType": "container_image", | ||
| "Results": [ | ||
| { | ||
| "Target": "pip-os-test:latest (alpine 3.22.8)", | ||
| "Class": "os-pkgs", | ||
| "Type": "alpine", | ||
| "Packages": [ | ||
| { | ||
| "Name": "alpine-baselayout", | ||
| "Version": "3.7.0-r0" | ||
| }, | ||
| { | ||
| "Name": "alpine-baselayout-data", | ||
| "Version": "3.7.0-r0" | ||
| }, | ||
| { | ||
| "Name": "py3-pip", | ||
| "Version": "25.1.1-r0" | ||
| }, | ||
| { | ||
| "Name": "py3-pip-pyc", | ||
| "Version": "25.1.1-r0" | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| { | ||
| "SchemaVersion": 2, | ||
| "ArtifactName": "fixture-scratch-pip-dist-info", | ||
| "ArtifactType": "container_image", | ||
| "Results": [ | ||
| { | ||
| "Target": "Python", | ||
| "Class": "lang-pkgs", | ||
| "Type": "python-pkg", | ||
| "Packages": [ | ||
| { | ||
| "Name": "pip", | ||
| "Version": "26.0", | ||
| "FilePath": "usr/local/lib/python3.13/site-packages/pip-26.0.dist-info/METADATA" | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.