Skip to content

Commit 897bb33

Browse files
committed
CSP: Updated handling of drawio URL to consider port
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 #5107
1 parent 767699a commit 897bb33

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

app/Util/CspService.php

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -133,18 +133,30 @@ protected function getAllowedIframeHosts(): array
133133

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

139-
// Extract drawing service url to allow embedding if active
139+
return array_filter($sources);
140+
}
141+
142+
/**
143+
* Extract the host name of the configured drawio URL for use in CSP.
144+
* Returns empty string if not in use.
145+
*/
146+
protected function getDrawioHost(): string
147+
{
140148
$drawioConfigValue = config('services.drawio');
141-
if ($drawioConfigValue) {
142-
$drawioSource = is_string($drawioConfigValue) ? $drawioConfigValue : 'https://embed.diagrams.net/';
143-
$drawioSourceParsed = parse_url($drawioSource);
144-
$drawioHost = $drawioSourceParsed['scheme'] . '://' . $drawioSourceParsed['host'];
145-
$hosts[] = $drawioHost;
149+
if (!$drawioConfigValue) {
150+
return '';
151+
}
152+
153+
$drawioSource = is_string($drawioConfigValue) ? $drawioConfigValue : 'https://embed.diagrams.net/';
154+
$drawioSourceParsed = parse_url($drawioSource);
155+
$drawioHost = $drawioSourceParsed['scheme'] . '://' . $drawioSourceParsed['host'];
156+
if (isset($drawioSourceParsed['port'])) {
157+
$drawioHost .= ':' . $drawioSourceParsed['port'];
146158
}
147159

148-
return $hosts;
160+
return $drawioHost;
149161
}
150162
}

tests/SecurityHeaderTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,18 @@ public function test_frame_src_csp_header_has_drawio_host_added()
139139
$this->assertEquals('frame-src \'self\' https://example.com https://diagrams.example.com', $scriptHeader);
140140
}
141141

142+
public function test_frame_src_csp_header_drawio_host_includes_port_if_existing()
143+
{
144+
config()->set([
145+
'app.iframe_sources' => 'https://example.com',
146+
'services.drawio' => 'https://diagrams.example.com:8080/testing?cat=dog',
147+
]);
148+
149+
$resp = $this->get('/');
150+
$scriptHeader = $this->getCspHeader($resp, 'frame-src');
151+
$this->assertEquals('frame-src \'self\' https://example.com https://diagrams.example.com:8080', $scriptHeader);
152+
}
153+
142154
public function test_cache_control_headers_are_set_on_responses()
143155
{
144156
// Public access

0 commit comments

Comments
 (0)