Skip to content
Myr

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

4 files+90 added−10 removed

modified:src/helper/ssg/ssg.test.tsx+34 added
561 unmodified lines
562 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 })566567 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')579580 expect(fsMock.mkdir).not.toHaveBeenCalled()581 expect(fsMock.writeFile).not.toHaveBeenCalled()582 })583584 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')596597 expect(fsMock.mkdir).not.toHaveBeenCalled()598 expect(fsMock.writeFile).not.toHaveBeenCalled()599 })600})601602describe('Dynamic route handling', () => {603 let app: Hono
455 unmodified lines
modified:src/helper/ssg/ssg.ts+18 added-9 removed
4 unmodified lines
5import { getExtension } from '../../utils/mime'6import type { AddedSSGDataRequest, SSGParams } from './middleware'7import { SSG_CONTEXT, X_HONO_DISABLE_SSG_HEADER_KEY } from './middleware'8import { defaultPlugin } from './plugins'9import { dirname, filterStaticGenerateRoutes, isDynamicRoute, joinPaths } from './utils'9import {10 dirname,11 ensureWithinOutDir,12 filterStaticGenerateRoutes,13 isDynamicRoute,14 joinPaths,15} from './utils'1617const DEFAULT_CONCURRENCY = 2 // default concurrency for ssg1819// 'default_content_type' is designed according to Bun's performance optimization,
34 unmodified lines
54 extensionMap?: Record<string, string>55): string => {56 const extension = determineExtension(mimeType, extensionMap)5758 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 }6856 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)7071 return filePath72}7374const parseResponseContent = async (response: Response): Promise<string | ArrayBuffer> => {75 const contentType = response.headers.get('Content-Type')
393 unmodified lines
modified:src/helper/ssg/utils.test.ts+26 added-1 removed
modified:src/helper/ssg/utils.ts+12 added