Skip to content

Commit 43686bb

Browse files
committed
feat: added proper 403 handling
1 parent f11005c commit 43686bb

28 files changed

+266
-73
lines changed

composer.lock

Lines changed: 19 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
53.2 KB
Binary file not shown.
53.3 KB
Binary file not shown.
52.9 KB
Binary file not shown.
52.3 KB
Binary file not shown.

phpmyfaq/assets/fonts/OFL.txt

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
Copyright 2020 Braille Institute of America, Inc.
2+
3+
This Font Software is licensed under the SIL Open Font License, Version 1.1.
4+
This license is copied below, and is also available with a FAQ at:
5+
https://openfontlicense.org
6+
7+
8+
-----------------------------------------------------------
9+
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
10+
-----------------------------------------------------------
11+
12+
PREAMBLE
13+
The goals of the Open Font License (OFL) are to stimulate worldwide
14+
development of collaborative font projects, to support the font creation
15+
efforts of academic and linguistic communities, and to provide a free and
16+
open framework in which fonts may be shared and improved in partnership
17+
with others.
18+
19+
The OFL allows the licensed fonts to be used, studied, modified and
20+
redistributed freely as long as they are not sold by themselves. The
21+
fonts, including any derivative works, can be bundled, embedded,
22+
redistributed and/or sold with any software provided that any reserved
23+
names are not used by derivative works. The fonts and derivatives,
24+
however, cannot be released under any other type of license. The
25+
requirement for fonts to remain under this license does not apply
26+
to any document created using the fonts or their derivatives.
27+
28+
DEFINITIONS
29+
"Font Software" refers to the set of files released by the Copyright
30+
Holder(s) under this license and clearly marked as such. This may
31+
include source files, build scripts and documentation.
32+
33+
"Reserved Font Name" refers to any names specified as such after the
34+
copyright statement(s).
35+
36+
"Original Version" refers to the collection of Font Software components as
37+
distributed by the Copyright Holder(s).
38+
39+
"Modified Version" refers to any derivative made by adding to, deleting,
40+
or substituting -- in part or in whole -- any of the components of the
41+
Original Version, by changing formats or by porting the Font Software to a
42+
new environment.
43+
44+
"Author" refers to any designer, engineer, programmer, technical
45+
writer or other person who contributed to the Font Software.
46+
47+
PERMISSION & CONDITIONS
48+
Permission is hereby granted, free of charge, to any person obtaining
49+
a copy of the Font Software, to use, study, copy, merge, embed, modify,
50+
redistribute, and sell modified and unmodified copies of the Font
51+
Software, subject to the following conditions:
52+
53+
1) Neither the Font Software nor any of its individual components,
54+
in Original or Modified Versions, may be sold by itself.
55+
56+
2) Original or Modified Versions of the Font Software may be bundled,
57+
redistributed and/or sold with any software, provided that each copy
58+
contains the above copyright notice and this license. These can be
59+
included either as stand-alone text files, human-readable headers or
60+
in the appropriate machine-readable metadata fields within text or
61+
binary files as long as those fields can be easily viewed by the user.
62+
63+
3) No Modified Version of the Font Software may use the Reserved Font
64+
Name(s) unless explicit written permission is granted by the corresponding
65+
Copyright Holder. This restriction only applies to the primary font name as
66+
presented to the users.
67+
68+
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
69+
Software shall not be used to promote, endorse or advertise any
70+
Modified Version, except to acknowledge the contribution(s) of the
71+
Copyright Holder(s) and the Author(s) or with their explicit written
72+
permission.
73+
74+
5) The Font Software, modified or unmodified, in part or in whole,
75+
must be distributed entirely under this license, and must not be
76+
distributed under any other license. The requirement for fonts to
77+
remain under this license does not apply to any document created
78+
using the Font Software.
79+
80+
TERMINATION
81+
This license becomes null and void if any of the above conditions are
82+
not met.
83+
84+
DISCLAIMER
85+
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
86+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
87+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
88+
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
89+
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
90+
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
91+
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
92+
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
93+
OTHER DEALINGS IN THE FONT SOFTWARE.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{% extends '@admin/index.twig' %}
2+
3+
{% block content %}
4+
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 mb-3 border-bottom">
5+
<h1 class="h2">
6+
<i aria-hidden="true" class="bi bi-exclamation-triangle"></i>
7+
{{ 'msgError403' | translate }}
8+
</h1>
9+
</div>
10+
11+
<p class="alert alert-danger">
12+
{{ 'msgError403Description' | translate }}
13+
</p>
14+
15+
<p class="small">
16+
{{ 'msgError403Hint' | translate }}
17+
</p>
18+
{% endblock %}

phpmyfaq/src/phpMyFAQ/Application.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
namespace phpMyFAQ;
2121

