Skip to content

[codex] Normalize null request paths during routing#232

Merged
ChiragAgg5k merged 1 commit into0.34.xfrom
codex/fix-null-path-routing-0-34
Apr 7, 2026
Merged

[codex] Normalize null request paths during routing#232
ChiragAgg5k merged 1 commit into0.34.xfrom
codex/fix-null-path-routing-0-34

Conversation

@ChiragAgg5k
Copy link
Copy Markdown
Member

What changed

  • Normalize route-matching path extraction so malformed or pathless request URIs fall back to / instead of passing null into Router::match().
  • Add regression coverage for both direct route matching and wildcard fallback when parse_url(..., PHP_URL_PATH) does not return a string.

Why

Edge runs on Swoole workers, and malformed HTTP requests can arrive with a request URI whose parsed path is null.

Before this change, Http::match() and wildcard fallback passed the raw result of parse_url($request->getURI(), PHP_URL_PATH) directly into Router::match() / $route->path(...). When parse_url() returned null, PHP raised a TypeError because the router expects a string path.

That uncaught fatal tears down the active Swoole worker process. In edge this shows up as worker exit code 255, pod restarts, and repeated crashes under malformed traffic.

This change makes the framework normalize those cases to /, so malformed or pathless URIs degrade to a normal root-path route lookup instead of crashing the worker.

Impact

  • Prevents malformed requests from killing Swoole workers.
  • Keeps the fix at the library boundary so downstream consumers like edge do not need to patch every app individually.

Validation

  • vendor/bin/phpunit tests/HttpTest.php --filter "testCanMatchRootRouteWhenUriHasNoPath|testWildcardRouteWhenUriHasNoPath"
  • vendor/bin/pint --test src/Http/Http.php tests/HttpTest.php

@ChiragAgg5k ChiragAgg5k marked this pull request as ready for review April 7, 2026 04:12
@ChiragAgg5k ChiragAgg5k requested a review from loks0n April 7, 2026 04:12
@greptile-apps
Copy link
Copy Markdown

greptile-apps bot commented Apr 7, 2026

Greptile Summary

This PR adds null-safety normalization around parse_url($request->getURI(), PHP_URL_PATH) in both Http::match() and the wildcard fallback inside Http::run(). When a malformed or path-less URI causes parse_url to return null, false, or an empty string, the result is now coerced to '/', preventing a TypeError that would otherwise crash Swoole worker processes (exit code 255) under malformed traffic.

  • src/Http/Http.php: Two two-line guards added — in match() (lines 680–681) and in the wildcard section of run() (lines 948–949) — using the same is_string + empty-string check pattern. The normalization correctly handles null (no path component), false (seriously malformed URI), and '' (empty path), all falling back to '/'.
  • tests/HttpTest.php: testCanMatchRootRouteWhenUriHasNoPath and testWildcardRouteWhenUriHasNoPath provide direct regression coverage using the URI 'https://example.com?x=1' (no path component), verifying both the match() path and the wildcard execution path.
  • Minor: the two-line normalization idiom is duplicated across two call sites; a private helper method would keep future changes DRY.

Confidence Score: 5/5

Safe to merge — targeted, correct fix with regression tests; only a minor DRY observation remains

No P0 or P1 issues. The normalization logic handles all edge cases correctly (null, false, empty string, normal string paths). Both changed code paths have new regression tests. The only remaining item is a P2 DRY suggestion about duplicated logic, which does not affect correctness or reliability.

No files require special attention

Important Files Changed

Filename Overview
src/Http/Http.php Adds null/false/empty-string guard on parse_url result in both match() and wildcard fallback; normalization is correct but the two-line idiom is duplicated
tests/HttpTest.php Adds testCanMatchRootRouteWhenUriHasNoPath and testWildcardRouteWhenUriHasNoPath regression tests covering both affected code paths

Reviews (1): Last reviewed commit: "Normalize null request paths during rout..." | Re-trigger Greptile

@ChiragAgg5k ChiragAgg5k merged commit c8e7e8f into 0.34.x Apr 7, 2026
5 checks passed
@ChiragAgg5k ChiragAgg5k deleted the codex/fix-null-path-routing-0-34 branch April 7, 2026 08:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants