Skip to content

Commit ad6714d

Browse files
authored
Merge pull request joomla#19 from tfeuerst/snipersister/tuf-j5
Fix invalid canonical JSON of TargetsMetadata in php-tuf
2 parents 765e023 + 35ceeb7 commit ad6714d

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#
2+
#
3+
--- a/src/Metadata/MetadataBase.php
4+
+++ b/src/Metadata/MetadataBase.php (date 1688224003871)
5+
@@ -51,19 +51,6 @@
6+
{
7+
}
8+
9+
- /**
10+
- * Returns a normalized array version of this object for JSON encoding.
11+
- *
12+
- * @see ::toCanonicalJson()
13+
- *
14+
- * @return array
15+
- * A normalized array representation of this object.
16+
- */
17+
- protected function toNormalizedArray(): array
18+
- {
19+
- return $this->getSigned();
20+
- }
21+
-
22+
/**
23+
* Returns a canonical JSON representation of this metadata object.
24+
*
25+
@@ -72,7 +59,7 @@
26+
*/
27+
public function toCanonicalJson(): string
28+
{
29+
- return static::encodeJson($this->toNormalizedArray());
30+
+ return static::encodeJson($this->getSigned());
31+
}
32+
33+
/**
34+
35+
36+
--- a/src/Metadata/TargetsMetadata.php
37+
+++ b/src/Metadata/TargetsMetadata.php (date 1688224236309)
38+
@@ -67,26 +67,32 @@
39+
40+
/**
41+
* {@inheritDoc}
42+
+ * Returns a canonical JSON representation of this metadata object.
43+
+ *
44+
+ * @return string
45+
+ * The canonical JSON representation of this object.
46+
*/
47+
- protected function toNormalizedArray(): array
48+
+ public function toCanonicalJson(): string
49+
{
50+
- $normalized = parent::toNormalizedArray();
51+
+ $metadata = $this->getSigned();
52+
53+
- foreach ($normalized['targets'] as $path => $target) {
54+
+ // Apply sorting
55+
+ self::sortKeys($metadata);
56+
+
57+
+ foreach ($metadata['targets'] as $path => $target) {
58+
// Custom target info should always encode to an object, even if
59+
// it's empty.
60+
if (array_key_exists('custom', $target)) {
61+
- $normalized['targets'][$path]['custom'] = (object) $target['custom'];
62+
+ $metadata['targets'][$path]['custom'] = (object) $target['custom'];
63+
}
64+
}
65+
66+
// Ensure that these will encode as objects even if they're empty.
67+
- $normalized['targets'] = (object) $normalized['targets'];
68+
- if (array_key_exists('delegations', $normalized)) {
69+
- $normalized['delegations']['keys'] = (object) $normalized['delegations']['keys'];
70+
+ $metadata['targets'] = (object) $metadata['targets'];
71+
+ if (array_key_exists('delegations', $metadata)) {
72+
+ $metadata['delegations']['keys'] = (object)$metadata['delegations']['keys'];
73+
}
74+
-
75+
- return $normalized;
76+
+ return static::encodeJson($metadata);
77+
}
78+
79+
/**
80+
81+
82+
--- a/src/CanonicalJsonTrait.php
83+
+++ b/src/CanonicalJsonTrait.php (date 1688223879893)
84+
@@ -53,7 +53,7 @@
85+
* @throws \RuntimeException
86+
* Thrown if sorting the array fails.
87+
*/
88+
- private static function sortKeys(array &$data): void
89+
+ protected static function sortKeys(array &$data): void
90+
{
91+
// If $data is numerically indexed, the keys are already sorted, by
92+
// definition.

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@
134134
"patches": {
135135
"php-tuf/php-tuf": {
136136
"Patch level optional on TUF signed metadata": "./build/composer_patches/5.0.0-2023-07-01_php-tuf_meta.patch",
137-
"Sort canonical json": "./build/composer_patches/5.0.0-2023-07-01_php-tuf_sort-canonical-json.patch"
137+
"Sort canonical json": "./build/composer_patches/5.0.0-2023-07-01_php-tuf_sort-canonical-json.patch",
138+
"Fix invalid canonical JSON of TargetsMetadata": "./build/composer_patches/5.0.0-2023-07-01_php-tuf_canonical-json-targetsMetadata.patch"
138139
}
139140
}
140141
},

0 commit comments

Comments
 (0)