Skip to content

fix(packages): treat OCI layer titles as data, never as a path - #1217

Merged
ti-chi-bot[bot] merged 1 commit into
mainfrom
fix/packages-oci-layer-title-untrusted
Nov 20, 2026
Merged

ti-chi-bot[bot] merged 1 commit into
mainfrom
fix/packages-oci-layer-title-untrusted

Conversation

@wuhuizuo

Copy link
Copy Markdown
Contributor

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-150
  • packages/scripts/compose-offline-packages-artifacts.sh.tmpl:85-98
oras manifest fetch $destination | yq ... ".layers | filter(.annotations[...] | test \"$to_match_file\") | .[0]" >blob.yaml
file="$(yq .annotations[...] blob.yaml)"
oras blob fetch --output $file $blob
echo "$file"
  • test is an unanchored regex match, so .src.path patterns such as tidb-ctl-.*-linux-amd64.tar.gz also match ../../../usr/local/bin/oras-tidb-ctl-x-linux-amd64.tar.gz or -rf-....
  • the title is then used unquoted: it becomes (a) the --output path written inside the build workspace and (b) extra argv words for oras, and later tar/unzip/cp/tiup mirror publish via the unquoted $tarball_file.
  • the sources are mutable staging tags (.../pingcap/tidb-ctl/package:master_linux_amd64), and these scripts run in the job that holds registry push credentials. Only the http branch had a guard (https + optional sha256sum -c).

What

  • The title is now only ever compared: anchored (^<.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.
  • The blob is downloaded to a fixed oci_downloads/<title> path, so a title can no longer choose where in the workspace the bytes land.
  • $tarball_file and {{ .src.url }} are quoted at every call site; repo is derived with ${destination%:*} instead of echo | cut.
  • check_file_in_oci_artifact() gets the same anchored match (its yq test was unanchored too).
  • verify-input-validation.sh gains 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 outside oci_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 on main).
  • gen-package-artifacts-with-config.sh / gen-package-offline-package-with-config.sh render, and shellcheck -S error is 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

`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/`.

@ti-chi-bot ti-chi-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 both build-package-artifacts.sh.tmpl and compose-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:
      function validate_title() {
          local title="$1"
          if ! [[ "$title" =~ ^[A-Za-z0-9][A-Za-z0-9._+-]*$ ]]; then
              return 1
          fi
      }
      Replace repeated checks with:
      if ! validate_title "$title"; then
          echo "Invalid layer title '$title'" >&2
          return 1
      fi
  • 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.sh are 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
  • 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.

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.

@ti-chi-bot ti-chi-bot Bot added the size/L label Nov 20, 2026
@wuhuizuo

Copy link
Copy Markdown
Contributor Author

/approve

@ti-chi-bot

ti-chi-bot Bot commented Nov 20, 2026

Copy link
Copy Markdown

[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

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ti-chi-bot ti-chi-bot Bot added the approved label Nov 20, 2026
@ti-chi-bot
ti-chi-bot Bot merged commit 5e70c9c into main Nov 20, 2026
4 checks passed
@ti-chi-bot
ti-chi-bot Bot deleted the fix/packages-oci-layer-title-untrusted branch November 20, 2026 03:29
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.

1 participant