Skip to content

Commit

Permalink
CSP: Updated handling of drawio URL to consider port
Browse files Browse the repository at this point in the history
Previously if a custom port was used in the DRAWIO option it would not
be considered in the CSP handling, which would block loading.

Added test to cover.
For BookStackApp#5107
  • Loading branch information
ssddanbrown committed Jul 14, 2024
1 parent 767699a commit 897bb33
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
30 changes: 21 additions & 9 deletions app/Util/CspService.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,18 +133,30 @@ protected function getAllowedIframeHosts(): array

protected function getAllowedIframeSources(): array
{
$sources = config('app.iframe_sources', '');
$hosts = array_filter(explode(' ', $sources));
$sources = explode(' ', config('app.iframe_sources', ''));
$sources[] = $this->getDrawioHost();

// Extract drawing service url to allow embedding if active
return array_filter($sources);
}

/**
* Extract the host name of the configured drawio URL for use in CSP.
* Returns empty string if not in use.
*/
protected function getDrawioHost(): string
{
$drawioConfigValue = config('services.drawio');
if ($drawioConfigValue) {
$drawioSource = is_string($drawioConfigValue) ? $drawioConfigValue : 'https://embed.diagrams.net/';
$drawioSourceParsed = parse_url($drawioSource);
$drawioHost = $drawioSourceParsed['scheme'] . '://' . $drawioSourceParsed['host'];
$hosts[] = $drawioHost;
if (!$drawioConfigValue) {
return '';
}

$drawioSource = is_string($drawioConfigValue) ? $drawioConfigValue : 'https://embed.diagrams.net/';
$drawioSourceParsed = parse_url($drawioSource);
$drawioHost = $drawioSourceParsed['scheme'] . '://' . $drawioSourceParsed['host'];
if (isset($drawioSourceParsed['port'])) {
$drawioHost .= ':' . $drawioSourceParsed['port'];
}

return $hosts;
return $drawioHost;
}
}
12 changes: 12 additions & 0 deletions tests/SecurityHeaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,18 @@ public function test_frame_src_csp_header_has_drawio_host_added()
$this->assertEquals('frame-src \'self\' https://example.com https://diagrams.example.com', $scriptHeader);
}

public function test_frame_src_csp_header_drawio_host_includes_port_if_existing()
{
config()->set([
'app.iframe_sources' => 'https://example.com',
'services.drawio' => 'https://diagrams.example.com:8080/testing?cat=dog',
]);

$resp = $this->get('/');
$scriptHeader = $this->getCspHeader($resp, 'frame-src');
$this->assertEquals('frame-src \'self\' https://example.com https://diagrams.example.com:8080', $scriptHeader);
}

public function test_cache_control_headers_are_set_on_responses()
{
// Public access
Expand Down

0 comments on commit 897bb33

Please sign in to comment.