diff --git a/lib/Service/Policy/Runtime/PolicySource.php b/lib/Service/Policy/Runtime/PolicySource.php index e3cd38a5b9..64b5d63258 100644 --- a/lib/Service/Policy/Runtime/PolicySource.php +++ b/lib/Service/Policy/Runtime/PolicySource.php @@ -21,6 +21,8 @@ use OCP\AppFramework\Services\IAppConfig; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\Exceptions\AppConfigTypeConflictException; +use OCP\Exceptions\AppConfigUnknownKeyException; +use OCP\IAppConfig as CoreAppConfig; use OCP\IDBConnection; use OCP\IL10N; @@ -32,6 +34,7 @@ public function __construct( private PolicyRegistry $registry, private IDBConnection $db, private IL10N $l10n, + private CoreAppConfig $coreAppConfig, ) { } @@ -717,52 +720,73 @@ public function clearSystemPolicy(string $policyKey): void { } private function readSystemValue(string $key, mixed $defaultValue): mixed { - try { - if (is_int($defaultValue)) { - return $this->appConfig->getAppValueInt($key, $defaultValue); + $storedType = $this->getStoredSystemValueType($key); + if ($storedType === CoreAppConfig::VALUE_MIXED || $storedType === $this->getValueTypeOf($defaultValue)) { + return $this->readSystemValueAs($key, $defaultValue); + } + + // Older releases stored some policies with another type than the one + // written today. Nextcloud logs a warning for every read that requests + // a type other than the stored one, so read the stored type instead. + $storedValue = $this->readSystemValueAs($key, match ($storedType) { + CoreAppConfig::VALUE_INT => 0, + CoreAppConfig::VALUE_FLOAT => 0.0, + CoreAppConfig::VALUE_BOOL => false, + CoreAppConfig::VALUE_ARRAY => [], + default => '', + }); + + if (is_string($defaultValue) && is_array($storedValue)) { + try { + return json_encode($storedValue, JSON_THROW_ON_ERROR); + } catch (\JsonException) { + return $defaultValue; } + } - if (is_bool($defaultValue)) { - return $this->appConfig->getAppValueBool($key, $defaultValue); - } + if (is_array($defaultValue) && !is_string($storedValue)) { + return $defaultValue; + } - if (is_float($defaultValue)) { - return $this->appConfig->getAppValueFloat($key, $defaultValue); - } + return $storedValue; + } - if (is_array($defaultValue)) { - return $this->appConfig->getAppValueArray($key, $defaultValue); - } + private function getStoredSystemValueType(string $key): int { + try { + return $this->coreAppConfig->getValueType(Application::APP_ID, $key, false); + } catch (AppConfigUnknownKeyException) { + return CoreAppConfig::VALUE_MIXED; + } + } - return $this->appConfig->getAppValueString($key, (string)$defaultValue); - } catch (AppConfigTypeConflictException $exception) { - if (is_string($defaultValue)) { - try { - $arrayValue = $this->appConfig->getAppValueArray($key, []); - return json_encode($arrayValue, JSON_THROW_ON_ERROR); - } catch (AppConfigTypeConflictException) { - return $this->appConfig->getAppValueBool($key, in_array(strtolower(trim($defaultValue)), ['1', 'true', 'yes', 'on'], true)); - } catch (\JsonException) { - return (string)$defaultValue; - } - } + private function getValueTypeOf(mixed $value): int { + return match (true) { + is_int($value) => CoreAppConfig::VALUE_INT, + is_bool($value) => CoreAppConfig::VALUE_BOOL, + is_float($value) => CoreAppConfig::VALUE_FLOAT, + is_array($value) => CoreAppConfig::VALUE_ARRAY, + default => CoreAppConfig::VALUE_STRING, + }; + } - if (is_bool($defaultValue)) { - return $this->appConfig->getAppValueString($key, $defaultValue ? '1' : '0'); - } + private function readSystemValueAs(string $key, mixed $defaultValue): mixed { + if (is_int($defaultValue)) { + return $this->appConfig->getAppValueInt($key, $defaultValue); + } - if (is_array($defaultValue)) { - // Value was stored as a scalar (e.g., by Nextcloud provisioning API). - // Return the raw string so the policy normalizer can decode it. - try { - return $this->appConfig->getAppValueString($key, ''); - } catch (AppConfigTypeConflictException) { - return $defaultValue; - } - } + if (is_bool($defaultValue)) { + return $this->appConfig->getAppValueBool($key, $defaultValue); + } + + if (is_float($defaultValue)) { + return $this->appConfig->getAppValueFloat($key, $defaultValue); + } - throw $exception; + if (is_array($defaultValue)) { + return $this->appConfig->getAppValueArray($key, $defaultValue); } + + return $this->appConfig->getAppValueString($key, (string)$defaultValue); } private function writeSystemValue(string $key, mixed $value): void { diff --git a/tests/php/Unit/InMemoryAppConfig.php b/tests/php/Unit/InMemoryAppConfig.php index 612b9bb8a8..72146f9fe6 100644 --- a/tests/php/Unit/InMemoryAppConfig.php +++ b/tests/php/Unit/InMemoryAppConfig.php @@ -14,9 +14,22 @@ final class InMemoryAppConfig implements IAppConfig { /** @var array> */ private array $values = []; + /** @var list */ + private array $typeConflicts = []; public function reset(): void { $this->values = []; + $this->typeConflicts = []; + } + + /** + * Reads that requested another type than the stored one. Nextcloud logs + * a warning for each of them before throwing the type conflict. + * + * @return list + */ + public function getTypeConflicts(): array { + return $this->typeConflicts; } public function getApps(): array { @@ -221,6 +234,7 @@ private function getTypedValue(string $app, string $key, mixed $default, string $value = $this->values[$app][$key]; if (gettype($value) !== $expectedType) { + $this->typeConflicts[] = sprintf('%s/%s is stored as %s but was requested as %s', $app, $key, gettype($value), $expectedType); throw new AppConfigTypeConflictException( sprintf('App config value %s/%s is not of type %s', $app, $key, $expectedType), ); diff --git a/tests/php/Unit/Service/Policy/Runtime/PolicySourceTest.php b/tests/php/Unit/Service/Policy/Runtime/PolicySourceTest.php index e18101f10c..a23fdccdc8 100644 --- a/tests/php/Unit/Service/Policy/Runtime/PolicySourceTest.php +++ b/tests/php/Unit/Service/Policy/Runtime/PolicySourceTest.php @@ -48,6 +48,7 @@ use OCA\Libresign\Service\Policy\Runtime\DefaultPolicyResolver; use OCA\Libresign\Service\Policy\Runtime\PolicyRegistry; use OCA\Libresign\Service\Policy\Runtime\PolicySource; +use OCA\Libresign\Tests\Unit\InMemoryAppConfig; use OCA\Libresign\Tests\Unit\TestCase; use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Services\IAppConfig as ScopedIAppConfig; @@ -919,6 +920,42 @@ public function testLoadSystemPolicyNormalizesLegacyTypedArrayForGroupsRequestSi $this->assertSame(['{"allowGroups":["admin","finance"],"denyGroups":[]}'], $layer->getAllowedValues()); } + /** + * Older releases stored some policies with another AppConfig type than the + * policy API writes today. Nextcloud logs a warning for every read that + * requests a type other than the stored one, so these values must be read + * with their stored type. + */ + #[DataProvider('providerLegacyTypedSystemValues')] + public function testLoadSystemPolicyReadsLegacyTypedValueWithItsStoredType( + string $policyKey, + string|bool|array $storedValue, + mixed $expectedRawValue, + ): void { + match (true) { + is_bool($storedValue) => $this->coreAppConfig->setValueBool(Application::APP_ID, $policyKey, $storedValue), + is_array($storedValue) => $this->setStoredAppConfigArray($policyKey, $storedValue), + default => $this->setStoredAppConfigString($policyKey, $storedValue), + }; + $this->setStoredAppConfigString($policyKey . '.allow_child_override', '0'); + + $layer = $this->getSource()->loadSystemPolicy($policyKey); + + $this->assertInstanceOf(InMemoryAppConfig::class, $this->coreAppConfig); + $this->assertSame([], $this->coreAppConfig->getTypeConflicts()); + $this->assertNotNull($layer); + $this->assertSame('global', $layer->getScope()); + $this->assertSame($this->registry->get($policyKey)->normalizeValue($expectedRawValue), $layer->getValue()); + } + + public static function providerLegacyTypedSystemValues(): iterable { + yield 'request sign groups stored as array' => [RequestSignGroupsPolicy::KEY, ['finance', 'admin'], ['finance', 'admin']]; + yield 'footer stored as boolean' => [FooterPolicy::KEY, false, false]; + // A legacy boolean carries no approvers, so the policy keeps its default. + yield 'identification documents stored as boolean' => [IdentificationDocumentsPolicy::KEY, true, null]; + yield 'identification documents stored as array' => [IdentificationDocumentsPolicy::KEY, ['enabled' => true, 'approvers' => ['legal']], ['enabled' => true, 'approvers' => ['legal']]]; + } + public function testLoadSystemPolicyTreatsEmptyStoredValueAsNoExplicitSystemValue(): void { // Simulate a state where the policy key exists in appconfig but with // an empty string value – as can happen when the value is written @@ -2523,6 +2560,7 @@ private function getSource(): PolicySource { $this->registry, $this->db, $this->l10n, + $this->coreAppConfig, ); } }