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

fix: ensure csrf token is string #9365

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

datlechin
Copy link

Description
Make sure the CSRF token must be a string, because hackers can fake the request and pass csrf_main as an array, the system will break 500.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@kenjis kenjis added the tests needed Pull requests that need tests label Jan 3, 2025
@michalsn
Copy link
Member

michalsn commented Jan 3, 2025

The same check should be made for php:://input data.

@neznaika0
Copy link
Contributor

Look at this:

    private function getPostedToken(RequestInterface $request): ?string
    {
        assert($request instanceof IncomingRequest);

        // Does the token exist in POST, HEADER or optionally php:://input - json data or PUT, DELETE, PATCH - raw data.

        if ($tokenValue = $request->getPost($this->config->tokenName)) {
            return is_string($tokenValue) ? $tokenValue : null;
        }

        if ($request->hasHeader($this->config->headerName)
            && $request->header($this->config->headerName)->getValue() !== ''
            && $request->header($this->config->headerName)->getValue() !== []) {
            return $request->header($this->config->headerName)->getValue();
        }

        $body = (string) $request->getBody();

        if ($body !== '') {
            $json = json_decode($body);
            if ($json !== null && json_last_error() === JSON_ERROR_NONE) {
                return (isset($json->{$this->config->tokenName}) && is_string($json->{$this->config->tokenName}))
                    ? $json->{$this->config->tokenName}
                    : null;
            }

            parse_str($body, $parsed);

            return (isset($parsed[$this->config->tokenName]) && is_string($parsed[$this->config->tokenName]))
                ? $parsed[$this->config->tokenName]
                : null;
        }

        return null;
    }

@michalsn michalsn added bug Verified issues on the current code behavior or pull requests that will fix them GPG-Signing needed Pull requests that need GPG-Signing and removed tests needed Pull requests that need tests labels Jan 4, 2025
Copy link
Member

@michalsn michalsn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Please also handle the case where the CSRF token comes from the php://input.

Your commits have to be signed:

You can fix the code style with the command:

composer cs-fix

@@ -49,6 +51,16 @@ private function createMockSecurity(?SecurityConfig $config = null): MockSecurit
return new MockSecurity($config);
}

private function getPostedTokenMethod(): ReflectionMethod
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use $this->getPrivateMethod() instead of making this method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them GPG-Signing needed Pull requests that need GPG-Signing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants