Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add headers for wasm support #3260

Merged
merged 3 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
'OCA\\Richdocuments\\Listener\\AddContentSecurityPolicyListener' => $baseDir . '/../lib/Listener/AddContentSecurityPolicyListener.php',
'OCA\\Richdocuments\\Listener\\AddFeaturePolicyListener' => $baseDir . '/../lib/Listener/AddFeaturePolicyListener.php',
'OCA\\Richdocuments\\Listener\\BeforeFetchPreviewListener' => $baseDir . '/../lib/Listener/BeforeFetchPreviewListener.php',
'OCA\\Richdocuments\\Listener\\BeforeTemplateRenderedListener' => $baseDir . '/../lib/Listener/BeforeTemplateRenderedListener.php',
'OCA\\Richdocuments\\Listener\\FileCreatedFromTemplateListener' => $baseDir . '/../lib/Listener/FileCreatedFromTemplateListener.php',
'OCA\\Richdocuments\\Listener\\LoadViewerListener' => $baseDir . '/../lib/Listener/LoadViewerListener.php',
'OCA\\Richdocuments\\Listener\\ReferenceListener' => $baseDir . '/../lib/Listener/ReferenceListener.php',
Expand Down
1 change: 1 addition & 0 deletions composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class ComposerStaticInitRichdocuments
'OCA\\Richdocuments\\Listener\\AddContentSecurityPolicyListener' => __DIR__ . '/..' . '/../lib/Listener/AddContentSecurityPolicyListener.php',
'OCA\\Richdocuments\\Listener\\AddFeaturePolicyListener' => __DIR__ . '/..' . '/../lib/Listener/AddFeaturePolicyListener.php',
'OCA\\Richdocuments\\Listener\\BeforeFetchPreviewListener' => __DIR__ . '/..' . '/../lib/Listener/BeforeFetchPreviewListener.php',
'OCA\\Richdocuments\\Listener\\BeforeTemplateRenderedListener' => __DIR__ . '/..' . '/../lib/Listener/BeforeTemplateRenderedListener.php',
'OCA\\Richdocuments\\Listener\\FileCreatedFromTemplateListener' => __DIR__ . '/..' . '/../lib/Listener/FileCreatedFromTemplateListener.php',
'OCA\\Richdocuments\\Listener\\LoadViewerListener' => __DIR__ . '/..' . '/../lib/Listener/LoadViewerListener.php',
'OCA\\Richdocuments\\Listener\\ReferenceListener' => __DIR__ . '/..' . '/../lib/Listener/ReferenceListener.php',
Expand Down
3 changes: 3 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use OCA\Richdocuments\Listener\AddContentSecurityPolicyListener;
use OCA\Richdocuments\Listener\AddFeaturePolicyListener;
use OCA\Richdocuments\Listener\BeforeFetchPreviewListener;
use OCA\Richdocuments\Listener\BeforeTemplateRenderedListener;
use OCA\Richdocuments\Listener\FileCreatedFromTemplateListener;
use OCA\Richdocuments\Listener\LoadViewerListener;
use OCA\Richdocuments\Listener\ReferenceListener;
Expand All @@ -53,6 +54,7 @@
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
use OCP\Collaboration\Reference\RenderReferenceEvent;
use OCP\Files\Template\FileCreatedFromTemplateEvent;
use OCP\Files\Template\ITemplateManager;
Expand Down Expand Up @@ -84,6 +86,7 @@ public function register(IRegistrationContext $context): void {
$context->registerEventListener(ShareLinkAccessedEvent::class, ShareLinkListener::class);
$context->registerEventListener(BeforePreviewFetchedEvent::class, BeforeFetchPreviewListener::class);
$context->registerEventListener(RenderReferenceEvent::class, ReferenceListener::class);
$context->registerEventListener(BeforeTemplateRenderedEvent::class, BeforeTemplateRenderedListener::class);
$context->registerReferenceProvider(OfficeTargetReferenceProvider::class);
$context->registerSensitiveMethods(WopiMapper::class, [
'getPathForToken',
Expand Down
6 changes: 6 additions & 0 deletions lib/Listener/AddContentSecurityPolicyListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
namespace OCA\Richdocuments\Listener;

use OCA\Richdocuments\AppConfig;
use OCA\Richdocuments\Service\CapabilitiesService;
use OCP\AppFramework\Http\EmptyContentSecurityPolicy;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
Expand All @@ -36,6 +37,7 @@ class AddContentSecurityPolicyListener implements IEventListener {
public function __construct(
private IRequest $request,
private AppConfig $config,
private CapabilitiesService $capabilitiesService,
) {
}

Expand All @@ -52,6 +54,10 @@ public function handle(Event $event): void {
$policy->addAllowedFrameDomain("'self'");
$policy->addAllowedFrameDomain("nc:");

if ($this->capabilitiesService->hasWASMSupport()) {
$policy->allowEvalWasm(true);
}

foreach ($this->config->getDomainList() as $url) {
$policy->addAllowedFrameDomain($url);
$policy->addAllowedFormActionDomain($url);
Expand Down
30 changes: 30 additions & 0 deletions lib/Listener/BeforeTemplateRenderedListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace OCA\Richdocuments\Listener;

use OCA\Richdocuments\Service\CapabilitiesService;
use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;

/** @template-implements IEventListener<BeforeTemplateRenderedEvent|Event> */
class BeforeTemplateRenderedListener implements IEventListener {
private CapabilitiesService $capabilitiesService;

public function __construct(CapabilitiesService $capabilitiesService) {
$this->capabilitiesService = $capabilitiesService;
}

public function handle(Event $event): void {
if (!$event instanceof BeforeTemplateRenderedEvent) {
return;
}

if ($this->capabilitiesService->hasWASMSupport()) {
$event->getResponse()->addHeader('Cross-Origin-Opener-Policy', 'same-origin');
$event->getResponse()->addHeader('Cross-Origin-Embedder-Policy', 'require-corp');
}
}
}
4 changes: 4 additions & 0 deletions lib/Service/CapabilitiesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ public function hasZoteroSupport(): bool {
return $this->getCapabilities()['hasZoteroSupport'] ?? false;
}

public function hasWASMSupport(): bool {
return $this->getCapabilities()['hasWASMSupport'] ?? false;
}

public function getProductName(): string {
$theme = $this->config->getAppValue(Application::APPNAME, 'theme', 'nextcloud');

Expand Down
25 changes: 24 additions & 1 deletion tests/lib/Listener/AddContentSecurityPolicyListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use OC\Security\CSP\ContentSecurityPolicyManager;
use OCA\Richdocuments\AppConfig;
use OCA\Richdocuments\Listener\AddContentSecurityPolicyListener;
use OCA\Richdocuments\Service\CapabilitiesService;
use OCA\Richdocuments\Service\FederationService;
use OCP\App\IAppManager;
use OCP\AppFramework\Http\ContentSecurityPolicy;
Expand All @@ -53,6 +54,7 @@ class AddContentSecurityPolicyListenerTest extends TestCase {
private $gsConfig;
/** @var FederationService|MockObject */
private $federationService;
private CapabilitiesService|MockObject $capabilitiesService;
private AddContentSecurityPolicyListener $listener;

public function setUp(): void {
Expand All @@ -76,11 +78,12 @@ public function setUp(): void {
])
->onlyMethods(['getCollaboraUrlPublic', 'getGlobalScaleTrustedHosts'])
->getMock();

$this->capabilitiesService = $this->createMock(CapabilitiesService::class);

$this->listener = new AddContentSecurityPolicyListener(
$this->request,
$this->config,
$this->capabilitiesService,
);
}

Expand Down Expand Up @@ -228,4 +231,24 @@ public static function assertArrayUnordered($expected, $actual, $msg = '') {
sort($actual);
self::assertSame($expected, $actual, $msg);
}

public function testWasm() {
$this->expectPageLoad();
$this->capabilitiesService->method('hasWASMSupport')
->willReturn(true);

$policy = $this->getMergedPolicy();

self::assertTrue(str_contains($policy->buildPolicy(), ' \'wasm-unsafe-eval\''));
}

public function testNoWasm() {
$this->expectPageLoad();
$this->capabilitiesService->method('hasWASMSupport')
->willReturn(false);

$policy = $this->getMergedPolicy();

self::assertTrue(!str_contains($policy->buildPolicy(), ' \'wasm-unsafe-eval\''));
}
}