22+
use phpMyFAQ\Controller\Exception\ForbiddenException;
2223
use phpMyFAQ\Core\Exception;
2324
use Symfony\Component\DependencyInjection\ContainerInterface;
2425
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
@@ -154,6 +155,17 @@ private function handleRequest(
154155
headers: ['Content-Type' => 'application/json'],
155156
);
156157
}
158+
} catch (ForbiddenException $exception) {
159+
$message = Environment::isDebugMode()
160+
? $this->formatExceptionMessage(
161+
template: 'An error occurred: :message at line :line at :file',
162+
exception: $exception,
163+
)
164+
: 'Bad Request';
165+
$response = new Response(
166+
content: $message,
167+
status: Response::HTTP_FORBIDDEN,
168+
);
157169
} catch (BadRequestException $exception) {
158170
$message = Environment::isDebugMode()
159171
? $this->formatExceptionMessage(

phpmyfaq/src/phpMyFAQ/Controller/AbstractController.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use OpenApi\Attributes as OA;
2323
use phpMyFAQ\Captcha\Captcha;
2424
use phpMyFAQ\Configuration;
25+
use phpMyFAQ\Controller\Exception\ForbiddenException;
2526
use phpMyFAQ\Core\Exception;
2627
use phpMyFAQ\Enums\PermissionType;
2728
use phpMyFAQ\Filter;
@@ -151,7 +152,7 @@ protected function hasValidToken(): void
151152
}
152153

153154
/**
154-
* @throws Exception|\Exception
155+
* @throws \Exception
155156
*/
156157
protected function isSecured(): void
157158
{
@@ -181,7 +182,7 @@ protected function userIsSuperAdmin(): void
181182
}
182183

183184
/**
184-
* @throws UnauthorizedHttpException
185+
* @throws ForbiddenException
185186
*/
186187
protected function userHasGroupPermission(): void
187188
{
@@ -192,12 +193,12 @@ protected function userHasGroupPermission(): void
192193
|| !$currentUser->perm->hasPermission($currentUser->getUserId(), PermissionType::USER_DELETE->value)
193194
|| !$currentUser->perm->hasPermission($currentUser->getUserId(), PermissionType::GROUP_EDIT->value)
194195
) {
195-
throw new UnauthorizedHttpException(challenge: 'User has no group permission.');
196+
throw new ForbiddenException(message: 'User has no group permission.');
196197
}
197198
}
198199

199200
/**
200-
* @throws UnauthorizedHttpException
201+
* @throws ForbiddenException
201202
*/
202203
protected function userHasUserPermission(): void
203204
{
@@ -207,18 +208,18 @@ protected function userHasUserPermission(): void
207208
|| !$currentUser->perm->hasPermission($currentUser->getUserId(), PermissionType::USER_EDIT->value)
208209
|| !$currentUser->perm->hasPermission($currentUser->getUserId(), PermissionType::USER_DELETE->value)
209210
) {
210-
throw new UnauthorizedHttpException(challenge: 'User has no user permission.');
211+
throw new ForbiddenException(message: 'User has no user permission.');
211212
}
212213
}
213214

214215
/**
215-
* @throws UnauthorizedHttpException
216+
* @throws ForbiddenException
216217
*/
217218
protected function userHasPermission(PermissionType $permissionType): void
218219
{
219220
$currentUser = $this->currentUser;
220221
if (!$currentUser->perm->hasPermission($currentUser->getUserId(), $permissionType->value)) {
221-
throw new UnauthorizedHttpException(sprintf('User has no "%s" permission.', $permissionType->value));
222+
throw new ForbiddenException(message: sprintf('User has no "%s" permission.', $permissionType->name));
222223
}
223224
}
224225

phpmyfaq/src/phpMyFAQ/Controller/Administration/AbstractAdministrationController.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Exception;
2323
use phpMyFAQ\Administration\Helper;
2424
use phpMyFAQ\Controller\AbstractController;
25+
use phpMyFAQ\Controller\Exception\ForbiddenException;
2526
use phpMyFAQ\Enums\PermissionType;
2627
use phpMyFAQ\Helper\LanguageHelper;
2728
use phpMyFAQ\Service\Gravatar;
@@ -30,6 +31,7 @@
3031
use phpMyFAQ\Translation;
3132
use phpMyFAQ\Twig\TwigWrapper;
3233
use Symfony\Component\HttpFoundation\Request;
34+
use Symfony\Component\HttpFoundation\Response;
3335

3436
abstract class AbstractAdministrationController extends AbstractController
3537
{
@@ -60,7 +62,6 @@ protected function getHeader(Request $request): array
6062
'pageDirection' => Translation::get(key: 'direction'),
6163
'userHasAccessPermission' => $adminHelper->canAccessContent($this->currentUser),
6264
'msgSessionExpiration' => Translation::get(key: 'ad_session_expiration'),
63-
'pageAction' => $request->query->get('action') ? '?action=' . $request->query->get('action') : '',
6465
'renderedLanguageSelection' => LanguageHelper::renderSelectLanguage(
6566
$this->configuration->getLanguage()->getLanguage(),
6667
true,
@@ -362,6 +363,32 @@ private function getGravatarImage(): string
362363
return '';
363364
}
364365

366+
protected function userHasPermission(PermissionType $permissionType): void
367+
{
368+
try {
369+
parent::userHasPermission($permissionType);
370+
} catch (ForbiddenException $exception) {
371+
$response = $this->getForbiddenPage($exception->getMessage());
372+
$response->send();
373+
} catch (Exception $exception) {
374+
$this->configuration->getLogger()->error($exception->getMessage());
375+
}
376+
}
377+
378+
/**
379+
* @throws Exception
380+
*/
381+
protected function getForbiddenPage(string $message = ''): Response
382+
{
383+
return $this->render(
384+
file: '@admin/error/forbidden.twig',
385+
context: [
386+
...$this->getHeader(Request::createFromGlobals()),
387+
...$this->getFooter(),
388+
],
389+
);
390+
}
391+
365392
/**
366393
* @return string[]
367394
*/

0 commit comments

Comments
 (0)