Repository navigation
fix(endpoints): validate the workspace scope before it reaches a path - #617
Merged
Merged
Conversation
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.
Contributor
Bowire PR report
API schema: schema identical. Tests: 0/1 passed, 1 failed. SecuritySecurity scan reportFindings: 1 high, 1 low By OWASP API Top 10
Findings[high] BWR-BUILTIN-TLS-001Rule: [low] BWR-BUILTIN-BANNER-SERVERRule: PerfPerf: no test moved more than 20% across 1 compared test(s). Generated by |
8 tasks done
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 Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes the four
cs/path-injectionalerts 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 inPath.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
CollectionStorebecause #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
WorkspaceScopeQuerythat all of them call:workspaceIdmust be a single segment from an allow-list. It becomes a path segment underworkspaces/, 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.storageRootmust 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.
The Git watcher gets the same guard — its
storageRootbecomes the directory aFileSystemWatcheris aimed at — which needed anInternalsVisibleTofor that package.Why a PR rather than a push to main
Two reasons, both deliberate:
pull_requesttrigger andpull-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
WorkspaceScopeQueryTestscovers 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...