mirror of
https://github.com/BookStackApp/BookStack.git
synced 2026-07-15 21:31:36 +03:00
Access: Hardened usage of referring URLs via login
Adds a more substantial URL check, via a new class which is shared and used in other parts of the app for consistency. Thanks to mfk25 for reporting.
This commit is contained in:
@@ -8,6 +8,7 @@ use BookStack\Exceptions\LoginAttemptEmailNeededException;
|
||||
use BookStack\Exceptions\LoginAttemptException;
|
||||
use BookStack\Facades\Activity;
|
||||
use BookStack\Http\Controller;
|
||||
use BookStack\Util\UrlComparison;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
@@ -186,7 +187,8 @@ class LoginController extends Controller
|
||||
{
|
||||
// Store the previous location for redirect after login
|
||||
$previous = url()->previous('');
|
||||
$isPreviousFromInstance = str_starts_with($previous, url('/'));
|
||||
$comparison = new UrlComparison($previous, url('/'));
|
||||
$isPreviousFromInstance = $comparison->originsMatch() && $comparison->pathsOverlap();
|
||||
if (!$previous || !setting('app-public') || !$isPreviousFromInstance) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ use BookStack\Theming\ThemeModule;
|
||||
use BookStack\Theming\ThemeModuleException;
|
||||
use BookStack\Theming\ThemeModuleManager;
|
||||
use BookStack\Theming\ThemeModuleZip;
|
||||
use BookStack\Util\UrlComparison;
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Str;
|
||||
@@ -199,7 +200,6 @@ class InstallModuleCommand extends Command
|
||||
{
|
||||
$httpRequests = app()->make(HttpRequestService::class);
|
||||
$client = $httpRequests->buildClient(30, ['stream' => true]);
|
||||
$originalUrl = parse_url($location);
|
||||
$currentLocation = $location;
|
||||
$maxRedirects = 3;
|
||||
$redirectCount = 0;
|
||||
@@ -212,12 +212,11 @@ class InstallModuleCommand extends Command
|
||||
if ($statusCode >= 300 && $statusCode < 400 && $redirectCount < $maxRedirects) {
|
||||
$redirectLocation = $resp->getHeaderLine('Location');
|
||||
if ($redirectLocation) {
|
||||
$redirectUrl = parse_url($redirectLocation);
|
||||
$redirectOriginMatches = ($originalUrl['host'] ?? '') === ($redirectUrl['host'] ?? '')
|
||||
&& ($originalUrl['scheme'] ?? '') === ($redirectUrl['scheme'] ?? '')
|
||||
&& ($originalUrl['port'] ?? '') === ($redirectUrl['port'] ?? '');
|
||||
$comparison = new UrlComparison($location, $redirectLocation);
|
||||
$redirectOriginMatches = $comparison->originsMatch();
|
||||
|
||||
if (!$redirectOriginMatches) {
|
||||
$redirectUrl = parse_url($redirectLocation);
|
||||
$redirectOrigin = ($redirectUrl['scheme'] ?? '') . '://' . ($redirectUrl['host'] ?? '') . (isset($redirectUrl['port']) ? ':' . $redirectUrl['port'] : '');
|
||||
$this->info("The download URL is redirecting to a different site: {$redirectOrigin}");
|
||||
$shouldContinue = $this->confirm("Do you trust downloading the module from this site?");
|
||||
|
||||
36
app/Util/UrlComparison.php
Normal file
36
app/Util/UrlComparison.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Util;
|
||||
|
||||
class UrlComparison
|
||||
{
|
||||
public function __construct(
|
||||
protected readonly string $a,
|
||||
protected readonly string $b,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the two URLs have the same origin.
|
||||
*/
|
||||
public function originsMatch(): bool
|
||||
{
|
||||
$aParts = parse_url($this->a);
|
||||
$bParts = parse_url($this->b);
|
||||
|
||||
return $aParts['host'] === $bParts['host']
|
||||
&& $aParts['scheme'] === $bParts['scheme']
|
||||
&& $aParts['port'] === $bParts['port'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if there's some overlap between the two URLs' paths.
|
||||
*/
|
||||
public function pathsOverlap(): bool
|
||||
{
|
||||
$aPath = parse_url($this->a, PHP_URL_PATH) ?? '';
|
||||
$bPath = parse_url($this->b, PHP_URL_PATH) ?? '';
|
||||
|
||||
return str_starts_with($aPath, $bPath) || str_starts_with($bPath, $aPath);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user