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

FeaturePolicy => PermissionPolicy #23825

Closed
wants to merge 3 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public function __construct(string $appName, array $urlParams = [], ServerContai
)
);
$dispatcher->registerMiddleware(
$server->query(OC\AppFramework\Middleware\Security\FeaturePolicyMiddleware::class)
$server->query(OC\AppFramework\Middleware\Security\PermissionPolicyMiddleware::class)
);
$dispatcher->registerMiddleware(
new OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,30 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OC\AppFramework\Middleware\Security;

use OC\Security\FeaturePolicy\FeaturePolicy;
use OC\Security\FeaturePolicy\FeaturePolicyManager;
use OC\Security\PermissionPolicy\PermissionPolicy;
use OC\Security\PermissionPolicy\PermissionPolicyManager;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\EmptyFeaturePolicy;
use OCP\AppFramework\Http\EmptyPermissionPolicy;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\Middleware;

class FeaturePolicyMiddleware extends Middleware {
class PermissionPolicyMiddleware extends Middleware {

/** @var FeaturePolicyManager */
private $policyManager;
private $featurePolicyManager;

/** @var PermissionPolicyManager */
private $permissionPolicyManager;

public function __construct(FeaturePolicyManager $policyManager) {
$this->policyManager = $policyManager;
public function __construct(FeaturePolicyManager $featurePolicyManager, PermissionPolicyManager $permissionPolicyManager) {
$this->featurePolicyManager = $featurePolicyManager;
$this->permissionPolicyManager = $permissionPolicyManager;
}

/**
Expand All @@ -50,15 +59,20 @@ public function __construct(FeaturePolicyManager $policyManager) {
* @return Response
*/
public function afterController($controller, $methodName, Response $response): Response {
$policy = !is_null($response->getFeaturePolicy()) ? $response->getFeaturePolicy() : new FeaturePolicy();

if (get_class($policy) === EmptyFeaturePolicy::class) {
return $response;
$featurePolicy = $response->getFeaturePolicy() ?? new FeaturePolicy();
if ($featurePolicy::class !== EmptyFeaturePolicy::class) {
$defaultPolicy = $this->featurePolicyManager->getDefaultPolicy();
$defaultPolicy = $this->featurePolicyManager->mergePolicies($defaultPolicy, $featurePolicy);
$response->setFeaturePolicy($defaultPolicy);
}

$defaultPolicy = $this->policyManager->getDefaultPolicy();
$defaultPolicy = $this->policyManager->mergePolicies($defaultPolicy, $policy);
$response->setFeaturePolicy($defaultPolicy);
$permissionPolicy = $response->getPermissionPolicy() ?? new PermissionPolicy();
if ($permissionPolicy::class !== EmptyPermissionPolicy::class) {
$defaultPolicy = $this->permissionPolicyManager->getDefaultPolicy();
$defaultPolicy = $this->permissionPolicyManager->mergePolicies($defaultPolicy, $permissionPolicy);
$defaultPolicy = $this->permissionPolicyManager->mergeFeaturePolicy($defaultPolicy, $response->getFeaturePolicy());
$response->setPermissionPolicy($defaultPolicy);
}

return $response;
}
Expand Down
76 changes: 76 additions & 0 deletions lib/private/Security/PermissionPolicy/PermissionPolicy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2020, Roeland Jago Douma <roeland@famdouma.nl>
*
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OC\Security\PermissionPolicy;

class PermissionPolicy extends \OCP\AppFramework\Http\PermissionPolicy {
public function getAutoplayDomains(): array {
return $this->autoplayDomains;
}

public function setAutoplayDomains(array $autoplayDomains): void {
$this->autoplayDomains = $autoplayDomains;
}

public function getCameraDomains(): array {
return $this->cameraDomains;
}

public function setCameraDomains(array $cameraDomains): void {
$this->cameraDomains = $cameraDomains;
}

public function getFullscreenDomains(): array {
return $this->fullscreenDomains;
}

public function setFullscreenDomains(array $fullscreenDomains): void {
$this->fullscreenDomains = $fullscreenDomains;
}

public function getGeolocationDomains(): array {
return $this->geolocationDomains;
}

public function setGeolocationDomains(array $geolocationDomains): void {
$this->geolocationDomains = $geolocationDomains;
}

public function getMicrophoneDomains(): array {
return $this->microphoneDomains;
}

public function setMicrophoneDomains(array $microphoneDomains): void {
$this->microphoneDomains = $microphoneDomains;
}

public function getPaymentDomains(): array {
return $this->paymentDomains;
}

public function setPaymentDomains(array $paymentDomains): void {
$this->paymentDomains = $paymentDomains;
}
}
94 changes: 94 additions & 0 deletions lib/private/Security/PermissionPolicy/PermissionPolicyManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2020, Roeland Jago Douma <roeland@famdouma.nl>
*
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/


namespace OC\Security\PermissionPolicy;

use OCP\AppFramework\Http\EmptyFeaturePolicy;
use OCP\AppFramework\Http\EmptyPermissionPolicy;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Security\PermissionPolicy\AddPermissionsPolicyEvent;

class PermissionPolicyManager {
/** @var EmptyPermissionPolicy[] */
private $policies = [];

/** @var IEventDispatcher */
private $dispatcher;

public function __construct(IEventDispatcher $dispatcher) {
$this->dispatcher = $dispatcher;
}

public function addDefaultPolicy(EmptyPermissionPolicy $policy): void {
$this->policies[] = $policy;
}

public function getDefaultPolicy(): PermissionPolicy {
$event = new AddPermissionsPolicyEvent($this);
$this->dispatcher->dispatchTyped($event);

$defaultPolicy = new PermissionPolicy();
foreach ($this->policies as $policy) {
$defaultPolicy = $this->mergePolicies($defaultPolicy, $policy);
}
return $defaultPolicy;
}

/**
* Merges the first given policy with the second one
*
*/
public function mergePolicies(PermissionPolicy $defaultPolicy,
EmptyPermissionPolicy $originalPolicy): PermissionPolicy {
foreach ((object)(array)$originalPolicy as $name => $value) {
$setter = 'set' . ucfirst($name);
if (\is_array($value)) {
$getter = 'get' . ucfirst($name);
$currentValues = \is_array($defaultPolicy->$getter()) ? $defaultPolicy->$getter() : [];
$defaultPolicy->$setter(\array_values(\array_unique(\array_merge($currentValues, $value))));
} elseif (\is_bool($value)) {
$defaultPolicy->$setter($value);
}
}

return $defaultPolicy;
}

public function mergeFeaturePolicy(PermissionPolicy $defaultPolicy, EmptyFeaturePolicy $featurePolicy): PermissionPolicy {
foreach ((object)(array)$featurePolicy as $name => $value) {
$setter = 'set' . ucfirst($name);
if (\is_array($value)) {
$getter = 'get' . ucfirst($name);
$currentValues = \is_array($defaultPolicy->$getter()) ? $defaultPolicy->$getter() : [];
$defaultPolicy->$setter(\array_values(\array_unique(\array_merge($currentValues, $value))));
} elseif (\is_bool($value)) {
$defaultPolicy->$setter($value);
}
}

return $defaultPolicy;
}
}
8 changes: 8 additions & 0 deletions lib/public/AppFramework/Http/EmptyFeaturePolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
*
* @see \OCP\AppFramework\Http\FeaturePolicy
* @since 17.0.0
* @deprecated 28.0.0 Use \OCP\AppFramework\Http\EmptyPermissionPolicy
*/
class EmptyFeaturePolicy {
/** @var string[] of allowed domains to autoplay media */
Expand All @@ -60,6 +61,7 @@ class EmptyFeaturePolicy {
* @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
* @return $this
* @since 17.0.0
* @deprecated 28.0.0 Use \OCP\AppFramework\Http\EmptyPermissionPolicy
*/
public function addAllowedAutoplayDomain(string $domain): self {
$this->autoplayDomains[] = $domain;
Expand All @@ -72,6 +74,7 @@ public function addAllowedAutoplayDomain(string $domain): self {
* @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
* @return $this
* @since 17.0.0
* @deprecated 28.0.0 Use \OCP\AppFramework\Http\EmptyPermissionPolicy
*/
public function addAllowedCameraDomain(string $domain): self {
$this->cameraDomains[] = $domain;
Expand All @@ -84,6 +87,7 @@ public function addAllowedCameraDomain(string $domain): self {
* @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
* @return $this
* @since 17.0.0
* @deprecated 28.0.0 Use \OCP\AppFramework\Http\EmptyPermissionPolicy
*/
public function addAllowedFullScreenDomain(string $domain): self {
$this->fullscreenDomains[] = $domain;
Expand All @@ -96,6 +100,7 @@ public function addAllowedFullScreenDomain(string $domain): self {
* @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
* @return $this
* @since 17.0.0
* @deprecated 28.0.0 Use \OCP\AppFramework\Http\EmptyPermissionPolicy
*/
public function addAllowedGeoLocationDomain(string $domain): self {
$this->geolocationDomains[] = $domain;
Expand All @@ -108,6 +113,7 @@ public function addAllowedGeoLocationDomain(string $domain): self {
* @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
* @return $this
* @since 17.0.0
* @deprecated 28.0.0 Use \OCP\AppFramework\Http\EmptyPermissionPolicy
*/
public function addAllowedMicrophoneDomain(string $domain): self {
$this->microphoneDomains[] = $domain;
Expand All @@ -120,6 +126,7 @@ public function addAllowedMicrophoneDomain(string $domain): self {
* @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
* @return $this
* @since 17.0.0
* @deprecated 28.0.0 Use \OCP\AppFramework\Http\EmptyPermissionPolicy
*/
public function addAllowedPaymentDomain(string $domain): self {
$this->paymentDomains[] = $domain;
Expand All @@ -131,6 +138,7 @@ public function addAllowedPaymentDomain(string $domain): self {
*
* @return string
* @since 17.0.0
* @deprecated 28.0.0 Use \OCP\AppFramework\Http\EmptyPermissionPolicy
*/
public function buildPolicy(): string {
$policy = '';
Expand Down
Loading