Skip to content

Commit c240663

Browse files
mnriemCopilot
andcommitted
Fix extension hook paths and native YAML scalar parity
Keep shared dispatch paths rooted when extension commands render, decode Bash backslash escapes, and reject implicitly typed identifiers in native resolvers. Add cross-runtime and extension rendering regressions. Assisted-by: GitHub Copilot (model: GPT-6 Sol, autonomous) Co-authored-by: Copilot App <[email protected]>
1 parent 87a744a commit c240663

5 files changed

Lines changed: 91 additions & 9 deletions

File tree

‎scripts/bash/pre-hooks.sh‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ hook_scalar() {
5656
r) HOOK_SCALAR+=$'\r' ;;
5757
b) HOOK_SCALAR+=$'\b' ;;
5858
f) HOOK_SCALAR+=$'\f' ;;
59-
'"'|'\\') HOOK_SCALAR+=$escaped ;;
59+
'"'|'\') HOOK_SCALAR+=$escaped ;;
6060
*) HOOK_ERROR="Unsupported YAML escape"; return ;;
6161
esac
6262
done
@@ -82,6 +82,16 @@ hook_field() {
8282
raw=${field:${#key}+1}
8383
hook_scalar "$raw"
8484
[[ -n $HOOK_ERROR ]] && return
85+
if [[ $key == extension || $key == command ]] && [[ $HOOK_QUOTED == false ]]; then
86+
local lowered=${HOOK_SCALAR,,}
87+
if [[ -z $HOOK_SCALAR || $lowered =~ ^(true|false|yes|no|on|off|null|~)$ ||
88+
$HOOK_SCALAR =~ ^[+-]?(0[xX][0-9a-fA-F_]+|0[oO][0-7_]+|0[bB][01_]+|[0-9][0-9_]*(\.[0-9_]*)?([eE][+-]?[0-9]+)?|\.[0-9_]+([eE][+-]?[0-9]+)?|\.([iI][nN][fF]|[nN][aA][nN]))$ ||
89+
$HOOK_SCALAR =~ ^[0-9]+(:[0-9]+)+$ ||
90+
$HOOK_SCALAR =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}([Tt\ ]|$) ]]; then
91+
HOOK_ERROR="hooks.$event needs extension and command"
92+
return
93+
fi
94+
fi
8595
if [[ $key == enabled || $key == optional ]]; then
8696
if [[ $HOOK_QUOTED == true || ( $HOOK_SCALAR != true && $HOOK_SCALAR != false ) ]]; then
8797
HOOK_ERROR="$key must be a boolean"

‎scripts/powershell/pre-hooks.ps1‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,19 @@ function Convert-HookScalar {
2323
}
2424

2525
function Add-HookField {
26-
param([hashtable]$Hook, [string]$Text)
26+
param([hashtable]$Hook, [string]$Text, [string]$Event)
2727
if ($Text -notmatch '^([a-z_]+):(?:\s|$)(.*)$') { throw "Unsupported YAML hook field" }
2828
$key = $Matches[1]
2929
$parsed = Convert-HookScalar $Matches[2]
3030
$value = $parsed.Value
31+
if ($key -in @('extension', 'command') -and -not $parsed.Quoted -and (
32+
-not $value -or $value -match '^(true|false|yes|no|on|off|null|~)$' -or
33+
$value -match '^[+-]?(0[xX][0-9a-fA-F_]+|0[oO][0-7_]+|0[bB][01_]+|[0-9][0-9_]*(\.[0-9_]*)?([eE][+-]?[0-9]+)?|\.[0-9_]+([eE][+-]?[0-9]+)?|\.([iI][nN][fF]|[nN][aA][nN]))$' -or
34+
$value -match '^[0-9]+(:[0-9]+)+$' -or
35+
$value -match '^[0-9]{4}-[0-9]{2}-[0-9]{2}([Tt ]|$)'
36+
)) {
37+
throw "hooks.$Event needs extension and command"
38+
}
3139
if ($key -in @('enabled', 'optional') -and ($parsed.Quoted -or $value -notin @('true', 'false'))) {
3240
throw "$key must be a boolean"
3341
}
@@ -118,12 +126,12 @@ function Resolve-HookConfig {
118126
if ($target) {
119127
$hook = @{ enabled = 'true'; optional = 'true'; priority = '10' }
120128
$hooks.Add($hook)
121-
Add-HookField $hook $body.Substring(2)
129+
Add-HookField $hook $body.Substring(2) $Event
122130
}
123131
continue
124132
}
125133
if ($itemIndent -ge 0 -and $indent -eq $itemIndent + 2) {
126-
if ($target) { Add-HookField $hook $body }
134+
if ($target) { Add-HookField $hook $body $Event }
127135
continue
128136
}
129137
throw "Unsupported YAML hook layout"

‎src/specify_cli/integrations/base.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -796,12 +796,12 @@ def resolve_hook_script_refs(
796796
for phase, name in (("PRE", "pre"), ("POST", "post")):
797797
if variant == "py":
798798
command = IntegrationBase.build_python_invocation(
799-
f"scripts/python/{name}_hooks.py", project_root
799+
f".specify/scripts/python/{name}_hooks.py", project_root
800800
)
801801
elif variant == "ps":
802-
command = f"scripts/powershell/{name}-hooks.ps1"
802+
command = f".specify/scripts/powershell/{name}-hooks.ps1"
803803
else:
804-
command = f"scripts/bash/{name}-hooks.sh"
804+
command = f".specify/scripts/bash/{name}-hooks.sh"
805805
content = content.replace(f"{{{phase}_HOOK_SCRIPT}}", command)
806806
return content
807807

‎tests/extensions/github/test_github_extension.py‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,15 @@ def test_rendered_command_points_at_a_script_that_ships(
429429
assert expected in content
430430
# The rendered path must resolve to a file the extension ships.
431431
assert (project / expected).is_file()
432-
# And it must not have been rewritten into the core script tree.
433-
assert ".specify/scripts/" not in content
432+
# The extension's own script stays local; hook dispatchers are shared.
433+
for phase in ("pre", "post"):
434+
hook_path = {
435+
"sh": f".specify/scripts/bash/{phase}-hooks.sh",
436+
"ps": f".specify/scripts/powershell/{phase}-hooks.ps1",
437+
"py": f".specify/scripts/python/{phase}_hooks.py",
438+
}[variant]
439+
assert hook_path in content
440+
assert hook_path.replace(".specify/scripts/", ".specify/extensions/github/scripts/") not in content
434441

435442
@pytest.mark.parametrize("agent", EXTENSION_REGISTRAR_AGENTS)
436443
def test_every_extension_registrar_integration_renders_the_command(
@@ -522,6 +529,9 @@ def test_rendered_skill_points_at_a_script_that_ships(self, tmp_path: Path):
522529
expected = f".specify/extensions/github/{SCRIPT_TWINS['sh']}"
523530
assert expected in content
524531
assert (project / expected).is_file()
532+
for phase in ("pre", "post"):
533+
assert f".specify/scripts/bash/{phase}-hooks.sh" in content
534+
assert f".specify/extensions/github/scripts/bash/{phase}-hooks.sh" not in content
525535

526536

527537
# -- Behaviour parity with the core command -----------------------------------

‎tests/test_hook_dispatch_scripts.py‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,60 @@ def test_canonical_cli_writer_keeps_multiline_prompt_on_one_yaml_line(tmp_path,
241241
assert data["hooks"][0]["prompt"] == "first\nsecond"
242242

243243

244+
@pytest.mark.parametrize("variant", ["sh", "ps", "py"])
245+
@pytest.mark.parametrize("phase", ["pre", "post"])
246+
def test_canonical_backslash_escape_matches_python(tmp_path, variant, phase):
247+
if variant == "ps" and not shutil.which("pwsh"):
248+
pytest.skip("PowerShell not installed")
249+
from specify_cli.extensions import HookExecutor
250+
251+
event = f"{'before' if phase == 'pre' else 'after'}_plan"
252+
prompt = "Path C:\\workspace\nkeep \\ literal"
253+
HookExecutor(tmp_path).save_project_config({
254+
"hooks": {event: [
255+
{"extension": "git", "command": "speckit.git.commit", "prompt": prompt}
256+
]}
257+
})
258+
code, data = run_hook(tmp_path, phase, variant=variant)
259+
assert code == 0
260+
assert data == run_hook(tmp_path, phase)[1]
261+
assert data["hooks"][0]["prompt"] == prompt
262+
263+
264+
@pytest.mark.parametrize("variant", ["sh", "ps", "py"])
265+
@pytest.mark.parametrize("phase", ["pre", "post"])
266+
@pytest.mark.parametrize("field,value", [
267+
("extension", "false"), ("command", "123"), ("extension", "yes"),
268+
("command", "0x10"), ("extension", "null"), ("command", "1.2"),
269+
("extension", "2026-10-08"), ("command", ".nan"),
270+
])
271+
def test_native_identifiers_reject_implicitly_typed_yaml(tmp_path, variant, phase, field, value):
272+
if variant == "ps" and not shutil.which("pwsh"):
273+
pytest.skip("PowerShell not installed")
274+
event = f"{'before' if phase == 'pre' else 'after'}_plan"
275+
write_config(
276+
tmp_path,
277+
f"hooks:\n {event}:\n - extension: git\n"
278+
f" command: speckit.git.commit\n {field}: {value}\n",
279+
)
280+
code, data = run_hook(tmp_path, phase, variant=variant)
281+
assert code == 1
282+
assert "needs extension and command" in data["error"]
283+
assert data["hooks"] == []
284+
285+
286+
@pytest.mark.parametrize("variant", ["sh", "ps", "py"])
287+
@pytest.mark.parametrize("phase", ["pre", "post"])
288+
def test_quoted_numeric_identifier_remains_string(tmp_path, variant, phase):
289+
if variant == "ps" and not shutil.which("pwsh"):
290+
pytest.skip("PowerShell not installed")
291+
event = f"{'before' if phase == 'pre' else 'after'}_plan"
292+
write_config(tmp_path, f"hooks:\n {event}:\n - extension: '123'\n command: 'false'\n")
293+
code, data = run_hook(tmp_path, phase, variant=variant)
294+
assert code == 0
295+
assert data == run_hook(tmp_path, phase)[1]
296+
297+
244298
@pytest.mark.parametrize("variant", ["sh", "ps"])
245299
@pytest.mark.parametrize("phase", ["pre", "post"])
246300
def test_native_resolvers_do_not_invoke_python(tmp_path, variant, phase):

0 commit comments

Comments
 (0)