From 368b4bf6a61524360bf9abcc5b2c7a8694dd6b4f Mon Sep 17 00:00:00 2001 From: robert dennis <31261583+robertdrakedennis@users.noreply.github.com> Date: Tue, 6 Oct 2026 13:59:43 -0400 Subject: [PATCH] regex fixes --- app/Http/Requests/Base/LocaleRequest.php | 6 +++--- .../Extensions/ExtensionJobProgress.php | 2 +- app/Services/Extensions/ExtensionManifest.php | 8 ++++---- .../Extensions/ExtensionManifestValidator.php | 4 ++-- .../ExtensionPermissionRegistry.php | 4 ++-- .../Controllers/Base/LocaleControllerTest.php | 11 +++++++++++ .../Extensions/ExtensionJobProgressTest.php | 2 ++ .../Extensions/ExtensionManifestTest.php | 19 +++++++++++++++++-- .../ExtensionPermissionRegistryTest.php | 1 + 9 files changed, 43 insertions(+), 14 deletions(-) diff --git a/app/Http/Requests/Base/LocaleRequest.php b/app/Http/Requests/Base/LocaleRequest.php index dcac96828..dd780ed54 100644 --- a/app/Http/Requests/Base/LocaleRequest.php +++ b/app/Http/Requests/Base/LocaleRequest.php @@ -14,10 +14,10 @@ class LocaleRequest extends FormRequest public function rules(): array { return [ - 'locale' => ['required', 'string', 'regex:/^[a-z][a-z]$/'], - 'namespace' => ['required', 'string', 'regex:/^(?:[a-z]{1,191}|ext-[a-z][a-z0-9-]{0,47}::[a-z][a-z0-9_-]{0,63})$/'], + 'locale' => ['required', 'string', 'regex:/^[a-z][a-z]$/D'], + 'namespace' => ['required', 'string', 'regex:/^(?:[a-z]{1,191}|ext-[a-z][a-z0-9-]{0,47}::[a-z][a-z0-9_-]{0,63})$/D'], // The revision of an extension's translations, which only makes the URL unique. - 'revision' => ['sometimes', 'string', 'regex:/^[a-f0-9]{32}$/'], + 'revision' => ['sometimes', 'string', 'regex:/^[a-f0-9]{32}$/D'], ]; } } diff --git a/app/Services/Extensions/ExtensionJobProgress.php b/app/Services/Extensions/ExtensionJobProgress.php index 2f4fa4bef..d2839c8c3 100644 --- a/app/Services/Extensions/ExtensionJobProgress.php +++ b/app/Services/Extensions/ExtensionJobProgress.php @@ -18,7 +18,7 @@ final class ExtensionJobProgress public function begin(string $extension, User $user, ?Server $server = null, ?string $permission = null): ExtensionJobSnapshot { throw_unless(preg_match(ExtensionManifest::ID_REGEX, $extension), InvalidArgumentException::class, 'Invalid extension identifier.'); - throw_if($permission !== null && (! $server instanceof Server || ! preg_match('/'.ExtensionPermissionRegistry::PERMISSION_PATTERN.'/', $permission) || ! str_starts_with($permission, 'ext.'.$extension.'.')), InvalidArgumentException::class, 'Progress permissions must belong to the extension and require a server.'); + throw_if($permission !== null && (! $server instanceof Server || ! preg_match('/'.ExtensionPermissionRegistry::PERMISSION_PATTERN.'/D', $permission) || ! str_starts_with($permission, 'ext.'.$extension.'.')), InvalidArgumentException::class, 'Progress permissions must belong to the extension and require a server.'); $snapshot = new ExtensionJobSnapshot(Str::uuid()->toString(), $extension, $user->uuid, $server?->uuid, $permission, 'running', 0, '', 1, now()->toIso8601String()); $this->store($snapshot); diff --git a/app/Services/Extensions/ExtensionManifest.php b/app/Services/Extensions/ExtensionManifest.php index 27252c56b..94f4aa0fc 100644 --- a/app/Services/Extensions/ExtensionManifest.php +++ b/app/Services/Extensions/ExtensionManifest.php @@ -20,11 +20,11 @@ class ExtensionManifest 'server.files.manager', ]; - /** Lowercase slug: starts with a letter, then letters/digits/hyphens, max 48. */ - public const string ID_REGEX = '/^[a-z][a-z0-9-]{0,47}$/'; + /** Lowercase slug: starts with a letter, then letters/digits/hyphens, max 48, with no trailing newline. */ + public const string ID_REGEX = '/^[a-z][a-z0-9-]{0,47}$/D'; /** A lucide icon name (kebab-case), such as "life-buoy" or "grid-2x2". */ - public const string ICON_REGEX = '/^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/'; + public const string ICON_REGEX = '/^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/D'; /** Identifiers that would collide with core routes/assets. */ public const array RESERVED_IDS = ['pterodactyl', 'panel', 'core']; @@ -40,7 +40,7 @@ class ExtensionManifest public const array RESERVED_NAMESPACES = ['Pterodactyl\\', 'Illuminate\\', 'Laravel\\', 'Symfony\\']; /** Lowercase slug a top-level URL prefix claimed under `routes.root` must match. */ - public const string ROOT_PREFIX_REGEX = '/^[a-z][a-z0-9-]{0,47}$/'; + public const string ROOT_PREFIX_REGEX = '/^[a-z][a-z0-9-]{0,47}$/D'; /** * Top-level URL segments an extension can never claim: the SPA's own client-side diff --git a/app/Services/Extensions/ExtensionManifestValidator.php b/app/Services/Extensions/ExtensionManifestValidator.php index a8a9c7121..543b00341 100644 --- a/app/Services/Extensions/ExtensionManifestValidator.php +++ b/app/Services/Extensions/ExtensionManifestValidator.php @@ -116,9 +116,9 @@ class ExtensionManifestValidator 'ui.components.*' => ['required', 'string', 'distinct:strict', Rule::in(ExtensionManifest::COMPONENT_NAMES)], 'ui.screens' => ['sometimes', 'array', 'list', 'max:64'], 'ui.screens.*' => ['required', 'array:id,area,path,nav,permission,parent,when'], - 'ui.screens.*.id' => ['required', 'string', 'max:48', 'regex:/^[a-z][a-z0-9-]*$/', 'distinct:strict'], + 'ui.screens.*.id' => ['required', 'string', 'max:48', 'regex:/^[a-z][a-z0-9-]*$/D', 'distinct:strict'], 'ui.screens.*.area' => ['required', 'string', 'in:account,server,admin'], - 'ui.screens.*.path' => ['required', 'string', 'max:255', 'regex:~^[a-z][a-z0-9-]*(?:/(?:[a-z][a-z0-9-]*|\\$[a-zA-Z][a-zA-Z0-9_]*))*/?$~'], + 'ui.screens.*.path' => ['required', 'string', 'max:255', 'regex:~^[a-z][a-z0-9-]*(?:/(?:[a-z][a-z0-9-]*|\\$[a-zA-Z][a-zA-Z0-9_]*))*/?$~D'], 'ui.screens.*.parent' => ['sometimes', 'string', 'prohibited_unless:ui.screens.*.area,admin', 'in:admin.node,admin.server,admin.egg,admin.user'], 'ui.screens.*.nav' => ['sometimes', 'array:label,exact,params,order,group,badge,icon', 'required_array_keys:label'], 'ui.screens.*.nav.label' => ['required_with:ui.screens.*.nav', 'string', 'max:100'], diff --git a/app/Services/Extensions/ExtensionPermissionRegistry.php b/app/Services/Extensions/ExtensionPermissionRegistry.php index 1f4adf5ac..45c4f89aa 100644 --- a/app/Services/Extensions/ExtensionPermissionRegistry.php +++ b/app/Services/Extensions/ExtensionPermissionRegistry.php @@ -13,9 +13,9 @@ use InvalidArgumentException; */ class ExtensionPermissionRegistry { - public const string KEY_REGEX = '/^[a-z][a-z0-9-]{0,47}$/'; + public const string KEY_REGEX = '/^[a-z][a-z0-9-]{0,47}$/D'; - /** OpenAPI pattern every extension permission matches: `ext..`. */ + /** OpenAPI pattern every extension permission matches: `ext..`. Wrap it in `/.../D` for PHP. */ public const string PERMISSION_PATTERN = '^ext\.[a-z][a-z0-9-]{0,47}\.[a-z][a-z0-9-]{0,47}$'; /** @var array}> */ diff --git a/tests/Unit/Http/Controllers/Base/LocaleControllerTest.php b/tests/Unit/Http/Controllers/Base/LocaleControllerTest.php index 45cc378ba..0b9d957ed 100644 --- a/tests/Unit/Http/Controllers/Base/LocaleControllerTest.php +++ b/tests/Unit/Http/Controllers/Base/LocaleControllerTest.php @@ -25,6 +25,17 @@ test('serves namespaced extension translations and rejects path traversal', func } }); +test('locale rules reject values with a trailing newline', function (array $query): void { + $rules = (new \Pterodactyl\Http\Requests\Base\LocaleRequest)->rules(); + + expect(\Illuminate\Support\Facades\Validator::make(['locale' => 'en', 'namespace' => 'ext-probe::messages', 'revision' => str_repeat('a', 32)], $rules)->passes())->toBeTrue(); + expect(\Illuminate\Support\Facades\Validator::make(['locale' => 'en', 'namespace' => 'auth', ...$query], $rules)->passes())->toBeFalse(); +})->with([ + 'locale' => [['locale' => "en\n"]], + 'extension namespace' => [['namespace' => "ext-probe::messages\n"]], + 'revision' => [['revision' => str_repeat('a', 32)."\n"]], +]); + test('caches translations whose URL changes with them and makes the browser revalidate the rest', function (): void { $directory = sys_get_temp_dir().'/ptero-locale-'.uniqid(); \Illuminate\Support\Facades\File::ensureDirectoryExists($directory.'/en'); diff --git a/tests/Unit/Services/Extensions/ExtensionJobProgressTest.php b/tests/Unit/Services/Extensions/ExtensionJobProgressTest.php index 6bcab05c3..a5ca1cb09 100644 --- a/tests/Unit/Services/Extensions/ExtensionJobProgressTest.php +++ b/tests/Unit/Services/Extensions/ExtensionJobProgressTest.php @@ -34,6 +34,8 @@ test('progress rejects invalid updates and permissions outside its namespace', f $progress = new ExtensionJobProgress; expect(fn () => $progress->begin('probe', $user, $server, 'ext.other.view'))->toThrow(InvalidArgumentException::class); expect(fn () => $progress->begin('probe', $user, permission: 'ext.probe.view'))->toThrow(InvalidArgumentException::class); + expect(fn () => $progress->begin("probe\n", $user))->toThrow(InvalidArgumentException::class, 'Invalid extension identifier.'); + expect(fn () => $progress->begin('probe', $user, $server, "ext.probe.view\n"))->toThrow(InvalidArgumentException::class); $started = $progress->begin('probe', $user, $server, 'ext.probe.view'); expect(fn () => $progress->update('probe', $started->id, 101))->toThrow(InvalidArgumentException::class); expect(fn () => $progress->update('probe', $started->id, 10, str_repeat('x', 1001)))->toThrow(InvalidArgumentException::class); diff --git a/tests/Unit/Services/Extensions/ExtensionManifestTest.php b/tests/Unit/Services/Extensions/ExtensionManifestTest.php index 8ef5abe3e..1e2299ae7 100644 --- a/tests/Unit/Services/Extensions/ExtensionManifestTest.php +++ b/tests/Unit/Services/Extensions/ExtensionManifestTest.php @@ -52,6 +52,20 @@ test('rejects invalid manifests', function (array $overrides, string $messageFra $this->expectExceptionMessageMatches('/'.preg_quote($messageFragment, '/').'/'); (new ExtensionManifestValidator)->fromDirectory($this->directory); })->with('invalidManifestProvider'); +test('identifiers and slugs with a trailing newline are rejected', function (array $overrides, string $message): void { + writeManifest(array_merge(['id' => 'valid-id', 'name' => 'Valid', 'version' => '1.0.0'], $overrides)); + + expect(preg_match(ExtensionManifest::ID_REGEX, "my-ext\n"))->toBe(0); + expect(fn (): ExtensionManifest => (new ExtensionManifestValidator)->fromDirectory($this->directory))->toThrow(InvalidExtensionException::class, $message); +})->with([ + 'extension id' => [['id' => "my-ext\n"], 'Extension id "my-ext'], + 'dependency id' => [['requires' => ['extensions' => ["base\n" => '*']]], 'Extension dependencies must be valid identifiers'], + 'root path prefix' => [['routes' => ['root' => ["go\n"]]], 'Manifest "routes.root" prefixes must match'], + 'screen id' => [['ui' => ['entry' => 'dist/client.js', 'screens' => [['id' => "main\n", 'area' => 'account', 'path' => 'probe']]]], 'ui.screens.0.id'], + 'screen path' => [['ui' => ['entry' => 'dist/client.js', 'screens' => [['id' => 'main', 'area' => 'account', 'path' => "probe\n"]]]], 'ui.screens.0.path'], + 'navigation icon' => [['ui' => ['entry' => 'dist/client.js', 'screens' => [['id' => 'main', 'area' => 'account', 'path' => 'probe', 'nav' => ['label' => 'Probe', 'icon' => "life-buoy\n"]]]]], 'Navigation "icon" must be a lucide icon name'], +]); + test('rejects autoload namespaces that are not PSR-4 prefixes', function (string $prefix): void { writeManifest(['id' => 'valid-id', 'name' => 'Valid', 'version' => '1.0.0', 'autoload' => [$prefix => 'src']]); @@ -312,7 +326,8 @@ test('matches the SDK manifest component catalog', function (): void { $schema = File::json(base_path('packages/sdk/manifest.schema.json')); expect($schema['properties']['ui']['properties']['components']['items']['enum'])->toBe(ExtensionManifest::COMPONENT_NAMES); - expect('/'.$schema['properties']['ui']['properties']['screens']['items']['properties']['nav']['properties']['icon']['pattern'].'/')->toBe(ExtensionManifest::ICON_REGEX); + expect('/'.$schema['properties']['ui']['properties']['screens']['items']['properties']['nav']['properties']['icon']['pattern'].'/D')->toBe(ExtensionManifest::ICON_REGEX); + expect('/'.$schema['properties']['id']['pattern'].'/D')->toBe(ExtensionManifest::ID_REGEX); }); test('preserves the declared tailwind prefix', function (string $prefix): void { @@ -410,7 +425,7 @@ test('reserves every top-level route of the SPA and matches the SDK manifest sch $items = $schema['properties']['routes']['properties']['root']['items']; expect($items['not']['enum'])->toBe(ExtensionManifest::RESERVED_ROOT_PREFIXES); - expect('/'.$items['pattern'].'/')->toBe(ExtensionManifest::ROOT_PREFIX_REGEX); + expect('/'.$items['pattern'].'/D')->toBe(ExtensionManifest::ROOT_PREFIX_REGEX); // Client-side routes mounted directly under the root or the authenticated layout. preg_match_all('/getParentRoute: \(\) => (?:rootRoute|authenticatedRoute),\s+path: \'([a-z][a-z0-9-]*)/', File::get(resource_path('scripts/router/routeTree.ts')), $matches); diff --git a/tests/Unit/Services/Extensions/ExtensionPermissionRegistryTest.php b/tests/Unit/Services/Extensions/ExtensionPermissionRegistryTest.php index 68612365a..f6aaf5c29 100644 --- a/tests/Unit/Services/Extensions/ExtensionPermissionRegistryTest.php +++ b/tests/Unit/Services/Extensions/ExtensionPermissionRegistryTest.php @@ -24,4 +24,5 @@ test('rejects permission keys that would not form a valid permission', function 'dotted key' => [['votes.view' => 'View votes.']], 'uppercase key' => [['View' => 'View votes.']], 'wildcard key' => [['*' => 'Everything.']], + 'trailing newline' => [["view\n" => 'View votes.']], ]);