Skip to content

Commit 39c2912

Browse files
authored
Merge pull request #110 from Hypercart-Dev-Tools/fix/add-missed-detection-ajax-unsanitized-post
Codex Audit and Improvements to previous fixes to Development
2 parents 58662b0 + 8701318 commit 39c2912

4 files changed

Lines changed: 361 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,36 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [2.2.4] - 2026-02-07
9+
10+
### Fixed
11+
12+
#### wc-coupon-in-thankyou Validator Not Applied in Main Scanner Flow
13+
14+
- **Issue:** The context-aware validator existed and passed unit tests, but the primary `check-performance.sh` coupon check path still used legacy matching without validator filtering.
15+
- **Impact:** False positives persisted for commented-out hooks and non-thank-you contexts when scanning real projects.
16+
- **Fix:** Wired `dist/bin/validators/wc-coupon-thankyou-context-validator.sh` into the main WooCommerce coupon thank-you check loop in `dist/bin/check-performance.sh`.
17+
- Exit `1` findings are now suppressed as false positives.
18+
- Exit `2` findings are marked with `[NEEDS REVIEW]`.
19+
20+
#### cached_grep Single-File Directory Regression (Paths with Spaces)
21+
22+
- **Issue:** `cached_grep` handled paths with spaces for multi-file directories, but failed for directories containing exactly one PHP file by grepping the cache list file instead of the actual PHP file.
23+
- **Impact:** Missed findings in common local paths with spaces (for one-file plugin/theme repro cases).
24+
- **Fix:** Updated `cached_grep` in `dist/bin/check-performance.sh` to:
25+
- Scan direct file targets via `PATHS` when `--paths` is a file.
26+
- Use null-delimited cached list processing (`tr ... | xargs -0`) for any cached directory scan with one or more PHP files.
27+
28+
### Added
29+
30+
- **Regression checks:** Added scanner-level regression test script:
31+
- `dist/bin/test-fix-audit-regressions.sh`
32+
- Covers:
33+
- checkout hook false positive suppression
34+
- commented hook false positive suppression
35+
- thank-you true positive retention
36+
- one-file and multi-file path-with-spaces unsanitized superglobal detection
37+
838
## [2.2.3] - 2026-02-07
939

