Skip to content

Commit 83467f8

Browse files
committed
Merge pull request #382 from TheFRedFox/master
Removed inline javascript and style code to support Content Security Policy
2 parents 0ca3033 + c214f8c commit 83467f8

File tree

8 files changed

+52
-30
lines changed

8 files changed

+52
-30
lines changed

src/Controller/RequestsController.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public function beforeFilter(Event $event)
3636
if (!Configure::read('debug')) {
3737
throw new NotFoundException();
3838
}
39+
40+
$this->response->header(['Content-Security-Policy' => '']);
3941
}
4042

4143
/**

src/Routing/Filter/DebugBarFilter.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,7 @@ protected function _injectScripts($id, $response)
233233
return;
234234
}
235235
$url = Router::url('/', true);
236-
$script = "<script>var __debug_kit_id = '${id}', __debug_kit_base_url = '${url}';</script>";
237-
$script .= '<script src="' . Router::url('/debug_kit/js/toolbar.js') . '"></script>';
236+
$script = "<script id=\"__debug_kit\" data-id=\"{$id}\" data-url=\"{$url}\" src=\"" . Router::url('/debug_kit/js/toolbar.js') . '"></script>';
238237
$body = substr($body, 0, $pos) . $script . substr($body, $pos);
239238
$response->body($body);
240239
}

src/Template/Layout/toolbar.ctp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@
1313
</body>
1414
<?= $this->Html->script('DebugKit.jquery') ?>
1515
<?= $this->Html->script('DebugKit.toolbar-app') ?>
16-
<?= $this->fetch('scripts') ?>
16+
<?= $this->fetch('script') ?>
1717
</html>

src/Template/Requests/view.ctp

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use Cake\Core\Configure;
1212

1313
<ul id="toolbar" class="toolbar">
1414
<?php foreach ($toolbar->panels as $panel): ?>
15-
<li class="panel" data-id="<?= $panel->id ?>" style="display: none;">
15+
<li class="panel hidden" data-id="<?= $panel->id ?>">
1616
<span class="panel-button">
1717
<?= h($panel->title) ?>
1818
</span>
@@ -28,25 +28,4 @@ use Cake\Core\Configure;
2828
['alt' => 'Debug Kit', 'title' => 'CakePHP ' . Configure::version() . ' Debug Kit']) ?>
2929
</li>
3030
</ul>
31-
<?php $this->start('scripts') ?>
32-
<script>
33-
var baseUrl = "<?= Router::url('/', true) ?>";
34-
var toolbar;
35-
36-
$(document).ready(function() {
37-
toolbar = new Toolbar({
38-
button: $('#toolbar'),
39-
content: $('#panel-content-container'),
40-
panelButtons: $('.panel'),
41-
panelClose: $('#panel-close'),
42-
keyboardScope : $(document),
43-
currentRequest: '<?= $toolbar->id ?>',
44-
originalRequest: '<?= $toolbar->id ?>',
45-
baseUrl: <?= json_encode($this->Url->build('/')) ?>
46-
});
47-
48-
toolbar.initialize();
49-
50-
});
51-
</script>
52-
<?php $this->end() ?>
31+
<?php $this->Html->script('DebugKit.debug_kit', ['block' => true, 'id' => '__debug_kit', 'data-id' => $toolbar->id, 'data-url' => json_encode($this->Url->build('/')), 'data-full-url' => Router::url('/', true)]) ?>

tests/TestCase/Routing/Filter/DebugBarFilterTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,8 @@ public function testAfterDispatchSavesData()
146146
$this->assertEquals('Sql Log', $result->panels[7]->title);
147147

148148
$expected = '<html><title>test</title><body><p>some text</p>' .
149-
"<script>var __debug_kit_id = '" . $result->id . "', " .
150-
"__debug_kit_base_url = 'http://localhost/';</script>" .
151-
'<script src="/debug_kit/js/toolbar.js"></script>' .
149+
'<script id="__debug_kit" data-id="' . $result->id . '" ' .
150+
'data-url="http://localhost/" src="/debug_kit/js/toolbar.js"></script>' .
152151
'</body>';
153152
$this->assertTextEquals($expected, $response->body());
154153
}

webroot/css/toolbar.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ p {
6565
overflow: auto;
6666
}
6767

68+
.hidden {
69+
display: none;
70+
}
71+
6872
/* Open close button */
6973
#panel-button,
7074
.panel {

webroot/js/debug_kit.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var baseUrl, toolbar;
2+
3+
var elem = document.getElementById("__debug_kit");
4+
if (elem) {
5+
window.__debug_kit_id = elem.getAttribute("data-id");
6+
window.__debug_kit_base_url = elem.getAttribute("data-url");
7+
baseUrl = elem.getAttribute("data-full-url");
8+
elem = null;
9+
}
10+
11+
$(document).ready(function() {
12+
toolbar = new Toolbar({
13+
button: $('#toolbar'),
14+
content: $('#panel-content-container'),
15+
panelButtons: $('.panel'),
16+
panelClose: $('#panel-close'),
17+
keyboardScope : $(document),
18+
currentRequest: __debug_kit_id,
19+
originalRequest: __debug_kit_id,
20+
baseUrl: __debug_kit_base_url
21+
});
22+
23+
toolbar.initialize();
24+
25+
});

webroot/js/toolbar.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
var __debug_kit_id, __debug_kit_base_url;
2+
var elem = document.getElementById("__debug_kit");
3+
if (elem) {
4+
__debug_kit_id = elem.getAttribute("data-id");
5+
__debug_kit_base_url = elem.getAttribute("data-url");
6+
elem = null;
7+
}
8+
19
(function(win, doc) {
210
var iframe;
311
var bodyOverflow;
@@ -29,7 +37,13 @@
2937
}
3038
var body = doc.body;
3139
iframe = doc.createElement('iframe');
32-
iframe.setAttribute('style', 'position: fixed; bottom: 0; right: 0; border: 0; outline: 0; overflow: hidden; z-index: 99999;');
40+
iframe.style.position = 'fixed';
41+
iframe.style.bottom = 0;
42+
iframe.style.right = 0;
43+
iframe.style.border = 0;
44+
iframe.style.outline = 0;
45+
iframe.style.overflow = 'hidden';
46+
iframe.style.zIndex = 99999;
3347
iframe.height = 40;
3448
iframe.width = 40;
3549
iframe.src = __debug_kit_base_url + 'debug_kit/toolbar/' + __debug_kit_id;

0 commit comments

Comments
 (0)