# GHSA-hw6r-g8gj-2987: Actions expression injection in `filter-test-configs` (`GHSL-2023-181`)

> Attackers can inject code via malicious branch names in PyTorch's filter-test-configs action, potentially leaking secrets and executing commands.

- **Severity:** Not scored
- **Exploited in the wild:** Not known
- **Login needed:** Any signed-in user
- **Attackable:** Over the internet
- **Victim action needed:** Not needed
- **Fix available:** No
- **Published:** 30 Aug 2023

## What is GHSA-hw6r-g8gj-2987?

GHSA-hw6r-g8gj-2987 is a code injection vulnerability in https://github.com/pytorch/pytorch/.github/actions/filter-test-configs, published on 30 Aug 2023. Any signed-in user can exploit it over the network, with no action from the victim.

### Official description

The `pytorch/pytorch` `filter-test-configs` workflow is vulnerable to an expression injection in Actions, allowing an attacker to potentially leak secrets and alter the repository using the workflow.

### Details

The [`filter-test-configs`](https://github.com/pytorch/pytorch/blob/ec26947c586dd323d741da80008403664c533f65/.github/actions/filter-test-configs/action.yml) workflow is using the raw `github.event.workflow_run.head_branch` value inside the `filter` step:

```yaml
- name: Select all requested test configurations
  shell: bash
  env:
    GITHUB_TOKEN: ${{ inputs.github-token }}
    JOB_NAME: ${{ steps.get-job-name.outputs.job-name }}
  id: filter
  run: |
    ...
    python3 "${GITHUB_ACTION_PATH}/../../scripts/filter_test_configs.py" \
      ...
      --branch "${{ github.event.workflow_run.head_branch }}"
```

In the event of a repository using `filter-test-configs` in a `pull_request_target`-triggered workflow, an attacker could use a malicious branch name to gain command execution in the step and potentially leak secrets.

```yml
name: Example

on: pull_request_target

jobs:
  example:
    runs-on: ubuntu-latest
    steps:
      - name: Filter
        uses: pytorch/pytorch/.github/actions/filter-test-configs@v2
```

#### Impact

This issue may lead to stealing workflow secrets.

#### Remediation

1. Use an intermediate environment variable for potentially attacker-controlled values such as `github.event.workflow_run.head_branch`:
```yaml
- name: Select all requested test configurations
  shell: bash
  env:
    GITHUB_TOKEN: ${{ inputs.github-token }}
    JOB_NAME: ${{ steps.get-job-name.outputs.job-name }}
    HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }}
  id: filter
  run: |
    ...
    python3 "${GITHUB_ACTION_PATH}/../../scripts/filter_test_configs.py" \
      ...
      --branch "$HEAD_BRANCH"
```

#### Resources

* [CodeQL for JavaScript - Expression injection in Actions](https://codeql.github.com/codeql-query-help/javascript/js-actions-command-injection/)
* [Keeping your GitHub Actions and workflows secure Part 2: Untrusted input](https://securitylab.github.com/research/github-actions-untrusted-input/)
* [Keeping your GitHub Actions and workflows secure Part 1: Preventing pwn requests](https://securitylab.github.com/research/github-actions-preventing-pwn-requests/)

## Which versions are affected by GHSA-hw6r-g8gj-2987?

1 open source package ship an affected version, including https://github.com/pytorch/pytorch/.github/actions/filter-test-configs.


| Package | Ecosystem | Affected | Fixed in |
| --- | --- | --- | --- |
| `https://github.com/pytorch/pytorch/.github/actions/filter-test-configs` | GitHub Actions | All versions | None |

## How do I fix GHSA-hw6r-g8gj-2987?

No fix has been published for GHSA-hw6r-g8gj-2987 yet. Limit exposure of the affected software until one is available.

## Is GHSA-hw6r-g8gj-2987 being exploited?

There is no known exploitation of GHSA-hw6r-g8gj-2987 in the wild and no public exploit code.

## Fix prompt for AI coding agents

```text
Check this codebase for GHSA-hw6r-g8gj-2987 and fix it if it is affected.

What it is: GHSA-hw6r-g8gj-2987 is a code injection vulnerability in https://github.com/pytorch/pytorch/.github/actions/filter-test-configs, published on 30 Aug 2023. Any signed-in user can exploit it over the network, with no action from the victim.
Exploitation: There is no known exploitation of GHSA-hw6r-g8gj-2987 in the wild and no public exploit code.

Affected packages and fixed versions:
- https://github.com/pytorch/pytorch/.github/actions/filter-test-configs (GitHub Actions): affected all versions; no fix listed

Task:
1. Search the dependency manifests and lockfiles (for example package.json, package-lock.json, requirements.txt, poetry.lock, uv.lock, pom.xml, build.gradle, go.mod, Cargo.lock, Gemfile.lock) and any Dockerfiles for the affected packages, including transitive dependencies.
2. If an affected version is in use, upgrade it to the fixed version for its release line with the project's package manager, so the lockfile is regenerated. Prefer the smallest upgrade that fixes it.
3. If there is no fix, or the upgrade would break the build, do not guess: explain the risk and the safest workaround instead.
4. Run the tests and summarise what changed and why.
5. If nothing in the project is affected, say so and change nothing.

Reference: https://insights.sbomify.com/GHSA-hw6r-g8gj-2987/ (Markdown: https://insights.sbomify.com/GHSA-hw6r-g8gj-2987/index.md)
```

## Sources

- OSV: https://osv.dev/vulnerability/GHSA-hw6r-g8gj-2987

---
From sbomify Insights: https://insights.sbomify.com/GHSA-hw6r-g8gj-2987/
Summaries are generated and may contain mistakes; the official description above is the record.
