Repository navigation
fix(skills): Windows/AppLocker portability + ordering fixes for scan scripts #2729
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a53ac5e
f44101f
d48275b
ba6aad3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,16 +53,44 @@ date_ago() { | |
| date -u -d "${n} days ago" +%Y-%m-%dT%H:%M:%SZ | ||
| } | ||
|
|
||
| # Count observations matching a file path since a cutoff timestamp | ||
| count_obs() { | ||
| local file="$1" cutoff="$2" | ||
| if [[ ! -f "$OBSERVATIONS" ]]; then | ||
| echo 0 | ||
| return | ||
| fi | ||
| jq -r --arg p "$file" --arg c "$cutoff" \ | ||
| 'select(.tool=="Read" and .path==$p and .timestamp>=$c) | 1' \ | ||
| "$OBSERVATIONS" 2>/dev/null | wc -l | tr -d ' ' | ||
| # Bash-3.2-compatible recursive discovery. Avoids `find | sort` on | ||
| # AppLocker-hardened Windows and avoids Bash 4's globstar on stock macOS. | ||
| discovered_files=() | ||
| collect_markdown_files() { | ||
| local dir="$1" | ||
| local entry | ||
| for entry in "$dir"/..?* "$dir"/.[!.]* "$dir"/*; do | ||
| [[ -e "$entry" || -L "$entry" ]] || continue | ||
| if [[ -d "$entry" && ! -L "$entry" ]]; then | ||
| collect_markdown_files "$entry" | ||
| elif [[ -f "$entry" && "$entry" == *.md ]]; then | ||
| discovered_files+=("$entry") | ||
| fi | ||
| done | ||
| } | ||
|
|
||
| sort_discovered_files() { | ||
| local sort_index sort_cursor sort_value | ||
| for ((sort_index = 1; sort_index < ${#discovered_files[@]}; sort_index += 1)); do | ||
| sort_value="${discovered_files[$sort_index]}" | ||
| sort_cursor=$((sort_index - 1)) | ||
| while ((sort_cursor >= 0)) && [[ "${discovered_files[$sort_cursor]}" > "$sort_value" ]]; do | ||
| discovered_files[$((sort_cursor + 1))]="${discovered_files[$sort_cursor]}" | ||
| sort_cursor=$((sort_cursor - 1)) | ||
| done | ||
| discovered_files[$((sort_cursor + 1))]="$sort_value" | ||
| done | ||
| } | ||
|
|
||
| stream_json_files() { | ||
| local dir="$1" | ||
| local json_file line | ||
| for json_file in "$dir"/*.json; do | ||
| [[ -f "$json_file" ]] || continue | ||
| while IFS= read -r line || [[ -n "$line" ]]; do | ||
| printf '%s\n' "$line" | ||
| done < "$json_file" | ||
| done | ||
| } | ||
|
|
||
| # Scan a directory and produce a JSON array of skill objects | ||
|
|
@@ -85,29 +113,49 @@ scan_dir_to_json() { | |
| local obs_7d_counts obs_30d_counts | ||
| obs_7d_counts="" | ||
| obs_30d_counts="" | ||
| # awk counting instead of `sort | uniq -c`: AppLocker-hardened machines shim | ||
| # sort (silent-empty on GNU flags); awk needs no sorted input. | ||
| if [[ -f "$OBSERVATIONS" ]]; then | ||
| obs_7d_counts=$(jq -r --arg c "$c7" \ | ||
| 'select(.tool=="Read" and .timestamp>=$c) | .path' \ | ||
| "$OBSERVATIONS" 2>/dev/null | sort | uniq -c) | ||
| "$OBSERVATIONS" 2>/dev/null | awk '{c[$0]++} END {for (p in c) printf "%d %s\n", c[p], p}') | ||
| obs_30d_counts=$(jq -r --arg c "$c30" \ | ||
| 'select(.tool=="Read" and .timestamp>=$c) | .path' \ | ||
| "$OBSERVATIONS" 2>/dev/null | sort | uniq -c) | ||
| "$OBSERVATIONS" 2>/dev/null | awk '{c[$0]++} END {for (p in c) printf "%d %s\n", c[p], p}') | ||
| fi | ||
|
Comment on lines
118
to
125
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win Report invalid observation data. Lines 119-124 discard Remove 🤖 Prompt for AI AgentsSource: Coding guidelines |
||
|
|
||
| discovered_files=() | ||
| collect_markdown_files "$dir" | ||
| sort_discovered_files | ||
|
|
||
| local i=0 | ||
| while IFS= read -r file; do | ||
| local name desc mtime u7 u30 dp | ||
| local file | ||
| local file_index | ||
| for ((file_index = 0; file_index < ${#discovered_files[@]}; file_index += 1)); do | ||
| file="${discovered_files[$file_index]}" | ||
| local name desc mtime u7 u30 dp fname | ||
| name=$(extract_field "$file" "name") | ||
| desc=$(extract_field "$file" "description") | ||
| mtime=$(date -u -r "$file" +%Y-%m-%dT%H:%M:%SZ) | ||
| # Use awk exact field match to avoid substring false-positives from grep -F. | ||
| # uniq -c output format: " N /path/to/file" — path is always field 2. | ||
| u7=$(echo "$obs_7d_counts" | awk -v f="$file" '$2 == f {print $1}' | head -1) | ||
| # Parse only the leading count and compare the complete remaining path so | ||
| # spaces in directory or skill names remain significant. | ||
| u7=$(printf '%s\n' "$obs_7d_counts" | awk -v f="$file" ' | ||
| match($0, /^[0-9]+ /) && substr($0, RLENGTH + 1) == f { | ||
| print substr($0, 1, RLENGTH - 1) | ||
| exit | ||
| }') | ||
| u7="${u7:-0}" | ||
| u30=$(echo "$obs_30d_counts" | awk -v f="$file" '$2 == f {print $1}' | head -1) | ||
| u30=$(printf '%s\n' "$obs_30d_counts" | awk -v f="$file" ' | ||
| match($0, /^[0-9]+ /) && substr($0, RLENGTH + 1) == f { | ||
| print substr($0, 1, RLENGTH - 1) | ||
| exit | ||
| }') | ||
| u30="${u30:-0}" | ||
| dp="${file/#$HOME/~}" | ||
|
|
||
| # Zero-pad so the later "$tmpdir"/*.json glob keeps insertion order past 9 | ||
| # files (unpadded, 10.json sorts before 2.json lexicographically). | ||
| printf -v fname '%06d.json' "$i" | ||
| jq -n \ | ||
| --arg path "$dp" \ | ||
| --arg name "$name" \ | ||
|
|
@@ -116,15 +164,10 @@ scan_dir_to_json() { | |
| --argjson use_7d "$u7" \ | ||
| --argjson use_30d "$u30" \ | ||
| '{path:$path,name:$name,description:$description,use_7d:$use_7d,use_30d:$use_30d,mtime:$mtime}' \ | ||
| > "$tmpdir/$i.json" | ||
| > "$tmpdir/$fname" | ||
| i=$((i+1)) | ||
| done < <(find "$dir" -name "*.md" -type f 2>/dev/null | sort) | ||
|
|
||
| if [[ $i -eq 0 ]]; then | ||
| echo "[]" | ||
| else | ||
| jq -s '.' "$tmpdir"/*.json | ||
| fi | ||
| done | ||
| stream_json_files "$tmpdir" | jq -s '.' | ||
| } | ||
|
|
||
| # --- Main --- | ||
|
|
@@ -151,20 +194,18 @@ if [[ -n "$CWD_SKILLS_DIR" && -d "$CWD_SKILLS_DIR" ]]; then | |
| project_count=$(echo "$project_skills" | jq 'length') | ||
| fi | ||
|
|
||
| # Merge global + project skills into one array | ||
| all_skills=$(jq -s 'add' <(echo "$global_skills") <(echo "$project_skills")) | ||
|
|
||
| jq -n \ | ||
| # Stream arrays on stdin: this works with native Windows jq and cannot exceed | ||
| # the operating system's command-line argument limit. | ||
| printf '%s\n%s\n' "$global_skills" "$project_skills" | jq -s \ | ||
| --arg global_found "$global_found" \ | ||
| --argjson global_count "$global_count" \ | ||
| --arg project_found "$project_found" \ | ||
| --arg project_path "$project_path" \ | ||
| --argjson project_count "$project_count" \ | ||
| --argjson skills "$all_skills" \ | ||
| '{ | ||
| scan_summary: { | ||
| global: { found: ($global_found == "true"), count: $global_count }, | ||
| project: { found: ($project_found == "true"), path: $project_path, count: $project_count } | ||
| }, | ||
| skills: $skills | ||
| skills: (.[0] + .[1]) | ||
| }' | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔒 Security & Privacy | 🟠 Major | ⚡ Quick win
Exclude symbolic links to files outside the scan directory.
[[ -f "$entry" ]]dereferences symbolic links. A linked Markdown orSKILL.mdfile can resolve outside the supplied scan directory, and the scanners then read and serialize its metadata. Require both-fand! -Lin each file branch.skills/rules-distill/scripts/scan-rules.sh#L30-L31: exclude linked Markdown files before appending tofiles.skills/skill-stocktake/scripts/scan.sh#L66-L67: exclude linked Markdown files before appending todiscovered_files.skills/rules-distill/scripts/scan-skills.sh#L64-L65: exclude linkedSKILL.mdfiles before appending todiscovered_files.As per path instructions, treat
skills/**/scripts/**as untrusted-input tooling and flag path traversal risks.📍 Affects 3 files
skills/rules-distill/scripts/scan-rules.sh#L30-L31(this comment)skills/skill-stocktake/scripts/scan.sh#L66-L67skills/rules-distill/scripts/scan-skills.sh#L64-L65🤖 Prompt for AI Agents
Source: Path instructions