Skip to content

Commit 955f41f

Browse files
Add Regexer and more tests
1 parent 4eff285 commit 955f41f

17 files changed

Lines changed: 497 additions & 13 deletions

File tree

src/Detector.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
namespace CaptainHook\Secrets;
1515

16-
use CaptainHook\Secrets\Detector\Result;
1716
use CaptainHook\Secrets\Regex\Supplier;
1817
use RuntimeException;
1918

@@ -98,7 +97,7 @@ public function useRegex(string ...$regularExpressions): self
9897
* Detect secrets in string
9998
*
10099
* @param string $content
101-
* @return \CaptainHook\Secrets\Detector\Result
100+
* @return \CaptainHook\Secrets\Result
102101
*/
103102
public function detectIn(string $content): Result
104103
{

src/Regex/Grouped.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CaptainHook Secrets.
5+
*
6+
* (c) Sebastian Feldmann <sf@sebastian-feldmann.info>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace CaptainHook\Secrets\Regex;
15+
16+
/**
17+
* Grouped Interface
18+
*
19+
* @package CaptainHook-Secrets
20+
* @author Sebastian Feldmann <sf@sebastian-feldmann.info>
21+
* @link https://github.com/captainhookphp/secrets
22+
* @since Class available since Release 0.9.4
23+
*/
24+
interface Grouped extends Supplier
25+
{
26+
/**
27+
* Returns the capture group index of the potential password
28+
*
29+
* @return array<int>
30+
*/
31+
public function indexes(): array;
32+
}

src/Regex/Supplier/Aws.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ public function patterns(): array
4646
// AWS secrets, keys, access token
4747
'#' . Util::OPTIONAL_QUOTE . self::AWS . '(SECRET|secret|Secret)?_?(ACCESS|access|Access)?_?(KEY|key|Key)'
4848
. Util::OPTIONAL_QUOTE . Util::CONNECT
49-
. Util::OPTIONAL_QUOTE . '[A-Za-z0-9/\\+=]{40}' . Util::OPTIONAL_QUOTE . '#',
49+
. Util::OPTIONAL_QUOTE . '([A-Za-z0-9/\\+=]{40})' . Util::OPTIONAL_QUOTE . '#',
5050

5151
// AWS account id
5252
'#' . Util::OPTIONAL_QUOTE . self::AWS . '(ACCOUNT|account|Account)_?(ID|id|Id)?' . Util::OPTIONAL_QUOTE
53-
. Util::CONNECT . Util::OPTIONAL_QUOTE . '[0-9]{4}\\-?[0-9]{4}\\-?[0-9]{4}' . Util::OPTIONAL_QUOTE . '#',
53+
. Util::CONNECT . Util::OPTIONAL_QUOTE . '([0-9]{4}\\-?[0-9]{4}\\-?[0-9]{4})' . Util::OPTIONAL_QUOTE . '#',
5454
];
5555
}
5656
}

src/Regex/Supplier/GitHub.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@ public function patterns(): array
3636
{
3737
return [
3838
// Personal Access Token (Classic)
39-
'#' . Util::OPTIONAL_QUOTE . 'ghp_[a-zA-Z0-9]{36}' . Util::OPTIONAL_QUOTE . '#',
39+
'#' . Util::OPTIONAL_QUOTE . '(ghp_[a-zA-Z0-9]{36})' . Util::OPTIONAL_QUOTE . '#',
4040

4141
// Personal Access Token (Fine-Grained)
42-
'#' . Util::OPTIONAL_QUOTE . 'github_pat_[a-zA-Z0-9]{22}_[a-zA-Z0-9]{59}' . Util::OPTIONAL_QUOTE . '#',
42+
'#' . Util::OPTIONAL_QUOTE . '(github_pat_[a-zA-Z0-9]{22}_[a-zA-Z0-9]{59})' . Util::OPTIONAL_QUOTE . '#',
4343

4444
// User-To-Server Access Token
45-
'#' . Util::OPTIONAL_QUOTE . 'ghu_[a-zA-Z0-9]{36}' . Util::OPTIONAL_QUOTE . '#',
45+
'#' . Util::OPTIONAL_QUOTE . '(ghu_[a-zA-Z0-9]{36})' . Util::OPTIONAL_QUOTE . '#',
4646

4747
// Server-To-Server Access Token
48-
'#' . Util::OPTIONAL_QUOTE . 'ghs_[a-zA-Z0-9]{36}' . Util::OPTIONAL_QUOTE . '#',
48+
'#' . Util::OPTIONAL_QUOTE . '(ghs_[a-zA-Z0-9]{36})' . Util::OPTIONAL_QUOTE . '#',
4949
];
5050
}
5151
}

