Skip to content

A free open source IT asset/license management system

Tighten png validation on signatures (follow up to 76rv-6364)

snipe59a576068e4c
diff --git a/app/Http/Controllers/Account/AcceptanceController.php b/app/Http/Controllers/Account/AcceptanceController.php
index 530fec523f..d122f07edf 100644
--- a/app/Http/Controllers/Account/AcceptanceController.php
+++ b/app/Http/Controllers/Account/AcceptanceController.php
@@ -171,26 +171,36 @@ class AcceptanceController extends Controller
return redirect()->back()->with('error', trans('general.shitty_browser'));
}
- // Validate the decoded bytes are an image and bound dimensions
+ // Validate the decoded bytes are a PNG and bound dimensions
// BEFORE allocating any pixel buffer. getimagesizefromstring
// reads headers only, so a crafted payload with large declared
// width/height is rejected without imagecreatefromstring
// decoding the raster or the flatten path allocating a second
- // same-size buffer.
+ // same-size buffer. IMAGETYPE_PNG is enforced because the
+ // signature-pad JavaScript creates only PNG files.
$imgInfo = @getimagesizefromstring($decoded_image);
- if ($imgInfo === false || $imgInfo[0] > 2000 || $imgInfo[1] > 2000) {
+ if ($imgInfo === false
+ || $imgInfo[2] !== IMAGETYPE_PNG
+ || $imgInfo[0] > 2000
+ || $imgInfo[1] > 2000
+ ) {
return redirect()->back()->with('error', trans('general.shitty_browser'));
}
- $flattened = $this->flattenSignatureBackgroundToWhite($decoded_image);
- if ($flattened !== null) {
- $decoded_image = $flattened;
+ // Flatten the signature's transparent background onto
+ // white and canonically re-encode. Fail-closed if that
+ // round-trip does not produce bytes: the previous
+ // "keep the original bytes" fallback let header-only
+ // PNG payloads (valid IHDR, no IDAT) through
+ // getimagesizefromstring's shallow check and down to
+ // Storage::put under a .png filename. Reported by
+ // Wojciech Ciemski (WojciechCiemski) post-FD-57825 fix.
+ $decoded_image = $this->flattenSignatureBackgroundToWhite($decoded_image);
+ if ($decoded_image === null) {
+ Log::warning('Acceptance signature re-encode failed. Rejecting the submission to avoid storing non-decodable bytes.');
+
+ return redirect()->back()->with('error', trans('general.shitty_browser'));
}
- // Keep the original validated bytes if flattening is unavailable
- // (no GD) or allocation failed. Dimensions already clear the
- // 2000x2000 cap, and getimagesizefromstring already confirmed
- // the content is a real image rather than attacker-supplied
- // text passed through base64.
$encodedSignatureImage = base64_encode($decoded_image);
// Storage::put returns false on silent write failures on
@@ -484,10 +494,9 @@ class AcceptanceController extends Controller
private function flattenSignatureBackgroundToWhite(string $signatureBinary): ?string
{
// Fail-closed on every unexpected branch rather than returning
- // the original bytes. The previous shape fell through on
- // decode failure, which let non-image input survive to the
- // Storage::put below and land in private_uploads/signatures
- // with a .png filename.
+ // the original bytes. Each branch below returns null so the
+ // the submission gets rejected instead of storing an
+ // unverified blob under a .png filename.
if (! function_exists('imagecreatefromstring') || ! function_exists('imagecreatetruecolor')) {
return null;
}
@@ -513,12 +522,20 @@ class AcceptanceController extends Controller
imagecopy($flattened, $source, 0, 0, 0, 0, $width, $height);
ob_start();
- imagepng($flattened);
+ $encoded = @imagepng($flattened);
$output = ob_get_clean();
imagedestroy($source);
imagedestroy($flattened);
- return is_string($output) ? $output : $signatureBinary;
+ // Fail-closed if either imagepng() reported failure or the
+ // output buffer did not produce a string. Previously fell back
+ // to the original bytes, which bypassed the canonicalization
+ // guarantee the caller is relying on.
+ if ($encoded === false || !is_string($output) || $output === '') {
+ return null;
+ }
+
+ return $output;
}
}
diff --git a/tests/Feature/CheckoutAcceptances/AcceptanceSignatureImageValidationTest.php b/tests/Feature/CheckoutAcceptances/AcceptanceSignatureImageValidationTest.php
index 8ec43fceeb..4d9a03a97d 100644
--- a/tests/Feature/CheckoutAcceptances/AcceptanceSignatureImageValidationTest.php
+++ b/tests/Feature/CheckoutAcceptances/AcceptanceSignatureImageValidationTest.php
@@ -50,10 +50,10 @@ class AcceptanceSignatureImageValidationTest extends TestCase
{
$signature = "\x89PNG\r\n\x1a\n";
$ihdrData = pack('NNCCCCC', $width, $height, 8, 2, 0, 0, 0);
- $ihdrChunk = pack('N', 13) . 'IHDR' . $ihdrData . pack('N', crc32('IHDR' . $ihdrData));
- $iendChunk = pack('N', 0) . 'IEND' . pack('N', crc32('IEND'));
+ $ihdrChunk = pack('N', 13).'IHDR'.$ihdrData.pack('N', crc32('IHDR'.$ihdrData));
+ $iendChunk = pack('N', 0).'IEND'.pack('N', crc32('IEND'));
- return $signature . $ihdrChunk . $iendChunk;
+ return $signature.$ihdrChunk.$iendChunk;
}
public function test_signature_output_with_non_image_bytes_is_rejected_and_nothing_is_written(): void
@@ -67,7 +67,7 @@ class AcceptanceSignatureImageValidationTest extends TestCase
$marker = 'SNIPE-CAND-11-NOT-A-PNG';
$payload = [
'asset_acceptance' => 'accepted',
- 'signature_output' => 'data:image/png;base64,' . base64_encode($marker),
+ 'signature_output' => 'data:image/png;base64,'.base64_encode($marker),
];
$this->actingAs($target)
@@ -96,7 +96,7 @@ class AcceptanceSignatureImageValidationTest extends TestCase
$bombHeader = $this->pngWithIhdrDimensions(10000, 10000);
$payload = [
'asset_acceptance' => 'accepted',
- 'signature_output' => 'data:image/png;base64,' . base64_encode($bombHeader),
+ 'signature_output' => 'data:image/png;base64,'.base64_encode($bombHeader),
];
$this->actingAs($target)
@@ -118,7 +118,7 @@ class AcceptanceSignatureImageValidationTest extends TestCase
$payload = [
'asset_acceptance' => 'accepted',
- 'signature_output' => 'data:image/png;base64,' . str_repeat('A', 2_100_000),
+ 'signature_output' => 'data:image/png;base64,'.str_repeat('A', 2_100_000),
];
$this->actingAs($target)
@@ -129,6 +129,77 @@ class AcceptanceSignatureImageValidationTest extends TestCase
$this->assertNull($acceptance->accepted_at);
}
+ /**
+ * Reporter-identified residual (Wojciech Ciemski post-fd61abe
+ * patch): a header-only PNG (valid signature + IHDR + IEND, no
+ * IDAT) passes getimagesizefromstring's shallow check,
+ * imagecreatefromstring fails on it, and the earlier shape then
+ * kept the original attacker bytes and wrote them under a .png
+ * filename. Verify the submission is now rejected and no file is
+ * written.
+ */
+ public function test_signature_output_with_valid_header_but_no_idat_is_rejected(): void
+ {
+ Storage::fake();
+ $this->enableRequiredSignatures();
+
+ $target = User::factory()->create();
+ $acceptance = $this->pendingAcceptance($target);
+
+ // Valid signature + IHDR + IEND. No IDAT, so the decoder can't
+ // reconstruct a raster even though getimagesizefromstring is
+ // satisfied by the IHDR alone. Attacker could tack arbitrary
+ // bytes on the end up to the size cap.
+ $headerOnlyPng = $this->pngWithIhdrDimensions(2, 2).'ATTACKER-CONTROLLED-TRAILING-BYTES';
+
+ $this->actingAs($target)
+ ->post(route('account.store-acceptance', $acceptance), [
+ 'asset_acceptance' => 'accepted',
+ 'signature_output' => 'data:image/png;base64,'.base64_encode($headerOnlyPng),
+ ])
+ ->assertSessionHas('error');
+
+ $acceptance->refresh();
+ $this->assertNull($acceptance->accepted_at, 'A header-only PNG must not finalize the acceptance.');
+ $this->assertEmpty(Storage::disk()->files('private_uploads/signatures'), 'A header-only PNG must not land any file under signatures/.');
+ }
+
+ /**
+ * Reporter-identified residual (Wojciech Ciemski post-fd61abe
+ * patch): the IMAGETYPE check used to accept any image type
+ * getimagesizefromstring recognized, including JPEG / GIF / BMP /
+ * WEBP. A valid JPEG then landed under a .png filename.
+ */
+ public function test_signature_output_with_non_png_image_type_is_rejected(): void
+ {
+ Storage::fake();
+ $this->enableRequiredSignatures();
+
+ $target = User::factory()->create();
+ $acceptance = $this->pendingAcceptance($target);
+
+ // Real JPEG (not a PNG with lying MIME, actual JPEG bytes).
+ // getimagesizefromstring returns IMAGETYPE_JPEG, the strict
+ // IMAGETYPE_PNG check now fires, and the request is rejected
+ // before flattening.
+ $canvas = imagecreatetruecolor(16, 16);
+ ob_start();
+ imagejpeg($canvas);
+ $jpegBytes = (string) ob_get_clean();
+ imagedestroy($canvas);
+
+ $this->actingAs($target)
+ ->post(route('account.store-acceptance', $acceptance), [
+ 'asset_acceptance' => 'accepted',
+ 'signature_output' => 'data:image/png;base64,'.base64_encode($jpegBytes),
+ ])
+ ->assertSessionHas('error');
+
+ $acceptance->refresh();
+ $this->assertNull($acceptance->accepted_at, 'A JPEG masquerading as a PNG must not finalize the acceptance.');
+ $this->assertEmpty(Storage::disk()->files('private_uploads/signatures'), 'A non-PNG image type must not land any file under signatures/.');
+ }
+
public function test_legitimate_tiny_png_signature_still_finalizes_acceptance(): void
{
// Companion: the fix must not break the happy path. A real tiny
@@ -142,7 +213,7 @@ class AcceptanceSignatureImageValidationTest extends TestCase
$payload = [
'asset_acceptance' => 'accepted',
- 'signature_output' => 'data:image/png;base64,' . base64_encode($this->tinyPng()),
+ 'signature_output' => 'data:image/png;base64,'.base64_encode($this->tinyPng()),
];
$this->actingAs($target)