From b99d435ccd76b73f1135fec732ca5a811e421cba Mon Sep 17 00:00:00 2001 From: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> Date: Fri, 13 Nov 2026 17:14:58 +0000 Subject: [PATCH 1/7] fix(openapi): declare optional sign-request headers Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> --- lib/Controller/IdDocsController.php | 4 +++ .../SignatureElementsController.php | 3 +++ .../Middleware/InjectionMiddlewareTest.php | 25 +++++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/lib/Controller/IdDocsController.php b/lib/Controller/IdDocsController.php index f7d6823084..b83a5af186 100644 --- a/lib/Controller/IdDocsController.php +++ b/lib/Controller/IdDocsController.php @@ -21,6 +21,7 @@ use OCP\AppFramework\Http\Attribute\NoCSRFRequired; use OCP\AppFramework\Http\Attribute\OpenAPI; use OCP\AppFramework\Http\Attribute\PublicPage; +use OCP\AppFramework\Http\Attribute\RequestHeader; use OCP\AppFramework\Http\DataResponse; use OCP\IL10N; use OCP\IRequest; @@ -63,6 +64,7 @@ public function __construct( #[AnonRateLimit(limit: 30, period: 60)] #[NoAdminRequired] #[NoCSRFRequired] + #[RequestHeader(name: 'libresign-sign-request-uuid', description: 'The UUID of the sign request, used to identify the request', indirect: true)] #[RequireSignRequestUuid(skipIfAuthenticated: true)] #[OpenAPI(tags: ['identity'])] #[ApiRoute(verb: 'POST', url: '/api/{apiVersion}/id-docs', requirements: ['apiVersion' => '(v1)'])] @@ -113,6 +115,7 @@ public function addFiles(array $files): DataResponse { #[AnonRateLimit(limit: 30, period: 60)] #[NoAdminRequired] #[NoCSRFRequired] + #[RequestHeader(name: 'libresign-sign-request-uuid', description: 'The UUID of the sign request, used to identify the request', indirect: true)] #[RequireSignRequestUuid(skipIfAuthenticated: true)] #[OpenAPI(tags: ['identity'])] #[ApiRoute(verb: 'DELETE', url: '/api/{apiVersion}/id-docs/{nodeId}', requirements: ['apiVersion' => '(v1)'])] @@ -153,6 +156,7 @@ public function delete(int $nodeId, ?string $uuid = null): DataResponse { #[PublicPage] #[AnonRateLimit(limit: 30, period: 60)] #[NoCSRFRequired] + #[RequestHeader(name: 'libresign-sign-request-uuid', description: 'The UUID of the sign request, used to identify the request', indirect: true)] #[RequireSignRequestUuid(skipIfAuthenticated: true)] #[OpenAPI(tags: ['identity'])] #[ApiRoute(verb: 'GET', url: '/api/{apiVersion}/id-docs', requirements: ['apiVersion' => '(v1)'])] diff --git a/lib/Controller/SignatureElementsController.php b/lib/Controller/SignatureElementsController.php index bbb1f0fad1..4fdb810246 100644 --- a/lib/Controller/SignatureElementsController.php +++ b/lib/Controller/SignatureElementsController.php @@ -239,6 +239,7 @@ public function useSavedSignatureElements(string $token = ''): DataResponse { #[NoAdminRequired] #[PublicPage] #[NoCSRFRequired] + #[RequestHeader(name: 'libresign-sign-request-uuid', description: 'The UUID of the sign request, used to identify the request', indirect: true)] #[RequireSignRequestUuid(skipIfAuthenticated: true)] #[OpenAPI(tags: ['appearance'])] #[ApiRoute(verb: 'GET', url: '/api/{apiVersion}/signature/elements/preview/{nodeId}', requirements: ['apiVersion' => '(v1)'])] @@ -322,6 +323,7 @@ public function getSignatureElement(int $nodeId): DataResponse { #[NoAdminRequired] #[PublicPage] #[NoCSRFRequired] + #[RequestHeader(name: 'libresign-sign-request-uuid', description: 'The UUID of the sign request, used to identify the request', indirect: true)] #[RequireSignRequestUuid(skipIfAuthenticated: true)] #[OpenAPI(tags: ['appearance'])] #[ApiRoute(verb: 'PATCH', url: '/api/{apiVersion}/signature/elements/{nodeId}', requirements: ['apiVersion' => '(v1)'])] @@ -380,6 +382,7 @@ public function patchSignatureElement(int $nodeId, string $type = '', array $fil #[NoAdminRequired] #[NoCSRFRequired] #[PublicPage] + #[RequestHeader(name: 'libresign-sign-request-uuid', description: 'The UUID of the sign request, used to identify the request', indirect: true)] #[RequireSignRequestUuid(skipIfAuthenticated: true)] #[OpenAPI(tags: ['appearance'])] #[ApiRoute(verb: 'DELETE', url: '/api/{apiVersion}/signature/elements/{nodeId}', requirements: ['apiVersion' => '(v1)'])] diff --git a/tests/php/Unit/Middleware/InjectionMiddlewareTest.php b/tests/php/Unit/Middleware/InjectionMiddlewareTest.php index bb6b94bc3b..4846bd0406 100644 --- a/tests/php/Unit/Middleware/InjectionMiddlewareTest.php +++ b/tests/php/Unit/Middleware/InjectionMiddlewareTest.php @@ -115,6 +115,31 @@ public function setUp(): void { $this->userId = null; } + /** @return array */ + public static function uuidSourceProvider(): array { + return [ + 'header without route UUID' => [null, 'header-uuid', 'header-uuid'], + 'route UUID without header' => ['route-uuid', '', 'route-uuid'], + 'route UUID takes precedence over header' => ['route-uuid', 'header-uuid', 'route-uuid'], + 'neither source provided' => [null, '', ''], + 'unsupported UUID is forwarded to the authorization validator' => [null, 'invalid-uuid', 'invalid-uuid'], + ]; + } + + #[DataProvider('uuidSourceProvider')] + public function testUuidSourcePrecedence(?string $routeUuid, string $headerUuid, ?string $expectedUuid): void { + $this->request->expects($this->once()) + ->method('getHeader') + ->with('libresign-sign-request-uuid') + ->willReturn($headerUuid); + $this->request->expects($this->once()) + ->method('getParam') + ->with('uuid', $headerUuid) + ->willReturn($routeUuid ?? $headerUuid); + + self::assertSame($expectedUuid, self::invokePrivate($this->getInjectionMiddleware(), 'getUuidFromRequest')); + } + public function testGuestSigningUuidReplacesEarlierRequestInSession(): void { $this->userSession->method('getUser')->willReturn(null); $this->session->method('get')->with('libresign-uuid')->willReturn('earlier-sign-request'); From 357abe12104b04f788833a29b03ebf400a6af8d1 Mon Sep 17 00:00:00 2001 From: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> Date: Fri, 13 Nov 2026 17:15:25 +0000 Subject: [PATCH 2/7] test(openapi): assert sign-request headers and regenerate SDK types Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> --- openapi-full.json | 48 ++++++++++++++ openapi.json | 48 ++++++++++++++ src/types/openapi/openapi-full.ts | 12 ++++ src/types/openapi/openapi.ts | 12 ++++ .../php/openapi/OpenApiRequestHeadersTest.php | 66 +++++++++++++++++++ tests/php/openapi/phpunit.xml | 1 + 6 files changed, 187 insertions(+) create mode 100644 tests/php/openapi/OpenApiRequestHeadersTest.php diff --git a/openapi-full.json b/openapi-full.json index 567fa483c0..9ebcb7784d 100644 --- a/openapi-full.json +++ b/openapi-full.json @@ -8367,6 +8367,14 @@ "default": "v1" } }, + { + "name": "libresign-sign-request-uuid", + "in": "header", + "description": "The UUID of the sign request, used to identify the request", + "schema": { + "type": "string" + } + }, { "name": "OCS-APIRequest", "in": "header", @@ -8510,6 +8518,14 @@ "default": null } }, + { + "name": "libresign-sign-request-uuid", + "in": "header", + "description": "The UUID of the sign request, used to identify the request", + "schema": { + "type": "string" + } + }, { "name": "OCS-APIRequest", "in": "header", @@ -8634,6 +8650,14 @@ "default": null } }, + { + "name": "libresign-sign-request-uuid", + "in": "header", + "description": "The UUID of the sign request, used to identify the request", + "schema": { + "type": "string" + } + }, { "name": "OCS-APIRequest", "in": "header", @@ -14379,6 +14403,14 @@ "format": "int64" } }, + { + "name": "libresign-sign-request-uuid", + "in": "header", + "description": "The UUID of the sign request, used to identify the request", + "schema": { + "type": "string" + } + }, { "name": "OCS-APIRequest", "in": "header", @@ -14638,6 +14670,14 @@ "format": "int64" } }, + { + "name": "libresign-sign-request-uuid", + "in": "header", + "description": "The UUID of the sign request, used to identify the request", + "schema": { + "type": "string" + } + }, { "name": "OCS-APIRequest", "in": "header", @@ -14750,6 +14790,14 @@ "format": "int64" } }, + { + "name": "libresign-sign-request-uuid", + "in": "header", + "description": "The UUID of the sign request, used to identify the request", + "schema": { + "type": "string" + } + }, { "name": "OCS-APIRequest", "in": "header", diff --git a/openapi.json b/openapi.json index bdd25f2ca3..8a5f3ccde9 100644 --- a/openapi.json +++ b/openapi.json @@ -7630,6 +7630,14 @@ "default": "v1" } }, + { + "name": "libresign-sign-request-uuid", + "in": "header", + "description": "The UUID of the sign request, used to identify the request", + "schema": { + "type": "string" + } + }, { "name": "OCS-APIRequest", "in": "header", @@ -7773,6 +7781,14 @@ "default": null } }, + { + "name": "libresign-sign-request-uuid", + "in": "header", + "description": "The UUID of the sign request, used to identify the request", + "schema": { + "type": "string" + } + }, { "name": "OCS-APIRequest", "in": "header", @@ -7897,6 +7913,14 @@ "default": null } }, + { + "name": "libresign-sign-request-uuid", + "in": "header", + "description": "The UUID of the sign request, used to identify the request", + "schema": { + "type": "string" + } + }, { "name": "OCS-APIRequest", "in": "header", @@ -13642,6 +13666,14 @@ "format": "int64" } }, + { + "name": "libresign-sign-request-uuid", + "in": "header", + "description": "The UUID of the sign request, used to identify the request", + "schema": { + "type": "string" + } + }, { "name": "OCS-APIRequest", "in": "header", @@ -13901,6 +13933,14 @@ "format": "int64" } }, + { + "name": "libresign-sign-request-uuid", + "in": "header", + "description": "The UUID of the sign request, used to identify the request", + "schema": { + "type": "string" + } + }, { "name": "OCS-APIRequest", "in": "header", @@ -14013,6 +14053,14 @@ "format": "int64" } }, + { + "name": "libresign-sign-request-uuid", + "in": "header", + "description": "The UUID of the sign request, used to identify the request", + "schema": { + "type": "string" + } + }, { "name": "OCS-APIRequest", "in": "header", diff --git a/src/types/openapi/openapi-full.ts b/src/types/openapi/openapi-full.ts index 84d80aa12f..23bc90583e 100644 --- a/src/types/openapi/openapi-full.ts +++ b/src/types/openapi/openapi-full.ts @@ -4340,6 +4340,8 @@ export interface operations { length?: number | null; }; header: { + /** @description The UUID of the sign request, used to identify the request */ + "libresign-sign-request-uuid"?: string; /** @description Required to be true for the API request to pass */ "OCS-APIRequest": boolean; }; @@ -4384,6 +4386,8 @@ export interface operations { parameters: { query?: never; header: { + /** @description The UUID of the sign request, used to identify the request */ + "libresign-sign-request-uuid"?: string; /** @description Required to be true for the API request to pass */ "OCS-APIRequest": boolean; }; @@ -4438,6 +4442,8 @@ export interface operations { uuid?: string | null; }; header: { + /** @description The UUID of the sign request, used to identify the request */ + "libresign-sign-request-uuid"?: string; /** @description Required to be true for the API request to pass */ "OCS-APIRequest": boolean; }; @@ -6886,6 +6892,8 @@ export interface operations { parameters: { query?: never; header: { + /** @description The UUID of the sign request, used to identify the request */ + "libresign-sign-request-uuid"?: string; /** @description Required to be true for the API request to pass */ "OCS-APIRequest": boolean; }; @@ -6987,6 +6995,8 @@ export interface operations { parameters: { query?: never; header: { + /** @description The UUID of the sign request, used to identify the request */ + "libresign-sign-request-uuid"?: string; /** @description Required to be true for the API request to pass */ "OCS-APIRequest": boolean; }; @@ -7033,6 +7043,8 @@ export interface operations { parameters: { query?: never; header: { + /** @description The UUID of the sign request, used to identify the request */ + "libresign-sign-request-uuid"?: string; /** @description Required to be true for the API request to pass */ "OCS-APIRequest": boolean; }; diff --git a/src/types/openapi/openapi.ts b/src/types/openapi/openapi.ts index 592e6f3506..f89d8136e3 100644 --- a/src/types/openapi/openapi.ts +++ b/src/types/openapi/openapi.ts @@ -3778,6 +3778,8 @@ export interface operations { length?: number | null; }; header: { + /** @description The UUID of the sign request, used to identify the request */ + "libresign-sign-request-uuid"?: string; /** @description Required to be true for the API request to pass */ "OCS-APIRequest": boolean; }; @@ -3822,6 +3824,8 @@ export interface operations { parameters: { query?: never; header: { + /** @description The UUID of the sign request, used to identify the request */ + "libresign-sign-request-uuid"?: string; /** @description Required to be true for the API request to pass */ "OCS-APIRequest": boolean; }; @@ -3876,6 +3880,8 @@ export interface operations { uuid?: string | null; }; header: { + /** @description The UUID of the sign request, used to identify the request */ + "libresign-sign-request-uuid"?: string; /** @description Required to be true for the API request to pass */ "OCS-APIRequest": boolean; }; @@ -6324,6 +6330,8 @@ export interface operations { parameters: { query?: never; header: { + /** @description The UUID of the sign request, used to identify the request */ + "libresign-sign-request-uuid"?: string; /** @description Required to be true for the API request to pass */ "OCS-APIRequest": boolean; }; @@ -6425,6 +6433,8 @@ export interface operations { parameters: { query?: never; header: { + /** @description The UUID of the sign request, used to identify the request */ + "libresign-sign-request-uuid"?: string; /** @description Required to be true for the API request to pass */ "OCS-APIRequest": boolean; }; @@ -6471,6 +6481,8 @@ export interface operations { parameters: { query?: never; header: { + /** @description The UUID of the sign request, used to identify the request */ + "libresign-sign-request-uuid"?: string; /** @description Required to be true for the API request to pass */ "OCS-APIRequest": boolean; }; diff --git a/tests/php/openapi/OpenApiRequestHeadersTest.php b/tests/php/openapi/OpenApiRequestHeadersTest.php new file mode 100644 index 0000000000..615da1bb0c --- /dev/null +++ b/tests/php/openapi/OpenApiRequestHeadersTest.php @@ -0,0 +1,66 @@ + ['openapi.json'], + 'administration scope' => ['openapi-administration.json'], + 'full scope' => ['openapi-full.json'], + ]; + } + + #[DataProvider('specificationProvider')] + public function testCustomHeadersMatchActualMiddlewareConsumers(string $file): void { + $operations = OpenApiFunctionalTags::loadOperations(dirname(__DIR__, 3) . '/' . $file); + $present = []; + foreach ($operations as $operation) { + $id = $operation['operationId']; + $parameters = $operation['raw']['parameters'] ?? []; + $headers = array_values(array_filter( + $parameters, + static fn (array $parameter): bool => ($parameter['in'] ?? null) === 'header' + && strtolower((string)($parameter['name'] ?? '')) === self::SIGN_REQUEST_HEADER, + )); + self::assertCount(in_array($id, self::OPERATIONS, true) ? 1 : 0, $headers, "Unexpected sign-request header in {$file}: {$id}"); + if ($headers !== []) { + $present[] = $id; + self::assertSame(self::SIGN_REQUEST_HEADER, $headers[0]['name']); + self::assertSame(self::DESCRIPTION, $headers[0]['description']); + self::assertFalse($headers[0]['required'] ?? false, "The header is optional for {$id}"); + self::assertSame('string', $headers[0]['schema']['type'] ?? null); + } + } + + $expected = $file === 'openapi-administration.json' ? [] : self::OPERATIONS; + sort($present); + sort($expected); + self::assertSame($expected, $present, "Missing or unexpected sign-request UUID operations in {$file}"); + } +} diff --git a/tests/php/openapi/phpunit.xml b/tests/php/openapi/phpunit.xml index 4ca765e58c..8845dd9e2c 100644 --- a/tests/php/openapi/phpunit.xml +++ b/tests/php/openapi/phpunit.xml @@ -15,6 +15,7 @@ OpenApiFunctionalTagsTest.php OpenApiGenerationTest.php + OpenApiRequestHeadersTest.php From 7a76eb91ef0e236aaf67a0363426b2938b783a0d Mon Sep 17 00:00:00 2001 From: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> Date: Fri, 13 Nov 2026 21:50:33 +0000 Subject: [PATCH 3/7] fix(openapi): discover UUID header consumers dynamically Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> --- lib/Controller/SignFileController.php | 2 + openapi-full.json | 8 ++ openapi.json | 8 ++ src/types/openapi/openapi-full.ts | 2 + src/types/openapi/openapi.ts | 2 + .../php/openapi/OpenApiRequestHeadersTest.php | 126 +++++++++++++++--- 6 files changed, 131 insertions(+), 17 deletions(-) diff --git a/lib/Controller/SignFileController.php b/lib/Controller/SignFileController.php index bd35b85e7f..261b6dbe29 100644 --- a/lib/Controller/SignFileController.php +++ b/lib/Controller/SignFileController.php @@ -40,6 +40,7 @@ use OCP\AppFramework\Http\Attribute\NoCSRFRequired; use OCP\AppFramework\Http\Attribute\OpenAPI; use OCP\AppFramework\Http\Attribute\PublicPage; +use OCP\AppFramework\Http\Attribute\RequestHeader; use OCP\AppFramework\Http\DataResponse; use OCP\IL10N; use OCP\IRequest; @@ -488,6 +489,7 @@ public function requestCodeBySignerUuid( */ #[NoAdminRequired] #[NoCSRFRequired] + #[RequestHeader(name: 'libresign-sign-request-uuid', description: 'The UUID of the sign request, used to identify the request', indirect: true)] #[RequireSigner] #[PublicPage] #[OpenAPI(tags: ['signing'])] diff --git a/openapi-full.json b/openapi-full.json index 9ebcb7784d..17f075f57f 100644 --- a/openapi-full.json +++ b/openapi-full.json @@ -13915,6 +13915,14 @@ "format": "int64" } }, + { + "name": "libresign-sign-request-uuid", + "in": "header", + "description": "The UUID of the sign request, used to identify the request", + "schema": { + "type": "string" + } + }, { "name": "OCS-APIRequest", "in": "header", diff --git a/openapi.json b/openapi.json index 8a5f3ccde9..ad7808898c 100644 --- a/openapi.json +++ b/openapi.json @@ -13178,6 +13178,14 @@ "format": "int64" } }, + { + "name": "libresign-sign-request-uuid", + "in": "header", + "description": "The UUID of the sign request, used to identify the request", + "schema": { + "type": "string" + } + }, { "name": "OCS-APIRequest", "in": "header", diff --git a/src/types/openapi/openapi-full.ts b/src/types/openapi/openapi-full.ts index 23bc90583e..457ff0cd3e 100644 --- a/src/types/openapi/openapi-full.ts +++ b/src/types/openapi/openapi-full.ts @@ -6677,6 +6677,8 @@ export interface operations { parameters: { query?: never; header: { + /** @description The UUID of the sign request, used to identify the request */ + "libresign-sign-request-uuid"?: string; /** @description Required to be true for the API request to pass */ "OCS-APIRequest": boolean; }; diff --git a/src/types/openapi/openapi.ts b/src/types/openapi/openapi.ts index f89d8136e3..2f6d85fac3 100644 --- a/src/types/openapi/openapi.ts +++ b/src/types/openapi/openapi.ts @@ -6115,6 +6115,8 @@ export interface operations { parameters: { query?: never; header: { + /** @description The UUID of the sign request, used to identify the request */ + "libresign-sign-request-uuid"?: string; /** @description Required to be true for the API request to pass */ "OCS-APIRequest": boolean; }; diff --git a/tests/php/openapi/OpenApiRequestHeadersTest.php b/tests/php/openapi/OpenApiRequestHeadersTest.php index 615da1bb0c..1596cb4573 100644 --- a/tests/php/openapi/OpenApiRequestHeadersTest.php +++ b/tests/php/openapi/OpenApiRequestHeadersTest.php @@ -14,18 +14,18 @@ final class OpenApiRequestHeadersTest extends TestCase { private const SIGN_REQUEST_HEADER = 'libresign-sign-request-uuid'; private const DESCRIPTION = 'The UUID of the sign request, used to identify the request'; + private const OCS_PREFIX = '/ocs/v2.php/apps/libresign'; - /** The optional header applies only to methods that can identify a request without a route UUID. */ - private const OPERATIONS = [ - 'signature_elements-create-signature-element', - 'signature_elements-get-signature-elements', - 'signature_elements-use-saved-signature-elements', - 'signature_elements-preview-signature-element', - 'signature_elements-patch-signature-element', - 'signature_elements-delete-signature-element', - 'id_docs-add-files', - 'id_docs-delete', - 'id_docs-list-unauthenticated-signer-documents', + /** + * Middleware attributes that consume a sign request UUID. Unlike operation IDs, + * this list changes only if the authorization contract changes. + */ + private const UUID_CONSUMERS = [ + 'RequireSigner', + 'RequireSignerUuid', + 'RequireSignRequestUuid', + 'RequireParticipantUuid', + 'CanSignRequestUuid', ]; public static function specificationProvider(): array { @@ -38,29 +38,121 @@ public static function specificationProvider(): array { #[DataProvider('specificationProvider')] public function testCustomHeadersMatchActualMiddlewareConsumers(string $file): void { + $expectedRoutes = self::discoverUuidConsumerRoutes(); + $expected = array_keys(array_filter( + $expectedRoutes, + static fn (string $scope): bool => $file === 'openapi-full.json' + || ($file === 'openapi-administration.json' ? $scope === 'administration' : $scope === 'default'), + )); + $operations = OpenApiFunctionalTags::loadOperations(dirname(__DIR__, 3) . '/' . $file); $present = []; foreach ($operations as $operation) { - $id = $operation['operationId']; + $route = OpenApiFunctionalTags::routeKey($operation); $parameters = $operation['raw']['parameters'] ?? []; $headers = array_values(array_filter( $parameters, static fn (array $parameter): bool => ($parameter['in'] ?? null) === 'header' && strtolower((string)($parameter['name'] ?? '')) === self::SIGN_REQUEST_HEADER, )); - self::assertCount(in_array($id, self::OPERATIONS, true) ? 1 : 0, $headers, "Unexpected sign-request header in {$file}: {$id}"); + self::assertCount(in_array($route, $expected, true) ? 1 : 0, $headers, "Unexpected sign-request header in {$file}: {$route}"); if ($headers !== []) { - $present[] = $id; + $present[] = $route; self::assertSame(self::SIGN_REQUEST_HEADER, $headers[0]['name']); self::assertSame(self::DESCRIPTION, $headers[0]['description']); - self::assertFalse($headers[0]['required'] ?? false, "The header is optional for {$id}"); + // A request parameter named uuid is also accepted by InjectionMiddleware. + self::assertFalse($headers[0]['required'] ?? false, "The header can be replaced by a request UUID for {$route}"); self::assertSame('string', $headers[0]['schema']['type'] ?? null); } } - $expected = $file === 'openapi-administration.json' ? [] : self::OPERATIONS; sort($present); sort($expected); - self::assertSame($expected, $present, "Missing or unexpected sign-request UUID operations in {$file}"); + self::assertSame($expected, $present, "Missing or unexpected sign-request UUID routes in {$file}"); + } + + /** @return array */ + private static function discoverUuidConsumerRoutes(): array { + $files = glob(dirname(__DIR__, 3) . '/lib/Controller/*Controller.php'); + self::assertNotFalse($files); + + $routes = []; + foreach ($files as $file) { + $source = file_get_contents($file); + self::assertNotFalse($source); + foreach (self::consumerRoutesInSource($source, $file) as $route => $scope) { + self::assertArrayNotHasKey($route, $routes, "Duplicate source route {$route}"); + $routes[$route] = $scope; + } + } + return $routes; + } + + /** + * Read controller attributes, not RequestHeader declarations or generated OpenAPI. + * Every ApiRoute must be parsed so a new syntax cannot silently skip consumers. + * + * @return array + */ + private static function consumerRoutesInSource(string $source, string $file): array { + $matches = []; + preg_match_all('/((?:^[ \t]*#\[[^\r\n]*\]\r?\n)+)[ \t]*public function [A-Za-z_]\w*/m', $source, $matches, PREG_SET_ORDER); + $foundRoutes = 0; + $routes = []; + + foreach ($matches as $match) { + $attributes = $match[1]; + if (!str_contains($attributes, '#[ApiRoute(')) { + continue; + } + $foundRoutes++; + + if (str_contains($attributes, 'SCOPE_IGNORE')) { + continue; + } + $consumesUuid = false; + foreach (self::UUID_CONSUMERS as $attribute) { + if (preg_match('/#\[' . $attribute . '(?:\]|\()/', $attributes)) { + $consumesUuid = true; + break; + } + } + if (!$consumesUuid) { + continue; + } + + self::assertSame(1, preg_match("/#\\[ApiRoute\\(verb:\\s*'([A-Z]+)',\\s*url:\\s*'([^']+)'/", $attributes, $route), "Cannot read ApiRoute in {$file}"); + // Routes already carrying {uuid} do not need a separate header. + if (str_contains($route[2], '{uuid}')) { + continue; + } + $key = $route[1] . ' ' . self::OCS_PREFIX . $route[2]; + self::assertArrayNotHasKey($key, $routes, "Duplicate route in {$file}: {$key}"); + $routes[$key] = str_contains($attributes, 'SCOPE_ADMINISTRATION') ? 'administration' : 'default'; + } + + self::assertSame(substr_count($source, '#[ApiRoute('), $foundRoutes, "Unparsed ApiRoute in {$file}; update the source discovery"); + return $routes; + } + + public function testNewUuidConsumerIsDiscoveredWithoutUpdatingAnOperationList(): void { + $source = <<<'PHP' + 'default'], + self::consumerRoutesInSource($source, 'synthetic controller'), + ); } } From d37c3c9e2466f633a26d21b0e90a99eaf2f035da Mon Sep 17 00:00:00 2001 From: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> Date: Sat, 14 Nov 2026 12:55:42 +0000 Subject: [PATCH 4/7] test(openapi): discover UUID consumers from middleware Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> --- .../php/openapi/OpenApiRequestHeadersTest.php | 121 +++++++++++++----- 1 file changed, 91 insertions(+), 30 deletions(-) diff --git a/tests/php/openapi/OpenApiRequestHeadersTest.php b/tests/php/openapi/OpenApiRequestHeadersTest.php index 1596cb4573..ee7305db59 100644 --- a/tests/php/openapi/OpenApiRequestHeadersTest.php +++ b/tests/php/openapi/OpenApiRequestHeadersTest.php @@ -16,18 +16,6 @@ final class OpenApiRequestHeadersTest extends TestCase { private const DESCRIPTION = 'The UUID of the sign request, used to identify the request'; private const OCS_PREFIX = '/ocs/v2.php/apps/libresign'; - /** - * Middleware attributes that consume a sign request UUID. Unlike operation IDs, - * this list changes only if the authorization contract changes. - */ - private const UUID_CONSUMERS = [ - 'RequireSigner', - 'RequireSignerUuid', - 'RequireSignRequestUuid', - 'RequireParticipantUuid', - 'CanSignRequestUuid', - ]; - public static function specificationProvider(): array { return [ 'default scope' => ['openapi.json'], @@ -73,6 +61,10 @@ public function testCustomHeadersMatchActualMiddlewareConsumers(string $file): v /** @return array */ private static function discoverUuidConsumerRoutes(): array { + $middlewareSource = file_get_contents(dirname(__DIR__, 3) . '/lib/Middleware/InjectionMiddleware.php'); + self::assertNotFalse($middlewareSource); + $consumers = self::uuidConsumerAttributesInMiddleware($middlewareSource); + $files = glob(dirname(__DIR__, 3) . '/lib/Controller/*Controller.php'); self::assertNotFalse($files); @@ -80,7 +72,7 @@ private static function discoverUuidConsumerRoutes(): array { foreach ($files as $file) { $source = file_get_contents($file); self::assertNotFalse($source); - foreach (self::consumerRoutesInSource($source, $file) as $route => $scope) { + foreach (self::consumerRoutesInSource($source, $file, $consumers) as $route => $scope) { self::assertArrayNotHasKey($route, $routes, "Duplicate source route {$route}"); $routes[$route] = $scope; } @@ -88,13 +80,51 @@ private static function discoverUuidConsumerRoutes(): array { return $routes; } + /** + * Discover authorization attributes directly from the middleware's UUID paths. + * Attributes checked in handleUuid share its UUID lookup; beforeController's + * guards count only when their direct handler reads the request UUID. + * + * @return list + */ + private static function uuidConsumerAttributesInMiddleware(string $source): array { + $handleUuid = self::middlewareMethodSource($source, 'handleUuid'); + preg_match_all('/getAttributes\((\w+)::class\)/', $handleUuid, $matches); + $consumers = $matches[1]; + + $beforeController = self::middlewareMethodSource($source, 'beforeController'); + preg_match_all('/getAttributes\((\w+)::class\)\)\)\s*\{\s*\$this->(\w+)\(/', $beforeController, $guards, PREG_SET_ORDER); + foreach ($guards as $guard) { + if (str_contains(self::middlewareMethodSource($source, $guard[2]), '$this->getUuidFromRequest(')) { + $consumers[] = $guard[1]; + } + } + + self::assertNotEmpty($consumers, 'No UUID middleware consumers discovered'); + return array_values(array_unique($consumers)); + } + + private static function middlewareMethodSource(string $source, string $method): string { + $declaration = '/^[ \t]*(?:public|protected|private)[ \t]+(?:static[ \t]+)?function[ \t]+' . preg_quote($method, '/') . '\s*\(/m'; + self::assertSame(1, preg_match($declaration, $source, $found, PREG_OFFSET_CAPTURE), "Missing middleware method {$method}"); + $start = $found[0][1]; + $bodyOffset = $start + strlen($found[0][0]); + $following = substr($source, $bodyOffset); + $nextMethod = '/^[ \t]*(?:public|protected|private)[ \t]+(?:static[ \t]+)?function[ \t]+[A-Za-z_]\w*\s*\(/m'; + if (preg_match($nextMethod, $following, $next, PREG_OFFSET_CAPTURE)) { + return substr($source, $start, $bodyOffset - $start + $next[0][1]); + } + return substr($source, $start); + } + /** * Read controller attributes, not RequestHeader declarations or generated OpenAPI. * Every ApiRoute must be parsed so a new syntax cannot silently skip consumers. * + * @param list $consumers * @return array */ - private static function consumerRoutesInSource(string $source, string $file): array { + private static function consumerRoutesInSource(string $source, string $file, array $consumers): array { $matches = []; preg_match_all('/((?:^[ \t]*#\[[^\r\n]*\]\r?\n)+)[ \t]*public function [A-Za-z_]\w*/m', $source, $matches, PREG_SET_ORDER); $foundRoutes = 0; @@ -111,8 +141,8 @@ private static function consumerRoutesInSource(string $source, string $file): ar continue; } $consumesUuid = false; - foreach (self::UUID_CONSUMERS as $attribute) { - if (preg_match('/#\[' . $attribute . '(?:\]|\()/', $attributes)) { + foreach ($consumers as $attribute) { + if (preg_match('/#\[' . preg_quote($attribute, '/') . '(?:\]|\()/', $attributes)) { $consumesUuid = true; break; } @@ -137,22 +167,53 @@ private static function consumerRoutesInSource(string $source, string $file): ar public function testNewUuidConsumerIsDiscoveredWithoutUpdatingAnOperationList(): void { $source = <<<'PHP' - 'default'], - self::consumerRoutesInSource($source, 'synthetic controller'), + self::consumerRoutesInSource($source, 'synthetic controller', self::uuidConsumerAttributesInMiddleware($middleware)), + ); + } + + public function testNewMiddlewareConsumerIsDiscoveredWithoutUpdatingAnAttributeList(): void { + $middleware = <<<'PHP' + getAttributes(FutureUuidGuard::class))) { + $this->checkFutureUuid(); + } + if (!empty($reflectionMethod->getAttributes(UnrelatedGuard::class))) { + $this->checkOther(); + } + } + private function handleUuid(): void { + if (!empty($reflectionMethod->getAttributes(OtherUuidGuard::class))) { + $this->processUuid(); + } + } + private function checkFutureUuid(): void { + $this->getUuidFromRequest(); + } + private function checkOther(): void {} + } + PHP; + self::assertSame( + ['OtherUuidGuard', 'FutureUuidGuard'], + self::uuidConsumerAttributesInMiddleware($middleware), ); } } From b3a6ae122c477d523fd18a443045c8519c0664f7 Mon Sep 17 00:00:00 2001 From: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> Date: Sat, 14 Nov 2026 12:56:35 +0000 Subject: [PATCH 5/7] style(test): align middleware discovery docblock Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> --- tests/php/openapi/OpenApiRequestHeadersTest.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/php/openapi/OpenApiRequestHeadersTest.php b/tests/php/openapi/OpenApiRequestHeadersTest.php index ee7305db59..1261724748 100644 --- a/tests/php/openapi/OpenApiRequestHeadersTest.php +++ b/tests/php/openapi/OpenApiRequestHeadersTest.php @@ -81,12 +81,12 @@ private static function discoverUuidConsumerRoutes(): array { } /** - * Discover authorization attributes directly from the middleware's UUID paths. - * Attributes checked in handleUuid share its UUID lookup; beforeController's - * guards count only when their direct handler reads the request UUID. - * - * @return list - */ + * Discover authorization attributes directly from the middleware's UUID paths. + * Attributes checked in handleUuid share its UUID lookup; beforeController's + * guards count only when their direct handler reads the request UUID. + * + * @return list + */ private static function uuidConsumerAttributesInMiddleware(string $source): array { $handleUuid = self::middlewareMethodSource($source, 'handleUuid'); preg_match_all('/getAttributes\((\w+)::class\)/', $handleUuid, $matches); From 66863d32062d3eb5c8bd076920c6da537c3e388e Mon Sep 17 00:00:00 2001 From: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> Date: Sat, 14 Nov 2026 13:14:35 +0000 Subject: [PATCH 6/7] fix(signing): require signer context when requesting verification code Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> --- .../_partials/ModalVerificationCode.spec.ts | 24 ++++++++++++++++++- .../_partials/ModalVerificationCode.vue | 22 ++++++----------- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/tests/views/SignPDF/_partials/ModalVerificationCode.spec.ts b/src/tests/views/SignPDF/_partials/ModalVerificationCode.spec.ts index 3cc2d48452..9e41bbdc46 100644 --- a/src/tests/views/SignPDF/_partials/ModalVerificationCode.spec.ts +++ b/src/tests/views/SignPDF/_partials/ModalVerificationCode.spec.ts @@ -144,6 +144,7 @@ describe('ModalVerificationCode (email mode)', () => { signStore.document = { ...signStore.document, fileId: 42, + signers: [{ me: true, sign_request_uuid: '11111111-1111-4111-8111-111111111111' }], } as typeof signStore.document signMethodsStore.settings.emailToken = { @@ -162,7 +163,8 @@ describe('ModalVerificationCode (email mode)', () => { expect(axios.post).toHaveBeenCalledTimes(1) - const [, body] = vi.mocked(axios.post).mock.calls[0] + const [url, body] = vi.mocked(axios.post).mock.calls[0] + expect(url).toBe('/ocs/v2.php/apps/libresign/api/v1/sign/uuid/11111111-1111-4111-8111-111111111111/code') expect(body).toEqual({ identifyMethod: 'email', @@ -617,6 +619,7 @@ describe('ModalVerificationCode requestCode route (#8365)', () => { signStore.document = { ...signStore.document, uuid: 'id-doc-file-uuid', + fileId: 42, signers: [{ me: false, sign_request_uuid: 'external-signer-uuid', email: 'external@example.com' }], settings: { isApprover: true }, } as typeof signStore.document @@ -636,6 +639,7 @@ describe('ModalVerificationCode requestCode route (#8365)', () => { signStore.document = { ...signStore.document, uuid: 'file-uuid', + fileId: 42, signers: [{ me: true, sign_request_uuid: 'my-signer-uuid' }], settings: { isApprover: false }, } as typeof signStore.document @@ -652,6 +656,7 @@ describe('ModalVerificationCode requestCode route (#8365)', () => { signStore.document = { ...signStore.document, uuid: 'file-uuid', + fileId: 42, signers: [{ me: true, sign_request_uuid: 'my-signer-uuid' }], settings: { isApprover: true }, } as typeof signStore.document @@ -662,6 +667,23 @@ describe('ModalVerificationCode requestCode route (#8365)', () => { const [url] = vi.mocked(axios.post).mock.calls[0] expect(url).toBe('/ocs/v2.php/apps/libresign/api/v1/sign/uuid/my-signer-uuid/code') }) + it('does not request a code with fileId alone when signer UUID is unavailable', async () => { + const signStore = useSignStore() + signStore.document = { + ...signStore.document, + fileId: 42, + uuid: 'file-uuid', + signers: [], + settings: { isApprover: false }, + } as typeof signStore.document + + const wrapper = mountToken() + await wrapper.vm.requestCode() + + expect(axios.post).not.toHaveBeenCalled() + expect(wrapper.vm.tokenRequested).toBe(false) + expect(wrapper.vm.loading).toBe(false) + }) }) describe('ModalVerificationCode reusing the saved signature of an email signer', () => { diff --git a/src/views/SignPDF/_partials/ModalVerificationCode.vue b/src/views/SignPDF/_partials/ModalVerificationCode.vue index da1fa026a9..ec6e57187b 100644 --- a/src/views/SignPDF/_partials/ModalVerificationCode.vue +++ b/src/views/SignPDF/_partials/ModalVerificationCode.vue @@ -459,22 +459,14 @@ async function requestCode() { signMethod: activeTokenMethod.value, } - if (signStore.document.fileId) { - await axios.post( - generateOcsUrl('/apps/libresign/api/v1/sign/file_id/{fileId}/code', { - fileId: signStore.document.fileId, - }), - params, - ) - } else { - // Same route/context decision as signing (Sign.vue). - const signRequestUuid = getSigningRouteUuid(signStore.document) - if (!signRequestUuid) { - // TRANSLATORS Error when a submitted document cannot be found while requesting a verification code. - throw new Error(t('libresign', 'Document not found')) - } - await axios.post(signStore.buildRequestCodeUrl(signRequestUuid), params) + // Use the same signer/approver context as signing. The file_id/code route + // also requires a UUID in middleware, so fileId alone is insufficient. + const signRequestUuid = getSigningRouteUuid(signStore.document) + if (!signRequestUuid) { + // TRANSLATORS Error when a submitted document cannot be found while requesting a verification code. + throw new Error(t('libresign', 'Document not found')) } + await axios.post(signStore.buildRequestCodeUrl(signRequestUuid), params) if (props.mode === 'email') { signMethodsStore.setHasEmailConfirmCode(true) From f8baf71a5ee271df6866bd7053bfde4bfaac6bf9 Mon Sep 17 00:00:00 2001 From: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> Date: Sat, 14 Nov 2026 13:16:31 +0000 Subject: [PATCH 7/7] chore(signing): remove obsolete route helper import Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> --- src/views/SignPDF/_partials/ModalVerificationCode.vue | 1 - 1 file changed, 1 deletion(-) diff --git a/src/views/SignPDF/_partials/ModalVerificationCode.vue b/src/views/SignPDF/_partials/ModalVerificationCode.vue index ec6e57187b..d34a2d6557 100644 --- a/src/views/SignPDF/_partials/ModalVerificationCode.vue +++ b/src/views/SignPDF/_partials/ModalVerificationCode.vue @@ -147,7 +147,6 @@ import md5 from 'blueimp-md5' import axios from '@nextcloud/axios' import { showError } from '@nextcloud/dialogs' import { loadState } from '@nextcloud/initial-state' -import { generateOcsUrl } from '@nextcloud/router' import NcButton from '@nextcloud/vue/components/NcButton' import NcDialog from '@nextcloud/vue/components/NcDialog'