Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 61 additions & 37 deletions lib/Service/Policy/Runtime/PolicySource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -32,6 +34,7 @@ public function __construct(
private PolicyRegistry $registry,
private IDBConnection $db,
private IL10N $l10n,
private CoreAppConfig $coreAppConfig,
) {
}

Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions tests/php/Unit/InMemoryAppConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,22 @@
final class InMemoryAppConfig implements IAppConfig {
/** @var array<string, array<string, string|int|float|bool|array>> */
private array $values = [];
/** @var list<string> */
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<string>
*/
public function getTypeConflicts(): array {
return $this->typeConflicts;
}

public function getApps(): array {
Expand Down Expand Up @@ -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),
);
Expand Down
38 changes: 38 additions & 0 deletions tests/php/Unit/Service/Policy/Runtime/PolicySourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -2523,6 +2560,7 @@ private function getSource(): PolicySource {
$this->registry,
$this->db,
$this->l10n,
$this->coreAppConfig,
);
}
}