Skip to content

Commit 991f03c

Browse files
committed
fix(code-placeholders): harden shell placeholder interpolation in arithmetic contexts
1 parent 14e6c0a commit 991f03c

3 files changed

Lines changed: 755 additions & 186 deletions

File tree

‎apps/sim/lib/execution/code-placeholders/compiler.test.ts‎

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,6 +1053,67 @@ describe('code placeholder compiler', () => {
10531053
'total=$(( $(( 1 + 1 )) + {{KEY}} ))',
10541054
'cat <<PAYLOAD\n$(( {{KEY}} * 2 ))\nPAYLOAD',
10551055
'cat <<PAYLOAD\n$[ {{KEY}} * 2 ]\nPAYLOAD',
1056+
'echo $(( $(cat <<EOF\n{{KEY}}\nEOF\n) + 1 ))',
1057+
"echo $(( $(cat <<'EOF'\n{{KEY}}\nEOF\n) + 1 ))",
1058+
"echo $(( $(cat <<-'EOF'\n\t{{KEY}}\n\tEOF\n) + 1 ))",
1059+
'[[ {{KEY}} -eq 0 ]] && echo zero',
1060+
'[[ "{{KEY}}" -eq 0 ]] && echo zero',
1061+
'if [[ 0 -lt {{KEY}} ]]; then echo positive; fi',
1062+
'[[ -n x && ( "{{KEY}}" -ge 1 ) ]]',
1063+
'[[ $(printf "%s" "{{KEY}}") -ne 0 ]]',
1064+
'[[ $(printf "%s" "{{KEY}}"; echo 1) -le 0 ]]',
1065+
'[[ $(cat <<EOF\n{{KEY}}\nEOF\n) -gt 0 ]]',
1066+
'let "x={{KEY}}"',
1067+
'let x={{KEY}}+1',
1068+
'declare -i x="{{KEY}}"',
1069+
'typeset -i x={{KEY}}',
1070+
'f() { local -i x="{{KEY}}"; }',
1071+
'declare -ai values=("{{KEY}}")',
1072+
'declare -x -i x="{{KEY}}"',
1073+
'let x="$(printf "%s" "{{KEY}}")"',
1074+
'declare -i x="$(printf "%s" "{{KEY}}")"',
1075+
'>/dev/null let x="{{KEY}}"',
1076+
'2>/dev/null let x="{{KEY}}"',
1077+
'builtin let x="{{KEY}}"',
1078+
'command let x="{{KEY}}"',
1079+
'declare "-i" n="{{KEY}}"',
1080+
// biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1081+
'[[ ${missing:-{{KEY}}} -eq 0 ]]',
1082+
'declare -i n; echo "$(declare +i n)"; n="{{KEY}}"',
1083+
// biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1084+
'printf "%s" "${missing:-{text}"; let x="{{KEY}}"',
1085+
'declare -i n; n="$(printf "%s" "{{KEY}}")"',
1086+
// biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1087+
'declare -i n; n="${missing:-{{KEY}}}"',
1088+
'declare -i n; declare n="{{KEY}}"',
1089+
'> /dev/null let x="{{KEY}}"',
1090+
'let x="$(cat <<EOF\n{{KEY}}\nEOF\n)"',
1091+
'unset "a[{{KEY}}]"',
1092+
'unset a[{{KEY}}]',
1093+
'a[{{KEY}}]=1',
1094+
'a[{{KEY}}]+=1',
1095+
'declare -i n; n="{{KEY}}"',
1096+
'declare -i n\nn="{{KEY}}"',
1097+
// biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1098+
'a=(1 2); echo "${a[{{KEY}}]}"',
1099+
// biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1100+
'a=(1 2); echo "${a[0]:{{KEY}}}"',
1101+
// biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1102+
'a=(1 2); echo "${a[0]:0:{{KEY}}}"',
1103+
// biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1104+
's=abc; echo "${s:{{KEY}}}"',
1105+
// biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1106+
's=abc; echo "${s:0:{{KEY}}}"',
1107+
// biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1108+
's=abc; echo "${s: -1:{{KEY}}}"',
1109+
// biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1110+
'set -- a b; echo "${@:{{KEY}}}"',
1111+
// biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1112+
's=abc; printf "%s\\n" "${missing:-"${s:{{KEY}}}"}"',
1113+
// biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1114+
'cat <<PAYLOAD\n${a[{{KEY}}]}\nPAYLOAD',
1115+
// biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1116+
'cat <<PAYLOAD\n${s:{{KEY}}}\nPAYLOAD',
10561117
])('rejects shell placeholders whose values enter arithmetic: %s', async (code) => {
10571118
await expect(
10581119
compileCodePlaceholders({
@@ -1087,6 +1148,86 @@ describe('code placeholder compiler', () => {
10871148
)
10881149
})
10891150

1151+
it.each([
1152+
// biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1153+
'printf "%s\\n" "${missing:-https://{{KEY}}}"',
1154+
// biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1155+
'printf "%s\\n" "${missing:-items[{{KEY}}]}"',
1156+
// biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1157+
's=abc; printf "%s\\n" "${s#[{{KEY}}]}"',
1158+
// biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1159+
's=abc; printf "%s\\n" "${s/[{{KEY}}]/x}"',
1160+
'f() { local -i n; }; n="{{KEY}}"',
1161+
'PREFIX="$(printf "%s" "{{KEY}}")" let total=2',
1162+
'printf "%s" a[{{KEY}}]=1',
1163+
'declare -i n; printf "%s" n={{KEY}}',
1164+
'declare -i n; declare +i n; n="{{KEY}}"',
1165+
'declare -i n; n=1 printf "%s" "{{KEY}}"',
1166+
'declare -i n; n=5 text="{{KEY}}"',
1167+
'declare -i +i n="{{KEY}}"',
1168+
'declare -i n; echo "$(declare +i n; n=\'{{KEY}}\'; printf "%s" "$n")"',
1169+
'$(let x=1 <<EOF\n{{KEY}}\nEOF\n)',
1170+
// biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1171+
'declare -A m; m[{{KEY}}]=x; printf "%s\\n" "${m[{{KEY}}]}"',
1172+
'declare -A m; printf "%s\\n" "${m[{{KEY}}]}"',
1173+
])('compiles parameter and prefix text that is not an arithmetic operand: %s', async (code) => {
1174+
// These forms use the value as pattern, default, or a shielded prefix — never arithmetic — so a
1175+
// conservative scanner must not reject them. Each would fail compilation if wrongly rejected.
1176+
await expect(
1177+
compileCodePlaceholders({
1178+
code,
1179+
language: CodeLanguage.Shell,
1180+
environmentVariables: { KEY: 'values[$(printf injected >&2)]' },
1181+
})
1182+
).resolves.toBeDefined()
1183+
})
1184+
1185+
it('keeps literal single quotes inside a double-quoted default expansion', async () => {
1186+
const compiled = await compileCodePlaceholders({
1187+
// biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1188+
code: 'printf "%s\\n" "${missing:-\'{{KEY}}\'}"',
1189+
language: CodeLanguage.Shell,
1190+
environmentVariables: { KEY: 'hello world' },
1191+
})
1192+
expect(executeShell(compiled.code, compiled.bindings)).toBe("'hello world'\n")
1193+
})
1194+
1195+
it('compiles shell placeholders beside arithmetic that never evaluates them', async () => {
1196+
const value = 'values[$(printf injected >&2)]'
1197+
const compiled = await compileCodePlaceholders({
1198+
code: [
1199+
'[ "{{KEY}}" -eq 0 ] 2>/dev/null || printf "%s\\n" not-zero',
1200+
'[[ "{{KEY}}" == values* && 1 -eq 1 ]] && printf "%s\\n" matched',
1201+
'let total=1+1; printf "%s\\n" "{{KEY}}"',
1202+
'f() { local copy="{{KEY}}"; printf "%s\\n" "$copy"; }; f',
1203+
'declare copy="{{KEY}}"; printf "%s\\n" "$copy"',
1204+
// biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1205+
'printf "%s\\n" "${missing:-{{KEY}}}"',
1206+
// biome-ignore lint/suspicious/noTemplateCurlyInString: shell parameter expansion, not a JS template
1207+
'a=(x y); printf "%s\\n" "${a[1]}{{KEY}}"',
1208+
'printf "%s\\n" "let {{KEY}}"',
1209+
'printf "%s\\n" "items[{{KEY}}]"',
1210+
'PREFIX="{{KEY}}" let total=2',
1211+
'# declare -i n',
1212+
'n="{{KEY}}"; printf "%s\\n" "$n"',
1213+
'let total=1 <<EOF',
1214+
'{{KEY}}',
1215+
'EOF',
1216+
'printf "%s\\n" "$total"',
1217+
'cat <<PAYLOAD',
1218+
'{{KEY}} $(( 1 + 1 ))',
1219+
'PAYLOAD',
1220+
].join('\n'),
1221+
language: CodeLanguage.Shell,
1222+
environmentVariables: { KEY: value },
1223+
})
1224+
1225+
expect(executeShell(`{\n${compiled.code}\n} 2>&1`, compiled.bindings)).toBe(
1226+
`not-zero\nmatched\n${value}\n${value}\n${value}\n${value}\ny${value}\n` +
1227+
`let ${value}\nitems[${value}]\n${value}\n1\n${value} 2\n`
1228+
)
1229+
})
1230+
10901231
it('discovers shell arithmetic placeholders without compiling missing values', async () => {
10911232
const code = 'total=$(( {{MISSING}} + {{KEY}} ))'
10921233
await expect(analyzeCodePlaceholders(code, CodeLanguage.Shell)).resolves.toEqual([

‎apps/sim/lib/execution/code-placeholders/shared.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,12 +327,12 @@ export function createOffsetRangeLookup(
327327
* The placeholders lying wholly inside `[start, end]`. Occurrences are ordered and never
328328
* overlap, so the matches are one contiguous run found by bisection.
329329
*/
330-
export function occurrencesWithin(
331-
occurrences: readonly CodePlaceholderOccurrence[],
330+
export function occurrencesWithin<T extends Pick<CodePlaceholderOccurrence, 'start' | 'end'>>(
331+
occurrences: readonly T[],
332332
start: number,
333333
end: number
334-
): CodePlaceholderOccurrence[] {
335-
const matches: CodePlaceholderOccurrence[] = []
334+
): T[] {
335+
const matches: T[] = []
336336
for (
337337
let index = partitionPoint(occurrences, (occurrence) => occurrence.start < start);
338338
index < occurrences.length && occurrences[index].end <= end;

0 commit comments

Comments
 (0)