Skip to content

fix(endpoints): validate the workspace scope before it reaches a path - #617

Merged
thomas-stegemann merged 4 commits into
mainfrom
fix/path-injection-guard
Aug 26, 2026
Merged

thomas-stegemann merged 4 commits into
mainfrom
fix/path-injection-guard

Conversation

@thomas-stegemann

Copy link
Copy Markdown
Member

Closes the four cs/path-injection alerts on the code-scanning board, and the five files they did not point at.

What CodeQL found

?workspaceId= and ?storageRoot= arrive on the query string and end up in Path.Combine. Nothing between the browser and the filesystem checked either one — a request could name any directory on the machine and have Bowire read or write there, with the response reporting success.

The alerts landed on CollectionStore because #612 gave it a new path to reach. But the same read appears in six endpoint files: recordings, presets, mock configs, schema changes, collections, and the Git workspace watcher. Patching the four flagged lines would have left the rest doing it.

What this does

One WorkspaceScopeQuery that all of them call:

  • workspaceId must be a single segment from an allow-list. It becomes a path segment under workspaces/, so anything that can traverse or re-root is refused. Allow-list rather than deny-list — enumerating what to forbid means keeping up with separators, alternate encodings and reserved device names across three platforms.
  • storageRoot must be absolute, contain no .., and name a directory that already exists. That last one is the difference between writing into a folder the operator chose and one the request invented.

Invalid input gets a 400 naming which of the two was wrong.

"It only listens on loopback" bounds who can reach this, not what a request may ask for once it does: any page in any tab can POST to localhost, and the workbench renders content from the APIs it is pointed at.

The Git watcher gets the same guard — its storageRoot becomes the directory a FileSystemWatcher is aimed at — which needed an InternalsVisibleTo for that package.

Why a PR rather than a push to main

Two reasons, both deliberate:

  1. This machine has been refusing builds for hours — three consecutive timeouts at ~2.5 GB free RAM with no dotnet processes to blame. The code is unverified locally, so CI is the check.
  2. Split bowire-pr into its own public kuestenlogik/bowire-action repo #582 has one task left open: "Validate the comment upsert on a live PR". The self-test workflow has a pull_request trigger and pull-requests: write, but has only ever run on pushes to main — so that path has never actually executed. This PR exercises it on real work rather than a throwaway branch.

Tests

WorkspaceScopeQueryTests covers both halves: ordinary ids accepted, traversal / separators / absolute paths / NUL / percent-encoding refused, and the storage-root rules including the case this exists for — pointing a workspace at a system directory via ...

CodeQL flagged four cs/path-injection alerts in CollectionStore, and it
was right: `?workspaceId=` and `?storageRoot=` come off the query string
and end up in Path.Combine with nothing in between checking them. A
request could name any directory on the machine and have Bowire read or
write there, reporting success.

The alerts pointed at CollectionStore because #612 gave it a new path to
reach, but the pattern was already in six endpoint files — recordings,
presets, mock configs, schema changes and the Git workspace watcher all
read the same pair the same way. Fixing the four flagged lines would
have left the other five files doing it, so this is one guard they all
call instead.

WorkspaceScopeQuery validates both: a workspace id must be a single
segment from an allow-list (it becomes a path segment under
"workspaces/", so anything that can traverse or re-root is refused), and
a storage root must be absolute, free of "..", and name a directory that
already exists — the difference between writing into a folder the
operator chose and one the request invented. An allow-list rather than a
deny-list, because enumerating what to forbid means keeping up with
separators, encodings and reserved device names on three platforms.

"It only listens on loopback" bounds who can reach this, not what a
request may ask for once it does: any page in any tab can POST to
localhost, and the workbench renders content from the APIs it is
pointed at.

The Git watcher gets the same treatment — its storageRoot becomes the
directory a FileSystemWatcher is aimed at — which needed an
InternalsVisibleTo for that package.