src/Regex/Supplier/Google.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function patterns(): array
3636
{
3737
return [
3838
// API Key
39-
'#' . Util::OPTIONAL_QUOTE . 'AIza[0-9A-Za-z\-_]{35}' . Util::OPTIONAL_QUOTE . '#',
39+
'#' . Util::OPTIONAL_QUOTE . '(AIza[0-9A-Za-z\-_]{35})' . Util::OPTIONAL_QUOTE . '#',
4040
];
4141
}
4242
}

src/Regex/Supplier/Ini.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CaptainHook Secrets.
5+
*
6+
* (c) Sebastian Feldmann <sf@sebastian-feldmann.info>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace CaptainHook\Secrets\Regex\Supplier;
13+
14+
use CaptainHook\Secrets\Regex\Grouped;
15+
16+
/**
17+
* Find any possible string assignment in a php file
18+
*
19+
* Finds:
20+
* - foo = "string"
21+
* - foo = string
22+
*/
23+
class Ini implements Grouped
24+
{
25+
/**
26+
* Returns a list of patterns to check
27+
*
28+
* @return array<string>
29+
*/
30+
public function patterns(): array
31+
{
32+
return [
33+
'#=\\s*("?)([^\n]*)\\1+\\s*#i',
34+
];
35+
}
36+
37+
/**
38+
* Return capture group to access the password
39+
*
40+
* @return array<int>
41+
*/
42+
public function indexes(): array
43+
{
44+
return [2];
45+
}
46+
}

src/Regex/Supplier/Json.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CaptainHook Secrets.
5+
*
6+
* (c) Sebastian Feldmann <sf@sebastian-feldmann.info>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace CaptainHook\Secrets\Regex\Supplier;
13+
14+
use CaptainHook\Secrets\Regex\Grouped;
15+
16+
/**
17+
* Find any possible string assignment in a json file
18+
*
19+
* Finds:
20+
* - "foo": "string"
21+
*/
22+
class Json implements Grouped
23+
{
24+
/**
25+
* Returns a list of patterns to check
26+
*
27+
* @return array<string>
28+
*/
29+
public function patterns(): array
30+
{
31+
return [
32+
// detecting any string assignment
33+
'#:\\s*' . Util::QUOTE . '(.*?)' . Util::QUOTE . '#i',
34+
];
35+
}
36+
37+
/**
38+
* Return capture group to access the password
39+
*
40+
* @return array<int>
41+
*/
42+
public function indexes(): array
43+
{
44+
return [2];
45+
}
46+
}

src/Regex/Supplier/PHP.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CaptainHook Secrets.
5+
*
6+
* (c) Sebastian Feldmann <sf@sebastian-feldmann.info>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace CaptainHook\Secrets\Regex\Supplier;
13+
14+
use CaptainHook\Secrets\Regex\Grouped;
15+
16+
/**
17+
* Find any possible string assignment in a php file
18+
*
19+
* Finds:
20+
* - $foo = "string"
21+
* - $foo = ["foo" => "string"]
22+
*/
23+
class PHP implements Grouped
24+
{
25+
/**
26+
* Returns a list of patterns to check
27+
*
28+
* @return array<string>
29+
*/
30+
public function patterns(): array
31+
{
32+
return [
33+
// detecting any string assignment
34+
// = "some string", => 'some-string' return 'some-string
35+
'#(=>?|return)\\s*' . Util::QUOTE . '(.*?)' . Util::QUOTE . '#i',
36+
];
37+
}
38+
39+
/**
40+
* Return capture group to access the password
41+
*
42+
* @return array<int>
43+
*/
44+
public function indexes(): array
45+
{
46+
return [3];
47+
}
48+
}

src/Regex/Supplier/Password.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function patterns(): array
3737
return [
3838
// Generic passwords
3939
'#password' . Util::OPTIONAL_QUOTE . Util::CONNECT . Util::OPTIONAL_QUOTE
40-
. '[a-z\\-_\\#/\\+0-9]{16,}' . Util::OPTIONAL_QUOTE . '#i',
40+
. '([a-z\\-_\\#/\\+0-9]{16,})' . Util::OPTIONAL_QUOTE . '#i',
4141
];
4242
}
4343
}

src/Regex/Supplier/Stripe.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function patterns(): array
3636
{
3737
return [
3838
// Standard API Key & Restricted API Key
39-
'#' . Util::OPTIONAL_QUOTE . 'sk_live_[0-9a-z]{24}' . Util::OPTIONAL_QUOTE . '#',
39+
'#' . Util::OPTIONAL_QUOTE . '(sk_live_[0-9a-z]{24})' . Util::OPTIONAL_QUOTE . '#',
4040
];
4141
}
4242
}

0 commit comments

Comments
 (0)