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.

* fix(ssg): prevent output path traversal * fix(ssg): distinguish Windows output path roots

authored by Taku Amano · committed by GitHub · parent 9c28d72 · Browse files · 4 files+208 added−12 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+208 added−12 removed

  • tests:+164 added−0 removed
  • other:+44 added−12 removed

4 files+208 added−12 removed

modified:src/helper/ssg/ssg.test.tsx+122 added
160 unmodified lines
161 expect(result.error).toBeDefined()162 expect(result.files).toStrictEqual([])163 })164165 it('Should reject consecutive parent segments from ssgParams', async () => {166 const app = new Hono()167 app.get('/:id', ssgParams([{ id: 'a/b/../../../pwned' }]), (c) =>168 c.text('attacker-controlled-body')169 )170 const fsMock: FileSystemModule = {171 writeFile: vi.fn(() => Promise.resolve()),172 mkdir: vi.fn(() => Promise.resolve()),173 }174175 const result = await toSSG(app, fsMock, { dir: './static' })176177 expect(result.success).toBe(false)178 expect(result.files).toStrictEqual([])179 expect(result.error?.message).toContain('Path traversal detected')180 expect(fsMock.mkdir).not.toHaveBeenCalled()181 expect(fsMock.writeFile).not.toHaveBeenCalled()182 })183184 it('Should generate files when dir is the current directory', async () => {185 const app = new Hono()186 app.get('/', (c) => c.html('Hello, World!'))187 app.get('/about/some', (c) => c.text('About Page 2tier'))188 const fsMock: FileSystemModule = {189 writeFile: vi.fn(() => Promise.resolve()),190 mkdir: vi.fn(() => Promise.resolve()),191 }192193 const result = await toSSG(app, fsMock, { dir: '.' })194195 expect(result.success).toBe(true)196 expect(result.files).toHaveLength(2)197 expect(fsMock.writeFile).toHaveBeenCalledWith('index.html', expect.any(String))198 expect(fsMock.writeFile).toHaveBeenCalledWith('about/some.txt', expect.any(String))199 expect(fsMock.mkdir).not.toHaveBeenCalledWith('', expect.anything())200 })201202 it('Should reject absolute Windows paths when dir is the current directory', async () => {203 const app = new Hono()204 app.get(205 '/:drive/:dir/:file',206 ssgParams([{ drive: 'C:', dir: 'Windows', file: 'pwned' }]),207 (c) => c.text('pwned')208 )209 const fsMock: FileSystemModule = {210 writeFile: vi.fn(() => Promise.resolve()),211 mkdir: vi.fn(() => Promise.resolve()),212 }213214 const result = await toSSG(app, fsMock, { dir: '.' })215216 expect(result.success).toBe(false)217 expect(result.error?.message).toContain('Path traversal detected')218 expect(fsMock.mkdir).not.toHaveBeenCalled()219 expect(fsMock.writeFile).not.toHaveBeenCalled()220 })221222 it('Should reject drive-relative Windows paths when dir is the current directory', async () => {223 const app = new Hono()224 app.get('/:id', ssgParams([{ id: 'C:pwned' }]), (c) => c.text('pwned'))225 const fsMock: FileSystemModule = {226 writeFile: vi.fn(() => Promise.resolve()),227 mkdir: vi.fn(() => Promise.resolve()),228 }229230 const result = await toSSG(app, fsMock, { dir: '.' })231232 expect(result.success).toBe(false)233 expect(result.error?.message).toContain('Path traversal detected')234 expect(fsMock.mkdir).not.toHaveBeenCalled()235 expect(fsMock.writeFile).not.toHaveBeenCalled()236 })237238 it('Should reject a drive-relative Windows output root', async () => {239 const app = new Hono()240 app.get('/', (c) => c.text('Hello, World!'))241 const fsMock: FileSystemModule = {242 writeFile: vi.fn(() => Promise.resolve()),243 mkdir: vi.fn(() => Promise.resolve()),244 }245246 const result = await toSSG(app, fsMock, { dir: 'C:' })247248 expect(result.success).toBe(false)249 expect(result.error?.message).toContain('Path traversal detected')250 expect(fsMock.mkdir).not.toHaveBeenCalled()251 expect(fsMock.writeFile).not.toHaveBeenCalled()252 })253254 it('Should preserve a UNC output directory', async () => {255 const app = new Hono()256 app.get('/', (c) => c.html('Hello, World!'))257 const fsMock: FileSystemModule = {258 writeFile: vi.fn(() => Promise.resolve()),259 mkdir: vi.fn(() => Promise.resolve()),260 }261262 const result = await toSSG(app, fsMock, { dir: '\\\\server\\share\\out' })263264 expect(result.success).toBe(true)265 expect(fsMock.mkdir).toHaveBeenCalledWith('//server/share/out', { recursive: true })266 expect(fsMock.writeFile).toHaveBeenCalledWith(267 '//server/share/out/index.html',268 expect.any(String)269 )270 })271272 it('Should normalize redundant leading separators in a POSIX output directory', async () => {273 const app = new Hono()274 app.get('/', (c) => c.html('Hello, World!'))275 const fsMock: FileSystemModule = {276 writeFile: vi.fn(() => Promise.resolve()),277 mkdir: vi.fn(() => Promise.resolve()),278 }279280 const result = await toSSG(app, fsMock, { dir: '///tmp/out' })281282 expect(result.success).toBe(true)283 expect(fsMock.mkdir).toHaveBeenCalledWith('/tmp/out', { recursive: true })284 expect(fsMock.writeFile).toHaveBeenCalledWith('/tmp/out/index.html', expect.any(String))285 })286287 it('Should correctly generate files with the expected paths', async () => {288 app.get('/data', (c) =>289 c.text(JSON.stringify({ title: 'hono' }), 200, {290 'Content-Type': 'text/x-foo',
906 unmodified lines
modified:src/helper/ssg/ssg.ts+2 added-1 removed
319 unmodified lines
320 const { routePath, content, mimeType } = awaitedData321 const filePath = generateFilePath(routePath, outDir, mimeType, extensionMap)322 const dirPath = dirname(filePath)323324 // `dirPath` is empty when outDir itself is the current directory324 if (!createdDirs.has(dirPath)) {325 if (dirPath !== '' && !createdDirs.has(dirPath)) {326 await fsModule.mkdir(dirPath, { recursive: true })327 createdDirs.add(dirPath)328 }329 if (typeof content === 'string') {
142 unmodified lines
modified:src/helper/ssg/utils.test.ts+42 added
modified:src/helper/ssg/utils.ts+42 added-11 removed