Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.

Commit de8507d

Browse files
author
Joshua Moon
committed
.ASC signature testing.
1 parent 6d3592c commit de8507d

File tree

9 files changed

+231
-59
lines changed

9 files changed

+231
-59
lines changed

src/Detectives/PlaintextDetective.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace InfinityNext\Sleuth\Detectives;
4+
5+
use InfinityNext\Sleuth\Contracts\DetectiveContract;
6+
use InfinityNext\Sleuth\Traits\DetectiveTrait;
7+
8+
/**
9+
* Handles files which are plain text but which may conform to container standards.
10+
*/
11+
class PlaintextDetective implements DetectiveContract
12+
{
13+
use DetectiveTrait;
14+
15+
protected $pgpType = null;
16+
17+
const PGP_INVALID = 0;
18+
const PGP_PUBKEY = 1;
19+
const PGP_SIGNATURE = 2;
20+
const PGP_CLEARSIGN = 3;
21+
22+
/**
23+
* Checks if thise file is an 'Armored ASCII' PGP file.
24+
*
25+
* @return boolean|null
26+
*/
27+
protected function leadASC()
28+
{
29+
$contents = trim($this->getFileContents());
30+
31+
if (mb_ereg_match("^-+BEGIN PGP PUBLIC KEY BLOCK-+.*-+END PGP PUBLIC KEY BLOCK-+$", $contents)) {
32+
$this->pgpType = static::PGP_PUBKEY;
33+
return $this->closeCase("asc", "text/plain");
34+
}
35+
elseif (mb_ereg_match("^-+BEGIN PGP SIGNED MESSAGE-+.*-+BEGIN PGP SIGNATURE-+.*-+END PGP SIGNATURE-+$", $contents)) {
36+
$this->pgpType = static::PGP_CLEARSIGN;
37+
return $this->closeCase("asc", "text/plain");
38+
}
39+
elseif (mb_ereg_match("^-+BEGIN PGP SIGNATURE-+.*-+END PGP SIGNATURE-+$", $contents)) {
40+
$this->pgpType = static::PGP_SIGNATURE;
41+
return $this->closeCase("asc", "text/plain");
42+
}
43+
44+
return null;
45+
}
46+
47+
public function isPgp()
48+
{
49+
return !is_null($this->pgpType);
50+
}
51+
52+
public function getPgpType()
53+
{
54+
return $this->pgpType;
55+
}
56+
57+
/**
58+
* Can this file type potentially cause damage or intrude on a user's privacy?
59+
* This means executable programs, or file formats that can contact remote servers in any way (even SVGs).
60+
*
61+
* @return boolean
62+
* @throws \InfinityNext\Sleuth\Exceptions\CaseNotSolved
63+
*/
64+
public function isRisky()
65+
{
66+
parent::isRisky();
67+
68+
return false;
69+
}
70+
71+
public static function on()
72+
{
73+
return function_exists("mb_ereg_match");
74+
}
75+
}

src/Detectives/ffmpegDetective.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php namespace InfinityNext\Sleuth\Detectives;
1+
<?php
2+
3+
namespace InfinityNext\Sleuth\Detectives;
24

35
use InfinityNext\Sleuth\Contracts\DetectiveContract;
46
use InfinityNext\Sleuth\Traits\DetectiveTrait;

src/Detectives/svgDetective.php

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,53 +6,53 @@
66

