Skip to content

Commit b26c53f

Browse files
committed
Disable the incompatible template names from being added to legalTemplates.
1 parent f34e00a commit b26c53f

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

ProcessGraphQLConfig.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use \ProcessWire\GraphQL\Type\InterfaceType\PageInterfaceType;
44
use \ProcessWire\GraphQL\Type\InterfaceType\PageFileInterfaceType;
5+
use \ProcessWire\GraphQL\Settings;
56

67
require_once $this->config->paths->site . 'modules/ProcessGraphQL/vendor/autoload.php';
78

@@ -31,6 +32,15 @@ public function getDefaults()
3132
);
3233
}
3334

35+
public static function isLegalTemplateName($name)
36+
{
37+
if (!$name) return false;
38+
if (preg_match('/^[_A-Za-z][-_0-9A-Za-z]*$/', $name) !== 1) return false; // the GraphQL naming requirement
39+
if (strpos($name, '__') === 0) return false; // the names with `__` prefix are reserved by GraphQL
40+
if (in_array($name, Settings::getReservedWords())) return false; // some words that used now and might be for future
41+
return true;
42+
}
43+
3444
public function getInputFields()
3545
{
3646
$inputfields = parent::getInputFields();
@@ -58,10 +68,22 @@ public function getInputFields()
5868
$f->attr('name', 'legalTemplates');
5969
$f->label = 'Legal Templates';
6070
$f->description = 'The pages with the templates that you select below will be available via your GraphQL api.';
61-
$f->notes = 'Please be careful with what you are exposing to the public. Choosing templates marked as `system` can lead to security vulnerabilities.';
62-
foreach (\ProcessWire\wire('templates') as $template) {
63-
$f->addOption($template->name, $template->flags & Template::flagSystem ? "{$template->name} `(system)`" : $template->name);
71+
$gotDisabledFields = false;
72+
foreach (\ProcessWire\wire('templates') as $template) {
73+
$attributes = [];
74+
if (!self::isLegalTemplateName($template->name)) {
75+
$attributes['disabled'] = true;
76+
$gotDisabledFields = true;
77+
}
78+
$label = $template->flags & Template::flagSystem ? "{$template->name} `(system)`" : $template->name;
79+
$f->addOption($template->name, $label, $attributes);
80+
}
81+
$notes = "Please be careful with what you are exposing to the public. Choosing templates marked as `system` can lead to security vulnerabilities.";
82+
if ($gotDisabledFields) {
83+
$notes .= PHP_EOL;
84+
$notes .= "The template is disabled if it's name is incompatible or reserved for ProcessGraphQL module.";
6485
}
86+
$f->notes = $notes;
6587
$inputfields->add($f);
6688

6789
// legalFields
@@ -71,7 +93,7 @@ public function getInputFields()
7193
$f->label = 'Legal Fields';
7294
$f->description = 'The fields that you select below will be available via your GraphQL api.';
7395
$f->notes = 'Please be careful with what you are exposing to the public. Choosing fields marked as `system` can to lead security vulnerabilities.';
74-
foreach (\ProcessWire\wire('fields')->find("name!=pass") as $field) {
96+
foreach (\ProcessWire\wire('fields')->find("name!=pass") as $field) {
7597
if ($field->type instanceof FieldtypeFieldsetOpen) continue;
7698
if ($field->type instanceof FieldtypeFieldsetClose) continue;
7799
if ($field->type instanceof FieldtypeFieldsetTabOpen) continue;

src/Settings.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,16 @@ public static function getLegalPageFileFields()
4949
return self::module()->legalPageFileFields;
5050
}
5151

52+
public static function getReservedWords()
53+
{
54+
return [
55+
'me', 'debug', 'login', 'logout',
56+
'pages','templates','template','fields',
57+
'roles', 'permissions', 'config', 'system',
58+
'wire', 'enum', 'trash', 'users', 'setup',
59+
'modules', 'access', 'find', 'logs', 'site',
60+
'core',
61+
];
62+
}
63+
5264
}

0 commit comments

Comments
 (0)