From c74c33fc9cff7d6d3d981f4272cca9198438516d Mon Sep 17 00:00:00 2001 From: Ostap Brehin Date: Sun, 6 Sep 2026 18:19:00 +0100 Subject: [PATCH] feat(http): validate session identifiers --- .../http/src/Session/InvalidSessionId.php | 17 +++++++ packages/http/src/Session/SessionId.php | 12 ++++- packages/http/tests/Session/SessionIdTest.php | 50 +++++++++++++++++++ 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 packages/http/src/Session/InvalidSessionId.php create mode 100644 packages/http/tests/Session/SessionIdTest.php diff --git a/packages/http/src/Session/InvalidSessionId.php b/packages/http/src/Session/InvalidSessionId.php new file mode 100644 index 0000000000..8b1c519ca5 --- /dev/null +++ b/packages/http/src/Session/InvalidSessionId.php @@ -0,0 +1,17 @@ +assertSame($id, (string) new SessionId($id)); + } + + #[Test] + #[TestWith(['../../../../etc/passwd'])] + #[TestWith(['../../tmp/marker'])] + #[TestWith(['foo/bar'])] + #[TestWith(['with space'])] + #[TestWith(['with.dot'])] + #[TestWith(["null\0byte"])] + #[TestWith(["trailing-newline\n"])] + #[TestWith([''])] + public function rejects_traversal_and_unsafe_identifiers(string $id): void + { + $this->expectException(InvalidSessionId::class); + + new SessionId($id); + } + + #[Test] + public function rejects_overly_long_identifiers(): void + { + $this->expectException(InvalidSessionId::class); + + new SessionId(str_repeat('a', 129)); + } +}