Skip to content

Commit 99b6475

Browse files
committed
Fix hyperparam source on dot files
1 parent 9b62645 commit 99b6475

File tree

3 files changed

+31
-29
lines changed

3 files changed

+31
-29
lines changed

bin/serve.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ const mimeTypes = {
3232
'.woff2': 'font/woff2',
3333
}
3434

35+
// Paths to ignore for logging
36+
const ignorePrefix = [
37+
'/favicon.svg',
38+
'/assets/',
39+
]
40+
3541
/**
3642
* @template T
3743
* @typedef {T | Promise<T>} Awaitable<T>
@@ -286,6 +292,11 @@ function startServer(port, path) {
286292
pipe(content, res)
287293
}
288294

295+
// Ignored logging paths
296+
if (ignorePrefix.some(p => req.url?.startsWith(p))) {
297+
return
298+
}
299+
289300
// log request
290301
const endTime = new Date()
291302
const ms = endTime.getTime() - startTime.getTime()

src/lib/sources/hyperparamSource.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@ export interface HyperparamFileMetadata {
88
lastModified: string
99
}
1010

11-
function canParse(sourceId: string): boolean {
12-
/// we expect relative paths, such as path/to/file or path/to/dir/
13-
/// let's just check that it is empty or starts with a "word" character
14-
return sourceId === '' || /^[\w]/.test(sourceId)
15-
}
16-
1711
function getSourceParts(sourceId: string): SourcePart[] {
1812
const parts = sourceId.split('/')
1913
const sourceParts = [
@@ -66,12 +60,10 @@ async function listFiles(prefix: string, { endpoint, requestInit }: {endpoint: s
6660
}
6761

6862
export function getHyperparamSource(sourceId: string, { endpoint, requestInit }: {endpoint: string, requestInit?: RequestInit}): FileSource | DirSource | undefined {
69-
if (!URL.canParse(endpoint)) {
70-
throw new Error('Invalid endpoint')
71-
}
72-
if (!canParse(sourceId)) {
73-
return undefined
74-
}
63+
if (!URL.canParse(endpoint)) throw new Error('Invalid endpoint')
64+
if (sourceId.startsWith('/')) throw new Error('Source cannot start with a /')
65+
if (sourceId.includes('..')) throw new Error('Source cannot include ..')
66+
7567
const sourceParts = getSourceParts(sourceId)
7668
if (getKind(sourceId) === 'file') {
7769
return {

test/lib/sources/hyperparamSource.test.ts

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,26 @@ globalThis.fetch = vi.fn()
66
describe('getHyperparamSource', () => {
77
const endpoint = 'http://localhost:3000'
88

9-
test.for([
10-
'test.txt',
11-
'no-extension',
12-
'folder/subfolder/test.txt',
13-
])('recognizes a local file path', (sourceId: string) => {
14-
expect(getHyperparamSource(sourceId, { endpoint })?.kind).toBe('file')
9+
it('recognizes local files', () => {
10+
expect(getHyperparamSource('test.txt', { endpoint })?.kind).toBe('file')
11+
expect(getHyperparamSource('no-extension', { endpoint })?.kind).toBe('file')
12+
expect(getHyperparamSource('folder/subfolder/test.txt', { endpoint })?.kind).toBe('file')
1513
})
1614

17-
test.for([
18-
'',
19-
'folder1/',
20-
'folder1/folder2/',
21-
])('recognizes a folder', (sourceId: string) => {
22-
expect(getHyperparamSource(sourceId, { endpoint })?.kind).toBe('directory')
15+
it('recognizes folders', () => {
16+
expect(getHyperparamSource('', { endpoint })?.kind).toBe('directory')
17+
expect(getHyperparamSource('folder1/', { endpoint })?.kind).toBe('directory')
18+
expect(getHyperparamSource('folder1/folder2/', { endpoint })?.kind).toBe('directory')
2319
})
2420

25-
test.for([
26-
'/',
27-
'////',
28-
])('does not support a heading slash', (sourceId: string) => {
29-
expect(getHyperparamSource(sourceId, { endpoint })).toBeUndefined()
21+
it('throws on leading slash', () => {
22+
expect(() => getHyperparamSource('/', { endpoint })).toThrow('Source cannot start with a /')
23+
expect(() => getHyperparamSource('/folder/', { endpoint })).toThrow('Source cannot start with a /')
24+
})
25+
26+
it('throws on .. in path', () => {
27+
expect(() => getHyperparamSource('..', { endpoint })).toThrow('Source cannot include ..')
28+
expect(() => getHyperparamSource('folder/../file.txt', { endpoint })).toThrow('Source cannot include ..')
3029
})
3130

3231
test.for([

0 commit comments

Comments
 (0)