Skip to content

Commit c800372

Browse files
authored
Whitelist functions as soon as they are used (#269)
1 parent a514ed6 commit c800372

19 files changed

+466
-29
lines changed

specs/const/const-declaration-with-global-whitelisting.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
'whitelist' => [],
2121
'whitelist-global-constants' => true,
2222
'whitelist-global-classes' => false,
23-
'whitelist-global-functions' => true,
23+
'whitelist-global-functions' => false,
2424
'registered-classes' => [],
2525
'registered-functions' => [],
2626
],

specs/const/const-declaration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
'whitelist' => [],
2121
'whitelist-global-constants' => false,
2222
'whitelist-global-classes' => false,
23-
'whitelist-global-functions' => true,
23+
'whitelist-global-functions' => false,
2424
'registered-classes' => [],
2525
'registered-functions' => [],
2626
],

specs/function/global-scope-global-func-with-single-level-use-statement-and-alias.php

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
'whitelist' => [],
2121
'whitelist-global-constants' => true,
2222
'whitelist-global-classes' => false,
23-
'whitelist-global-functions' => true,
23+
'whitelist-global-functions' => false,
2424
'registered-classes' => [],
2525
'registered-functions' => [],
2626
],
@@ -42,6 +42,28 @@
4242
PHP
4343
,
4444

45+
'Global function call imported with a use statement in the global scope with global functions whitelisted' => [
46+
'whitelist-global-functions' => true,
47+
'registered-functions' => [
48+
['main', 'Humbug\main'],
49+
],
50+
'payload' => <<<'PHP'
51+
<?php
52+
53+
use function main as foo;
54+
55+
foo();
56+
----
57+
<?php
58+
59+
namespace Humbug;
60+
61+
use function Humbug\main as foo;
62+
\Humbug\main();
63+
64+
PHP
65+
],
66+
4567
'Global FQ function call imported with a use statement in the global scope' => <<<'PHP'
4668
<?php
4769
@@ -58,4 +80,26 @@
5880

5981
PHP
6082
,
83+
84+
'Global FQ function call imported with a use statement in the global scope with global functions whitelisted' => [
85+
'whitelist-global-functions' => true,
86+
'registered-functions' => [
87+
['foo', 'Humbug\foo'],
88+
],
89+
'payload' => <<<'PHP'
90+
<?php
91+
92+
use function main as foo;
93+
94+
\foo();
95+
----
96+
<?php
97+
98+
namespace Humbug;
99+
100+
use function Humbug\main as foo;
101+
\Humbug\foo();
102+
103+
PHP
104+
],
61105
];

specs/function/global-scope-global-func-with-single-level-use-statement.php

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
'whitelist' => [],
2121
'whitelist-global-constants' => true,
2222
'whitelist-global-classes' => false,
23-
'whitelist-global-functions' => true,
23+
'whitelist-global-functions' => false,
2424
'registered-classes' => [],
2525
'registered-functions' => [],
2626
],
@@ -42,6 +42,28 @@
4242
PHP
4343
,
4444

45+
'Global function call imported with a use statement in the global scope with global functions whitelisted' => [
46+
'whitelist-global-functions' => true,
47+
'registered-functions' => [
48+
['main', 'Humbug\main'],
49+
],
50+
'payload' => <<<'PHP'
51+
<?php
52+
53+
use function main;
54+
55+
main();
56+
----
57+
<?php
58+
59+
namespace Humbug;
60+
61+
use function Humbug\main;
62+
\Humbug\main();
63+
64+
PHP
65+
],
66+
4567
'Global FQ function call imported with a use statement in the global scope' => <<<'PHP'
4668
<?php
4769
@@ -58,4 +80,26 @@
5880

5981
PHP
6082
,
83+
84+
'Global FQ function call imported with a use statement in the global scope with global functions whitelisted' => [
85+
'whitelist-global-functions' => true,
86+
'registered-functions' => [
87+
['main', 'Humbug\main'],
88+
],
89+
'payload' => <<<'PHP'
90+
<?php
91+
92+
use function main;
93+
94+
\main();
95+
----
96+
<?php
97+
98+
namespace Humbug;
99+
100+
use function Humbug\main;
101+
\Humbug\main();
102+
103+
PHP
104+
],
61105
];

specs/function/global-scope-global-func.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
'whitelist' => [],
2121
'whitelist-global-constants' => true,
2222
'whitelist-global-classes' => false,
23-
'whitelist-global-functions' => true,
23+
'whitelist-global-functions' => false,
2424
'registered-classes' => [],
2525
'registered-functions' => [],
2626
],

specs/function/global-scope-single-part-namespaced-func.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
'whitelist' => [],
2121
'whitelist-global-constants' => true,
2222
'whitelist-global-classes' => false,
23-
'whitelist-global-functions' => true,
23+
'whitelist-global-functions' => false,
2424
'registered-classes' => [],
2525
'registered-functions' => [],
2626
],
@@ -55,6 +55,9 @@
5555

