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

Broken PSR-7 immutability #198

Closed
zonuexe opened this issue Apr 12, 2022 · 2 comments
Closed

Broken PSR-7 immutability #198

zonuexe opened this issue Apr 12, 2022 · 2 comments
Labels

Comments

@zonuexe
Copy link

zonuexe commented Apr 12, 2022

This issue is related to php/php-src#8351 and vimeo/psalm#7857.

Most PSR-7 interfaces ensure immutability by providing withXXX instead of setXXX,
but many PSR-7 implementations expose constructors so objects can be easily modified.

$uri = $psr17Factory->createURI('http://tnyholm.se');
var_dump([spl_object_id($uri) => (string)$uri]);

$uri->__construct('https://evil.example.com/');
var_dump([spl_object_id($uri) => (string)$uri]);

This issue seems to have been mentioned in a 2017 article.
https://www.simonholywell.com/post/2017/03/php-and-immutability/

And yesterday I learned about this issue by @twada at a conference in Japan called "PHPerKaigi 2022".
https://speakerdeck.com/twada/growing-reliable-code-phperkaigi-2022?slide=84

PSR-7 interfaces do not define those constructors, so it's just an implementation package issue.

I don't think there are any security holes due to this issue, but it makes it easier for application implementers to implement against the PSR-7 and PSR-15 concepts.

@nicolas-grekas
Copy link
Collaborator

nicolas-grekas commented Apr 14, 2022

I replied on php/php-src#8351 and I think this applies here to:

"Encapsulation" relates to the public API of some software design. A constructor doesn't belong to what "encapsulation" refers to. Yes, you can mutate the object. So does reflection with private properties. Calling the constructor is like using reflection to me: do it if you know why you shouldn't 🤷 ...

I'd suggest closing this as "won't fix".

@Nyholm Nyholm added the wontfix label Jun 22, 2022
@Nyholm
Copy link
Owner

Nyholm commented Jun 22, 2022

You are very correct. But I think we should not really care in this case.

Thank you for reporting.

@Nyholm Nyholm closed this as completed Jun 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants