generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 261
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify shell escaping - escape chars instead of quoting whole string
- Loading branch information
Showing
3 changed files
with
19 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,21 @@ | ||
import shellEscape from '../src/shell-escape' | ||
|
||
test('simple path escaped', () => { | ||
expect(shellEscape('file')).toBe("'file'") | ||
test('simple filename should not be modified', () => { | ||
expect(shellEscape('file.txt')).toBe('file.txt') | ||
}) | ||
|
||
test('path with space is wrapped with single quotes', () => { | ||
expect(shellEscape('file with space')).toBe("'file with space'") | ||
test('directory separator should be preserved and not escaped', () => { | ||
expect(shellEscape('path/to/file.txt')).toBe('path/to/file.txt') | ||
}) | ||
|
||
test('path with quote is divided into quoted segments and escaped quote', () => { | ||
expect(shellEscape("file'with quote")).toBe("'file'\\''with quote'") | ||
test('spaces should be escaped with backslash', () => { | ||
expect(shellEscape('file with space')).toBe('file\\ with\\ space') | ||
}) | ||
test('path with leading quote does not have double quotes at beginning', () => { | ||
expect(shellEscape("'file-leading-quote")).toBe("\\''file-leading-quote'") | ||
|
||
test('quotes should be escaped with backslash', () => { | ||
expect(shellEscape('file\'with quote"')).toBe('file\\\'with\\ quote\\"') | ||
}) | ||
|
||
test('$variables sould be escaped', () => { | ||
expect(shellEscape('$var')).toBe('\\$var') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
// Credits to https://github.com/xxorax/node-shell-escape | ||
|
||
// Uses easy safe set of characters which can be left unescaped to keep it readable. | ||
// Every other character will be backslash-escaped | ||
export default function shellEscape(value: string): string { | ||
return `'${value.replace(/'/g, "'\\''")}'` | ||
.replace(/^(?:'')+/g, '') // unduplicate single-quote at the beginning | ||
.replace(/\\'''/g, "\\'") // remove non-escaped single-quote if there are enclosed between 2 escaped | ||
return value.replace(/([^a-zA-Z0-9,._+:@%/-])/gm, '\\$1') | ||
} |