77
class svgDetective implements DetectiveContract
88
{
9-
use DetectiveTrait;
10-
11-
/**
12-
* Checks if thise file is a valid SVG.
13-
*
14-
* @return boolean|null
15-
*/
16-
protected function leadSVG()
17-
{
18-
// Create a new sanitizer instance
19-
$sanitizer = new Sanitizer();
20-
21-
// Load the dirty svg
22-
$dirtySVG = file_get_contents($this->file);
23-
24-
// Pass it to the sanitizer and get it back clean
25-
$cleanSVG = $sanitizer->sanitize($dirtySVG);
26-
27-
if (is_string($cleanSVG))
28-
{
29-
return $this->closeCase("svg", "image/svg+xml");
30-
}
31-
32-
return false;
33-
}
34-
35-
/**
36-
* Can this file type potentially cause damage or intrude on a user's privacy?
37-
* This means executable programs, or file formats that can contact remote servers in any way (even SVGs).
38-
*
39-
* @return boolean
40-
* @throws \InfinityNext\Sleuth\Exceptions\CaseNotSolved
41-
*/
42-
public function isRisky()
43-
{
44-
parent::isRisky();
45-
46-
return true;
47-
}
48-
49-
/**
50-
* Can the system run this Detective?
51-
*
52-
* @return boolean True if we can run, False if not.
53-
*/
54-
public static function on()
55-
{
56-
return @class_exists(Sanitizer::class);
57-
}
9+
use DetectiveTrait;
10+
11+
/**
12+
* Checks if thise file is a valid SVG.
13+
*
14+
* @return boolean|null
15+
*/
16+
protected function leadSVG()
17+
{
18+
// Create a new sanitizer instance
19+
$sanitizer = new Sanitizer();
20+
21+
// Load the dirty svg
22+
$dirtySVG = file_get_contents($this->file);
23+
24+
// Pass it to the sanitizer and get it back clean
25+
$cleanSVG = $sanitizer->sanitize($dirtySVG);
26+
27+
if (is_string($cleanSVG))
28+
{
29+
return $this->closeCase("svg", "image/svg+xml");
30+
}
31+
32+
return false;
33+
}
34+
35+
/**
36+
* Can this file type potentially cause damage or intrude on a user's privacy?
37+
* This means executable programs, or file formats that can contact remote servers in any way (even SVGs).
38+
*
39+
* @return boolean
40+
* @throws \InfinityNext\Sleuth\Exceptions\CaseNotSolved
41+
*/
42+
public function isRisky()
43+
{
44+
parent::isRisky();
45+
46+
return true;
47+
}
48+
49+
/**
50+
* Can the system run this Detective?
51+
*
52+
* @return boolean True if we can run, False if not.
53+
*/
54+
public static function on()
55+
{
56+
return @class_exists(Sanitizer::class);
57+
}
5858
}

tests/DocTXTTest.php

Lines changed: 0 additions & 9 deletions
This file was deleted.

tests/PgpASCTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
use InfinityNext\Sleuth\Detectives\PlaintextDetective;
4+
use PHPUnit\Framework\TestCase;
5+
6+
class PgpASCTest extends TestCase
7+
{
8+
public function testClearsign()
9+
{
10+
$detective = new PlaintextDetective;
11+
$detective->check(__DIR__ . "/files/clearsign.asc");
12+
$this->assertEquals('asc', $detective->getExtension());
13+
$this->assertTrue($detective->isPgp());
14+
$this->assertEquals(PlaintextDetective::PGP_CLEARSIGN, $detective->getPgpType());
15+
}
16+
17+
public function testPubkey()
18+
{
19+
$detective = new PlaintextDetective;
20+
$detective->check(__DIR__ . "/files/pubkey.asc");
21+
$this->assertEquals('asc', $detective->getExtension());
22+
$this->assertTrue($detective->isPgp());
23+
$this->assertEquals(PlaintextDetective::PGP_PUBKEY, $detective->getPgpType());
24+
}
25+
26+
public function testSignature()
27+
{
28+
$detective = new PlaintextDetective;
29+
$detective->check(__DIR__ . "/files/detached.asc");
30+
$this->assertEquals('asc', $detective->getExtension());
31+
$this->assertTrue($detective->isPgp());
32+
$this->assertEquals(PlaintextDetective::PGP_SIGNATURE, $detective->getPgpType());
33+
}
34+
35+
public function testNoMismatch()
36+
{
37+
$detective = new PlaintextDetective;
38+
$detective->check(__DIR__ . "/files/clearsign.asc"); // use clearsign because it contains nested identifiers
39+
$this->assertEquals('asc', $detective->getExtension());
40+
$this->assertTrue($detective->isPgp());
41+
$this->assertNotEquals(PlaintextDetective::PGP_INVALID, $detective->getPgpType());
42+
$this->assertNotEquals(PlaintextDetective::PGP_PUBKEY, $detective->getPgpType());
43+
$this->assertNotEquals(PlaintextDetective::PGP_SIGNATURE, $detective->getPgpType());
44+
}
45+
}

tests/PlainTXTTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
use InfinityNext\Sleuth\Detectives\PlaintextDetective;
4+
use InfinityNext\Sleuth\Exceptions\CaseNotSolved;
5+
use PHPUnit\Framework\TestCase;
6+
7+
class PlainTXTtest extends TestCase
8+
{
9+
## TODO: Add actual .txt verification.
10+
public function testNotPgp()
11+
{
12+
$this->expectException(CaseNotSolved::class);
13+
$detective = new PlaintextDetective;
14+
$detective->check(__DIR__ . "/files/normal.txt"); // use clearsign because it contains nested identifiers
15+
$this->assertNotEquals('asc', $detective->getExtension());
16+
//$this->asserFalse($detective->isPgp());
17+
//$this->assertNotEquals(PlaintextDetective::PGP_INVALID, $detective->getPgpType());
18+
}
19+
20+
}

