Merge commit from fork
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by GitHub · parent 9aff14b · Browse files · 4 files·+90 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+90 added−10 removed
- tests:+60 added−1 removed
- other:+30 added−9 removed
Provenance
- Commit
b470278- Tree
995b546- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
9aff14b
4 files changed+90 added−10 removed
modified:src/helper/ssg/ssg.test.tsx+34 added
561 unmodified lines562 expect(fsMock.writeFile).toHaveBeenCalledWith('static/yaml2.xyml', yamlContent)563 expect(fsMock.writeFile).toHaveBeenCalledWith('static/html.htm', yamlContent) // extensionMap564 expect(fsMock.writeFile).toHaveBeenCalledWith('static/html.html', yamlContent) // default + extensionMap565 })566+567+ it('should reject writing files outside outDir via path traversal', async () => {568+ await expect(569+ saveContentToFile(570+ Promise.resolve({571+ routePath: '/../pwned',572+ content: 'owned',573+ mimeType: 'text/html',574+ }),575+ fsMock,576+ './static'577+ )578+ ).rejects.toThrow('Path traversal detected')579+580+ expect(fsMock.mkdir).not.toHaveBeenCalled()581+ expect(fsMock.writeFile).not.toHaveBeenCalled()582+ })583+584+ it('should reject paths that only partially match outDir name', async () => {585+ await expect(586+ saveContentToFile(587+ Promise.resolve({588+ routePath: '/../static-evil/pwned',589+ content: 'owned',590+ mimeType: 'text/html',591+ }),592+ fsMock,593+ './static'594+ )595+ ).rejects.toThrow('Path traversal detected')596+597+ expect(fsMock.mkdir).not.toHaveBeenCalled()598+ expect(fsMock.writeFile).not.toHaveBeenCalled()599+ })600 })601 602 describe('Dynamic route handling', () => {603 let app: Hono455 unmodified linesmodified:src/helper/ssg/ssg.ts+18 added-9 removed
4 unmodified lines5 import { getExtension } from '../../utils/mime'6 import type { AddedSSGDataRequest, SSGParams } from './middleware'7 import { SSG_CONTEXT, X_HONO_DISABLE_SSG_HEADER_KEY } from './middleware'8 import { defaultPlugin } from './plugins'9-import { dirname, filterStaticGenerateRoutes, isDynamicRoute, joinPaths } from './utils'9+import {10+ dirname,11+ ensureWithinOutDir,12+ filterStaticGenerateRoutes,13+ isDynamicRoute,14+ joinPaths,15+} from './utils'16 17 const DEFAULT_CONCURRENCY = 2 // default concurrency for ssg18 19 // 'default_content_type' is designed according to Bun's performance optimization,34 unmodified lines54 extensionMap?: Record<string, string>55 ): string => {56 const extension = determineExtension(mimeType, extensionMap)57 58+ let filePath: string59 if (routePath.endsWith(`.${extension}`)) {53- return joinPaths(outDir, routePath)60+ filePath = joinPaths(outDir, routePath)61+ } else if (routePath === '/') {62+ filePath = joinPaths(outDir, `index.${extension}`)63+ } else if (routePath.endsWith('/')) {64+ filePath = joinPaths(outDir, routePath, `index.${extension}`)65+ } else {66+ filePath = joinPaths(outDir, `${routePath}.${extension}`)67 }68 56- if (routePath === '/') {57- return joinPaths(outDir, `index.${extension}`)58- }59- if (routePath.endsWith('/')) {60- return joinPaths(outDir, routePath, `index.${extension}`)61- }62- return joinPaths(outDir, `${routePath}.${extension}`)69+ ensureWithinOutDir(outDir, filePath)70+71+ return filePath72 }73 74 const parseResponseContent = async (response: Response): Promise<string | ArrayBuffer> => {75 const contentType = response.headers.get('Content-Type')393 unmodified linesmodified:src/helper/ssg/utils.test.ts+26 added-1 removed
122345619 unmodified lines2627282930313233343536373839404142434445464748495051525354555657585960import { describe, expect, it } from 'vitest'import { dirname, joinPaths } from './utils'import { dirname, ensureWithinOutDir, joinPaths } from './utils'
describe('joinPath', () => { it('Should joined path is valid.', () => { expect(joinPaths('test')).toBe('test') //single19 unmodified lines it('Should windows path is valid.', () => { expect(joinPaths('a\\b\\c', 'd\\e')).toBe('a/b/c/d/e') })})
describe('dirname', () => { it('Should dirname is valid.', () => { expect(dirname('parent/child')).toBe('parent') expect(dirname('windows\\test.txt')).toBe('windows') })})
describe('ensureWithinOutDir', () => { it('Should not throw for paths within outDir', () => { expect(() => ensureWithinOutDir('./static', 'static/index.html')).not.toThrow() expect(() => ensureWithinOutDir('./static', 'static/sub/page.html')).not.toThrow() expect(() => ensureWithinOutDir('/out', '/out/index.html')).not.toThrow() expect(() => ensureWithinOutDir('./static', 'static/a/../b.html')).not.toThrow() })
it('Should throw for paths outside outDir via traversal', () => { expect(() => ensureWithinOutDir('./static', 'pwned.txt')).toThrow('Path traversal detected') expect(() => ensureWithinOutDir('./static', '../pwned.txt')).toThrow('Path traversal detected') expect(() => ensureWithinOutDir('./out', 'pwned.txt')).toThrow('Path traversal detected') expect(() => ensureWithinOutDir('./static', 'static/../../pwned.txt')).toThrow( 'Path traversal detected' ) })
it('Should throw for paths that partially match outDir name', () => { expect(() => ensureWithinOutDir('./static', 'static-evil/pwned.html')).toThrow( 'Path traversal detected' ) })})modified:src/helper/ssg/utils.ts+12 added
71 unmodified lines7273747576777879808182838485868771 unmodified lines
export const isDynamicRoute = (path: string): boolean => { return path.split('/').some((segment) => segment.startsWith(':') || segment.includes('*'))}
export const ensureWithinOutDir = (outDir: string, filePath: string): void => { const normalizedOutDir = joinPaths('/', outDir) const normalizedFilePath = joinPaths('/', filePath)
if ( normalizedFilePath !== normalizedOutDir && !normalizedFilePath.startsWith(`${normalizedOutDir}/`) ) { throw new Error(`Path traversal detected: "${filePath}" is outside the output directory`) }}