-
Notifications
You must be signed in to change notification settings - Fork 3
/
Plugin.php
169 lines (145 loc) · 5.61 KB
/
Plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php namespace OFFLINE\CSP;
use Backend\Classes\FormField;
use Backend\Facades\Backend;
use Backend\FormWidgets\CodeEditor;
use Event;
use Illuminate\Contracts\Http\Kernel;
use Illuminate\Http\Response;
use OFFLINE\CSP\Classes\CSPMiddleware;
use OFFLINE\CSP\Classes\NonceInjector;
use OFFLINE\CSP\Classes\Policy;
use OFFLINE\CSP\Console\DisableCSPPlugin;
use OFFLINE\CSP\Models\CSPSettings;
use OFFLINE\LaravelCSP\Nonce\NonceGenerator;
use OFFLINE\LaravelCSP\Nonce\RandomString;
use System\Classes\PluginBase;
use System\Controllers\Settings;
use System\Traits\ViewMaker;
use Request;
class Plugin extends PluginBase
{
const REPORT_ENDPOINT = 'csp-endpoint';
const REPORT_URI = '/_csp/report-uri';
use ViewMaker;
public function boot()
{
$this->app->singleton(NonceGenerator::class, RandomString::class);
$this->app->singleton('csp-nonce', function () {
if (Request::pjax() && $nonce = Request::header('X-Turbo-Nonce')) {
return $nonce;
}
return app(NonceGenerator::class)->generate();
});
// Register the CSP middleware if it is enabled.
if ((bool)CSPSettings::get('enabled')) {
$this->app[Kernel::class]->pushMiddleware(CSPMiddleware::class);
}
if (CSPSettings::get('inject_nonce')) {
// Automatically inject the nonce attribute into each script and style tag.
Event::listen('cms.page.postprocess', function ($controller, $url, $page, $dataHolder) {
if ( ! is_object($dataHolder) || ! property_exists($dataHolder, 'content')) {
return;
}
$dataHolder->content = $this->handleNonceInjection($dataHolder->content);
});
}
// Register the onShowCSP handler for the backend settings page.
\System\Controllers\Settings::extend(function ($controller) {
$controller->addDynamicMethod('onShowCSP', function () use ($controller) {
$csp = (string)(new Policy(post('CSPSettings', [])))->configure();
$formWidget = $this->buildCodeEditor($controller, $csp);
return $this->makePartial('$/offline/csp/models/cspsettings/_csp_modal.htm', [
'widget' => $formWidget,
'csp' => $csp,
]);
});
});
}
public function register()
{
$this->registerConsoleCommand('csp.disable', DisableCSPPlugin::class);
}
public function registerMarkupTags()
{
return [
'functions' => [
'csp_nonce' => function () {
return app()->bound('csp-nonce') ? app('csp-nonce') : '';
},
'csp_meta' => function () {
$record = CSPSettings::getSettingsRecord();
if ( ! $record) {
return '';
}
$settings = $record->value;
// reporting is not supported in meta tags.
$settings['report_mode'] = 'disabled';
return (string)(new Policy($settings))->configure();
},
],
];
}
public function registerSettings()
{
return [
'csp' => [
'label' => 'CSP',
'description' => 'offline.csp::lang.settings.description',
'category' => 'CSP',
'icon' => 'icon-lock',
'class' => CSPSettings::class,
'order' => 600,
'keywords' => 'csp security content policy',
'permissions' => ['offline.csp.manage_settings'],
],
'csp_logs' => [
'label' => 'offline.csp::lang.log.label',
'description' => 'offline.csp::lang.log.description',
'category' => 'CSP',
'icon' => 'icon-list',
'url' => Backend::url('offline/csp/csplogs'),
'order' => 601,
'keywords' => 'csp security content policy',
'permissions' => ['offline.csp.manage_settings'],
],
];
}
protected function handleNonceInjection($response)
{
$injector = NonceInjector::withNonce(app('csp-nonce'));
// String response, we can inject directly.
if (is_string($response)) {
return $injector->inject($response);
}
// If it is neither a String nor a proper Response response,
// we just return the original value.
if ( ! $response instanceof Response) {
return $response;
}
// Don't inject into redirects.
if ($response->isRedirect()) {
return $response;
}
// If this is a json response, we don't inject anything.
$isJson = $response->headers->get('content-type') === 'application/json';
if ($isJson) {
return $response;
}
// Simple response, just replace the content.
return $response->setContent($injector->inject($response->getContent()));
}
protected function buildCodeEditor(Settings $controller, string $csp): CodeEditor
{
$field = new FormField('csp', 'csp');
$config = [
'fontSize' => 13,
'margin' => 15,
'showGutter' => false,
'displayIndentGuides' => false,
'showPrintMargin' => false,
];
// Make sure every directive starts on its own line for better visibility.
$field->value = str_replace('; ', ";\n", $csp);
return new CodeEditor($controller, $field, $config);
}
}