tests/files/clearsign.asc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-----BEGIN PGP SIGNED MESSAGE-----
2+
Hash: SHA256
3+
4+
This is a signed message for the Infinity Next File Sleuth unit tests. I hope you are having a fantastic day.
5+
-----BEGIN PGP SIGNATURE-----
6+
7+
iQEzBAEBCAAdFiEEFGcLKutf59Mtq7dqbfaGNREILy0FAl6n91UACgkQbfaGNREI
8+
Ly0dxggA6X2TW2UvWAvV/KWGXaFxnL7hy6czsZSEAfHQIncQtKHN4IXu5rV1QeYM
9+
mfvSHTNoQ2KqEaNtYHHVGsA4rYBNWug67rWl016ahyazWUB+NR0XL75mc8EF8cGN
10+
pKsYEKpjIlQVqDXpmgXr45yZfaH9l2ejm8Xr60AmMEcrqjImBNUyaaY/dSbuWpz0
11+
pYE8eHbW4zTnxeY/nU1vtVrYSCIIwQ1Y/epoutLuOlDIy7CF4UEi3qbD0cjxC4Wk
12+
vQ2ognjYcoblrP9ofQgUFcRmG0cNqGnbxDzaDqcGh1a3PAXPTDxnIkN7w2r3PhdB
13+
l22GxC0EhqFpyxDpEfs0xJGTSffe8Q==
14+
=x3dL
15+
-----END PGP SIGNATURE-----

tests/files/detached.asc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-----BEGIN PGP SIGNATURE-----
2+
3+
iQEzBAEBCAAdFiEEFGcLKutf59Mtq7dqbfaGNREILy0FAl6n91UACgkQbfaGNREI
4+
Ly0dxggA6X2TW2UvWAvV/KWGXaFxnL7hy6czsZSEAfHQIncQtKHN4IXu5rV1QeYM
5+
mfvSHTNoQ2KqEaNtYHHVGsA4rYBNWug67rWl016ahyazWUB+NR0XL75mc8EF8cGN
6+
pKsYEKpjIlQVqDXpmgXr45yZfaH9l2ejm8Xr60AmMEcrqjImBNUyaaY/dSbuWpz0
7+
pYE8eHbW4zTnxeY/nU1vtVrYSCIIwQ1Y/epoutLuOlDIy7CF4UEi3qbD0cjxC4Wk
8+
vQ2ognjYcoblrP9ofQgUFcRmG0cNqGnbxDzaDqcGh1a3PAXPTDxnIkN7w2r3PhdB
9+
l22GxC0EhqFpyxDpEfs0xJGTSffe8Q==
10+
=x3dL
11+
-----END PGP SIGNATURE-----

tests/files/pubkey.asc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
-----BEGIN PGP PUBLIC KEY BLOCK-----
2+
3+
mDMEXqccThYJKwYBBAHaRw8BAQdAoBPB5EOC2Oh8rJputN437oUaFkDDIjP74BPA
4+
LyTnqWW0HEpvc2h1YSBNb29uIDxqb3NoQDljaGFuLm9yZz6IlgQTFggAPhYhBDbj
5+
e/lshXTxEPG2D5/lmW6jDNVNBQJepxxOAhsDBQkB14nSBQsJCAcCBhUKCQgLAgQW
6+
AgMBAh4BAheAAAoJEJ/lmW6jDNVNCBgA/REhOFOGDQHXTHjTxq82C+to/MlDSmYw
7+
TApi0+L01OwDAQDCVfamRi25db5k/qlVlXYNjLrk2VitVXJNf51TUbjjAbg4BF6n
8+
HE4SCisGAQQBl1UBBQEBB0BGxGI0dUKOhPouTW3BFk1Whg3SnXiNy7j3oH1TUUPm
9+
cQMBCAeIfgQYFggAJhYhBDbje/lshXTxEPG2D5/lmW6jDNVNBQJepxxOAhsMBQkB
10+
14nSAAoJEJ/lmW6jDNVN3+YBAM4qvladpkomsVSCHPRB84CU9JpFm2MMqiJbqXgN
11+
LBf7AQCqoi+MIIdCbiNAnRn5aMRAhkZLL/1aHso9ztDZZnhdDQ==
12+
=OlWf
13+
-----END PGP PUBLIC KEY BLOCK-----

0 commit comments

Comments
 (0)