Security Advisory: CSRF bypass with _METHOD overrides in Slim and Slim-Csrf
Two related security issues have recently been reported in Slim and Slim-Csrf that let a cross-site form skip the CSRF token check by using Slim’s method override.
Impact
Slim’s method override lets a POST request set its HTTP method with a _METHOD field in the body. CSRF middleware only checks the token for methods that change state, and if it runs after the override, it sees the overridden method. A cross-site HTML form can use this in two ways:
- Slim: the form POSTs
_METHOD=GET. The request becomes a GET, so the CSRF middleware skips the token check. The router dispatches it to any route that accepts GET, such as one set up with$app->any()or$app->map(['GET', 'POST'], ...), and the handler receives the POST body. HEAD, OPTIONS, TRACE and CONNECT work the same way, as does QUERY on Slim 4. - Slim-Csrf: the form POSTs
_METHOD=PURGE, or any other custom method. TheGuardmiddleware only validated the token for POST, PUT, DELETE and PATCH, so a route mapped to a custom method ran without a token. The check was also case-sensitive, so a lower-case method such aspostskipped it too.
In both cases, an attacker can make a logged-in user’s browser trigger a state-changing action without a CSRF token.
Your application is affected if all of these are true:
- It uses
_METHODoverrides. On Slim 4 these come fromMethodOverrideMiddleware. On Slim 3 they are built intoRequest::getMethod(). - Its CSRF middleware runs after the override. On Slim 4, that covers route-level and group-level middleware, and app-level middleware added before
MethodOverrideMiddleware. On Slim 3, CSRF middleware always sees the overridden method. - A handler that changes state either accepts GET, HEAD or OPTIONS (or QUERY, on Slim 4) and acts on the request body, or is mapped to a custom method.
- Its session cookies don’t use
SameSite=LaxorSameSite=Strict, which block the attack.
Handlers that only act when $request->getMethod() is POST, PUT, PATCH or DELETE are not affected by the Slim issue.
Severity: Low, CVSS 3.1 (CVSS:3.1/AV:N/AC:H/PR:N/UI:R/S:U/C:N/I:L/A:N) for each issue.
Affected versions
- Slim 4: all versions from 4.0.0 to 4.16.0 inclusive.
- Slim 3: all versions from 3.0.0 to 3.13.1 inclusive.
- Slim-Csrf 1.x: all versions from 1.0.0 to 1.5.1 inclusive.
- Slim-Csrf 0.x: all versions up to and including 0.8.3.
Patches
The issues are fixed in Slim 4.16.1 and 3.13.2, and Slim-Csrf 1.5.2 and 0.8.4.
Slim now ignores a _METHOD value of GET, HEAD, OPTIONS, TRACE or CONNECT, and Slim 4 also ignores QUERY, so the request stays a POST. _METHOD can still set PUT, PATCH, DELETE and custom methods. The X-Http-Method-Override header is unchanged, as a cross-site form cannot set custom headers.
Slim-Csrf 1.5.2 validates the token for every method except GET, HEAD, OPTIONS, QUERY and TRACE. Slim-Csrf 0.8.4 does the same, except for GET, HEAD, OPTIONS and TRACE. Both compare the method case-insensitively. After upgrading, requests with custom methods that pass through the Guard need a valid token, so if non-browser clients call such routes, exclude those routes from the Guard.
To be fully protected, upgrade both packages: Slim 4.16.1 with Slim-Csrf 1.5.2, or Slim 3.13.2 with Slim-Csrf 0.8.4.
Workarounds
If you can’t upgrade yet, you can reduce or remove the exposure in these ways:
- On Slim 4, register the CSRF middleware so that it runs before
MethodOverrideMiddleware. The last middleware added runs first, so add the CSRF middleware after it. The CSRF middleware then sees the original POST and checks the token. - Set session cookies with
SameSite=LaxorSameSite=Strict. - Don’t change state in routes that accept GET.
Acknowledgments
We are grateful to and thank Leandro Meiners for reporting these issues.
Further information
- GHSA-w62v-rcw8-mxvw advisory on GitHub (Slim)
- GHSA-p7vx-gggp-8hm7 advisory on GitHub (Slim-Csrf)