-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpre-commit
More file actions
executable file
·337 lines (284 loc) · 16.1 KB
/
pre-commit
File metadata and controls
executable file
·337 lines (284 loc) · 16.1 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#!/usr/bin/env bash
# Pre-commit hook: secrets + architecture bans + TIMING LEAK DETECTION + platform compliance
# This hook enforces all rules discovered during the Feb 2026 cross-platform audit.
set -euo pipefail
ERRORS=0
WARNINGS=0
err() { echo "❌ $1"; ERRORS=$((ERRORS + 1)); }
warn() { echo "⚠️ $1"; WARNINGS=$((WARNINGS + 1)); }
# ── Secret scanning (gitleaks) ──────────────────────────────────────
if command -v gitleaks > /dev/null 2>&1; then
if ! gitleaks git --pre-commit --staged --no-banner 2>/dev/null; then
echo ""
err "SECRETS DETECTED in staged files! Commit blocked."
echo " Remove secrets and use environment variables or gitignored local files."
exit 1
fi
else
STAGED_DIFF=$(git diff --cached --diff-filter=ACM)
if echo "$STAGED_DIFF" | grep -qEi '(ghp_|gho_|github_pat_|sk-[a-zA-Z0-9]{20,}|AKIA[0-9A-Z]{16}|password\s*=\s*["\x27][^"\x27]{8,})'; then
err "Possible secret detected in staged changes!"
echo " Install gitleaks for better detection: brew install gitleaks"
fi
fi
STAGED_KT=$(git diff --cached --name-only --diff-filter=ACM | grep '\.kt$' || true)
STAGED_SWIFT=$(git diff --cached --name-only --diff-filter=ACM | grep '\.swift$' || true)
STAGED_XML=$(git diff --cached --name-only --diff-filter=ACM | grep '\.xml$' || true)
STAGED_ALL=$(git diff --cached --name-only --diff-filter=ACM || true)
# ═══════════════════════════════════════════════════════════════════
# TIMING LEAK DETECTION — Random timer must NEVER reveal countdown
# ═══════════════════════════════════════════════════════════════════
# ── Android: Chronometer countdown in notifications ────────────────
if [ -n "$STAGED_KT" ]; then
while IFS= read -r file; do
[ -f "$file" ] || continue
# Ban setUsesChronometer / setChronometerCountDown — reveals countdown in notification
if grep -n 'setUsesChronometer\|setChronometerCountDown' "$file" > /dev/null 2>&1; then
err "$file: Uses chronometer countdown — reveals when timer fires"
fi
# Ban passing remainingDuration to UI composables (allowed in data models/services)
if echo "$file" | grep -q '/ui/'; then
if grep -n 'remainingDuration\s*=' "$file" | grep -v '//' > /dev/null 2>&1; then
# Allow data class assignments, block composable parameter passing
if grep -n 'remainingDuration\s*=\s*state\.' "$file" > /dev/null 2>&1; then
err "$file: Passes remainingDuration to UI — timing leak"
fi
fi
fi
# Ban formatDuration import in service/notification files
if echo "$file" | grep -qi 'service\|notification'; then
if grep -n 'import.*formatDuration' "$file" > /dev/null 2>&1; then
err "$file: Imports formatDuration in service — trap for timing leaks"
fi
fi
# Ban MediaSession (stripped in Feb 2026 audit — not for timer apps)
if grep -n 'MediaSession\|MediaMetadata\|PlaybackState\|MediaStyle' "$file" | grep -v '//' > /dev/null 2>&1; then
err "$file: Uses MediaSession — not appropriate for timer apps (use specialUse FGS)"
fi
# Ban WARNING_THRESHOLD / DANGER_THRESHOLD — leaks timing through status
if grep -n 'WARNING_THRESHOLD\|DANGER_THRESHOLD' "$file" > /dev/null 2>&1; then
err "$file: Uses WARNING/DANGER thresholds — status changes leak timing info"
fi
# Ban TimerStatus.WARNING / TimerStatus.DANGER in status determination logic
if grep -n 'TimerStatus\.WARNING\|TimerStatus\.DANGER' "$file" | grep -v 'import\|enum\|//' | grep -v 'WARNING,\|DANGER,' > /dev/null 2>&1; then
# Allow enum definition and imports, block usage in when/if
if grep -n '-> TimerStatus\.WARNING\|-> TimerStatus\.DANGER\|= TimerStatus\.WARNING\|= TimerStatus\.DANGER' "$file" > /dev/null 2>&1; then
err "$file: Sets timer status to WARNING/DANGER — leaks remaining time ranges"
fi
fi
done <<< "$STAGED_KT"
fi
# ── iOS: Timing data in Live Activities ────────────────────────────
if [ -n "$STAGED_SWIFT" ]; then
while IFS= read -r file; do
[ -f "$file" ] || continue
# Ban real endDate/remainingDuration going to Live Activity
if echo "$file" | grep -qi 'liveactivity\|activity'; then
# Must use liveActivityEndDate, not endDate
if grep -n 'state\.endDate' "$file" | grep -v 'staleDate\|liveActivity\|//' > /dev/null 2>&1; then
err "$file: Passes state.endDate to Live Activity — use liveActivityEndDate instead"
fi
# Must use liveActivityRemainingSeconds, not remainingDuration
if grep -n 'state\.remainingDuration' "$file" | grep -v 'liveActivity\|//' > /dev/null 2>&1; then
err "$file: Passes state.remainingDuration to Live Activity — use liveActivityRemainingSeconds"
fi
fi
# Ban remainingDuration as View parameter (allowed in data models)
if echo "$file" | grep -q '/UI/'; then
if grep -n 'let remainingDuration:' "$file" > /dev/null 2>&1; then
err "$file: View accepts remainingDuration parameter — timing leak vector"
fi
fi
# Ban LiveActivityManager.swift (dead code with timing leaks, superseded by LiveActivityService)
if echo "$file" | grep -q 'LiveActivityManager\.swift'; then
err "$file: LiveActivityManager is dead code — use LiveActivityService (sanitized)"
fi
# Ban calculateProgress based on real time
if grep -n 'calculateProgress\|targetDuration.*endDate' "$file" | grep -v '//' > /dev/null 2>&1; then
if echo "$file" | grep -qi 'widget\|liveactivity\|activity'; then
err "$file: Calculates real progress in widget — use decorative/random animation"
fi
fi
done <<< "$STAGED_SWIFT"
fi
# ── Both platforms: Dead countdown layouts ─────────────────────────
if [ -n "$STAGED_XML" ]; then
while IFS= read -r file; do
[ -f "$file" ] || continue
if echo "$file" | grep -q 'notification.*timer\|timer.*notification'; then
if grep -q 'notification_time\|notification_progress\|android:text="00:00"' "$file" > /dev/null 2>&1; then
err "$file: Contains countdown display elements — delete this dead layout"
fi
fi
done <<< "$STAGED_XML"
fi
# ═══════════════════════════════════════════════════════════════════
# ARCHITECTURE BAN CHECKS
# ═══════════════════════════════════════════════════════════════════
# ── Kotlin ─────────────────────────────────────────────────────────
if [ -n "$STAGED_KT" ]; then
while IFS= read -r file; do
[ -f "$file" ] || continue
# Ban AndroidViewModel
if grep -n 'AndroidViewModel' "$file" | grep -v '// allow-android-viewmodel' > /dev/null 2>&1; then
err "$file: Uses AndroidViewModel — migrate to ViewModel with injected dependencies"
fi
# Ban collectAsState()
if grep -n '\.collectAsState()' "$file" > /dev/null 2>&1; then
err "$file: Uses .collectAsState() — use .collectAsStateWithLifecycle() instead"
fi
# Ban Handler(Looper
if grep -n 'Handler(Looper' "$file" | grep -v '// allow-handler' > /dev/null 2>&1; then
err "$file: Uses Handler(Looper) — use coroutines with delay() instead"
fi
# Ban MediaPlayer.create (must use AudioAttributes)
if grep -n 'MediaPlayer\.create(' "$file" > /dev/null 2>&1; then
err "$file: Uses MediaPlayer.create() — construct manually and set AudioAttributes USAGE_ALARM"
fi
# Ban wrong audio focus type
if grep -n 'AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK' "$file" > /dev/null 2>&1; then
err "$file: Uses AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK — use AUDIOFOCUS_GAIN_TRANSIENT for alarms"
fi
done <<< "$STAGED_KT"
if command -v trunk > /dev/null 2>&1; then
echo "$STAGED_KT" | xargs trunk check --no-progress --filter=ktlint 2>/dev/null || ERRORS=$((ERRORS + 1))
fi
fi
# ── Swift ──────────────────────────────────────────────────────────
if [ -n "$STAGED_SWIFT" ]; then
while IFS= read -r file; do
[ -f "$file" ] || continue
# Ban print() in production code
if echo "$file" | grep -qvE '(Tests|Preview)'; then
if grep -n 'print(' "$file" | grep -v '// allow-print' > /dev/null 2>&1; then
warn "$file: Uses print() — prefer Logger from os.log"
fi
fi
# Ban deprecated vibration APIs
if grep -n 'AudioServicesPlaySystemSound' "$file" > /dev/null 2>&1; then
err "$file: Uses AudioServicesPlaySystemSound — use Core Haptics (CHHapticEngine)"
fi
if grep -n 'kSystemSoundID_Vibrate' "$file" > /dev/null 2>&1; then
err "$file: Uses kSystemSoundID_Vibrate — use UIFeedbackGenerator or Core Haptics"
fi
done <<< "$STAGED_SWIFT"
if command -v swiftlint > /dev/null 2>&1; then
LINT_OUTPUT=$(echo "$STAGED_SWIFT" | xargs swiftlint lint --strict --quiet 2>&1) || true
if echo "$LINT_OUTPUT" | grep -q 'Fatal error\|sourcekitd\|terminated with signal'; then
warn "swiftlint crashed (SourceKit issue) — skipping lint"
elif [ -n "$LINT_OUTPUT" ]; then
echo "$LINT_OUTPUT"
ERRORS=$((ERRORS + 1))
fi
fi
fi
# ═══════════════════════════════════════════════════════════════════
# UI STATE BINDING AUDIT — Prevent hardcoded values masking real state
# ═══════════════════════════════════════════════════════════════════
if [ -n "$STAGED_KT" ]; then
while IFS= read -r file; do
[ -f "$file" ] || continue
# Only check Screen composables (where state is bound to components)
if echo "$file" | grep -qi 'Screen'; then
# Ban hardcoded progress values in CircularTimer calls
if grep -Pn 'progress\s*=\s*(if\s*\(|0f|1f)' "$file" 2>/dev/null | grep -v '// allow-hardcoded-progress' > /dev/null 2>&1; then
err "$file: Hardcoded progress in CircularTimer — use state.progress"
fi
# Warn if CircularTimer is called but state.progress is never referenced
if grep -q 'CircularTimer(' "$file" 2>/dev/null; then
if ! grep -q 'state\.progress' "$file" 2>/dev/null; then
warn "$file: CircularTimer called but state.progress not referenced — verify binding"
fi
fi
fi
done <<< "$STAGED_KT"
fi
# ═══════════════════════════════════════════════════════════════════
# ACCESSIBILITY CHECKS
# ═══════════════════════════════════════════════════════════════════
# Android: CircularTimer must have clearAndSetSemantics
if [ -n "$STAGED_KT" ]; then
while IFS= read -r file; do
[ -f "$file" ] || continue
if echo "$file" | grep -q 'CircularTimer'; then
if grep -q 'fun CircularTimer' "$file" > /dev/null 2>&1; then
if ! grep -q 'clearAndSetSemantics' "$file" > /dev/null 2>&1; then
err "$file: CircularTimer missing clearAndSetSemantics — TalkBack may read timing data"
fi
fi
fi
done <<< "$STAGED_KT"
fi
# iOS: Files calling CircularTimerView must add accessibility overrides
if [ -n "$STAGED_SWIFT" ]; then
while IFS= read -r file; do
[ -f "$file" ] || continue
# Only check callers (Screen files), not the component definition itself
if echo "$file" | grep -qi 'Screen\|View' && ! echo "$file" | grep -q 'CircularTimerView\.swift'; then
if grep -q 'CircularTimerView(' "$file" > /dev/null 2>&1; then
if ! grep -q 'accessibilityElement\|accessibilityLabel' "$file" > /dev/null 2>&1; then
warn "$file: CircularTimerView used without accessibility overrides — VoiceOver may read timing data"
fi
fi
fi
done <<< "$STAGED_SWIFT"
fi
# ═══════════════════════════════════════════════════════════════════
# BRANDING CONSISTENCY
# ═══════════════════════════════════════════════════════════════════
for file in $STAGED_SWIFT $STAGED_KT; do
if [ -n "$file" ] && [ -f "$file" ]; then
if grep -n '"Random Timer"' "$file" 2>/dev/null | grep -qv 'test\|Test'; then
warn "$file: Contains 'Random Timer' — should be 'Random Tactical Timer'"
fi
fi
done
# ═══════════════════════════════════════════════════════════════════
# FEATURE PARITY CHECKS
# ═══════════════════════════════════════════════════════════════════
# If alarm notification is modified, ensure both Silence AND Dismiss actions exist
if [ -n "$STAGED_KT" ]; then
while IFS= read -r file; do
[ -f "$file" ] || continue
if grep -q 'createAlarmNotification' "$file" > /dev/null 2>&1; then
if ! grep -q 'ACTION_SILENCE_ALARM\|Silence\|createSilenceIntent' "$file" > /dev/null 2>&1; then
err "$file: Alarm notification missing Silence action — must match iOS parity"
fi
fi
done <<< "$STAGED_KT"
fi
# If iOS notification actions are modified, ensure Silence action exists
if [ -n "$STAGED_SWIFT" ]; then
while IFS= read -r file; do
[ -f "$file" ] || continue
if grep -q 'UNNotificationCategory' "$file" > /dev/null 2>&1; then
if ! grep -q 'SILENCE_ACTION\|silenceAction' "$file" > /dev/null 2>&1; then
err "$file: Notification category missing Silence action"
fi
fi
done <<< "$STAGED_SWIFT"
fi
# ═══════════════════════════════════════════════════════════════════
# REGRESSION GUARDS — Voice + Play release workflow truth
# ═══════════════════════════════════════════════════════════════════
if [ -n "$STAGED_ALL" ]; then
if command -v python3 > /dev/null 2>&1; then
if ! python3 scripts/regression_guards.py --mode staged; then
err "Regression guard failed — fix voice/store workflow regressions before committing"
fi
else
err "python3 is required to run scripts/regression_guards.py"
fi
fi
# ═══════════════════════════════════════════════════════════════════
# RESULT
# ═══════════════════════════════════════════════════════════════════
if [ "$ERRORS" -gt 0 ]; then
echo ""
echo "Pre-commit: $ERRORS error(s), $WARNINGS warning(s). Fix errors before committing."
echo "To bypass (emergency only): git commit --no-verify"
exit 1
fi
if [ "$WARNINGS" -gt 0 ]; then
echo "Pre-commit: $WARNINGS warning(s) (non-blocking)."
fi