mirror of
https://code.forgejo.org/actions/checkout.git
synced 2026-08-28 21:14:59 -04:00
Escape single quotes in submodule Foreach shell commands
This commit is contained in:
parent
f548e57e54
commit
3a65af85aa
5 changed files with 143 additions and 6 deletions
43
__test__/shell-escape.test.ts
Normal file
43
__test__/shell-escape.test.ts
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
import {describe, it, expect} from '@jest/globals'
|
||||
import {escapeSingleQuote} from '../src/shell-escape.js'
|
||||
|
||||
describe('shell-escape tests', () => {
|
||||
it('handles empty string', () => {
|
||||
expect(escapeSingleQuote('')).toBe('')
|
||||
})
|
||||
|
||||
it('leaves strings without single quotes unchanged', () => {
|
||||
expect(escapeSingleQuote('https://github.com')).toBe('https://github.com')
|
||||
expect(escapeSingleQuote('git@github.com:')).toBe('git@github.com:')
|
||||
expect(escapeSingleQuote('core.sshCommand')).toBe('core.sshCommand')
|
||||
expect(escapeSingleQuote('http.https://github.com/.extraheader')).toBe(
|
||||
'http.https://github.com/.extraheader'
|
||||
)
|
||||
})
|
||||
|
||||
it('escapes single quotes with POSIX close-escape-reopen sequence', () => {
|
||||
expect(escapeSingleQuote("it's")).toBe("it'\\''s")
|
||||
expect(escapeSingleQuote("'foo'")).toBe("'\\''foo'\\''")
|
||||
expect(escapeSingleQuote("a'b'c")).toBe("a'\\''b'\\''c")
|
||||
})
|
||||
|
||||
it('escapes single quotes in URLs with decoded percent-encoded characters', () => {
|
||||
// new URL('https://evil%27$(id)host.com') results in hostname "evil'$(id)host.com"
|
||||
const decodedUrlKey = "http.https://evil'$(id)host.com/.extraheader"
|
||||
expect(escapeSingleQuote(decodedUrlKey)).toBe(
|
||||
"http.https://evil'\\''$(id)host.com/.extraheader"
|
||||
)
|
||||
|
||||
const decodedInsteadOfValue = "git@evil'$(id)host.com:"
|
||||
expect(escapeSingleQuote(decodedInsteadOfValue)).toBe(
|
||||
"git@evil'\\''$(id)host.com:"
|
||||
)
|
||||
})
|
||||
|
||||
it('preserves other shell special characters literally inside single quotes', () => {
|
||||
const specialChars = '`$(id) && rm -rf /; echo "hello" \\ * ? < > |'
|
||||
expect(escapeSingleQuote(specialChars)).toBe(
|
||||
'`$(id) && rm -rf /; echo "hello" \\ * ? < > |'
|
||||
)
|
||||
})
|
||||
})
|
||||
Loading…
Reference in a new issue