Skip to content

Commit

Permalink
Normalize paths to always have a leading slash when comparing against…
Browse files Browse the repository at this point in the history
… the CSRF blacklist (#775)
  • Loading branch information
alexweissman committed Aug 9, 2017
1 parent ef78c7a commit 8f3a40d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Change Log

## v4.1.8-alpha
- Normalize paths to always have a leading slash when comparing against the CSRF blacklist (#775)
- Reimplement `Builder::exclude` to maintain a list of excluded columns, and then automatically update list of columns to fetch in `get()`
- Deprecate `Model::queryBuilder` and `Model::export`
- Update nginx config file from spdy to http2
Expand Down
7 changes: 4 additions & 3 deletions app/sprinkles/core/src/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,17 @@ public static function getSubscribedEvents()
*/
public function onAddGlobalMiddleware(Event $event)
{
// Hacky fix to prevent sessions from being hit too much: ignore CSRF middleware for requests for raw assets ;-)
// See https://github.com/laravel/framework/issues/8172#issuecomment-99112012 for more information on why it's bad to hit Laravel sessions multiple times in rapid succession.
$request = $this->ci->request;
$path = $request->getUri()->getPath();
$method = $request->getMethod();

$csrfBlacklist = $this->ci->config['csrf.blacklist'];
// Normalize path to always have a leading slash
$path = '/' . ltrim($path, '/');

$csrfBlacklist = $this->ci->config['csrf.blacklist'];
$isBlacklisted = false;

// Go through the blacklist and determine if the path and method match any of the blacklist entries.
foreach ($csrfBlacklist as $pattern => $methods) {
$methods = array_map('strtoupper', (array) $methods);
if (in_array($method, $methods) && $pattern != '' && preg_match('~' . $pattern . '~', $path)) {
Expand Down
5 changes: 3 additions & 2 deletions app/sprinkles/core/src/ServicesProvider/ServicesProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,10 @@ public function register(ContainerInterface $container)
$config['site.uri.public'] = trim($public, '/');
}

// Add asset URLs to the CSRF blacklist
// Hacky fix to prevent sessions from being hit too much: ignore CSRF middleware for requests for raw assets ;-)
// See https://github.com/laravel/framework/issues/8172#issuecomment-99112012 for more information on why it's bad to hit Laravel sessions multiple times in rapid succession.
$csrfBlacklist = $config['csrf.blacklist'];
$csrfBlacklist['^' . $config['assets.raw.path']] = [
$csrfBlacklist['^/' . $config['assets.raw.path']] = [
'GET'
];

Expand Down

0 comments on commit 8f3a40d

Please sign in to comment.