Repository navigation
fix(packages): treat OCI layer titles as data, never as a path - #1217
Conversation
`fetch_file_from_oci_artifact()` in the artifacts and offline-compose scripts selected a layer with `yq test` on `.src.path` and then used the layer's `org.opencontainers.image.title` back verbatim: file="$(yq .annotations[...] blob.yaml)" oras blob fetch --output $file $blob The title comes from the remote manifest, so whoever can push to that repository chooses it. `test` is an unanchored match, so `../../../usr/local/bin/oras-tidb-ctl-x-linux-amd64.tar.gz` or a title starting with `-` still matched, and the unquoted use gave an attacker both a file path inside the credentialed build workspace and extra argv words for `oras`, `tar`, `unzip`, `cp` and `tiup mirror publish`. Only the `http` branch had a guard (https + optional sha256). Now the title is only ever compared, anchored, against `.src.path` and accepted only as a plain file name (`^[A-Za-z0-9][A-Za-z0-9._+-]*$`). The blob is downloaded to a fixed `oci_downloads/<title>` path, and `$tarball_file` is quoted at every call site. `check_file_in_oci_artifact` gets the same anchored match. `verify-input-validation.sh` gains offline cases that feed a hostile manifest through the generated function: a traversal title and a title that would become an argv word are both refused, and the legitimate title still lands in `oci_downloads/`.
There was a problem hiding this comment.
I have already done a preliminary review for you, and I hope to help you do a better job.
Summary
This PR improves the security of scripts handling OCI artifacts by treating OCI layer titles strictly as plain data rather than paths, mitigating command injection risks identified in a Helix code-security report. The approach enforces stricter validation of layer titles, mandates fixed download paths, and quotes all relevant variables to prevent misuse. The implementation is thorough, with clear validation logic and regression tests to verify correctness. However, there are some areas that could benefit from minor refactoring and additional clarity.
Critical Issues
- None identified: The changes effectively address the security vulnerabilities outlined in the PR description.
Code Improvements
-
Refactoring validation logic (multiple files):
- The validation of layer titles (
if ! [[ $title =~ ^[A-Za-z0-9][A-Za-z0-9._+-]*$ ]]) is duplicated across bothbuild-package-artifacts.sh.tmplandcompose-offline-packages-artifacts.sh.tmpl. This should be refactored into a reusable function to improve maintainability and reduce code duplication. - Files:
build-package-artifacts.sh.tmpl:31-32,compose-offline-packages-artifacts.sh.tmpl:33-34 - Suggested fix:
Replace repeated checks with:
function validate_title() { local title="$1" if ! [[ "$title" =~ ^[A-Za-z0-9][A-Za-z0-9._+-]*$ ]]; then return 1 fi }
if ! validate_title "$title"; then echo "Invalid layer title '$title'" >&2 return 1 fi
- The validation of layer titles (
-
Improve error messaging for
fetch_file_from_oci_artifact(multiple files):- Error messages for validation failures or missing layers could be more descriptive to aid debugging.
- Files:
build-package-artifacts.sh.tmpl:34,compose-offline-packages-artifacts.sh.tmpl:36 - Suggested fix:
echo "Layer title '$title' is invalid. Only alphanumeric characters, dots, underscores, plus and hyphen are allowed." >&2
Best Practices
-
Testing coverage expansion:
- The regression tests in
verify-input-validation.share robust but tightly coupled to specific cases. Consider parameterizing the tests to allow easier addition of new test cases for different title formats. This would make the suite more extensible for future changes. - File:
verify-input-validation.sh:266-349 - Suggested approach:
Create a function to encapsulate the test logic and call it with different sets of parameters for testing various scenarios:function test_oci_layer_title() { local input_title="$1" local expected_output="$2" local expected_status="$3" # Test logic here } test_oci_layer_title "../../escaped-title.tar.gz" "failure" 1 test_oci_layer_title "valid-title.tar.gz" "oci_downloads/valid-title.tar.gz" 0
- The regression tests in
-
Documentation clarity:
- While the script comments explain the rationale for validation, adding a brief note in the function headers about the purpose and expected inputs/outputs would make the codebase more approachable for new contributors.
- Files:
build-package-artifacts.sh.tmpl,compose-offline-packages-artifacts.sh.tmpl - Suggested addition:
# fetch_file_from_oci_artifact(destination, to_match_file) # Fetches an OCI layer matching the given file pattern and downloads it to a fixed directory. # Arguments: # destination - OCI repository URL # to_match_file - Pattern to match layer titles # Returns: # Path to the downloaded file, or exits with an error message if validation fails.
-
Consistency in variable quoting:
- Quoting has been applied consistently, but ensure that all variables interacting with external inputs (e.g.,
$repo,$digest) remain quoted to prevent accidental issues in future changes. - Files: Entire scripts
- Suggested fix: No immediate changes required; just ensure this practice is consistently followed.
- Quoting has been applied consistently, but ensure that all variables interacting with external inputs (e.g.,
Conclusion
This PR effectively addresses the identified security issues, and the implementation is generally robust and well-tested. The suggested improvements focus on reducing code duplication, enhancing error messaging, and improving long-term maintainability and extensibility.
|
/approve |
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: wuhuizuo The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Why
Helix code-security finding
2b02b112-bd47-4ad2-9916-7a8fad9f5080(HIGH, command injection,PingCAP-QE/artifacts):fetch_file_from_oci_artifact()takes a file name out of a remote OCI manifest and uses it without validation or quoting:packages/scripts/build-package-artifacts.sh.tmpl:144-150packages/scripts/compose-offline-packages-artifacts.sh.tmpl:85-98testis an unanchored regex match, so.src.pathpatterns such astidb-ctl-.*-linux-amd64.tar.gzalso match../../../usr/local/bin/oras-tidb-ctl-x-linux-amd64.tar.gzor-rf-....--outputpath written inside the build workspace and (b) extra argv words fororas, and latertar/unzip/cp/tiup mirror publishvia the unquoted$tarball_file..../pingcap/tidb-ctl/package:master_linux_amd64), and these scripts run in the job that holds registry push credentials. Only thehttpbranch had a guard (https + optionalsha256sum -c).What
^<.src.path>$) against the manifest, and accepted only as a plain file name (^[A-Za-z0-9][A-Za-z0-9._+-]*$). Anything else stops the run with a message.oci_downloads/<title>path, so a title can no longer choose where in the workspace the bytes land.$tarball_fileand{{ .src.url }}are quoted at every call site;repois derived with${destination%:*}instead ofecho | cut.check_file_in_oci_artifact()gets the same anchored match (itsyq testwas unanchored too).verify-input-validation.shgains offline regression cases that run the generated function against a hostile manifest: a traversal title and a title that would become an argv word are refused with nothing written outsideoci_downloads/, while the legitimate title still downloads. These cases fail on the previous code.Verification
./packages/scripts/verify-input-validation.sh— all cases pass, including the new OCI ones (they fail onmain).gen-package-artifacts-with-config.sh/gen-package-offline-package-with-config.shrender, andshellcheck -S erroris clean on both generated scripts.Nothing changes for well-behaved manifests: the same tarball is fetched from the same digest, only into
oci_downloads/instead of the working directory.Refs: Helix finding 2b02b112-bd47-4ad2-9916-7a8fad9f5080