1040
### Fixed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# P1 Fix Audit Plan: Validator Integration + Path-with-Spaces Reliability
2+
3+
**Created:** 2026-02-07
4+
**Started:** 2026-02-07
5+
**Updated:** 2026-02-07
6+
**Status:** In Progress
7+
**Priority:** Critical
8+
**Assigned Version:** v2.2.4
9+
10+
## Problem Statement
11+
12+
Two recent fixes are partially implemented and not fully effective in end-to-end scanner execution:
13+
14+
1. `wc-coupon-in-thankyou` context-aware validator is not applied in the primary scan path, so false positives still appear for commented/safe-hook scenarios.
15+
2. `cached_grep` path-with-spaces fix works for multi-file scans but fails when a directory has exactly one PHP file due to cache file path handling.
16+
17+
## Scope
18+
19+
- Fix integration of validated pattern execution for `wc-coupon-in-thankyou`.
20+
- Fix single-file cached path handling in `cached_grep`.
21+
- Add regression tests for both failure modes.
22+
- Update changelog/version metadata for release consistency.
23+
24+
## Non-Goals
25+
26+
- Broad refactor of all legacy hardcoded checks.
27+
- Pattern architecture redesign beyond required wiring for this incident.
28+
- New detection rules unrelated to these two regressions.
29+
30+
## Acceptance Criteria
31+
32+
- [x] Commented-out hook fixture is not flagged by full scanner run.
33+
- [x] Safe checkout hook fixture is not flagged by full scanner run.
34+
- [x] True thank-you hook fixture is flagged by full scanner run.
35+
- [x] Unsanitized superglobal is detected in a path containing spaces with exactly one PHP file.
36+
- [x] Unsanitized superglobal is detected in a path containing spaces with multiple PHP files.
37+
- [x] Existing validator unit tests continue to pass.
38+
- [x] CHANGELOG and scanner version are aligned with delivered fix.
39+
40+
## Implementation Plan
41+
42+
### Phase 1: Validator Integration Repair
43+
44+
- [x] Chosen minimal-risk approach: integrated context-aware validator directly into existing legacy `wc-coupon-in-thankyou` scanner block in `dist/bin/check-performance.sh`.
45+
- [x] Added validator-based suppression handling (exit `1`) and manual-review marking (exit `2`) in main scanner flow.
46+
- [x] Verified false-positive suppression and true-positive retention with full scanner fixture runs.
47+
48+
### Phase 2: cached_grep Single-File Path Fix
49+
50+
- [x] Corrected `cached_grep` single-file handling to scan `PATHS` directly when `--paths` is a file.
51+
- [x] Preserved path-with-spaces safety (`xargs -0`) for cached directory scans (one or more files).
52+
- [x] Verified no regression for single-file directory and multi-file directory scans.
53+
54+
### Phase 3: Regression Test Coverage
55+
56+
- [x] Added scanner-level integration test script: `dist/bin/test-fix-audit-regressions.sh`.
57+
- [x] Added scanner-level regression checks for path-with-spaces scenarios:
58+
- one PHP file
59+
- multiple PHP files
60+
- [x] Kept tests lightweight and executable in local/dev CI contexts.
61+
62+
### Phase 4: Release Hygiene
63+
64+
- [x] Updated `CHANGELOG.md` with concrete fixed behaviors and regression coverage.
65+
- [x] Aligned `dist/bin/check-performance.sh` header version and `SCRIPT_VERSION` SOT value to `2.2.4`.
66+
- [x] Updated this project task document status/progress.
67+
68+
## Testing Matrix
69+
70+
- [x] `bash dist/bin/test-wc-coupon-validator.sh`
71+
- [x] Full scanner run on:
72+
- [x] `dist/bin/fixtures/wc-coupon-thankyou-false-positive-checkout-hook.php`
73+
- [x] `dist/bin/fixtures/wc-coupon-thankyou-false-positive-commented-hook.php`
74+
- [x] `dist/bin/fixtures/wc-coupon-thankyou-true-positive.php`
75+
- [x] Full scanner run on temporary path-with-spaces fixtures:
76+
- [x] one-file unsanitized `$_POST` case
77+
- [x] multi-file unsanitized `$_POST` case
78+
79+
## Risks and Mitigations
80+
81+
- **Risk:** Changing detection routing may affect other JSON patterns.
82+
**Mitigation:** Limit change to existing supported schema path and validate representative scripted patterns.
83+
84+
- **Risk:** Grep helper edits can impact many checks.
85+
**Mitigation:** Keep function change minimal and rerun core critical checks in smoke scans.
86+
87+
- **Risk:** Duplicate results from legacy + JSON execution paths.
88+
**Mitigation:** Ensure single source of execution for `wc-coupon-in-thankyou` and confirm no double counting.
89+
90+
## Deliverables
91+
92+
- Code fixes in scanner + pattern wiring.
93+
- Regression integration test scripts/fixtures updates.
94+
- Changelog and version alignment updates.
95+
- This document updated to completed state after verification.
96+
97+
## Progress
98+
99+
- [x] Audit completed and root causes verified with reproductions.
100+
- [x] Implementation completed.
101+
- [x] Tests passing.
102+
- [x] Ready for release.
103+
104+
## Next Immediate Step
105+
106+
Move this task document to `PROJECT/3-COMPLETED/` after user sign-off.

dist/bin/check-performance.sh

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env bash
22
#
33
# WP Code Check by Hypercart - Performance Analysis Script
4-
# Version: 2.2.3
4+
# Version: 2.2.4
55
#
66
# Fast, zero-dependency WordPress performance analyzer
77
# Catches critical issues before they crash your site
@@ -81,7 +81,7 @@ source "$REPO_ROOT/lib/pattern-loader.sh"
8181
# This is the ONLY place the version number should be defined.
8282
# All other references (logs, JSON, banners) use this variable.
8383
# Update this ONE line when bumping versions - never hardcode elsewhere.
84-
SCRIPT_VERSION="2.2.1"
84+
SCRIPT_VERSION="2.2.4"
8585

