Skip to content
Merged
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
10 changes: 10 additions & 0 deletions infection.json.dist
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
"FediE2EE\\PKD\\Features\\FetchTrait::fetchUnverifiedAuxData",
"FediE2EE\\PKD\\Features\\FetchTrait::fetchUnverifiedAuxDataByID",
"FediE2EE\\PKD\\Features\\FetchTrait::fetchRecentMerkleRoot",
"FediE2EE\\PKD\\Features\\PublishTrait::preamble",
"FediE2EE\\PKD\\Features\\VerifyTrait::fetchPublicKeys",
"FediE2EE\\PKD\\Features\\VerifyTrait::fetchAuxDataByID",
"FediE2EE\\PKD\\Features\\VerifyTrait::fetchAuxData"
]
},
Expand Down Expand Up @@ -72,17 +74,25 @@
},
"IncrementInteger": {
"ignore": [
"FediE2EE\\PKD\\Features\\VerifyTrait",
"FediE2EE\\PKD\\Features\\APTrait::ensureHttpClientConfigured"
]
},
"DecrementInteger": {
"ignore": [
"FediE2EE\\PKD\\Features\\VerifyTrait",
"FediE2EE\\PKD\\Features\\APTrait::ensureHttpClientConfigured",
"FediE2EE\\PKD\\Features\\VerifyTrait::verifyInclusionProofInternal"
]
},
"Continue_": {
"ignore": [
"FediE2EE\\PKD\\Features\\VerifyTrait"
],
},
"MatchArmRemoval": {
"ignore": [
"FediE2EE\\PKD\\Features\\VerifyTrait",
"FediE2EE\\PKD\\Features\\PublishTrait::getInternalHpke"
]
},
Expand Down
7 changes: 5 additions & 2 deletions src/Features/FetchTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
use FediE2EE\PKD\Extensions\ExtensionException;
use FediE2EE\PKD\Values\AuxData;
use FediE2EE\PKD\Crypto\Exceptions\{
CryptoException,
HttpSignatureException,
JsonException,
NetworkException,
NotImplementedException
};
NotImplementedException};
use GuzzleHttp\Exception\GuzzleException;
use ParagonIE\Certainty\Exception\CertaintyException;
use SodiumException;
use function is_array, is_null, is_string, urlencode;

Expand All @@ -32,7 +33,9 @@
*
* @return PublicKey[]
*
* @throws CertaintyException
* @throws ClientException
* @throws CryptoException
* @throws GuzzleException
* @throws HttpSignatureException
* @throws JsonException
Expand Down Expand Up @@ -71,7 +74,7 @@
$pk->setMetadata($meta);
$publicKeys[] = $pk;
}
return $publicKeys;

Check warning on line 77 in src/Features/FetchTrait.php

View workflow job for this annotation

GitHub Actions / Infection on PHP 8.4

