Skip to content

Commit

Permalink
test: custom base path
Browse files Browse the repository at this point in the history
  • Loading branch information
zeyu2001 committed Sep 2, 2024
1 parent f8ae1c7 commit 391c79c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
18 changes: 17 additions & 1 deletion packages/safe-fs/src/__tests__/fs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,13 @@ describe('getter', () => {
})

describe('security tests', () => {
beforeEach(() => {
const sensitiveDir = '/etc'

vol.reset()
vol.mkdirSync(sensitiveDir, { recursive: true })
})

it('should prevent path traversal attempts', () => {
const maliciousPath = '../../../etc/passwd'
const content = 'Malicious content'
Expand Down Expand Up @@ -159,9 +166,18 @@ describe('getter', () => {
})

it('should allow operations within the base path', () => {
const sfs2 = new Proxy<typeof fs>(fs, { get: createGetter('/etc') }) // unsafe usage of the library
const maliciousPath = 'passwd'
const content = 'Valid content'

expect(() => sfs2.writeFileSync(maliciousPath, content)).not.toThrow()
expect(() => sfs2.readFileSync(maliciousPath)).not.toThrow()
expect(() => sfs2.renameSync(maliciousPath, 'new.txt')).not.toThrow()
expect(() => sfs2.statSync('new.txt')).not.toThrow()
expect(() => sfs2.unlinkSync('new.txt')).not.toThrow()

const validPath = 'valid/nested/path.txt'
const newPath = 'valid/new.txt'
const content = 'Valid content'

expect(() =>
sfs.mkdirSync('valid/nested', { recursive: true }),
Expand Down
3 changes: 1 addition & 2 deletions packages/safe-fs/src/sanitizers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { PathLike } from 'node:fs'
import path from 'node:path'

import { PathLike } from 'memfs/lib/node/types/misc'

const LEADING_DOT_SLASH_REGEX = /^(\.\.(\/|\\|$))+/

export const sanitizePath = (
Expand Down

0 comments on commit 391c79c

Please sign in to comment.