user() || ! $access->governanceAvailable() || ! preg_match('#^(perfil/|perfis/|minhas-oportunidades)#', $request->path())) { return $next($request); } try { $response = $next($request); } catch (\Throwable $error) { $status = $error instanceof HttpExceptionInterface ? $error->getStatusCode() : ($error instanceof ValidationException ? 422 : 500); $this->record($request, $access, $status, $error instanceof ValidationException ? $this->qaSaveDiagnostic($request, $error) : []); throw $error; } if ($response->getStatusCode() >= 400) { // Laravel's routing pipeline can render the exception before returning here. $errors = $response instanceof JsonResponse && $response->getStatusCode() === 422 ? ($response->getData(true)['errors'] ?? []) : []; $metadata = is_array($errors) && $errors !== [] ? $this->qaSaveDiagnostic($request, ValidationException::withMessages($errors)) : []; $this->record($request, $access, $response->getStatusCode(), $metadata); } return $response; } /** Temporary metadata-only QA diagnostic; never record values or validation messages. */ private function qaSaveDiagnostic(Request $request, ValidationException $error): array { $qaPath = str_replace('\\', '/', base_path()); if (! str_ends_with($qaPath, '/portal-stability-source-20261011_083742') || time() > strtotime('2026-10-14T10:00:00+00:00') || (int) $request->user()->id !== 21 || $request->method() !== 'PUT' || $request->path() !== 'minhas-oportunidades/32/draft') { return []; } $target = DB::selectOne('SELECT current_database() AS database, inet_server_port() AS port'); if ($target->database !== 'stability_consumers_20261011_084534_test' || (int) $target->port !== 55441) { return []; } $allowed = ['reason', 'etag', 'creation_key', 'title', 'summary', 'description', 'sector_id', 'promoter_type', 'promoter_name', 'opportunity_category', 'organization_id', 'maturity_level', 'maturity_stage', 'maturity_catalogue_version', 'estimated_investment_amount', 'estimated_investment_currency', 'canonical', 'canonical.official_title', 'canonical.executive_summary', 'canonical.demand_statement', 'canonical.opportunity_category']; $fields = array_keys($error->errors()); $safeFields = array_values(array_intersect($fields, $allowed)); $failedRules = $error->validator->failed(); $safeRules = []; foreach ($safeFields as $field) { $safeRules[$field] = array_values(array_intersect(array_keys($failedRules[$field] ?? []), ['Required', 'String', 'Min', 'Max', 'Size', 'Uuid', 'In', 'Exists', 'Integer', 'Array'])); } $length = static fn ($value) => is_string($value) ? mb_strlen($value) : null; return ['qa_save_diagnostic' => ['case' => 32, 'expires_at' => '2026-10-14T10:00:00+00:00', 'validation_fields' => $safeFields, 'unlisted_validation_fields_count' => count($fields) - count($safeFields), 'validation_rules' => $safeRules, 'reason_present' => $request->exists('reason'), 'reason_characters' => $length($request->input('reason')), 'demand_statement_characters' => $length($request->input('canonical.demand_statement')), 'field_contents_and_authentication_not_recorded' => true]]; } private function record(Request $request, PortalProfileAccess $access, int $status, array $metadata = []): void { $access->audit($request->user(), 'request_denied', ['method' => $request->method(), 'path' => $request->path(), 'status' => $status, ...$metadata]); } }