Escaped Mutant for Mutator "ArrayOneItem": @@ @@ $pk->setMetadata($meta); $publicKeys[] = $pk; } - return $publicKeys; + return count($publicKeys) > 1 ? array_slice($publicKeys, 0, 1, true) : $publicKeys; } /**
}

/**
Expand Down Expand Up @@ -165,7 +168,7 @@
*/
public function fetchAuxDataByID(string $actor, string $auxDataTypeID): ?AuxData
{
$this->ensureHttpClientConfigured();

Check warning on line 171 in src/Features/FetchTrait.php

View workflow job for this annotation

GitHub Actions / Infection on PHP 8.4

Escaped Mutant for Mutator "MethodCallRemoval": @@ @@ */ public function fetchAuxDataByID(string $actor, string $auxDataTypeID): ?AuxData { - $this->ensureHttpClientConfigured(); + $canonical = $this->canonicalize($actor); return $this->fetchAuxDataInternal($canonical, $auxDataTypeID); }
$canonical = $this->canonicalize($actor);
return $this->fetchAuxDataInternal($canonical, $auxDataTypeID);
}
Expand Down Expand Up @@ -200,7 +203,7 @@
$body = $this->parseJsonResponse($auxDataResponse, 'fedi-e2ee:v1/api/actor/get-aux');
$this->assertKeysExist($body, ['aux-id', 'aux-type', 'aux-data', 'actor-id']);
$typeValidator = $this->registry->lookup($body['aux-type']);
} catch (ClientException | ExtensionException) {

Check warning on line 206 in src/Features/FetchTrait.php

View workflow job for this annotation

GitHub Actions / Infection on PHP 8.4

Escaped Mutant for Mutator "Catch_": @@ @@ $body = $this->parseJsonResponse($auxDataResponse, 'fedi-e2ee:v1/api/actor/get-aux'); $this->assertKeysExist($body, ['aux-id', 'aux-type', 'aux-data', 'actor-id']); $typeValidator = $this->registry->lookup($body['aux-type']); - } catch (ClientException | ExtensionException) { + } catch (ClientException) { return null; } if (!$typeValidator->isValid($body['aux-data'])) {
return null;
}
if (!$typeValidator->isValid($body['aux-data'])) {
Expand Down
7 changes: 3 additions & 4 deletions src/Features/VerifyTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
{
if (!in_array($hashFunc, $this->getSupportedHashFunctions(), true)) {
throw new ClientException(
"Unsupported hash function: {$hashFunc}. " .

Check warning on line 45 in src/Features/VerifyTrait.php

View workflow job for this annotation

GitHub Actions / Infection on PHP 8.4

Escaped Mutant for Mutator "ConcatOperandRemoval": @@ @@ if (!in_array($hashFunc, $this->getSupportedHashFunctions(), true)) { throw new ClientException( "Unsupported hash function: {$hashFunc}. " . - "Supported: " . implode(', ', $this->getSupportedHashFunctions()) + "Supported: " ); } }

Check warning on line 45 in src/Features/VerifyTrait.php

View workflow job for this annotation

GitHub Actions / Infection on PHP 8.4

Escaped Mutant for Mutator "Concat": @@ @@ { if (!in_array($hashFunc, $this->getSupportedHashFunctions(), true)) { throw new ClientException( - "Unsupported hash function: {$hashFunc}. " . - "Supported: " . implode(', ', $this->getSupportedHashFunctions()) + "Unsupported hash function: {$hashFunc}. " . implode(', ', $this->getSupportedHashFunctions()) . "Supported: " ); } }

Check warning on line 45 in src/Features/VerifyTrait.php

View workflow job for this annotation

GitHub Actions / Infection on PHP 8.4

Escaped Mutant for Mutator "ConcatOperandRemoval": @@ @@ { if (!in_array($hashFunc, $this->getSupportedHashFunctions(), true)) { throw new ClientException( - "Unsupported hash function: {$hashFunc}. " . - "Supported: " . implode(', ', $this->getSupportedHashFunctions()) + "Unsupported hash function: {$hashFunc}. " . implode(', ', $this->getSupportedHashFunctions()) ); } }

Check warning on line 45 in src/Features/VerifyTrait.php

View workflow job for this annotation

GitHub Actions / Infection on PHP 8.4

Escaped Mutant for Mutator "Concat": @@ @@ { if (!in_array($hashFunc, $this->getSupportedHashFunctions(), true)) { throw new ClientException( - "Unsupported hash function: {$hashFunc}. " . - "Supported: " . implode(', ', $this->getSupportedHashFunctions()) + "Supported: " . "Unsupported hash function: {$hashFunc}. " . implode(', ', $this->getSupportedHashFunctions()) ); } }
"Supported: " . implode(', ', $this->getSupportedHashFunctions())
);
}
Expand Down Expand Up @@ -392,7 +392,7 @@
* @throws ClientException If the format is invalid
* Supported $hashFunction 'sha256', 'sha384', 'sha512', 'blake2b'
*/
protected function decodeMerkleRoot(string $merkleRoot, string $hashFunction): string
public function decodeMerkleRoot(string $merkleRoot, string $hashFunction): string
{
$prefix = 'pkd-mr-v1:';
if (!str_starts_with($merkleRoot, $prefix)) {
Expand All @@ -403,11 +403,10 @@
$decoded = Base64UrlSafe::decodeNoPadding($encoded);

$expectedByteLen = match ($hashFunction) {
'sha256' => 32,
'blake2b', 'sha256' => 32,
'sha384' => 48,
'sha512' => 64,
'blake2b' => 32, // variable-length 8 to 512 bits (1 to 64 bytes) but 32 minimum for safety.
default => 32, // Fallback to 32 bytes as a minimum but $hashFunc should never be null.
default => throw new ClientException("Hash function not in allow list"),
};

if (strlen($decoded) < $expectedByteLen) {
Expand Down
93 changes: 91 additions & 2 deletions tests/unit/Features/VerifyTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use GuzzleHttp\HandlerStack;
use ParagonIE\ConstantTime\Base64UrlSafe;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;
use Random\RandomException;
Expand Down Expand Up @@ -677,10 +678,12 @@ public function getAuxDataType(): string
{
return 'test-type';
}

public function getRejectionReason(): string
{
return 'Invalid';
}

public function isValid(string $auxData): bool
{
return true;
Expand Down Expand Up @@ -749,10 +752,12 @@ public function getAuxDataType(): string
{
return 'test-type';
}

public function getRejectionReason(): string
{
return 'Invalid';
}

public function isValid(string $auxData): bool
{
return true;
Expand Down Expand Up @@ -812,10 +817,12 @@ public function getAuxDataType(): string
{
return 'test-type';
}

public function getRejectionReason(): string
{
return 'Invalid';
}

public function isValid(string $auxData): bool
{
return true;
Expand Down Expand Up @@ -884,10 +891,12 @@ public function getAuxDataType(): string
{
return 'wanted-type';
}

public function getRejectionReason(): string
{
return 'Invalid';
}

public function isValid(string $auxData): bool
{
return true;
Expand Down Expand Up @@ -972,10 +981,12 @@ public function getAuxDataType(): string
{
return 'test-type';
}

public function getRejectionReason(): string
{
return 'Invalid';
}

public function isValid(string $auxData): bool
{
return true;
Expand Down Expand Up @@ -1182,7 +1193,7 @@ public function testFetchPublicKeysTreeSizeCoercion(): void
'leaf-index' => $proof->index,
]],
'merkle-root' => $merkleRoot,
'tree-size' => (string) $tree->getSize(),
'tree-size' => (string)$tree->getSize(),
],
'fedi-e2ee:v1/api/actor/get-keys'
);
Expand Down Expand Up @@ -1343,10 +1354,12 @@ public function getAuxDataType(): string
{
return 'test-type';
}

public function getRejectionReason(): string
{
return 'Invalid';
}

public function isValid(string $auxData): bool
{
return true;
Expand Down Expand Up @@ -1398,10 +1411,12 @@ public function getAuxDataType(): string
{
return 'test-type';
}

public function getRejectionReason(): string
{
return 'Invalid';
}

public function isValid(string $auxData): bool
{
return true;
Expand Down Expand Up @@ -1454,10 +1469,12 @@ public function getAuxDataType(): string
{
return 'test-type';
}

public function getRejectionReason(): string
{
return 'Invalid';
}

public function isValid(string $auxData): bool
{
return true;
Expand Down Expand Up @@ -1499,7 +1516,7 @@ public function isValid(string $auxData): bool
'leaf-index' => $proof->index
]],
'merkle-root' => $merkleRoot,
'tree-size' => (string) $tree->getSize()
'tree-size' => (string)$tree->getSize()
],
'fedi-e2ee:v1/api/actor/aux-info'
);
Expand Down Expand Up @@ -1563,10 +1580,12 @@ public function getAuxDataType(): string
{
return 'test-type';
}

public function getRejectionReason(): string
{
return 'Invalid';
}

public function isValid(string $auxData): bool
{
return true;
Expand Down Expand Up @@ -1602,4 +1621,74 @@ public function isValid(string $auxData): bool

$client->fetchAuxData('alice@example.com', 'test-type');
}

public static function hashFunctionProvider(): array
{
return [
['blake2b'],
['sha256'],
['sha384'],
['sha512'],
];
}

/**
* @throws ClientException
* @throws CryptoException
* @throws NotImplementedException
* @throws SodiumException
*/
#[DataProvider("hashFunctionProvider")]
public function testHardCodedHashLengths(string $hashFunc): void
{
if ($hashFunc === 'blake2b') {
$expectedHashLength = 32;
} else {
$expectedHashLength = strlen(hash($hashFunc, '', true));
}
// strlen("pkd-mr-v1:") == 10
$expectedEncodedLength = 10 + (int) ceil($expectedHashLength * 4 / 3);
$serverPk = $this->serverKey->getPublicKey();
$client = new ReadOnlyClient('http://pkd.test', $serverPk);

// Build a simple tree with known leaves
$leaves = ['leaf1', 'leaf2', 'leaf3', 'leaf4'];
$tree = new Tree($leaves, $hashFunc);
$rawRoot = $tree->getRoot();
$this->assertNotNull($rawRoot);
$this->assertSame($expectedHashLength, strlen($rawRoot));
$merkleRoot = $tree->getEncodedRoot();
$this->assertSame($expectedEncodedLength, strlen($merkleRoot));

// We need to actually fail if the code is mutated:
$proof = $tree->getInclusionProof('leaf1');
$result = $client->verifyInclusionProof(
$hashFunc,
$merkleRoot,
'leaf1',
$proof,
$tree->getSize()
);
$this->assertTrue($result, 'Inclusion proof verification failed');
}

public static function merkleRootProvider(): array
{
return [
['blake2b', 32],
['sha256', 32],
['sha384', 48],
['sha512', 64],
];
}

#[DataProvider("merkleRootProvider")]
public function testDecodeMerkleRoot(string $hashFunc, int $zeroes): void
{
$serverPk = $this->serverKey->getPublicKey();
$client = new ReadOnlyClient('http://pkd.test', $serverPk);
$encoded = (new Tree([], $hashFunc))->getEncodedRoot();
$out = $client->decodeMerkleRoot($encoded, $hashFunc);
$this->assertSame($zeroes, strlen($out));
}
}
Loading
Loading