Skip to content

Refuse API paths that aren't in the diff - #121

Merged
david-crespo merged 1 commit into
mainfrom
spr/refuse-api-paths-that-arent-in-the-diff
Nov 19, 2026
Merged

david-crespo merged 1 commit into
mainfrom
spr/refuse-api-paths-that-arent-in-the-diff

Conversation

@david-crespo

@david-crespo david-crespo commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

/api/file-contents and the two comment routes take a file path from the request and read or write that file under the repository directory. They didn't check the path, so one starting with ../ reached files outside the repository. GET /api/file-contents?path=../outside.txt returned outside.txt, and the comment routes could rewrite it. These routes are behind the per-run token, so only the page could do this. It matters more for the createApp export stacked on this PR (#124), where a host application can turn the token off and check requests itself.

The routes now accept only a path in the latest diff, or a rename's old path, and return 400 otherwise. That also blocks files inside the repository that the diff doesn't show, such as an ignored .env. When a path isn't in the last diff, the server runs the diff once more before refusing, so a file that just changed isn't rejected.

The 71 changed lines are 36 for the check and 35 in tests. We checked that /api/file-contents returns 400 for a file outside the repository and for an unchanged file inside it, and that both comment routes return 400 for the outside file and leave it unchanged. Both content requests returned 200 before the fix. The git test fixture now runs git add --intent-to-add, because two existing tests request untracked files, which git diff leaves out.

@david-crespo
david-crespo added this pull request to stack #125 November 19, 2026 17:58
@david-crespo
david-crespo merged commit 955e3b3 into main Nov 19, 2026
2 checks passed
@david-crespo
david-crespo deleted the spr/refuse-api-paths-that-arent-in-the-diff branch November 19, 2026 18:19
david-crespo added a commit that referenced this pull request Nov 19, 2026
Built on #121, this is the first of three PRs that let another Node
server mount `@oxide/skepsis` review pages. `buildDiffSource` turns the
range flags (`-r`, `-f`, `-t`, `--interdiff`) into the diff command to
run. It used to read the parsed flags from a top-level variable in
`cli.ts` and call `process.exit` on a bad combination, such as
`--interdiff` with `-r`. It now takes the flags as an argument and
throws on bad ones, and the CLI catches the error and prints it and
exits 1 as before. `buildApp` builds the routes and returns the `fetch`
handler and the per-run token without binding a port, so the calling app
can do what it wants with them. CLI behavior is unchanged.

Most of the diff is code moving without modification. The first commit
has the changes to `buildDiffSource`. The second moves it from `cli.ts`
to `server/diffSource.ts`, and the third moves the routes from
`server/main.ts` to `server/app.ts`. Of the roughly 390 non-blank lines
those two commits remove, about 375 reappear unchanged. Apart from
imports, the license headers of the two new files, and the tests
described below, these are the lines in those two commits that are not
straight moves:

```diff
 // commit 2: cli.ts → server/diffSource.ts
-function detectVcs(): 'jj' | 'git' {
+export function detectVcs(): 'jj' | 'git' {
-function buildDiffSource(flags: {
+export function buildDiffSource(flags: {

 // commit 3: server/main.ts → server/app.ts
-export async function startServer(opts: {
-  port?: number
-  hostname?: string
-}): Promise<{ port: number; token: string; close: () => void }> {
-  const { diffSource, port = 0, hostname = 'localhost', cwd, poll = true, send } = opts
+export interface AppOptions {
+}
+export async function buildApp(opts: AppOptions, url: () => string) {
+  const { diffSource, cwd, poll = true, send } = opts
-        const env = { SKEPSIS_URL: serverUrl, SKEPSIS_ROOT: cwd }
+        const env = { SKEPSIS_URL: url(), SKEPSIS_ROOT: cwd }
+  return { fetch: app.fetch, token }

 // commit 3: what is left in server/main.ts
+export async function startServer(
+  opts: AppOptions & { port?: number; hostname?: string },
+): Promise<{ port: number; token: string; close: () => void }> {
+  const { port = 0, hostname = 'localhost' } = opts
+  const { fetch, token } = await buildApp(opts, () => serverUrl)
-    const server = serve({ fetch: app.fetch, port, hostname }, (info) => {
+    const server = serve({ fetch, port, hostname }, (info) => {
-        `skepsis on ${serverUrl}/#token=${token} (${displayCommand(diffSource)})`,
+        `skepsis on ${serverUrl}/#token=${token} (${displayCommand(opts.diffSource)})`,
```

The second commit also changes tests. Three tests in `cli.test.ts` ran
the CLI with a bad `--interdiff` combination and checked for exit code 1
and the message on stderr. They are now unit tests in
`server/diffSource.test.ts` that check `buildDiffSource` throws the same
messages, plus a fourth test checking that an interdiff's diff source
has no endpoints. No test checks the CLI's exit code and stderr for
these combinations anymore.
david-crespo added a commit that referenced this pull request Nov 19, 2026
Built on #122, this PR lets one process serve diffs from more than one
repository. `jj` and `git` invocations, working-copy reads, and comment
writes now take the repository directory as an argument. The static file
middleware also receives an absolute path to the web files' directory,
so nothing depends on the process's working directory. The CLI passes
`process.cwd()`, so its behavior is the same. The directory is fixed
when an app is built and each app has its own token, so a request can't
choose a directory or reach another app's repository, and #121 keeps its
file paths to the files in the diff. A new test builds two apps for
separate repositories in one process and checks that each returns its
own diff and file contents. About 160 of the 220 changed lines are
tests: that test, and new unit tests for `buildDiffSource` and
`detectVcs` in a directory other than the process's working directory.
The source change is about 55 lines that add the directory argument.
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