{"record":{"id":"81553310d827fb5e","repo":"passbolt/passbolt_api","slug":"invalid-user-id-format-metadatapopulateuserkeyidtrait","errorCode":null,"errorMessage":"Invalid user ID format.","messagePattern":"Invalid user ID format\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"plugins/PassboltCe/Metadata/src/Utility/MetadataPopulateUserKeyIdTrait.php","lineNumber":39,"sourceCode":"use Cake\\Validation\\Validation;\nuse InvalidArgumentException;\nuse Passbolt\\Metadata\\Model\\Dto\\MetadataDto;\nuse Passbolt\\Metadata\\Model\\Entity\\MetadataKey;\n\ntrait MetadataPopulateUserKeyIdTrait\n{\n    /**\n     * Update sent v5 entities request data when METADATA_KEY_TYPE TYPE_USER_KEY is set to null\n     * By dynamically inserting the current gpgkey_id in its place\n     *\n     * @param string $userId uuid\n     * @param mixed $data request data\n     * @return array\n     */\n    public function populatedMetadataUserKeyId(string $userId, mixed $data): array\n    {\n        if (!Validation::uuid($userId)) {\n            throw new InvalidArgumentException(__('Invalid user ID format.'));\n        }\n        if (!isset($data) || !is_array($data)) {\n            throw new BadRequestException(__('The data is required.'));\n        }\n        if (\n            isset($data[MetadataDto::METADATA])\n            && isset($data[MetadataDto::METADATA_KEY_TYPE])\n            && is_string($data[MetadataDto::METADATA_KEY_TYPE])\n            && $data[MetadataDto::METADATA_KEY_TYPE] === MetadataKey::TYPE_USER_KEY\n            && !isset($data[MetadataDto::METADATA_KEY_ID])\n        ) {\n            $keyTable = TableRegistry::getTableLocator()->get('Gpgkeys');\n            $key = $keyTable->find('current', userId: $userId)->firstOrFail();\n            $id = $key->get('id');\n            if (Validation::uuid($id)) {\n                $data[MetadataDto::METADATA_KEY_ID] = $id;\n            }\n        }","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/passbolt/passbolt_api/blob/31c1bbc10f32808a607fa9bd81891e898779c0bc/plugins/PassboltCe/Metadata/src/Utility/MetadataPopulateUserKeyIdTrait.php#L21-L57","documentation":"Thrown by populatedMetadataUserKeyId when the supplied $userId is not a valid UUID (Validation::uuid fails). The trait uses the user ID to populate metadata user key ids in request data, and refuses to proceed with a malformed identifier.","triggerScenarios":"Calling a controller/service action using this trait with a user ID that is null, empty, non-UUID string, or an integer id instead of a UUID string; routing parameters bound incorrectly so the id placeholder holds something else.","commonSituations":"Passing a username/email or numeric legacy id where a UUID is expected; URL building mistakes that omit the id segment; tests or CLI code constructing request data with dummy ids.","solutions":["Pass a valid UUID string (36 chars, canonical 8-4-4-4-12 hex format) as $userId","Validate the id client-side before calling, e.g. with the same UUID regex used by CakePHP Validation::uuid()","Check routing/config so the user id is correctly bound to the action parameter","If the id comes from an entity, use $entity->id rather than a manually supplied value"],"exampleFix":"// before\n$service->populatedMetadataUserKeyId($userId, $data); // $userId = 'admin'\n// after\nif (!Validation::uuid($userId)) {\n    throw new BadRequestException(__('A valid user identifier is required.'));\n}\n$service->populatedMetadataUserKeyId($userId, $data);","handlingStrategy":"validation","validationCode":"use Cake\\Validation\\Validation;\nif (!is_string($userId) || !Validation::uuid($userId)) {\n    throw new BadRequestException(__('Invalid user ID.'));\n}","typeGuard":"function isUuidString(mixed $value): bool {\n    return is_string($value) && (bool)preg_match('/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i', $value);\n}","tryCatchPattern":"try {\n    $data = $trait->populatedMetadataUserKeyId($userId, $data);\n} catch (InvalidArgumentException $e) {\n    return $this->response->withStatus(400)->withStringBody(json_encode(['error' => 'Invalid user ID format']));\n}","preventionTips":["Always source user ids from entity ->id, not user input like username/email","Validate route parameters as UUIDs before calling services","Use CakePHP route type constraints (uuid placeholder) in routes.php","Add a pre-call Validation::uuid() assertion in controllers"],"tags":["validation","uuid","invalid-argument"],"backgroundTag":"invalid-identifier-format","analyzedSha":"31c1bbc10f32808a607fa9bd81891e898779c0bc","analyzedAt":"2026-09-17T00:04:38.960Z","contentChangedAt":"2026-09-17T00:04:38.960Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}