Skip to content

Commit e613bfc

Browse files
committed
Add optional browser storage (localStorage) support for HTML attachments
1 parent 853cdd7 commit e613bfc

5 files changed

Lines changed: 26 additions & 2 deletions

File tree

src/Controller/AttachmentFileController.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,16 @@ public function htmlSandbox(Attachment $attachment, AttachmentsSettings $attachm
6666
'attachment' => $attachment,
6767
'content' => $attachmentContent,
6868
'show_warning' => $attachmentsSettings->showHTMLAttachmentWarning,
69+
'allow_storage' => $attachmentsSettings->allowHTMLAttachmentStorage,
6970
]);
7071

7172
//Set an CSP that allows to run inline scripts, styles and images from external ressources, but does not allow any connections or others.
7273
//Also set the sandbox CSP directive with only "allow-script" to run basic scripts
73-
$response->headers->set('Content-Security-Policy', "default-src 'none'; script-src 'unsafe-inline'; style-src 'unsafe-inline' *; img-src data: *; sandbox allow-scripts allow-downloads allow-modals;");
74+
$sandboxDirectives = 'allow-scripts allow-downloads allow-modals';
75+
if ($attachmentsSettings->allowHTMLAttachmentStorage) {
76+
$sandboxDirectives .= ' allow-same-origin';
77+
}
78+
$response->headers->set('Content-Security-Policy', "default-src 'none'; script-src 'unsafe-inline'; style-src 'unsafe-inline' *; img-src data: *; sandbox {$sandboxDirectives};");
7479

7580
//Forbid to embed the attachment render page in an iframe to prevent clickjacking, as it is not used anywhere else for now
7681
$response->headers->set('X-Frame-Options', 'DENY');

src/Services/InfoProviderSystem/Providers/LCSCProvider.php

100644100755
File mode changed.

src/Settings/SystemSettings/AttachmentsSettings.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,11 @@ class AttachmentsSettings
7272
envVar: "bool:ATTACHMENT_SHOW_HTML_WARNING", envVarMode: EnvVarMode::OVERWRITE
7373
)]
7474
public bool $showHTMLAttachmentWarning = true;
75+
76+
#[SettingsParameter(
77+
label: new TM("settings.system.attachments.allowHTMLAttachmentStorage"),
78+
description: new TM("settings.system.attachments.allowHTMLAttachmentStorage.help"),
79+
envVar: "bool:ATTACHMENT_ALLOW_HTML_STORAGE", envVarMode: EnvVarMode::OVERWRITE
80+
)]
81+
public bool $allowHTMLAttachmentStorage = false;
7582
}

templates/attachments/html_sandbox.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767

6868
<iframe referrerpolicy="no-referrer" class="content-frame"
6969
{# When changing this sandbox, also change the sandbox CSP in the controller #}
70-
sandbox="allow-scripts allow-downloads allow-modals"
70+
sandbox="allow-scripts allow-downloads allow-modals{% if allow_storage %} allow-same-origin{% endif %}"
7171
srcdoc="{{ content|e('html_attr') }}"
7272
></iframe>
7373

translations/messages.en.xlf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13013,6 +13013,18 @@ Buerklin-API Authentication server:
1301313013
<target>When enabled, a warning banner is shown when viewing user uploaded HTML attachments in the sandbox, reminding the user that the content is untrusted.</target>
1301413014
</segment>
1301513015
</unit>
13016+
<unit id="Cx4Pqr9" name="settings.system.attachments.allowHTMLAttachmentStorage">
13017+
<segment state="translated">
13018+
<source>settings.system.attachments.allowHTMLAttachmentStorage</source>
13019+
<target>Allow HTML attachments to use browser storage (localStorage)</target>
13020+
</segment>
13021+
</unit>
13022+
<unit id="Dy5Rst0" name="settings.system.attachments.allowHTMLAttachmentStorage.help">
13023+
<segment state="translated">
13024+
<source>settings.system.attachments.allowHTMLAttachmentStorage.help</source>
13025+
<target>⚠️ When enabled, scripts inside user uploaded HTML attachments can read and write to the browser&apos;s localStorage. This allows data to persist across page reloads, but grants the attachment access to Part-DB&apos;s browser origin. Only enable this if you fully trust all users who can upload files.</target>
13026+
</segment>
13027+
</unit>
1301613028
<unit id="bRcdnJK" name="attachment.sandbox.back_to_partdb">
1301713029
<segment state="translated">
1301813030
<source>attachment.sandbox.back_to_partdb</source>

0 commit comments

Comments
 (0)