Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@williammartin
Copy link
Member

Description

Fixes #10007

Implementation

As described in #10007 (comment), the issue at hand is that when a specific artifact is targeted, gh will unpack it in the directory specified by --dir. If unspecified, it defaults to the current dir .. Therefore, we were checking whether . descended from . and determined that it did not, incorrectly reporting a file traversal.

In my opinion, it's extremely challenging to reason about all the combinations of relative paths that could be provided (just look at these tests. I think it's better for us to work with absolute paths where possible.

I've previously proposed canonicalization of paths (also resolving symlinks) but for the moment enforcing absolute paths seems sufficient. We rely on a new type safepaths.Absolute that can be joined with elements to create new absolute paths, or will error if the new path would result in a traversal.

Testing

I added a new acceptance test that asserts correct traversal failure.

I update the previous acceptance test to cover a few more cases, one of which is the same as #10007. If this test is run against trunk, it will fail.

@williammartin williammartin requested a review from a team as a code owner December 4, 2024 20:25
@williammartin williammartin requested review from Copilot and removed request for andyfeller December 4, 2024 20:26
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 5 out of 10 changed files in this pull request and generated no suggestions.

Files not reviewed (5)
  • acceptance/testdata/workflow/run-download-traversal.txtar: Language not supported
  • acceptance/testdata/workflow/run-download.txtar: Language not supported
  • pkg/cmd/run/download/http_test.go: Evaluated as low risk
  • pkg/cmd/run/download/zip.go: Evaluated as low risk
  • pkg/cmd/run/download/http.go: Evaluated as low risk
Comments skipped due to low confidence (1)

internal/safepaths/absolute.go:67

  • [nitpick] The Absolute type does not have a method to convert it to a string representation that includes its fields, which could make debugging more difficult.
type PathTraversalError struct {

Tip: Copilot only keeps its highest confidence comments to reduce noise and keep you focused. Learn more

Copy link
Member

@andyfeller andyfeller left a comment

Choose a reason for hiding this comment

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

Looks good with only 1 significant concern

Comment on lines 52 to 54
if dir := filepath.Dir(dest.String()); dir != "." {
if extractErr = os.MkdirAll(dir, dirMode); extractErr != nil {
return
}
}
Copy link
Member

Choose a reason for hiding this comment

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

With dest becoming an absolute path, is there any possibility it would be .?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good call out. The only place this makes sense is when dest is filepath that has no dir e.g. file.txt, in which case we would get back . from filepath.Dir. Since we're working in absolute paths now, this doesn't matter.

Copy link
Member

@andyfeller andyfeller left a comment

Choose a reason for hiding this comment

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

Fantastic work as always! ✨

@andyfeller andyfeller merged commit acfdae4 into trunk Dec 5, 2024
@andyfeller andyfeller deleted the wm/safepaths branch December 5, 2024 16:43
tmeijn pushed a commit to tmeijn/dotfiles that referenced this pull request Dec 6, 2024
This MR contains the following updates:

| Package | Update | Change |
|---|---|---|
| [cli/cli](https://github.com/cli/cli) | patch | `v2.63.1` -> `v2.63.2` |

MR created with the help of [el-capitano/tools/renovate-bot](https://gitlab.com/el-capitano/tools/renovate-bot).

**Proposed changes to behavior should be submitted there as MRs.**

---

### Release Notes

<details>
<summary>cli/cli (cli/cli)</summary>

### [`v2.63.2`](https://github.com/cli/cli/releases/tag/v2.63.2): GitHub CLI 2.63.2

[Compare Source](cli/cli@v2.63.1...v2.63.2)

#### What's Changed

-   Use consistent slice ordering in run download tests by [@&#8203;williammartin](https://github.com/williammartin) in cli/cli#10006
-   Fix bug when fetching bundles from OCI registry by [@&#8203;malancas](https://github.com/malancas) in cli/cli#10019
-   Use safepaths for run download by [@&#8203;williammartin](https://github.com/williammartin) in cli/cli#10009
-   Error for mutually exclusive json and watch flags by [@&#8203;andyfeller](https://github.com/andyfeller) in cli/cli#10016

**Full Changelog**: cli/cli@v2.63.1...v2.63.2

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever MR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this MR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box

---

This MR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy40NDAuNyIsInVwZGF0ZWRJblZlciI6IjM3LjQ0MC43IiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6WyJSZW5vdmF0ZSBCb3QiXX0=-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Artifact download path traversal check fails on valid path

3 participants