Skip to content

Commit 3fc1dd1

Browse files
in case of invalid ID, only pageread and pageadd route redirect to corected ID. close #529
1 parent 6319c1f commit 3fc1dd1

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed

app/class/Model.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,22 @@ abstract class Model
5353
'version' => 'version',
5454
];
5555

56-
public const ID_REGEX = "%[^a-z0-9-_]%";
57-
public const MAX_ID_LENGTH = 64;
58-
public const PASSWORD_MIN_LENGTH = 4;
59-
public const PASSWORD_MAX_LENGTH = 128;
60-
public const MAX_COOKIE_CONSERVATION = 365;
61-
public const MAX_QUERY_LENGH = 512;
56+
/** Characters that are authorized in item ID */
57+
public const ID_AUTHORIZED_CHARS = 'a-z0-9-_';
58+
59+
/** Maximum database item ID length */
60+
public const MAX_ID_LENGTH = 64;
61+
62+
/** Regex for unauthorized characters in item IDs */
63+
public const ID_UNAUTHORIZED_CHARS_REGEX = '[^' . self::ID_AUTHORIZED_CHARS . ']';
64+
65+
/** Regex for database items IDs*/
66+
public const ID_REGEX = '[' . self::ID_AUTHORIZED_CHARS . ']{1,' . self::MAX_ID_LENGTH . '}';
67+
68+
public const PASSWORD_MIN_LENGTH = 4;
69+
public const PASSWORD_MAX_LENGTH = 128;
70+
public const MAX_COOKIE_CONSERVATION = 365;
71+
public const MAX_QUERY_LENGH = 512;
6272

6373

6474
public static function dirtopath(string $dir): string
@@ -139,7 +149,8 @@ public static function idclean(string $input, int $max = self::MAX_ID_LENGTH): s
139149
$replace = ['e', 'a', 'e', 'c', 'u', 'u', 'i', 'i', '-'];
140150
$input = str_replace($search, $replace, $input);
141151

142-
$input = preg_replace(static::ID_REGEX, '', strtolower(trim($input)));
152+
$regex = '%' . self::ID_UNAUTHORIZED_CHARS_REGEX . '%';
153+
$input = preg_replace($regex, '', strtolower(trim($input)));
143154
$input = mb_substr($input, 0, $max);
144155
}
145156
return $input;
@@ -153,7 +164,7 @@ public static function idclean(string $input, int $max = self::MAX_ID_LENGTH): s
153164
public static function idcheck(string $id, int $max = self::MAX_ID_LENGTH): bool
154165
{
155166
return (
156-
!((bool) preg_match(static::ID_REGEX, $id))
167+
!((bool) preg_match('%' . self::ID_UNAUTHORIZED_CHARS_REGEX . '%', $id))
157168
&& strlen($id) <= $max
158169
&& strlen($id) > 0
159170
);

app/class/Modelmedia.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ class Modelmedia extends Model
2323
'extension' => 'extension'
2424
];
2525

26-
public const ID_REGEX = "%[^a-z0-9-_.]%";
26+
/** Characters that are authorized for cleaned media filename */
27+
public const ID_AUTHORIZED_CHARS = 'a-z0-9-_.';
2728

2829
public const OPTIMIZE_IMG_MAX_WIDTH = 1920;
2930
public const OPTIMIZE_IMG_MAX_HEIGHT = 1920;

app/class/Routes.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ public function match(): void
1818
if (!empty(Config::basepath())) {
1919
$router->setBasePath('/' . Config::basepath());
2020
}
21-
$router->addMatchTypes(array('cid' => '[^/]+'));
21+
$router->addMatchTypes(array('noslash' => '[^/]+'));
22+
$router->addMatchTypes(array('cid' => Model::ID_REGEX));
2223
$router->addRoutes([
2324
['GET', '/api/v0/page/[cid:page]', 'Controllerapipage#get', 'apipageget'],
2425
['GET', '/api/v0/pages/list', 'Controllerapipage#list', 'apipagelist'],
@@ -69,13 +70,13 @@ public function match(): void
6970
['POST', '/!profile', 'Controllerprofile#update', 'profileupdate'],
7071
['POST', '/!profile/password', 'Controllerprofile#password', 'profilepassword'],
7172
['GET', '/!info', 'Controllerinfo#desktop', 'info'],
72-
['GET', '/[cid:page]/', 'Controllerpage#pagepermanentredirect', 'pageread/'],
73+
['GET', '/[noslash:page]/', 'Controllerpage#pagepermanentredirect', 'pageread/'],
7374
['HEAD', '/[cid:page]/', 'Controllerpage#pagepermanentredirect', 'pageread/head'],
7475
['POST', '/[cid:page]', 'Controllerpage#read', 'pagereadpost'], /** Used for password protected pages */
75-
['GET', '/[cid:page]', 'Controllerpage#read', 'pageread'],
76+
['GET', '/[noslash:page]', 'Controllerpage#read', 'pageread'],
7677
['HEAD', '/[cid:page]', 'Controllerpage#readhead', 'pagereadhead'],
77-
['GET', '/[cid:page]/add', 'Controllerpage#add', 'pageadd'],
78-
['GET', '/[cid:page]/add:[cid:copy]', 'Controllerpage#addascopy', 'pageaddascopy'],
78+
['GET', '/[noslash:page]/add', 'Controllerpage#add', 'pageadd'],
79+
['GET', '/[noslash:page]/add:[cid:copy]', 'Controllerpage#addascopy', 'pageaddascopy'],
7980
['GET', '/[cid:page]/edit', 'Controllerpage#edit', 'pageedit'],
8081
['GET', '/[cid:page]/render', 'Controllerpage#render', 'pagerender'],
8182
['GET', '/[cid:page]/log', 'Controllerpage#log', 'pagelog'],
@@ -86,7 +87,7 @@ public function match(): void
8687
['POST', '/workspace/update', 'Controllerworkspace#update', 'workspaceupdate'],
8788
['GET', '/[cid:page]/delete', 'Controllerpage#delete', 'pagedelete'],
8889
['POST', '/[cid:page]/delete', 'Controllerpage#confirmdelete', 'pageconfirmdelete'],
89-
['GET', '/[cid:page]/duplicate:[cid:duplicate]', 'Controllerpage#duplicate', 'pageduplicate'],
90+
['GET', '/[cid:page]/duplicate:[noslash:duplicate]', 'Controllerpage#duplicate', 'pageduplicate'],
9091
['GET', '/[cid:page]/[*:command]', 'Controllerpage#commandnotfound', 'pageread/etoile'],
9192
]);
9293

0 commit comments

Comments
 (0)