Skip to content

Commit a1eacc7

Browse files
authored
Remove unsafe script tag replace (#1716)
1 parent 987c9f2 commit a1eacc7

File tree

6 files changed

+16
-20
lines changed

6 files changed

+16
-20
lines changed

js/src/services/api.service.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class ApiService {
102102
}
103103
if (response && response.atkjs) {
104104
// Call evalResponse with proper context, js code and jQuery as $ var.
105-
atk.apiService.evalResponse.call(this, response.atkjs.replace(/<\/?script>/g, ''), jQuery);
105+
atk.apiService.evalResponse.call(this, response.atkjs, jQuery);
106106
}
107107
if (atk.apiService.afterSuccessCallbacks.length > 0) {
108108
const self = this;

public/atkjs-ui.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/App.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ public function run()
576576

577577
$this->html->template->set('title', $this->title);
578578
$this->html->renderAll();
579-
$this->html->template->dangerouslyAppendHtml('HEAD', $this->html->getJs());
579+
$this->html->template->dangerouslyAppendHtml('HEAD', $this->getTag('script', null, $this->html->getJs()));
580580
$this->is_rendering = false;
581581

582582
if (isset($_GET[Callback::URL_QUERY_TARGET]) && $this->catch_runaway_callbacks) {

src/View.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,9 @@ public function render(bool $forceReturn = true): string
743743
{
744744
$this->renderAll();
745745

746-
return $this->getJs($forceReturn)
746+
$js = $this->getJs($forceReturn);
747+
748+
return ($js !== '' ? $this->getApp()->getTag('script', null, $js) : '')
747749
. $this->renderTemplateToHtml();
748750
}
749751

@@ -1199,7 +1201,7 @@ public function getJs(bool $forceReturn = false)
11991201
}
12001202
}
12011203

1202-
if (!$actions) {
1204+
if (count($actions) === 0) {
12031205
return '';
12041206
}
12051207

@@ -1218,7 +1220,7 @@ public function getJs(bool $forceReturn = false)
12181220

12191221
$ready = new JsFunction($actions);
12201222

1221-
return '<script>' . "\n" . (new Jquery($ready))->jsRender() . "\n" . '</script>';
1223+
return (new Jquery($ready))->jsRender();
12221224
}
12231225

12241226
// }}}

src/VirtualPage.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function getHtml()
110110
if ($mode === 'popup') {
111111
$this->getApp()->html->template->set('title', $this->getApp()->title);
112112
$this->getApp()->html->template->dangerouslySetHtml('Content', parent::getHtml());
113-
$this->getApp()->html->template->dangerouslyAppendHtml('HEAD', $this->getJs());
113+
$this->getApp()->html->template->dangerouslyAppendHtml('HEAD', $this->getApp()->getTag('script', null, $this->getJs()));
114114

115115
$this->getApp()->terminateHtml($this->getApp()->html->template);
116116
}
@@ -142,7 +142,7 @@ public function getHtml()
142142

143143
$this->getApp()->html->template->dangerouslySetHtml('Content', $this->getApp()->layout->template->renderToHtml());
144144

145-
$this->getApp()->html->template->dangerouslyAppendHtml('HEAD', $this->getApp()->layout->getJs());
145+
$this->getApp()->html->template->dangerouslyAppendHtml('HEAD', $this->getApp()->getTag('script', null, $this->getApp()->layout->getJs()));
146146

147147
$this->getApp()->terminateHtml($this->getApp()->html->template);
148148
}

tests/JsIntegrationTest.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,9 @@ public function testBasicChain2(): void
5050
$j = $v->js(true)->hide();
5151
$v->getHtml();
5252

53-
$this->assertSame('<script>
54-
$(function() {
53+
$this->assertSame('$(function() {
5554
$("#b").hide();
56-
})
57-
</script>', $v->getJs());
55+
})', $v->getJs());
5856
}
5957

6058
/**
@@ -66,13 +64,11 @@ public function testBasicChain3(): void
6664
$v->js('click')->hide();
6765
$v->getHtml();
6866

69-
$this->assertSame('<script>
70-
$(function() {
67+
$this->assertSame('$(function() {
7168
$("#b").bind("click",function() {
7269
$("#b").hide();
7370
});
74-
})
75-
</script>', $v->getJs());
71+
})', $v->getJs());
7672
}
7773

7874
/**
@@ -87,14 +83,12 @@ public function testBasicChain4(): void
8783
$b1->on('click', $b2->js()->hide());
8884
$bb->getHtml();
8985

90-
$this->assertSame('<script>
91-
$(function() {
86+
$this->assertSame('$(function() {
9287
$("#b1").on("click",function(event) {
9388
event.preventDefault();
9489
event.stopPropagation();
9590
$("#b2").hide();
9691
});
97-
})
98-
</script>', $bb->getJs());
92+
})', $bb->getJs());
9993
}
10094
}

0 commit comments

Comments
 (0)