-
Notifications
You must be signed in to change notification settings - Fork 19
114 lines (94 loc) · 4.2 KB
/
sanity-check.yml
File metadata and controls
114 lines (94 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
name: PR Sanity Check
on:
pull_request:
types: [opened, synchronize, reopened]
permissions:
pull-requests: write
contents: read
jobs:
sanity-check:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Fetch base branch
run: git fetch origin ${{ github.base_ref }}
- name: Block disallowed files by extension
run: |
echo "Checking for disallowed file extensions..."
CHANGED_FILES=$(git diff --name-only origin/${{ github.base_ref }}...HEAD)
# Block by extension
BLOCKED=$(echo "$CHANGED_FILES" | grep -E '\.(exe|dll|out|ezdb|db|sqlite|sqlite3)$' || true)
if [ -n "$BLOCKED" ]; then
echo "::error::Disallowed file extensions detected: $BLOCKED"
exit 1
fi
# Block IDE/OS junk folders
JUNK=$(echo "$CHANGED_FILES" | grep -E '^(\.idea/|\.vs/|__pycache__/)' || true)
if [ -n "$JUNK" ]; then
echo "::error::IDE/OS junk detected: $JUNK"
exit 1
fi
# Block OS junk files
OS_JUNK=$(echo "$CHANGED_FILES" | grep -E '(Thumbs\.db|Desktop\.ini|\.DS_Store)$' || true)
if [ -n "$OS_JUNK" ]; then
echo "::error::OS junk files detected: $OS_JUNK"
exit 1
fi
echo "No disallowed file extensions detected."
- name: Block meta/config files from other ecosystems
run: |
echo "Checking for ecosystem files..."
CHANGED_FILES=$(git diff --name-only origin/${{ github.base_ref }}...HEAD)
# Block meta/config files from other language ecosystems
SUSPICIOUS=$(echo "$CHANGED_FILES" | grep -E '^(package\.json|package-lock\.json|yarn\.lock|pnpm-lock\.yaml|bun\.lockb|bunfig\.toml|deno\.json|deno\.jsonc|tsconfig\.json|jsconfig\.json|requirements\.txt|Pipfile|Gemfile|Cargo\.toml|Cargo\.lock)$|node_modules/' || true)
if [ -n "$SUSPICIOUS" ]; then
echo "::error::Ecosystem config files detected: $SUSPICIOUS"
exit 1
fi
echo "No suspicious ecosystem files detected."
- name: Reject binary files except images
run: |
echo "Checking for binary files..."
CHANGED_FILES=$(git diff --name-only origin/${{ github.base_ref }}...HEAD)
for file in $CHANGED_FILES; do
if [ -f "$file" ]; then
MIME=$(file --mime-type -b "$file")
# Allow common image formats
if [[ "$MIME" =~ ^image/(jpeg|png|gif|svg\+xml|webp)$ ]]; then
echo "Allowed image file: $file ($MIME)"
continue
fi
# Allow shell scripts (they report as "executable" but are text files)
if [[ "$file" =~ \.sh$ ]]; then
echo "Allowed shell script: $file"
continue
fi
# Allow WebAssembly binaries
if [[ "$file" =~ \.wasm$ ]]; then
echo "Allowed WASM file: $file"
continue
fi
# Reject executables and archives (use -b to exclude filename from output)
if file -b "$file" | grep -qE "executable|binary|archive|compressed"; then
echo "::error::Binary/executable file detected: $file"
exit 1
fi
# Also check charset=binary for other binary types (but not images)
if file -b --mime "$file" | grep -q "charset=binary" && [[ ! "$MIME" =~ ^image/ ]]; then
echo "::error::Binary file detected: $file ($MIME)"
exit 1
fi
fi
done
echo "No disallowed binary files detected."
- name: Close PR if checks fail
if: failure()
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh pr close ${{ github.event.pull_request.number }} \
--repo ${{ github.repository }} \
--comment "This PR has been automatically closed because it contains disallowed files (binaries, executables, archives, or meta/config files from other ecosystems). Please remove these files and open a new PR."