Skip to content

Commit 7e25e53

Browse files
committed
fix(http-signature): reject ambiguous signature input and malformed requests
1 parent 3519a43 commit 7e25e53

3 files changed

Lines changed: 128 additions & 7 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
- Signature validators ignore header name case. Before, an uncovered `Authorization` header was not detected
1515
- Signature validators return `false` for a non-string body instead of throwing a `TypeError`
1616
- `createSignatureHeaders` and `createHeaders` now treat the body `"0"` as a body
17+
- `createHeaders` no longer adds unsigned `Content-*` headers for an empty-string body
18+
- Signature validators return `false` for header names that differ only by case, header values with line breaks, a `Signature-Input` with more than one `sig1=`, and a missing or non-string `method` or `url`
1719

1820
### Added
1921
- GitHub Actions CI: PHPUnit, PHPStan and Pint

‎lib/Utils/HttpSignature.php‎

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,9 @@ function createSignatureInput(array $components, string $keyId): string
315315
function validateSignatureHeaders(array $request): bool
316316
{
317317
$request = normalizeHeaders($request);
318+
if ($request === null) {
319+
return false;
320+
}
318321
$sig = $request['headers']['signature'] ?? null;
319322
$sigInput = $request['headers']['signature-input'] ?? null;
320323
if (! $sig || ! $sigInput || ! is_string($sig) || ! is_string($sigInput)) {
@@ -339,6 +342,9 @@ function validateSignatureHeaders(array $request): bool
339342
function validateSignature(array $clientKey, array $request): bool
340343
{
341344
$request = normalizeHeaders($request);
345+
if ($request === null) {
346+
return false;
347+
}
342348
$sig = $request['headers']['signature'] ?? '';
343349
$sigInput = $request['headers']['signature-input'] ?? '';
344350

@@ -397,6 +403,10 @@ function publicKeyFromJwk(array $jwk): ?string
397403
function sigInputToChallenge(string $sigInput, array $request): ?string
398404
{
399405
$request = normalizeHeaders($request);
406+
if ($request === null || ! is_string($request['method'] ?? null) || ! is_string($request['url'] ?? null)) {
407+
return null;
408+
}
409+
400410
$sigInputComponents = getSigInputComponents($sigInput);
401411

402412
if ($sigInputComponents === null || ! validateSigInputComponents($sigInputComponents, $request)) {
@@ -407,15 +417,22 @@ function sigInputToChallenge(string $sigInput, array $request): ?string
407417

408418
foreach ($sigInputComponents as $component) {
409419
if ($component === '@method') {
410-
$signatureBase .= '"@method": '.strtoupper($request['method'])."\n";
420+
$value = strtoupper($request['method']);
411421
} elseif ($component === '@target-uri') {
412-
$signatureBase .= '"@target-uri": '.$request['url']."\n";
422+
$value = $request['url'];
413423
} else {
414-
$signatureBase .= "\"$component\": ".($request['headers'][$component] ?? '')."\n";
424+
$value = $request['headers'][$component] ?? '';
425+
}
426+
427+
// A line break in a value could inject extra lines into the signature base.
428+
if ((! is_string($value) && ! is_int($value)) || preg_match('/[\r\n]/', (string) $value)) {
429+
return null;
415430
}
431+
432+
$signatureBase .= "\"$component\": $value\n";
416433
}
417434

418-
$signatureBase .= '"@signature-params": '.str_replace('sig1=', '', $request['headers']['signature-input'] ?? '');
435+
$signatureBase .= '"@signature-params": '.substr($sigInput, strlen('sig1='));
419436

420437
return $signatureBase;
421438
}
@@ -429,6 +446,11 @@ function sigInputToChallenge(string $sigInput, array $request): ?string
429446
*/
430447
function getSigInputComponents(string $sigInput): ?array
431448
{
449+
// Only a single "sig1" signature is supported.
450+
if (! str_starts_with($sigInput, 'sig1=') || substr_count($sigInput, 'sig1=') !== 1) {
451+
return null;
452+
}
453+
432454
$messageComponents = explode('sig1=', $sigInput)[1] ?? '';
433455

434456
$components = explode(';', $messageComponents)[0] ?? '';
@@ -439,12 +461,19 @@ function getSigInputComponents(string $sigInput): ?array
439461

440462
/**
441463
* Lowercases header names so lookups do not depend on the caller's casing.
464+
* Returns null if headers is not an array or has names that differ only by case,
465+
* since it is not clear which of those values was signed.
442466
*/
443-
function normalizeHeaders(array $request): array
467+
function normalizeHeaders(array $request): ?array
444468
{
445-
$request['headers'] = array_change_key_case($request['headers'] ?? [], CASE_LOWER);
469+
$headers = $request['headers'] ?? [];
470+
if (! is_array($headers)) {
471+
return null;
472+
}
473+
474+
$request['headers'] = array_change_key_case($headers, CASE_LOWER);
446475

447-
return $request;
476+
return count($request['headers']) === count($headers) ? $request : null;
448477
}
449478

450479
/**
@@ -465,6 +494,9 @@ function validateSigInputComponents(array $sigInputComponents, array $request):
465494
}
466495

467496
$request = normalizeHeaders($request);
497+
if ($request === null) {
498+
return false;
499+
}
468500
$body = $request['body'] ?? '';
469501
$contentDigest = $request['headers']['content-digest'] ?? null;
470502

‎tests/Http/Signature/ValidateSignatureTest.php‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,91 @@ public function test_validate_signature_returns_false_for_invalid_input()
173173
$this->assertFalse(\OpenPayments\Utils\validateSignature(['kty' => 'OKP', 'crv' => 'Ed25519', 'x' => 'short'], $request));
174174
$this->assertFalse(\OpenPayments\Utils\validateSignature(['kty' => 'EC', 'crv' => 'P-256', 'x' => $jwk['x']], $request));
175175
}
176+
177+
public function test_validate_signature_rejects_tampered_method_url_or_authorization()
178+
{
179+
$jwk = \OpenPayments\Utils\generateJwk($this->keyId, $this->privateKey);
180+
$request = $this->signedRequest('POST', '{"amount":"1"}');
181+
182+
$tampered = $request;
183+
$tampered['method'] = 'PUT';
184+
$this->assertFalse(\OpenPayments\Utils\validateSignature($jwk, $tampered));
185+
186+
$tampered = $request;
187+
$tampered['url'] = 'https://example.com/outgoing-payments';
188+
$this->assertFalse(\OpenPayments\Utils\validateSignature($jwk, $tampered));
189+
190+
$tampered = $request;
191+
$tampered['headers']['authorization'] = 'GNAP other-token';
192+
$this->assertFalse(\OpenPayments\Utils\validateSignature($jwk, $tampered));
193+
}
194+
195+
public function test_validate_signature_accepts_byte_sequence_form()
196+
{
197+
$jwk = \OpenPayments\Utils\generateJwk($this->keyId, $this->privateKey);
198+
$request = $this->signedRequest('POST', '{"amount":"1"}');
199+
$request['headers']['signature'] = 'sig1=:'.substr($request['headers']['signature'], strlen('sig1=')).':';
200+
201+
$this->assertTrue(\OpenPayments\Utils\validateSignature($jwk, $request));
202+
}
203+
204+
public function test_rejects_header_names_that_differ_only_by_case()
205+
{
206+
$jwk = \OpenPayments\Utils\generateJwk($this->keyId, $this->privateKey);
207+
$request = $this->signedRequest('POST', '{"amount":"1"}', lowercase: false);
208+
$request['headers'] = ['authorization' => 'GNAP other-token'] + $request['headers'];
209+
210+
$this->assertFalse(\OpenPayments\Utils\validateSignatureHeaders($request));
211+
$this->assertFalse(\OpenPayments\Utils\validateSignature($jwk, $request));
212+
}
213+
214+
public function test_rejects_line_break_in_covered_header()
215+
{
216+
$jwk = \OpenPayments\Utils\generateJwk($this->keyId, $this->privateKey);
217+
$request = $this->signedRequest('GET', null, ['Authorization' => "GNAP token\n\"content-type\": text/plain"]);
218+
219+
$this->assertFalse(\OpenPayments\Utils\validateSignature($jwk, $request));
220+
}
221+
222+
public function test_rejects_signature_input_with_more_than_one_label()
223+
{
224+
$request = $this->signedRequest('POST', '{"amount":"1"}');
225+
$request['headers']['signature-input'] = str_replace('"authorization"', '"authorization"sig1=', $request['headers']['signature-input']);
226+
227+
$this->assertFalse(\OpenPayments\Utils\validateSignatureHeaders($request));
228+
}
229+
230+
public function test_validate_signature_returns_false_for_malformed_request()
231+
{
232+
$jwk = \OpenPayments\Utils\generateJwk($this->keyId, $this->privateKey);
233+
$request = $this->signedRequest('POST', '{"amount":"1"}');
234+
235+
$noMethod = $request;
236+
unset($noMethod['method']);
237+
$this->assertFalse(\OpenPayments\Utils\validateSignature($jwk, $noMethod));
238+
239+
$noUrl = $request;
240+
unset($noUrl['url']);
241+
$this->assertFalse(\OpenPayments\Utils\validateSignature($jwk, $noUrl));
242+
243+
$arrayHeader = $request;
244+
$arrayHeader['headers']['authorization'] = ['GNAP token'];
245+
$this->assertFalse(\OpenPayments\Utils\validateSignature($jwk, $arrayHeader));
246+
247+
$stringHeaders = $request;
248+
$stringHeaders['headers'] = 'not-an-array';
249+
$this->assertFalse(\OpenPayments\Utils\validateSignature($jwk, $stringHeaders));
250+
$this->assertFalse(\OpenPayments\Utils\validateSignatureHeaders($stringHeaders));
251+
}
252+
253+
public function test_create_headers_skips_content_headers_for_empty_body()
254+
{
255+
$headers = \OpenPayments\Utils\createHeaders([
256+
'request' => ['method' => 'POST', 'url' => 'https://example.com/incoming-payments', 'headers' => [], 'body' => ''],
257+
'privateKey' => $this->privateKey,
258+
'keyId' => $this->keyId,
259+
]);
260+
261+
$this->assertSame(['Signature', 'Signature-Input'], array_keys($headers));
262+
}
176263
}

0 commit comments

Comments
 (0)