Repository navigation
Refactor NoSQL seeding and align level vulnerability types #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
135 changes: 135 additions & 0 deletions
135
src/NoSQLInjectionVulnerability/NoSQLInjectionSeeder.php
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| <?php | ||
| declare(strict_types=1); | ||
| namespace nosqlinjection; | ||
|
|
||
| use MongoDB\BSON\ObjectId; | ||
| use MongoDB\Driver\BulkWrite; | ||
| use MongoDB\Driver\Command; | ||
| use MongoDB\Driver\Exception\Exception as MongoException; | ||
| use MongoDB\Driver\Query; | ||
| use mongo\MongoConnection; | ||
|
|
||
| require_once __DIR__ . "/../Mongo/MongoConnection.php"; | ||
|
|
||
| class NoSQLInjectionSeeder | ||
| { | ||
| private const MARKER = "nosql_injection_users"; | ||
| private const MAX_RETRIES = 10; | ||
| private const RETRY_DELAY_MICROSECONDS = 500000; | ||
|
|
||
| private static $seeded = false; | ||
|
|
||
| public static function seedIfRequired(): void | ||
| { | ||
| if (self::$seeded) { | ||
| return; | ||
| } | ||
|
|
||
| $connection = MongoConnection::instance(); | ||
|
|
||
| for ($attempt = 1; $attempt <= self::MAX_RETRIES; $attempt++) { | ||
| try { | ||
| self::ensureIndexes($connection); | ||
|
|
||
| $query = new Query(["seedMarker" => self::MARKER], ["limit" => 1]); | ||
| $cursor = $connection->manager()->executeQuery($connection->namespace("seed_metadata"), $query); | ||
| if (count($cursor->toArray()) > 0) { | ||
| self::$seeded = true; | ||
| return; | ||
| } | ||
|
|
||
| $users = self::buildSeedUsers(); | ||
| $bulkWrite = new BulkWrite(); | ||
| foreach ($users as $user) { | ||
| $bulkWrite->update( | ||
| ["level" => $user["level"], "username" => $user["username"]], | ||
| ['$set' => $user], | ||
| ["upsert" => true] | ||
| ); | ||
| } | ||
| $connection->manager()->executeBulkWrite($connection->namespace("users"), $bulkWrite); | ||
|
|
||
| $metadata = new BulkWrite(); | ||
| $metadata->update( | ||
| ["seedMarker" => self::MARKER], | ||
| [ | ||
| '$set' => [ | ||
| "seedMarker" => self::MARKER, | ||
| "createdAt" => time(), | ||
| ], | ||
| '$setOnInsert' => [ | ||
| "_id" => new ObjectId(), | ||
| ], | ||
| ], | ||
| ["upsert" => true] | ||
| ); | ||
| $connection->manager()->executeBulkWrite($connection->namespace("seed_metadata"), $metadata); | ||
| self::$seeded = true; | ||
| return; | ||
| } catch (MongoException $exception) { | ||
| if ($attempt === self::MAX_RETRIES) { | ||
| throw new \RuntimeException("MongoDB seed failed: " . $exception->getMessage(), 0, $exception); | ||
| } | ||
| usleep(self::RETRY_DELAY_MICROSECONDS); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static function ensureIndexes(MongoConnection $connection): void | ||
| { | ||
| $usersIndexCommand = new Command([ | ||
| "createIndexes" => "users", | ||
| "indexes" => [[ | ||
| "name" => "level_username_unique", | ||
| "key" => ["level" => 1, "username" => 1], | ||
| "unique" => true, | ||
| ]], | ||
| ]); | ||
| $connection->manager()->executeCommand($connection->database(), $usersIndexCommand); | ||
|
|
||
| $seedMetadataIndexCommand = new Command([ | ||
| "createIndexes" => "seed_metadata", | ||
| "indexes" => [[ | ||
| "name" => "seed_marker_unique", | ||
| "key" => ["seedMarker" => 1], | ||
| "unique" => true, | ||
| ]], | ||
| ]); | ||
| $connection->manager()->executeCommand($connection->database(), $seedMetadataIndexCommand); | ||
| } | ||
|
|
||
| private static function randomPassword(int $length = 20): string | ||
| { | ||
| $bytes = random_bytes((int) ceil($length / 2)); | ||
| return substr(bin2hex($bytes), 0, $length); | ||
| } | ||
|
|
||
| private static function buildSeedUsers(): array | ||
| { | ||
| $users = []; | ||
| for ($level = 1; $level <= 5; $level++) { | ||
| $password = self::randomPassword(); | ||
| $users[] = [ | ||
| "_id" => new ObjectId(), | ||
| "level" => "LEVEL_" . $level, | ||
| "username" => "level" . $level . "_user", | ||
| "password" => $password, | ||
| "passwordHash" => password_hash($password, PASSWORD_DEFAULT), | ||
| "role" => "user", | ||
| ]; | ||
|
|
||
| $adminPassword = self::randomPassword(); | ||
| $users[] = [ | ||
| "_id" => new ObjectId(), | ||
| "level" => "LEVEL_" . $level, | ||
| "username" => "level" . $level . "_admin", | ||
| "password" => $adminPassword, | ||
| "passwordHash" => password_hash($adminPassword, PASSWORD_DEFAULT), | ||
| "role" => "admin", | ||
| ]; | ||
| } | ||
|
|
||
| return $users; | ||
| } | ||
| } | ||
| ?> |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
Do not make global bootstrap depend on MongoDB seeding.
seedIfRequired()performs MongoDB operations and throwsRuntimeExceptionon failure. This call runs before any providers or routes are registered.A transient MongoDB failure therefore prevents
Bootstrapfrom initializing, including for vulnerability routes that do not use MongoDB. Invoke the seeder from the NoSQL injection request path, or isolate its failure from unrelated routes.🤖 Prompt for AI Agents