Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update escaping rules in io's rmRF #828

Merged
merged 13 commits into from
Jun 7, 2021
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions packages/io/__tests__/io.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,43 @@ describe('rmRF', () => {
await assertNotExists(symlinkFile)
await assertNotExists(outerDirectory)
})
} else {
it('correctly escapes % on windows', async () => {
const root: string = path.join(getTestTemp(), 'rmRF_escape_test_win')
const realDirectory: string = path.join(root, '%test%')
await io.mkdirP(root)
await io.mkdirP(realDirectory)
thboop marked this conversation as resolved.
Show resolved Hide resolved
process.env['%test%'] = 'thisshouldnotresolve'
thboop marked this conversation as resolved.
Show resolved Hide resolved

await io.rmRF(realDirectory)
await assertNotExists(realDirectory)
})

it('Should throw for invalid characters', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not at all necessary, but you can parameterize tests with

it.each([`*`, `"`, `<`, `>` `|`, async (invalidChar) => { })

to avoid duplication

const root: string = path.join(getTestTemp(), 'rmRF_invalidChar_Windows')
const errorString =
'File path must not contain `*`, `"`, `<`, `>` or `|` on Windows'
await expect(io.rmRF(path.join(root, '"'))).rejects.toHaveProperty(
'message',
errorString
)
await expect(io.rmRF(path.join(root, '<'))).rejects.toHaveProperty(
'message',
errorString
)
await expect(io.rmRF(path.join(root, '>'))).rejects.toHaveProperty(
'message',
errorString
)
await expect(io.rmRF(path.join(root, '|'))).rejects.toHaveProperty(
'message',
errorString
)
await expect(io.rmRF(path.join(root, '*'))).rejects.toHaveProperty(
'message',
errorString
)
})
}

it('removes symlink folder with missing source using rmRF', async () => {
Expand Down
5 changes: 5 additions & 0 deletions packages/io/src/io-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,8 @@ function isUnixExecutable(stats: fs.Stats): boolean {
((stats.mode & 64) > 0 && stats.uid === process.getuid())
)
}

// Get the path of cmd.exe in windows
export function getCmdPath(): string {
return process.env['COMSPEC'] ?? `C:\\WINDOWS\\system32\\cmd.exe`
thboop marked this conversation as resolved.
Show resolved Hide resolved
}
20 changes: 17 additions & 3 deletions packages/io/src/io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {promisify} from 'util'
import * as ioUtil from './io-util'

const exec = promisify(childProcess.exec)
const execFile = promisify(childProcess.execFile)

/**
* Interface for cp/mv options
Expand Down Expand Up @@ -117,11 +118,24 @@ export async function rmRF(inputPath: string): Promise<void> {
if (ioUtil.IS_WINDOWS) {
// Node doesn't provide a delete operation, only an unlink function. This means that if the file is being used by another
// program (e.g. antivirus), it won't be deleted. To address this, we shell out the work to rd/del.

// Check for invalid characters
// https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file
if (/[*"<>|]+/.test(inputPath)) {
thboop marked this conversation as resolved.
Show resolved Hide resolved
throw new Error(
'File path must not contain `*`, `"`, `<`, `>` or `|` on Windows'
)
}
try {
const cmdPath = ioUtil.getCmdPath()
if (await ioUtil.isDirectory(inputPath, true)) {
await exec(`rd /s /q "${inputPath}"`)
await exec(`${cmdPath} /s /c "rd /s /q "%inputPath%""`, {
env: {inputPath}
})
} else {
await exec(`del /f /a "${inputPath}"`)
await exec(`${cmdPath} /s /c "del /f /a "%inputPath%""`, {
env: {inputPath}
})
}
} catch (err) {
// if you try to delete a file that doesn't exist, desired result is achieved
Expand Down Expand Up @@ -149,7 +163,7 @@ export async function rmRF(inputPath: string): Promise<void> {
}

if (isDir) {
await exec(`rm -rf "${inputPath}"`)
await execFile(`rm`, [`-rf`, `${inputPath}`])
} else {
await ioUtil.unlink(inputPath)
}
Expand Down