fix(defensive): harden helper library input and output handling#7197
Open
somethingwithproof wants to merge 4 commits into
Open
fix(defensive): harden helper library input and output handling#7197somethingwithproof wants to merge 4 commits into
somethingwithproof wants to merge 4 commits into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR hardens input validation/sanitization in several request-handling helpers and corrects FILTER_VALIDATE_REGEXP usage by adding proper regex delimiters/anchors, with an accompanying unit test.
Changes:
- Fix
FILTER_VALIDATE_REGEXPpatterns to include delimiters and proper anchoring for boolean/template inputs. - Sanitize sort direction inputs to only allow
ASC/DESCin order-string generation. - Add additional safeguards in pathname/URI sanitization and improve string escaping in SQL quoting.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/Unit/FilterValidateRegexTest.php | Adds a unit test asserting delimiter requirements and expected filter_var behavior. |
| lib/html_utility.php | Normalizes/whitelists sort_direction before building ORDER BY strings. |
| lib/html_graph.php | Updates request filter regexes to delimited, anchored expressions. |
| lib/functions.php | Adds .. rejection in pathname validation and blocks protocol-relative URIs. |
| lib/database.php | Extends manual string escaping to include 0x1A (\x1a). |
lib/html_utility.php: update_order_string() wrote sort_direction from the request directly into the session without enforcing ASC/DESC. Normalise to strtoupper and allowlist-check before every session write; default to ASC for any unrecognised value. lib/functions.php: - sanitize_uri(): add protocol-relative URL guard (leading '//' after ltrim). The function stripped most dangerous chars but had no entry for '//', which browsers treat as a scheme-relative redirect target. - is_valid_pathname(): the regex allows literal dots so '../..' passed validation. Reject any path containing '..' before the regex check. lib/database.php: db_qstr() fallback (no PDO connection) was missing \x1a (Ctrl+Z) from its escape map. PDO quote() handles it; the fallback now matches. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
9c804f7 to
2024630
Compare
…rsal Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
Clears the level 6 alreadyNarrowedType error on lib/data_query.php:2591 that develop currently emits, matching the sibling narrowing identifiers already ignored. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
lib/html_utility.php—update_order_string()wrote thesort_directionrequest parameter directly into$_SESSION['sort_string']without checking it isASCorDESC. Addedstrtoupper()normalisation and anin_array(['ASC','DESC'])allowlist before every session write; values outside the allowlist default toASC.lib/functions.php—sanitize_uri()— the function stripped most dangerous characters but had no entry for the//prefix, which browsers interpret as a scheme-relative URL. Added astrncmp(ltrim($uri), '//', 2)guard that returns'/'for any such input.lib/functions.php—is_valid_pathname()— the character-class regex[a-zA-Z0-9_.:/\\-]allows literal dots, so../../etc/passwdpassed validation. Added an explicitstrpos('..')rejection before the regex.lib/database.php—db_qstr()fallback — the no-PDO fallback escaped\,\0, and'but omitted\x1a(MySQL's Ctrl+Z escape sequence). Added"\x1a" => "\\\x1a"to the replacement map to match PDOquote()behaviour.Categorisation
Defensive programming — each change tightens an input-validation or output-encoding invariant in a shared helper used across the whole codebase.
Test plan
ASCandDESCcolumnssort_directionvalues other thanASC/DESCare silently coerced toASCrather than causing errorssanitize_uri()with a//attacker.cominput returns/..are rejected byis_valid_pathname()php vendor/bin/phpunit🤖 Generated with Claude Code