Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Having multiple allowExceptCaseInsensitiveParams together with disallowedAttributes does not work #238

Closed
ruudk opened this issue Jan 8, 2024 · 3 comments
Assignees

Comments

@ruudk
Copy link
Contributor

ruudk commented Jan 8, 2024

I'm trying to prevent the following:

class Entity {
    #[Column(name: 'start_date_parsed', type: 'datetime', nullable: true)]
    private ?DateTimeInterface $parsedStartDate;
}

I can do that with the following config:

<?php

declare(strict_types=1);

return [
    'parameters' => [
        'disallowedAttributes' => [
            [
                'attribute' => Doctrine\ORM\Mapping\Column::class,
                'message' => 'use `utc_datetime_immutable` instead.',
                'allowExceptCaseInsensitiveParams' => [
                    [
                        'name' => 'type',
                        'position' => 2,
                        'value' => 'datetime',
                    ],
                ],
            ],
        ],
    ],
];

It produces:

Attribute Doctrine\ORM\Mapping\Column is forbidden, use `utc_datetime_immutable` instead.

But as soon as I add another disallowed attribute, it stops working:

<?php

declare(strict_types=1);

return [
    'parameters' => [
        'disallowedAttributes' => [
            [
                'attribute' => Doctrine\ORM\Mapping\Column::class,
                'message' => 'use `utc_datetime_immutable` instead.',
                'allowExceptCaseInsensitiveParams' => [
                    [
                        'name' => 'type',
                        'position' => 2,
                        'value' => 'datetime',
                    ],
                ],
            ],
            [
                'attribute' => Doctrine\ORM\Mapping\Column::class,
                'message' => 'use `utc_datetime_immutable` instead.',
                'allowExceptCaseInsensitiveParams' => [
                    [
                        'name' => 'type',
                        'position' => 2,
                        'value' => 'datetime_immutable',
                    ],
                ],
            ],
        ],
    ],
];

It's interesting, because this does seem to work for normal functions:

-
function: 'hash()'
message: 'use hash() with at least SHA-256 for secure hash, or password_hash() for passwords'
allowExceptCaseInsensitiveParams:
1: 'md5'
-
function: 'hash()'
message: 'use hash() with at least SHA-256 for secure hash, or password_hash() for passwords'
allowExceptCaseInsensitiveParams:
1: 'sha1'

@ruudk
Copy link
Contributor Author

ruudk commented Jan 8, 2024

For now, this seems to work:

<?php

declare(strict_types=1);

return [
    'parameters' => [
        'disallowedAttributes' => [
            [
                'attribute' => Doctrine\ORM\Mapping\Column::class,
                'message' => 'use `utc_datetime_immutable` instead.',
                'allowExceptParams' => [
                    [
                        'name' => 'type',
                        'position' => 2,
                        'typeString' => '"datetime"|"datetime_immutable"',
                    ],
                ],
            ],
        ],
    ],
];

@spaze
Copy link
Owner

spaze commented Jan 9, 2024

Thanks for the bug, nice catch! For some reason the isAllowed method is called differently for functions/methods and for attributes:

if (
$this->identifier->matches($disallowedCall->getCall(), $name, $disallowedCall->getExcludes())
&& $this->definedInMatches($disallowedCall, $definedIn)
&& !$this->allowed->isAllowed($scope, isset($node) ? $node->getArgs() : null, $disallowedCall)
) {

if ($this->allowed->isAllowed($scope, $attribute->args, $disallowedAttribute)) {
continue;
}

So that's probably why it works for functions, but not for attributes. (Update: no, it's not, it's almost the same code except the definedInMatches call)

For now, I have written a test f22ee42, a failing one, will fix once there's a bit more brain power. And more sleep 😆

@spaze spaze self-assigned this Jan 9, 2024
@spaze spaze added the bug Something isn't working label Jan 9, 2024
@spaze
Copy link
Owner

spaze commented Jan 11, 2024

The reason is that attributes and most other "calls" use a simple key when adding items from the config (the name of the thing):

$disallowedAttributes[$disallowedAttribute->getAttribute()] = $disallowedAttribute;

while calls use a composite key (the reason being that some functions/methods come pre-configured in .neon files and you may want to re-allow them based on the params used):

$disallowedCalls[$disallowedCall->getKey()] = $disallowedCall;

This is not very clever but I'm not sure how to do it better, there's always a "but". So for now this will stay.

@ruudk You can use the typeString config as you found out, or you can merge those two attribute configs into one like

[
	'attribute' => AttributeColumn::class,
	'message' => 'use `utc_datetime_immutable` instead.',
	'allowExceptCaseInsensitiveParams' => [
		[
			'name' => 'type',
			'position' => 2,
			'value' => 'datetime',
		],
		[
			'name' => 'type',
			'position' => 2,
			'value' => 'datetime_immutable',
		],
	],
],

Notice there's just one 'attribute' => AttributeColumn::class item but the allowExceptCaseInsensitiveParams array has two records for the type param. I'll add a test for this case.

@spaze spaze closed this as completed Jan 11, 2024
@spaze spaze closed this as not planned Won't fix, can't repro, duplicate, stale Jan 11, 2024
@spaze spaze removed the bug Something isn't working label Jan 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants