Skip to content

Commit 02daa91

Browse files
claudesjnims
authored andcommitted
fix(security): use jq for safe JSON output in example hooks
Example hook scripts were using shell string concatenation to embed variables in JSON output, which could cause malformed JSON if the variable contained special characters like quotes or backslashes. Changed: - validate-write.sh: Use jq --arg for file_path in error messages - read-settings-hook.sh: Use jq --arg for MAX_SIZE in error message Before: echo '{"systemMessage": "Path: '"$file_path"'"}' >&2 After: jq -n --arg path "$file_path" \ '{"systemMessage": "Path: \($path)"}' >&2 This ensures proper JSON escaping regardless of the variable content.
1 parent 1cf02a6 commit 02daa91

2 files changed

Lines changed: 8 additions & 4 deletions

File tree

plugins/plugin-dev/skills/hook-development/examples/validate-write.sh

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,22 @@ fi
1818

1919
# Check for path traversal
2020
if [[ "$file_path" == *".."* ]]; then
21-
echo '{"hookSpecificOutput": {"permissionDecision": "deny"}, "systemMessage": "Path traversal detected in: '"$file_path"'"}' >&2
21+
jq -n --arg path "$file_path" \
22+
'{"hookSpecificOutput": {"permissionDecision": "deny"}, "systemMessage": "Path traversal detected in: \($path)"}' >&2
2223
exit 2
2324
fi
2425

2526
# Check for system directories
2627
if [[ "$file_path" == /etc/* ]] || [[ "$file_path" == /sys/* ]] || [[ "$file_path" == /usr/* ]]; then
27-
echo '{"hookSpecificOutput": {"permissionDecision": "deny"}, "systemMessage": "Cannot write to system directory: '"$file_path"'"}' >&2
28+
jq -n --arg path "$file_path" \
29+
'{"hookSpecificOutput": {"permissionDecision": "deny"}, "systemMessage": "Cannot write to system directory: \($path)"}' >&2
2830
exit 2
2931
fi
3032

3133
# Check for sensitive files
3234
if [[ "$file_path" == *.env ]] || [[ "$file_path" == *secret* ]] || [[ "$file_path" == *credentials* ]]; then
33-
echo '{"hookSpecificOutput": {"permissionDecision": "ask"}, "systemMessage": "Writing to potentially sensitive file: '"$file_path"'"}' >&2
35+
jq -n --arg path "$file_path" \
36+
'{"hookSpecificOutput": {"permissionDecision": "ask"}, "systemMessage": "Writing to potentially sensitive file: \($path)"}' >&2
3437
exit 2
3538
fi
3639

plugins/plugin-dev/skills/plugin-settings/examples/read-settings-hook.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ if [[ -n "$MAX_SIZE" ]] && [[ "$MAX_SIZE" =~ ^[0-9]+$ ]]; then
5656
content_size=${#content}
5757

5858
if [[ $content_size -gt $MAX_SIZE ]]; then
59-
echo '{"hookSpecificOutput": {"permissionDecision": "deny"}, "systemMessage": "File exceeds configured max size: '"$MAX_SIZE"' bytes"}' >&2
59+
jq -n --arg size "$MAX_SIZE" \
60+
'{"hookSpecificOutput": {"permissionDecision": "deny"}, "systemMessage": "File exceeds configured max size: \($size) bytes"}' >&2
6061
exit 2
6162
fi
6263
fi

0 commit comments

Comments
 (0)