Skip to content

Commit b02c134

Browse files
committed
fix livereload pb with csp
1 parent 5723436 commit b02c134

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

src/Ubiquity/security/csp/ContentSecurity.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,7 @@ public function getPolicies(): array {
240240
*/
241241
public static function defaultUbiquity(): ContentSecurity {
242242
$csp = new self();
243-
$csp->addPolicy(CspDirectives::DEFAULT_SRC, 'self', 'cdn.jsdelivr.net', 'cdnjs.cloudflare.com');
244-
$csp->addPolicyDefault(CspDirectives::FONT_SRC, 'fonts.googleapis.com', 'fonts.gstatic.com', 'data:');
245-
$csp->addPolicyDefault(CspDirectives::STYLE_SRC, CspValues::UNSAFE_INLINE, 'fonts.googleapis.com');
246-
$csp->addPolicyDefault(CspDirectives::SCRIPT_SRC_ELM);
243+
$csp->addPolicyDefault(CspDirectives::CONNECT_SRC, CspValues::SELF);
247244
$csp->addPolicy(CspDirectives::IMG_SRC, 'data:');
248245
return $csp;
249246
}
@@ -258,9 +255,7 @@ public static function defaultUbiquityDebug(string $livereloadServer = '127.0.0.
258255
$csp = self::defaultUbiquity();
259256
$config = Startup::$config;
260257
if ($config['debug'] && \Ubiquity\debug\LiveReload::hasLiveReload()) {
261-
$csp->addHash('sha256-8Xnt4HKk9Yhr0dEXwbeeEDZpkRMxqi9xGg43hnmUurY=', CspDirectives::SCRIPT_SRC_ELM);
262258
$csp->addPolicyDefault(CspDirectives::CONNECT_SRC, "ws://$livereloadServer");
263-
$csp->addPolicy(CspDirectives::SCRIPT_SRC_ELM, "http://$livereloadServer");
264259
}
265260
return $csp;
266261
}

src/Ubiquity/security/csp/ContentSecurityManager.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@ public static function getNonce(string $name): string {
4343
return self::$nonceGenerator->getNonce($name);
4444
}
4545

46+
/**
47+
*
48+
* @param string $name
49+
* @return bool
50+
*/
51+
public static function hasNonce(string $name): bool {
52+
if (isset(self::$nonceGenerator)) {
53+
return self::$nonceGenerator->hasNonce($name);
54+
}
55+
return false;
56+
}
57+
4658
/**
4759
* Checks if the manager is started.
4860
*
@@ -86,7 +98,7 @@ public static function defaultUbiquity(?bool $reportOnly = null): ContentSecurit
8698
* @param string $livereloadServer
8799
* @return ContentSecurity
88100
*/
89-
public static function defaultUbiquityDebug(?bool $reportOnly = null,string $livereloadServer='127.0.0.1:35729'): ContentSecurity {
101+
public static function defaultUbiquityDebug(?bool $reportOnly = null, string $livereloadServer = '127.0.0.1:35729'): ContentSecurity {
90102
return self::$csp[] = ContentSecurity::defaultUbiquityDebug($livereloadServer)->reportOnly($reportOnly);
91103
}
92104

src/Ubiquity/security/csp/NonceGenerator.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,19 @@ public function getNonce(string $name, int $size = 32): string {
3434
return $this->nonces[$name] ??= self::_generateNonce($name, $size);
3535
}
3636

37+
/**
38+
*
39+
* @param string $name
40+
* @return bool
41+
*/
42+
public function hasNonce(string $name): bool {
43+
return isset($this->nonces[$name]);
44+
}
45+
46+
/**
47+
*
48+
* @return string
49+
*/
3750
public function __toString() {
3851
return \count($this->nonces);
3952
}

0 commit comments

Comments
 (0)