-
Notifications
You must be signed in to change notification settings - Fork 4
Enhancements to Mostly Static Resources (CFM-62) #442
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -167,6 +167,8 @@ function (RouteBuilder $builder) { | |
| // Main application routes | ||
| $routes->scope('/', function (RouteBuilder $builder) { | ||
| // Register scoped middleware for in scopes. | ||
| $builder->registerMiddleware('postMaxSizeCheck', new \App\Middleware\PostMaxSizeCheckMiddleware()); | ||
|
|
||
| $builder->registerMiddleware('csrf', new CsrfProtectionMiddleware([ | ||
| 'httponly' => true, | ||
| ])); | ||
|
|
@@ -237,6 +239,23 @@ function (RouteBuilder $builder) { | |
| $builder->fallbacks(); | ||
| }); | ||
|
|
||
| /** | ||
| * Mostly Static Resources need a postMaxSizeCheck when uploading files (add/edit). | ||
| * Check for post_max_size overage first: PHP wipes the POST data when the threshold is crossed | ||
| * and will cause a CSRF failure if not caught (because the crsf token will be missing from the POST). | ||
| */ | ||
| $routes->scope('/mostly-static-resources', function (RouteBuilder $builder) { | ||
| $builder->applyMiddleware('postMaxSizeCheck', 'csrf'); | ||
| $builder->connect( | ||
| '/add', | ||
| ['controller' => 'MostlyStaticResources', 'action' => 'add']); | ||
| $builder->connect( | ||
| '/edit/{id}', | ||
| ['controller' => 'MostlyStaticResources', 'action' => 'edit'], | ||
| ['id' => '\d+', 'pass' => ['id']] | ||
| ); | ||
|
Comment on lines
+249
to
+256
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't we inherit this from the default routes? ie, why do we need these and not |
||
| }); | ||
|
|
||
| /* | ||
| * If you need a different set of middleware or none at all, | ||
| * open new scope and define routes there. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -247,6 +247,9 @@ msgstr "Metadata" | |
| msgid "middle" | ||
| msgstr "Middle" | ||
|
|
||
| msgid "mime_type" | ||
| msgstr "Mime Type" | ||
|
|
||
| msgid "modifiable" | ||
| msgstr "Modifiable" | ||
|
|
||
|
|
@@ -931,11 +934,17 @@ msgstr "This Petition is complete and has been finalized. Please contact your ad | |
| msgid "MostlyStaticResources.file_content" | ||
| msgstr "File to Upload" | ||
|
|
||
| msgid "MostlyStaticResources.image" | ||
| msgstr "Resource Image" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there are reason not just to use |
||
|
|
||
| msgid "MostlyStaticResources.name" | ||
| msgstr "Slug" | ||
|
|
||
| msgid "MostlyStaticResources.name.desc" | ||
| msgstr "The URL fragment for this Resource, which must be unique and use only lowercase alphanumeric characters and dashes (-)" | ||
| msgstr "The URL fragment for this Resource, which must be unique and use only lowercase alphanumeric characters, dashes (-), underscores (_), and periods (.)" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See discussion in MostlyStaticResourcesTable, below. |
||
|
|
||
| msgid "MostlyStaticResources.url" | ||
| msgstr "Resource URL" | ||
|
|
||
| msgid "Notifications.actor_person_id" | ||
| msgstr "Actor" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| <?php | ||
| /** | ||
| * COmanage post_max_size Check for File Uploads | ||
| * | ||
| * Portions licensed to the University Corporation for Advanced Internet | ||
| * Development, Inc. ("UCAID") under one or more contributor license agreements. | ||
| * See the NOTICE file distributed with this work for additional information | ||
| * regarding copyright ownership. | ||
| * | ||
| * UCAID licenses this file to you under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with the | ||
| * License. You may obtain a copy of the License at: | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| * @link https://www.internet2.edu/comanage COmanage Project | ||
| * @package registry | ||
| * @since COmanage Registry v5.3.0 | ||
| * @license Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Middleware; | ||
|
|
||
| use Cake\Http\Exception\BadRequestException; | ||
| use Cake\Http\FlashMessage; | ||
| use Cake\Http\Response; | ||
| use Psr\Http\Message\ResponseInterface; | ||
| use Psr\Http\Message\ServerRequestInterface; | ||
| use Psr\Http\Server\MiddlewareInterface; | ||
| use Psr\Http\Server\RequestHandlerInterface; | ||
|
|
||
| class PostMaxSizeCheckMiddleware implements MiddlewareInterface { | ||
|
|
||
| public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { | ||
| if ($request->is('post')) { | ||
| $contentLength = (int)$request->getHeaderLine('Content-Length'); | ||
| $postMaxBytes = $this->iniSizeToBytes(ini_get('post_max_size')); | ||
|
|
||
| if ($contentLength > 0 && $postMaxBytes > 0 && $contentLength > $postMaxBytes) { | ||
| // Route the user back to where they came from with an error message if possible. | ||
| $referer = $request->getHeaderLine('Referer'); | ||
| $session = $request->getAttribute('session'); | ||
|
|
||
| if(empty($referer) || empty($session)) { | ||
| // There's no referer to reference (and possibly no session), so just show the more abrupt white-screen error. | ||
| throw new BadRequestException( | ||
| __d('error', 'upload.php.postmaxsize', [ini_get('post_max_size')]) | ||
| ); | ||
| } | ||
|
|
||
| // We have a referer, so set the flash message and return. | ||
| $flash = new FlashMessage($session); | ||
| $flash->error(__d('error', 'upload.php.postmaxsize', [ini_get('post_max_size')])); | ||
| return (new Response())->withStatus(302)->withHeader('Location', $referer); | ||
| } | ||
| } | ||
|
|
||
| return $handler->handle($request); | ||
| } | ||
|
|
||
| protected function iniSizeToBytes(string $val): int { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe just use PHP's ini_parse_quantity function instead? This imposes a minimum PHP of 8.2.0, but I think we're there already. |
||
| $val = trim($val); | ||
| if ($val === '') { | ||
| return 0; | ||
| } | ||
|
|
||
| $unit = strtolower($val[strlen($val) - 1]); | ||
| $num = (int)$val; | ||
|
|
||
| // Convert the value to bytes by letting it fall through the switch statement starting at | ||
| // the correct unit. If we start with "g", the value will be multiplied by 1024 three times. | ||
| switch ($unit) { | ||
| case 'g': $num *= 1024; | ||
| case 'm': $num *= 1024; | ||
| case 'k': $num *= 1024; | ||
| } | ||
|
|
||
| return $num; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -125,10 +125,10 @@ public function validationDefault(Validator $validator): Validator { | |
| $this->registerStringValidation($validator, $schema, 'name', true); | ||
|
|
||
| // AR-MostlyStaticResource-2 A Mostly Static Resource name may consist only of | ||
| // lowercase alphanumeric characters and dashes | ||
| // lowercase alphanumeric characters, dashes, underscores, and periods (for allowing extensions) | ||
| $validator->add('name', [ | ||
| 'slugfilter' => [ | ||
| 'rule' => ['custom', '/^[a-z0-9-]+$/'], | ||
| 'rule' => ['custom', '/^[a-z0-9._\-]+$/'], | ||
|
Comment on lines
-128
to
+131
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This makes me uncomfortable, though I'm not sure I have a good reason other than it's deviating from the out of the box pattern. Cake 3+ uses dashes, not underscores, in URLs. I get that someone might upload a file with an underscore, but we don't need to preserve the filename exactly as uploaded. Allowing periods for extensions also allows them for poorly constructed filenames. But maybe that's not a problem we should really be solving.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My goal here was to allow filenames to upload with minimal transformation - and dots and underscores are fairly common in image filenames (I much prefer dashes). I'm ok with skipping underscores or enforcing their change to dashes if we feel strongly about this. On extensions: we may not require them for rendering, since Content-Type is set explicitly from the stored mime_type column. But keeping the name field recognizable/familiar to whoever's managing these resources through the UI feels like better UX even if just a convenience. I'm hesitant to strip the extensions.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess I'd prefer to preserve these - but I can compromise on the underscore. I feel more strongly about the dot (for extensions). Thoughts?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, in the spirit of unnecessary compromise, let's convert underscores to dashes but leave the dots. |
||
| 'message' => __d('error', 'MostlyStaticPages.slug.invalid') | ||
| ] | ||
| ]); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not specific to this PR, but it looks like these
registerMiddlewarestatements are supposed to be defined outside of any scope?