You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Allow param conditions with allowInInstanceOf and disallowInInstanceOf
Combining instanceof-based directives with `allowParamsInAllowed` or `allowExceptParamsInAllowed` makes it possible to narrow which calls within a class hierarchy are reported, without introducing new config keys.
Copy file name to clipboardExpand all lines: docs/allow-in-instance-of.md
+38Lines changed: 38 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -40,6 +40,44 @@ parameters:
40
40
- 'MyInterface'
41
41
```
42
42
43
+
### Combining with parameter conditions
44
+
45
+
Both `allowInInstanceOf` and `disallowInInstanceOf` can be combined with `allowParamsInAllowed` and `allowExceptParamsInAllowed` to add parameter-based conditions within the class hierarchy scope. See [allow with parameters](allow-with-parameters.md) for details on parameter configuration.
46
+
47
+
For example, to allow `dispatch()` in classes implementing `HandlerInterface` but only when the first argument is of type `SafeEvent`:
48
+
49
+
```neon
50
+
parameters:
51
+
disallowedFunctionCalls:
52
+
-
53
+
function: 'dispatch()'
54
+
allowInInstanceOf:
55
+
- 'App\Handlers\HandlerInterface'
56
+
allowParamsInAllowed:
57
+
-
58
+
position: 1
59
+
name: 'event'
60
+
typeString: 'App\Events\SafeEvent'
61
+
```
62
+
63
+
To disallow `dispatch()` in `HandlerInterface` classes only when the first argument is of type `DangerousEvent`, and allow it with any other argument:
64
+
65
+
```neon
66
+
parameters:
67
+
disallowedFunctionCalls:
68
+
-
69
+
function: 'dispatch()'
70
+
disallowInInstanceOf:
71
+
- 'App\Handlers\HandlerInterface'
72
+
allowExceptParamsInAllowed:
73
+
-
74
+
position: 1
75
+
name: 'event'
76
+
typeString: 'App\Events\DangerousEvent'
77
+
```
78
+
79
+
The `allowExceptParamsInAllowed` counterpart works with `allowInInstanceOf` too (allowed in hierarchy except when the parameter matches), and `allowParamsInAllowed` works with `disallowInInstanceOf` (disallowed in hierarchy unless the parameter matches).
80
+
43
81
### Allow in `use` imports
44
82
The `allowInInstanceOf` configuration above will also report an error on the line with the import, if present:
Copy file name to clipboardExpand all lines: docs/allow-with-parameters.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -33,7 +33,7 @@ parameters:
33
33
value: true
34
34
```
35
35
36
-
When using `allowParamsInAllowed`, calls will be allowed only when they are in one of the `allowIn` paths, and are called with all parameters listed in `allowParamsInAllowed`.
36
+
When using `allowParamsInAllowed`, calls will be allowed only when they are in one of the `allowIn` paths (or in a class hierarchy matched by `allowInInstanceOf`), and are called with all parameters listed in `allowParamsInAllowed`.
37
37
With `allowParamsAnywhere`, calls are allowed when called with all parameters listed no matter in which file. In the example above, the `log()` method will be disallowed unless called as:
-`log('foo', true)` (or `log(message: 'foo', alert: true)`) in `another/file.php` or `optional/path/to/log.tests.php`
@@ -115,7 +115,7 @@ parameters:
115
115
```
116
116
This configuration will disallow calls like `waldo('foo', 'bar')` or `waldo('*', '*')`, but `waldo('foo')` or `waldo()` will be still allowed.
117
117
118
-
It's also possible to disallow functions and methods previously allowed by path (using `allowIn`) or by function/method name (`allowInMethods`) when they're called with specified parameters, and allow when called with any other parameter. This is done using the `allowExceptParamsInAllowed` config option.
118
+
It's also possible to disallow functions and methods previously allowed by path (using `allowIn`), by function/method name (`allowInMethods`), or by class hierarchy (`allowInInstanceOf`) when they're called with specified parameters, and allow when called with any other parameter. This is done using the `allowExceptParamsInAllowed` config option.
// outside the hierarchy: str_ends_with and str_contains calls are allowed regardless of params; str_starts_with is still disallowed (allowInInstanceOf)
37
+
class BarOutside
38
+
{
39
+
40
+
publicfunctionoutsideHierarchy(): void
41
+
{
42
+
str_starts_with('foo', 'forbidden');
43
+
str_starts_with('foo', 'allowed');
44
+
str_ends_with('foo', 'forbidden');
45
+
str_ends_with('foo', 'allowed');
46
+
str_contains('foo', 'allowed_param');
47
+
str_contains('foo', 'other');
48
+
}
49
+
50
+
}
51
+
52
+
// test allowInInstanceOf + allowParamsInAllowed: allowed in hierarchy only when param is 'allowed_chars'
53
+
class BarBaseForAllowParams extends BarBase
54
+
{
55
+
56
+
publicfunctioninHierarchy(): void
57
+
{
58
+
ltrim('foo', 'allowed_chars');
59
+
ltrim('foo', 'other');
60
+
}
61
+
62
+
}
63
+
64
+
// outside the hierarchy: all ltrim calls are disallowed (allowInInstanceOf)
65
+
class BarOutsideForAllowParams
66
+
{
67
+
68
+
publicfunctionoutsideHierarchy(): void
69
+
{
70
+
ltrim('foo', 'allowed_chars');
71
+
ltrim('foo', 'other');
72
+
}
73
+
74
+
}
75
+
76
+
class BarBaseChildForAllowParams extends BarBaseForAllowParams
0 commit comments