8686
# Get the start/end line range for the enclosing function/method.
8787
#
@@ -3347,13 +3347,14 @@ cached_grep() {
33473347
# [ "${DEBUG_CACHED_GREP:-}" = "1" ] && echo "[DEBUG cached_grep] Pattern: $pattern" >&2
33483348
# [ "${DEBUG_CACHED_GREP:-}" = "1" ] && echo "[DEBUG cached_grep] Args: ${grep_args[*]}" >&2
33493349

3350+
# If PATHS points to a single file, scan that file directly.
3351+
if [ -f "$PATHS" ]; then
3352+
grep -Hn "${grep_args[@]}" "$pattern" "$PATHS" 2>/dev/null || true
33503353
# If we have a cached PHP file list, use it; otherwise fall back to
33513354
# recursive grep on the original paths. This lets JS/Node-only repos
33523355
# (no PHP files) still be scanned safely without depending on the
33533356
# PHP_FILE_LIST cache.
3354-
if [ "$PHP_FILE_COUNT" -eq 1 ] && [ -n "$PHP_FILE_LIST" ] && [ -f "$PHP_FILE_LIST" ]; then
3355-
grep -Hn "${grep_args[@]}" "$pattern" "$PHP_FILE_LIST" 2>/dev/null || true
3356-
elif [ "$PHP_FILE_COUNT" -gt 1 ] && [ -n "$PHP_FILE_LIST" ] && [ -f "$PHP_FILE_LIST" ]; then
3357+
elif [ "$PHP_FILE_COUNT" -gt 0 ] && [ -n "$PHP_FILE_LIST" ] && [ -f "$PHP_FILE_LIST" ]; then
33573358
# Use cached file list with xargs for parallel processing
33583359
# -Hn adds filename and line number (like -rHn but without recursion)
33593360
# FIX v2.2.3: Use null-delimited input (tr '\n' '\0') with xargs -0
@@ -5339,6 +5340,13 @@ if [ "$COUPON_THANKYOU_SEVERITY" = "MEDIUM" ] || [ "$COUPON_THANKYOU_SEVERITY" =
53395340

53405341
text_echo "${BLUE}▸ WooCommerce coupon logic in thank-you context ${COUPON_THANKYOU_COLOR}[$COUPON_THANKYOU_SEVERITY]${NC}"
53415342

5343+
COUPON_THANKYOU_VALIDATOR="$REPO_ROOT/bin/validators/wc-coupon-thankyou-context-validator.sh"
5344+
COUPON_THANKYOU_VALIDATOR_AVAILABLE=false
5345+
COUPON_THANKYOU_VALIDATOR_SUPPRESSED=0
5346+
if [ -x "$COUPON_THANKYOU_VALIDATOR" ]; then
5347+
COUPON_THANKYOU_VALIDATOR_AVAILABLE=true
5348+
fi
5349+
53425350
# Step 1: Find files with thank-you/order-received context markers
53435351
THANKYOU_CONTEXT_FILES=$(grep -rlE \
53445352
'(add_action|do_action|apply_filters|add_filter)\([[:space:]]*['\''"]([a-z_]*woocommerce_thankyou[a-z_]*)['\''"]|is_order_received_page\(|is_wc_endpoint_url\([[:space:]]*['\''"]order-received['\''"]|woocommerce/checkout/(thankyou|order-received)\.php' \
@@ -5369,6 +5377,19 @@ if [ -n "$THANKYOU_CONTEXT_FILES" ]; then
53695377
continue
53705378
fi
53715379

5380+
# Apply context-aware validator when available.
5381+
# Exit 0 = confirmed issue, 1 = false positive, 2 = needs review.
5382+
if [ "$COUPON_THANKYOU_VALIDATOR_AVAILABLE" = true ]; then
5383+
validator_exit=0
5384+
"$COUPON_THANKYOU_VALIDATOR" "$file" "$line_num" >/dev/null 2>&1 || validator_exit=$?
5385+
if [ "$validator_exit" -eq 1 ]; then
5386+
((COUPON_THANKYOU_VALIDATOR_SUPPRESSED++)) || true
5387+
continue
5388+
elif [ "$validator_exit" -eq 2 ]; then
5389+
code="[NEEDS REVIEW] $code"
5390+
fi
5391+
fi
5392+
53725393
if ! should_suppress_finding "wc-coupon-in-thankyou" "$file"; then
53735394
COUPON_THANKYOU_ISSUES="${COUPON_THANKYOU_ISSUES}${file}:${line_num}:${code}"$'\n'
53745395
add_json_finding "wc-coupon-in-thankyou" "error" "$COUPON_THANKYOU_SEVERITY" "$file" "$line_num" "Coupon logic in thank-you/order-received context (should be in cart/checkout hooks)" "$code"
@@ -5389,10 +5410,16 @@ if [ "$COUPON_THANKYOU_FINDING_COUNT" -gt 0 ]; then
53895410
fi
53905411
if [ "$OUTPUT_FORMAT" = "text" ]; then
53915412
echo "$COUPON_THANKYOU_ISSUES" | head -5
5413+
if [ "$COUPON_THANKYOU_VALIDATOR_SUPPRESSED" -gt 0 ]; then
5414+
text_echo " ${BLUE} (${COUPON_THANKYOU_VALIDATOR_SUPPRESSED} suppressed by validator)${NC}"
5415+
fi
53925416
fi
53935417
add_json_check "WooCommerce coupon logic in thank-you context" "$COUPON_THANKYOU_SEVERITY" "failed" "$COUPON_THANKYOU_FINDING_COUNT"
53945418
else
53955419
text_echo "${GREEN} ✓ Passed${NC}"
5420+
if [ "$OUTPUT_FORMAT" = "text" ] && [ "$COUPON_THANKYOU_VALIDATOR_SUPPRESSED" -gt 0 ]; then
5421+
text_echo " ${BLUE} (${COUPON_THANKYOU_VALIDATOR_SUPPRESSED} suppressed by validator)${NC}"
5422+
fi
53965423
add_json_check "WooCommerce coupon logic in thank-you context" "$COUPON_THANKYOU_SEVERITY" "passed" 0
53975424
fi
53985425
text_echo ""

0 commit comments

Comments
 (0)