Skip to content

[Cache] FilesystemCommonTrait - Cannot use integers as namespace #23503

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

Closed
keyball opened this issue Jul 14, 2017 · 6 comments
Closed

[Cache] FilesystemCommonTrait - Cannot use integers as namespace #23503

keyball opened this issue Jul 14, 2017 · 6 comments

Comments

@keyball
Copy link

keyball commented Jul 14, 2017

Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no
Symfony version 3.3.4

Hi,
I found that the method Symfony\Component\Cache\Trait\FilesystemCommonTrait::init($namespace, $directory)
has some constructs which prevent the namespacing.

The line 33: if (isset($namespace[0])) { ...

will work only if the given namespace is a string. If it'll be an integer - the isset will return false - and no sub-directory in cache root will be created!

Example:

$x = 892;
echo 'First element: ' . $x[ 0 ] . PHP_EOL;
var_dump( isset( $x[ 0 ] ) );

results in:
First element:
bool(false)

but this notation:

$x = (string)892;
echo 'First element: ' . $x[ 0 ] . PHP_EOL;
var_dump( isset( $x[ 0 ] ) );

returns the correct values:
First element: 8
bool(true)

I would recommend to change the isset directly to a preg_match called one line letter, declare the $match array and check for matching entry in it to throw the exception - or explicitly cast the incoming parameters to string ( it will be obsolete in PHP7 if the method would get typed parameters) .

private function init($namespace, $directory)
    {
        $namespace = (string)$namespace;
        $directory = (string)$directory;

A similar notation can be found also in the line 28, but there is a lower probability to get an integer instead of string. I know that the "isset" is the fastest method to check if a string exists - but in this case it is not the best solution.

Best regards,
Lukas

@ro0NL
Copy link
Contributor

ro0NL commented Jul 14, 2017

will work only if the given namespace is a string.

Although not explicitly documented, i think it actually needs to be a string by design. So passing an integer violates the contract.. making this a non-issue :)

We may add constructor docblocks to clarify.

@keyball
Copy link
Author

keyball commented Jul 14, 2017

Ok, that should be at least a test with is_string in the constructor to be sure, if the given parameter is a string or not. That will protect from passing all other types to the method. And as a developer uses an external library, he must not even know, how it works to use the given methods. And the only return values should be a value or an exception. In the current case, if someone will use a value that can come from database, url, xml, json, etc. and pass it directly - he won't be sure if the cache will be stored correctly - and now it is possible to overwrite the cache keys from another users - if the namespace will be an user id - as no sub-directory with the correct name will be created. This is not an expected behaviour - so for me - a bug :)

Or do you follow the sentence: Our software never has bugs it just develops random features? :D

@ro0NL
Copy link
Contributor

ro0NL commented Jul 14, 2017

You need to be sure it's a string (if that's the contract). If the constructor arg defines @param string $some and you violate it... you're on your own :)

Asserting with is_string is just a safeguard for the contract, which is uncommon thing to do in SF.

Or do you follow the sentence: Our software never has bugs it just develops random features? :D

Software has bugs.. for sure; i wouldnt qualify this as such though :)

@keyball
Copy link
Author

keyball commented Jul 14, 2017

I got your point - but this isn't the case ;) There is no parameter description in the constructor of the FilesystemAdapter class yet :) But I'll be then more cautious to use the other methods now :)

Thanks for info :)

@ro0NL
Copy link
Contributor

ro0NL commented Jul 14, 2017

That's why i suggested

We may add constructor docblocks to clarify.

;-)

@linaori
Copy link
Contributor

linaori commented Jul 14, 2017

The interface (at least in 3.4):

public function __construct($namespace = '', $defaultLifetime = 0, $directory = null);

So I would say the type is most certainly defined as string here.

@javiereguiluz javiereguiluz changed the title [Symfony Cache] FilesystemCommonTrait - Cannot use integers as namespace [Cache] FilesystemCommonTrait - Cannot use integers as namespace Jul 17, 2017
fabpot added a commit that referenced this issue Jul 17, 2017
This PR was merged into the 3.3 branch.

Discussion
----------

[Cache] add constructor docblocks for clarity

| Q             | A
| ------------- | ---
| Branch?       | 3.3
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #23503
| License       | MIT
| Doc PR        |

Commits
-------

d1ce532 [Cache] add constructor docblocks for clarity
@fabpot fabpot closed this as completed Jul 17, 2017
fabpot added a commit that referenced this issue Jul 17, 2017
This PR was merged into the 3.2 branch.

Discussion
----------

[Cache] add constructor docblocks for clarity

| Q             | A
| ------------- | ---
| Branch?       | 3.2
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #23503
| License       | MIT
| Doc PR        |

Commits
-------

813a537 [Cache] add constructor docblocks for clarity
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants