Problem/Motivation
In SA-CORE-2021-006, we added these lines to Drupal\media\Controller\MediaFilterController:
self::checkCsrf($request, \Drupal::currentUser());
// ...
if (!\Drupal::csrfToken()->validate($token, $header)) {
In a security issue, it is appropriate to use the \Drupal class, but now we should follow up and inject the csrf_token and current_user services.
As long as we are updating the constructor, we can make some additional changes (or we could defer these changes to a follow-up issue):
- Use constructor promotion.
- Use autowiring.
- Remove the unused property
$mediaStorage, which was added in #3076773: Edit Media button within WYSIWYG should include media label for screen readers.
The argument for doing all at once is that (1) and (2) will simplify the main task of this issue, and (3) will simplify (2).
Steps to reproduce
N/A
Proposed resolution
- Inject the
csrf_tokenandcurrent_userservices. - (Optional, could be a follow-up) Use constructor promotion.
- (Optional, could be a follow-up) Use autowiring.
- (Optional, could be a follow-up) Remove the unused property
$mediaStorage.
Since this class is marked @internal, we are allowed to make these changes (updating the signature of the constructor) without the usual steps to ensure backwards compatibility. Compare with the changes for #3076773: Edit Media button within WYSIWYG should include media label for screen readers.
Remaining tasks
User interface changes
None
Introduced terminology
None
API changes
None, since Drupal\media\Controller\MediaFilterController is marked @internal.
Data model changes
None
Release notes snippet
N/A
Issue fork drupal-3577774
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 3577774-use-dependency-injection
changes, plain diff MR !15025
Comments
Comment #4
benjifisher@karthik_gundu:
Thanks for starting to work on this issue.
It looks as though your text editor stripped the NL from the last line of the file, which caused the linting step in the CI job to fail. I am setting the issue status to NW to fix that.
If you can configure your text editor to respect the
.editorconfigsettings, then you will not run into this problem.I have reviewed but not tested. It looks as though the changes in the MR handle (1) from the Proposed resolution. I would like to handle the remaining points, but that is open for discussion. If we postpone them, then we should open a follow-up issue and update the issue summary.
Since this is a refactoring issue, we might not need manual testing. If the automated tests pass, then that is probably good enough.
Comment #6
sivaji_ganesh_jojodae commentedI've fixed the PHPCS issue.
Since this change is already refactoring the constructor for dependency injection, would it make sense to also adopt constructor property promotion (PHP 8+) here to reduce boilerplate?
For example:
Happy to keep the current approach if we prefer to avoid mixing coding styles or keep the change minimal.
Comment #7
smustgrave commentedLeft a comment on the MR.
Comment #8
benjifisher@sivaji_ganesh_jojodae:
Thanks for fixing the line-ending error.
Yes, that is #2 in the "Proposed resolution" section of the issue summary. I would like to make all the changes listed there. If you are not familiar with autowiring, then I can give you a reference.
Comment #9
sivaji_ganesh_jojodae commented@benjifisher, can you provide reference for autowiring? The tests are failing. Not sure if I'm missing something.
Comment #10
sivaji_ganesh_jojodae commentedComment #11
phenaproximaOne minor point, but it need not block commit. With my media maintainer hat on, I cheerfully approve this clean-up!
Comment #15
amateescu commentedCommitted and pushed 860e74337bc to main and b3f0ecc7ef1 to 11.x. Thanks!