5656
'Whitelisted namespaced function call' => [
5757
'whitelist' => ['PHPUnit\main'],
58+
'registered-functions' => [
59+
['PHPUnit\main', 'Humbug\PHPUnit\main'],
60+
],
5861
'payload' => <<<'PHP'
5962
<?php
6063
@@ -71,6 +74,9 @@
7174

7275
'FQ whitelisted namespaced function call' => [
7376
'whitelist' => ['PHPUnit\main'],
77+
'registered-functions' => [
78+
['PHPUnit\main', 'Humbug\PHPUnit\main'],
79+
],
7480
'payload' => <<<'PHP'
7581
<?php
7682

specs/function/namespace-global-func-with-single-level-use-statement-and-alias.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
'whitelist' => [],
2121
'whitelist-global-constants' => true,
2222
'whitelist-global-classes' => false,
23-
'whitelist-global-functions' => true,
23+
'whitelist-global-functions' => false,
2424
'registered-classes' => [],
2525
'registered-functions' => [],
2626
],

specs/function/namespace-global-func-with-single-level-use-statement.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
'whitelist' => [],
2121
'whitelist-global-constants' => true,
2222
'whitelist-global-classes' => false,
23-
'whitelist-global-functions' => true,
23+
'whitelist-global-functions' => false,
2424
'registered-classes' => [],
2525
'registered-functions' => [],
2626
],

specs/function/namespace-global-func.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
'whitelist' => [],
2121
'whitelist-global-constants' => true,
2222
'whitelist-global-classes' => false,
23-
'whitelist-global-functions' => true,
23+
'whitelist-global-functions' => false,
2424
'registered-classes' => [],
2525
'registered-functions' => [],
2626
],

specs/function/namespace-global-scope-func.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
'whitelist' => [],
2121
'whitelist-global-constants' => true,
2222
'whitelist-global-classes' => false,
23-
'whitelist-global-functions' => true,
23+
'whitelist-global-functions' => false,
2424
'registered-classes' => [],
2525
'registered-functions' => [],
2626
],

specs/function/namespace-single-part-namespaced-func.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
'whitelist' => [],
2121
'whitelist-global-constants' => true,
2222
'whitelist-global-classes' => false,
23-
'whitelist-global-functions' => true,
23+
'whitelist-global-functions' => false,
2424
'registered-classes' => [],
2525
'registered-functions' => [],
2626
],
@@ -59,6 +59,7 @@
5959

6060
'Whitelisted namespaced function call' => [
6161
'whitelist' => ['PHPUnit\X\main'],
62+
// No function registered to the whitelist here since no FQ could be resolved
6263
'payload' => <<<'PHP'
6364
<?php
6465
@@ -77,6 +78,9 @@
7778

7879
'FQ whitelisted namespaced function call' => [
7980
'whitelist' => ['PHPUnit\main'],
81+
'registered-functions' => [
82+
['PHPUnit\main', 'Humbug\PHPUnit\main'],
83+
],
8084
'payload' => <<<'PHP'
8185
<?php
8286
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the humbug/php-scoper package.
7+
*
8+
* Copyright (c) 2017 Théo FIDRY <theo.fidry@gmail.com>,
9+
* Pádraic Brady <padraic.brady@gmail.com>
10+
*
11+
* For the full copyright and license information, please view the LICENSE
12+
* file that was distributed with this source code.
13+
*/
14+
15+
return [
16+
'meta' => [
17+
'title' => 'Whitelisting functions which are never declared but for which the existence is checked',
18+
// Default values. If not specified will be the one used
19+
'prefix' => 'Humbug',
20+
'whitelist' => [],
21+
'whitelist-global-constants' => false,
22+
'whitelist-global-classes' => false,
23+
'whitelist-global-functions' => false,
24+
'registered-classes' => [],
25+
'registered-functions' => [],
26+
],
27+
28+
'Non whitelisted global function call' => <<<'PHP'
29+
<?php
30+
31+
function_exists('main');
32+
----
33+
<?php
34+
35+
namespace Humbug;
36+
37+
\function_exists('Humbug\\main');
38+
39+
PHP
40+
,
41+
42+
'Whitelisted global function call' => [
43+
'whitelist' => ['main'],
44+
'registered-functions' => [
45+
['main', 'Humbug\main'],
46+
],
47+
'payload' => <<<'PHP'
48+
<?php
49+
50+
function_exists('main');
51+
----
52+
<?php
53+
54+
namespace Humbug;
55+
56+
\function_exists('Humbug\\main');
57+
58+
PHP
59+
],
60+
61+
'Global function call with whitelisted global functions' => [
62+
'whitelist-global-functions' => true,
63+
'registered-functions' => [
64+
['main', 'Humbug\main'],
65+
],
66+
'payload' => <<<'PHP'
67+
<?php
68+
69+
function_exists('main');
70+
----
71+
<?php
72+
73+
namespace Humbug;
74+
75+
\function_exists('Humbug\\main');
76+
77+
PHP
78+
],
79+
80+
'Global function call with non-whitelisted global functions' => <<<'PHP'
81+
<?php
82+
83+
function_exists('main');
84+
----
85+
<?php
86+
87+
namespace Humbug;
88+
89+
\function_exists('Humbug\\main');
90+
91+
PHP
92+
,
93+
94+
'Whitelisted namespaced function call' => [
95+
'whitelist' => ['Acme\main'],
96+
'registered-functions' => [
97+
['Acme\main', 'Humbug\Acme\main'],
98+
],
99+
'payload' => <<<'PHP'
100+
<?php
101+
102+
namespace Acme;
103+
104+
function_exists('Acme\main');
105+
----
106+
<?php
107+
108+
namespace Humbug\Acme;
109+
110+
\function_exists('Humbug\\Acme\\main');
111+
112+
PHP
113+
],
114+
];

0 commit comments

Comments
 (0)