Not built locally: this machine has been refusing builds for hours
(three consecutive timeouts at ~2.5 GB free RAM, no dotnet processes to
blame). Opened as a PR so CI is the check — which also exercises the
comment-upsert path that #582 still has open.
[Fact]
public void A_Storage_Root_Containing_DotDot_Is_Refused()
{
var root = Path.Combine(Path.GetTempPath(), "..", "elsewhere");
{
// The difference between writing into a directory the operator chose
// and writing into one the request invented.
var missing = Path.Combine(Path.GetTempPath(), "bowire-not-here-" + Guid.NewGuid().ToString("N"));
// The literal request this guard exists for: point the workspace at
// somewhere outside anything Bowire owns and have the server write
// collections.json there.
var traversal = Path.Combine(Path.GetTempPath(), "..", "..", "Windows", "System32");
CA3003 flagged WorkspaceScopeQuery's own Directory.Exists — taint analysis
cannot distinguish a validator from a sink, so the one type whose job is to
check these values gets flagged for touching them.

Excluded by symbol rather than turned off by severity: the rule found the
actual hole this PR fixes, and it should keep watching every other place a
query value could reach a path. A blanket 'severity = none' would trade a
real check for a green build.
@github-actions

Copy link
Copy Markdown
Contributor

Bowire PR report

Section Status
API schema 🟢
Tests 🟢
Security 🟢
Perf 🟢

API schema: schema identical.

Tests: 0/1 passed, 1 failed.

Security

Security scan report

Findings: 1 high, 1 low

By OWASP API Top 10

Entry Findings
API8-2023-SECMISCONF 2

Findings

[high] BWR-BUILTIN-TLS-001

Rule: BWR-BUILTIN-TLS-001 · OWASP: API8-2023-SECMISCONF · CVSS: 7.4 · Location: .github/workflows/bowire-pr-selftest.yml

[low] BWR-BUILTIN-BANNER-SERVER

Rule: BWR-BUILTIN-BANNER-SERVER · OWASP: API8-2023-SECMISCONF · CVSS: 3.7 · Location: .github/workflows/bowire-pr-selftest.yml

Perf

Perf: no test moved more than 20% across 1 compared test(s).


Generated by bowire — API / test / security / perf impact of this PR.

WorkspaceGitEventsEndpointTests has asserted "does not exist" for a
bogus storageRoot since before this guard existed. The new validator
said the same thing in different words ("does not name an existing
directory"), which broke a test that was still describing correct
behaviour.

Adjusting the message rather than the test: the assertion was there
first, it reads naturally, and rewriting an existing expectation to
match new prose is the wrong way round.
GET /api/mocks answers { "mocks": [...] }. The test asserted a bare
array and CI caught it.

Pinned deliberately rather than loosened: a rail reading body[0] would
work against a bare array and silently read nothing from an envelope, so
which of the two this is belongs in a test.
@codecov

codecov Bot commented Aug 26, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.12346% with 8 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...logik.Bowire/Endpoints/BowireRecordingEndpoints.cs 80.00% 6 Missing ⚠️
...ogik.Bowire/Endpoints/BowireCollectionEndpoints.cs 80.00% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

@thomas-stegemann
thomas-stegemann merged commit fe00d19 into main Aug 26, 2026
16 checks passed
@thomas-stegemann
thomas-stegemann deleted the fix/path-injection-guard branch August 26, 2026 07:49
thomas-stegemann added a commit that referenced this pull request Aug 26, 2026
…ites

bowire contract sat at 56% with its two most consequential outputs
untested.

The matrix is what someone scans in a CI log to decide whether a
contract broke, so the tests pin what the cells mean: a verdict carries
how many interactions backed it (11/12 and 0/12 are very different
mornings), and a pair that was never run reads as a dash — not a blank
and not a zero, both of which look like 'checked, nothing wrong' when
nothing was checked. Column width follows the widest name, so one long
service name cannot wrap the grid into unreadability.

Sanitize decides what a report file may be called, from consumer and
provider names that come out of contract files the person running the
command did not necessarily write. Traversal, separators and drive
letters all reduce to dashes — the same class of input that #617 just
dealt with on the endpoint side, which is why it is worth a test rather
than a reading.

Both become internal for the same reason LintCommand's renderers did.
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.

1 participant