-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Bug: get_cookie()
cookie prefix behavior
#6009
Comments
get_cookie()
cookie prefix behavior
I don't know this is a bug or not. |
In my understanding of the function, If a cookie with the name |
If we set prefix in the config, all issued cookies by CI have the prefixed names.
It seems the user guide says When the prefix is $_COOKIES = [
'prefix_test' => 'CI cookie',
'test' => 'Non CI cookie',
];
Why do we need to get cookie |
If there is no way to reliably only get the prefixed cookie and reject all others, then there isn't really any point to the entire cookie prefixing setup as it would not save collisions between 2 otherwise similar apps installed on the same domain, and if its not solving this problem, then what problem is it solving exactly? |
@colethorsen Good question! |
I thought about this. It seems the current $_COOKIES = [
'prefix_test' => 'CI cookie',
'test' => 'Non CI cookie',
]; If you want to get CI cookie surely, you must call with prefixed name like Even if the current behavior is correct, the user guide explanation is not correct. Probably we have to go one way or the other.
|
If you were to “fix the guide” then there is basically no useful way to globally prefix all the cookies and that entire functionality wouldn’t really be relevant. There is already the current cookie functionality incomingrequest::getCookie() which will allow users to get any cookie prefixed or not that they may need to. get_cookie() should work in tandem with the set_cookie() which prefixes or they should both be adjusted to just be direct shortcuts to the incomingrequest::getCookie() and additional functions added that work with the prefix setup. Given that the incoming request cookie functionality exists I don’t really see a reason why the get_cookie doesn’t function with the prefix as that’s how it’s always been documented and without it the entire prefixing setup as it’s designed doesn’t really work. |
This function has the parameter I certainly agree that the current |
The set_cookie works a bit differently if you start digging into it. There's basically an option within the I'm not overly sure of what real world benefits being able to set other prefixes has at the |
@colethorsen I sent another PR: #6082 |
Sure this appears to fix it it more or less the same way, except adding in support for a prefix from another spot where the prefix could be set. While not addressing the second part of the cookie prefix problem. I'm using #6024 in production and will continue to do so as this only addresses part of the issue with cookie prefix inconsistencies. |
After testing it out more, your PR has a major implementation drawback, that it can't actually check for an unprefixed cookie if you wanted to, where as the #6024 can because of the way its configured: using:
and then checking to see if its empty would always lead to different prefix, whereas using:
and then checking for null would allow you explicitly check for an unprefixed cookie by using |
Thank you for looking into #6082. But sorry, I don't get what you say. What's the second part of the cookie prefix problem?
|
Sorry you're right it would solve it just in the opposite manner by passing null instead of an empty string. The second part of the problem is that the session cookie sometimes adds the prefix and sometimes doesn't and ends up having multiple cookies and/or multiple sessions. This is solved in the #6024 |
Thank you for making it clear. Session cookie should not have the prefix, so there is a bug in Session. |
I've found the Session cookie name bug. #6091 |
PHP Version
7.3
CodeIgniter4 Version
4.1.9
CodeIgniter4 Installation Method
Composer (as dependency to an existing project)
Which operating systems have you tested for this bug?
macOS, Linux
Which server did you use?
apache
Database
MySQL 8
What happened?
The cookie prefixing to prevent collisions doesn't exactly work as expected. There is an issue with at least the get_cookie() helper function. The cookie prefix is specifically toted as a way to prevent collisions, however this function doesn't solve for collisions.
This function looks to see if the base cookie without the prefix exists, and if it does not exist then it looks for the prefixed cookie. It should look for the prefixed cookie and reject the un-prefixed cookie entirely, otherwise it creates collisions.
Steps to Reproduce
If you take the following example:
Config/App.php
public $cookiePrefix = 'prefix_';
Theoretical array of cookies:
get_cookie('test');
returns 'Wrong Value' by default, ideally it should return 'Right Value' and even if 'prefix_text' is not set, it should return null and never 'Wrong Value'
Expected Output
returns 'Wrong Value' by default, ideally it should return 'Right Value' and even if 'prefix_text' is not set, it should return null and never 'Wrong Value'
Anything else?
This could be fairly easily fixed by just enforcing the prefix:
$prefix = config(App::class)->cookiePrefix ?? '';
However, I'm not sure if this should be enforced at a deeper stage of getting the cookie, and/or the function should be extended to allow for a prefix to be set independently if desired like:
The text was updated successfully, but these errors were encountered: