-
Notifications
You must be signed in to change notification settings - Fork 30
fix: path traversal guards, report-server robustness, Windows-portable clean scripts #129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
droid-ash
wants to merge
2
commits into
main
Choose a base branch
from
fix/main-coderabbit-batch-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import * as fs from 'node:fs'; | ||
| import * as fsp from 'node:fs/promises'; | ||
| import * as os from 'node:os'; | ||
| import * as path from 'node:path'; | ||
| import test from 'node:test'; | ||
| import { rebuildRunIndex } from './runIndex.js'; | ||
| import { serveReportWorkspace } from './reportServer.js'; | ||
|
|
||
| async function withWorkspace<T>( | ||
| body: (workspace: { workspaceRoot: string; artifactsDir: string }) => Promise<T>, | ||
| ): Promise<T> { | ||
| const rootDir = fs.mkdtempSync(path.join(os.tmpdir(), 'finalrun-report-server-')); | ||
| const artifactsDir = path.join(rootDir, 'artifacts'); | ||
| await fsp.mkdir(artifactsDir, { recursive: true }); | ||
| try { | ||
| return await body({ workspaceRoot: rootDir, artifactsDir }); | ||
| } finally { | ||
| fs.rmSync(rootDir, { recursive: true, force: true }); | ||
| } | ||
| } | ||
|
|
||
| async function withServer<T>( | ||
| workspace: { workspaceRoot: string; artifactsDir: string }, | ||
| body: (baseUrl: string) => Promise<T>, | ||
| ): Promise<T> { | ||
| await rebuildRunIndex(workspace.artifactsDir); | ||
| const server = await serveReportWorkspace({ | ||
| workspaceRoot: workspace.workspaceRoot, | ||
| artifactsDir: workspace.artifactsDir, | ||
| port: 0, | ||
| }); | ||
| try { | ||
| return await body(server.url); | ||
| } finally { | ||
| await server.close(); | ||
| } | ||
| } | ||
|
|
||
| test('GET /api/report/runs/:runId returns 404 when the run is missing', async () => { | ||
| await withWorkspace(async (workspace) => { | ||
| await withServer(workspace, async (baseUrl) => { | ||
| const response = await fetch(`${baseUrl}/api/report/runs/missing-run`); | ||
| assert.equal(response.status, 404); | ||
| const body = (await response.json()) as { status: string }; | ||
| assert.equal(body.status, 'error'); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| test('GET /api/report/runs/:runId returns 404 for path-traversal runIds', async () => { | ||
| await withWorkspace(async (workspace) => { | ||
| await withServer(workspace, async (baseUrl) => { | ||
| const encoded = encodeURIComponent('../../../etc/passwd'); | ||
| const response = await fetch(`${baseUrl}/api/report/runs/${encoded}`); | ||
| assert.equal(response.status, 404); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| test('GET /api/report/runs/:runId returns 500 for corrupt run.json', async () => { | ||
| await withWorkspace(async (workspace) => { | ||
| const runDir = path.join(workspace.artifactsDir, 'corrupt-run'); | ||
| await fsp.mkdir(runDir, { recursive: true }); | ||
| await fsp.writeFile(path.join(runDir, 'run.json'), 'this is not json', 'utf-8'); | ||
| await withServer(workspace, async (baseUrl) => { | ||
| const response = await fetch(`${baseUrl}/api/report/runs/corrupt-run`); | ||
| assert.equal(response.status, 500); | ||
| }); | ||
| }); | ||
| }); |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import * as fs from 'node:fs'; | ||
| import * as fsp from 'node:fs/promises'; | ||
| import * as os from 'node:os'; | ||
| import * as path from 'node:path'; | ||
| import test from 'node:test'; | ||
| import { | ||
| RunManifestNotFoundError, | ||
| loadRunManifestRecord, | ||
| safeResolveWithin, | ||
| type ReportWorkspaceContext, | ||
| } from './reportViewModel.js'; | ||
|
|
||
| function mkArtifactsDir(): { artifactsDir: string; cleanup: () => void } { | ||
| const artifactsDir = fs.mkdtempSync(path.join(os.tmpdir(), 'finalrun-report-vm-')); | ||
| return { | ||
| artifactsDir, | ||
| cleanup: () => { | ||
| fs.rmSync(artifactsDir, { recursive: true, force: true }); | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| test('safeResolveWithin returns the resolved path for an in-bounds segment', () => { | ||
| const { artifactsDir, cleanup } = mkArtifactsDir(); | ||
| try { | ||
| const resolved = safeResolveWithin(artifactsDir, 'run-1', 'run.json'); | ||
| assert.equal(resolved, path.join(artifactsDir, 'run-1', 'run.json')); | ||
| } finally { | ||
| cleanup(); | ||
| } | ||
| }); | ||
|
|
||
| test('safeResolveWithin returns the base when no extra segments are passed', () => { | ||
| const { artifactsDir, cleanup } = mkArtifactsDir(); | ||
| try { | ||
| const resolved = safeResolveWithin(artifactsDir); | ||
| assert.equal(resolved, path.resolve(artifactsDir)); | ||
| } finally { | ||
| cleanup(); | ||
| } | ||
| }); | ||
|
|
||
| test('safeResolveWithin rejects parent-traversal segments', () => { | ||
| const { artifactsDir, cleanup } = mkArtifactsDir(); | ||
| try { | ||
| assert.equal(safeResolveWithin(artifactsDir, '..', 'etc', 'passwd'), undefined); | ||
| assert.equal(safeResolveWithin(artifactsDir, '../../../etc/passwd'), undefined); | ||
| } finally { | ||
| cleanup(); | ||
| } | ||
| }); | ||
|
|
||
| test('safeResolveWithin rejects absolute segments that escape the base', () => { | ||
| const { artifactsDir, cleanup } = mkArtifactsDir(); | ||
| try { | ||
| assert.equal(safeResolveWithin(artifactsDir, '/etc/passwd'), undefined); | ||
| } finally { | ||
| cleanup(); | ||
| } | ||
| }); | ||
|
|
||
| test('loadRunManifestRecord throws RunManifestNotFoundError for traversal runIds', async () => { | ||
| const { artifactsDir, cleanup } = mkArtifactsDir(); | ||
| const context: ReportWorkspaceContext = { workspaceRoot: artifactsDir, artifactsDir }; | ||
| try { | ||
| await assert.rejects( | ||
| () => loadRunManifestRecord('../../../etc/passwd', context), | ||
| (error: Error) => error instanceof RunManifestNotFoundError, | ||
| ); | ||
| } finally { | ||
| cleanup(); | ||
| } | ||
| }); | ||
|
|
||
| test('loadRunManifestRecord throws RunManifestNotFoundError for missing runs', async () => { | ||
| const { artifactsDir, cleanup } = mkArtifactsDir(); | ||
| const context: ReportWorkspaceContext = { workspaceRoot: artifactsDir, artifactsDir }; | ||
| try { | ||
| await assert.rejects( | ||
| () => loadRunManifestRecord('does-not-exist', context), | ||
| (error: Error) => error instanceof RunManifestNotFoundError, | ||
| ); | ||
| } finally { | ||
| cleanup(); | ||
| } | ||
| }); | ||
|
|
||
| test('loadRunManifestRecord surfaces non-ENOENT errors as generic errors', async () => { | ||
| const { artifactsDir, cleanup } = mkArtifactsDir(); | ||
| try { | ||
| const context: ReportWorkspaceContext = { workspaceRoot: artifactsDir, artifactsDir }; | ||
| const runDir = path.join(artifactsDir, 'corrupt-run'); | ||
| await fsp.mkdir(runDir, { recursive: true }); | ||
| await fsp.writeFile(path.join(runDir, 'run.json'), 'this is not json', 'utf-8'); | ||
| await assert.rejects( | ||
| () => loadRunManifestRecord('corrupt-run', context), | ||
| (error: Error) => !(error instanceof RunManifestNotFoundError), | ||
| ); | ||
| } finally { | ||
| cleanup(); | ||
| } | ||
| }); | ||
|
|
||
| test('loadRunManifestRecord rejects unsupported schema versions with a generic error', async () => { | ||
| const { artifactsDir, cleanup } = mkArtifactsDir(); | ||
| try { | ||
| const context: ReportWorkspaceContext = { workspaceRoot: artifactsDir, artifactsDir }; | ||
| const runDir = path.join(artifactsDir, 'old-schema'); | ||
| await fsp.mkdir(runDir, { recursive: true }); | ||
| await fsp.writeFile( | ||
| path.join(runDir, 'run.json'), | ||
| JSON.stringify({ schemaVersion: 1 }), | ||
| 'utf-8', | ||
| ); | ||
| await assert.rejects( | ||
| () => loadRunManifestRecord('old-schema', context), | ||
| (error: Error) => | ||
| !(error instanceof RunManifestNotFoundError) && | ||
| /Unsupported schema version/.test(error.message), | ||
| ); | ||
| } finally { | ||
| cleanup(); | ||
| } | ||
| }); |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import test from 'node:test'; | ||
| import { isSafeEnvName, isSafeRelativeSegment } from './submit.js'; | ||
|
|
||
| test('isSafeRelativeSegment accepts simple relative paths', () => { | ||
| assert.equal(isSafeRelativeSegment('foo.yml'), true); | ||
| assert.equal(isSafeRelativeSegment('subdir/foo.yml'), true); | ||
| assert.equal(isSafeRelativeSegment('a/b/c/d.yaml'), true); | ||
| }); | ||
|
|
||
| test('isSafeRelativeSegment rejects parent traversal', () => { | ||
| assert.equal(isSafeRelativeSegment('../foo.yml'), false); | ||
| assert.equal(isSafeRelativeSegment('../../etc/passwd'), false); | ||
| assert.equal(isSafeRelativeSegment('..'), false); | ||
| assert.equal(isSafeRelativeSegment('subdir/../../escape'), false); | ||
| }); | ||
|
|
||
| test('isSafeRelativeSegment rejects absolute paths across platforms', () => { | ||
| assert.equal(isSafeRelativeSegment('/etc/passwd'), false); | ||
| assert.equal(isSafeRelativeSegment('C:\\Windows\\System32\\drivers\\etc\\hosts'), false); | ||
| assert.equal(isSafeRelativeSegment('c:\\tmp\\a'), false); | ||
| assert.equal(isSafeRelativeSegment('\\\\server\\share\\file.yml'), false); | ||
| }); | ||
|
|
||
| test('isSafeRelativeSegment normalises Windows-style separators before checking', () => { | ||
| assert.equal(isSafeRelativeSegment('..\\foo.yml'), false); | ||
| assert.equal(isSafeRelativeSegment('subdir\\foo.yml'), true); | ||
| }); | ||
|
|
||
| test('isSafeRelativeSegment rejects empty values', () => { | ||
| assert.equal(isSafeRelativeSegment(''), false); | ||
| }); | ||
|
|
||
| test('isSafeEnvName accepts conservative names', () => { | ||
| assert.equal(isSafeEnvName('staging'), true); | ||
| assert.equal(isSafeEnvName('dev_1'), true); | ||
| assert.equal(isSafeEnvName('feature-x.2'), true); | ||
| }); | ||
|
|
||
| test('isSafeEnvName rejects path-like and exotic values', () => { | ||
| assert.equal(isSafeEnvName('../etc'), false); | ||
| assert.equal(isSafeEnvName('/etc'), false); | ||
| assert.equal(isSafeEnvName('a/b'), false); | ||
| assert.equal(isSafeEnvName('with space'), false); | ||
| assert.equal(isSafeEnvName(''), false); | ||
| }); |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.