Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Disallow artifact path traversal, escape-hatch experiment #2815

Merged
merged 4 commits into from
Jun 11, 2024

Conversation

DrJosh9000
Copy link
Contributor

@DrJosh9000 DrJosh9000 commented Jun 6, 2024

Description

Prevent .. path components in artifact records from causing artifact download to traverse outside the destination path, unless a new experiment is enabled.

A secondary change is the removal of the loop from target path calculation that compared every 100th download path component against the final destination path component; the probability anyone has exercised the code path is effectively zero, and the behaviour would be incredibly surprising if it were hit.

Context

https://linear.app/buildkite/issue/PS-68/fix-surprising-agent-behaviour-in-artifact-download

Changes

  • New code to trim .. path components
  • Add the experiment
  • The removal of the astonishing for ... i += 100 loop
  • A bunch of cleanup

Testing

  • Tests have run locally (with go test ./...). Buildkite employees may check this if the pipeline has run automatically.
  • Code is formatted (with go fmt ./...)

* Remove astonishing `i += 100` loop
* Rename getTargetPath to targetPath
* Rename args (they're both paths, with different purposes)
* Rename internal variables
* Use filepath.Join where appropriate
* Add call to filepath.Clean for dlPath
* Rewrite tests in table-driven form
* Remove ...x/x/x/x/x/x/x... test
* Remove astonishing `i += 100` loop
* Rename getTargetPath to targetPath
* Rename args (they're both paths, with different purposes)
* Rename internal variables
* Use filepath.Join where appropriate
* Add call to filepath.Clean for dlPath
* Rewrite tests in table-driven form
* Remove ...x/x/x/x/x/x/x... test
@DrJosh9000 DrJosh9000 force-pushed the ps-68-disallow-artifact-path-traversal branch from 4a4a257 to 822ac07 Compare June 6, 2024 07:11
@DrJosh9000 DrJosh9000 requested a review from a team June 6, 2024 07:13
// of the download path, then trim the last component of the destination.
lastIndex := len(destPathComponents) - 1
lastDestComponent := destPathComponents[lastIndex]
if lastDestComponent == dlPathComponents[0] {
Copy link
Contributor

Choose a reason for hiding this comment

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

not relevant to this PR, but this isn't mentioned anywhere in the doc: https://buildkite.com/docs/agent/v3/cli-artifact#downloading-artifacts-artifact-download-examples

😮

if c := dlPathComponents[0]; c != "" && c != ".." {
break
}
dlPathComponents = dlPathComponents[1:]
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel mutating users' intention is a bit questionable. It's likely to cause some support tickets. 🤔

And given they can add relative path in both download path and destination path, is this fix sufficient?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I answered this out of band, but basically, it's less surprising if a user has deliberately written the command:

buildkite-agent artifact download "*foo" ../../dest

than if they wrote the following (and "*foo" happened to match an artifact with path "../../dest"):

buildkite-agent artifact download "*foo" .

@DrJosh9000
Copy link
Contributor Author

I'm strongly considering inverting the experiment, as there's little evidence I can find that anyone is relying on the old behaviour.

@DrJosh9000 DrJosh9000 changed the title Experiment: disallow artifact path traversal Disallow artifact path traversal, escape-hatch experiment Jun 11, 2024
@DrJosh9000 DrJosh9000 merged commit c91329d into main Jun 11, 2024
1 check passed
@DrJosh9000 DrJosh9000 deleted the ps-68-disallow-artifact-path-traversal branch June 11, 2024 07:26
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants