Skip to content

Commit b90296f

Browse files
authored
Merge pull request #615 from aleixgil/bugfix/injectionJSInTickets
Escaping script tag on create and comment ticket.
2 parents 24597e0 + b811c7c commit b90296f

File tree

3 files changed

+67
-3
lines changed

3 files changed

+67
-3
lines changed

app/Http/Controllers/Api/CommentsController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class CommentsController extends ApiController
99
{
1010
public function store(Ticket $ticket)
1111
{
12-
$comment = $ticket->addComment(null, request('body'), request('new_status'));
12+
$comment = $ticket->addComment(null, strip_tags(request('body')), request('new_status'));
1313
if (! $comment) {
1414
return $this->respond(['id' => null, 'message' => 'Can not create a comment with empty body'], Response::HTTP_OK);
1515
}

app/Http/Controllers/Api/TicketsController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ public function store()
3838

3939
$ticket = Ticket::createAndNotify(
4040
request('requester'),
41-
request('title'),
42-
request('body'),
41+
strip_tags(request('title')),
42+
strip_tags(request('body')),
4343
request('tags')
4444
);
4545

tests/Feature/Api/SimpleTicketTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,51 @@ function ($notification, $channels) use ($ticket) {
8181
);
8282
}
8383

84+
/** @test */
85+
public function can_create_a_ticket_with_js_injection(){
86+
Notification::fake();
87+
$admin = factory(Admin::class)->create();
88+
$nonAdmin = factory(User::class)->create(["admin" => 0]);
89+
90+
$response = $this->post('api/tickets',[
91+
"requester" => [
92+
"name" => "johndoe",
93+
"email" => "[email protected]"
94+
],
95+
"title" => "App <script>is not working</script> >>>",
96+
"body" => "I can't log in into the application<script>alert(1)</script>",
97+
"tags" => ["xef"]
98+
],["token" => 'the-api-token']);
99+
100+
$response->assertStatus( Response::HTTP_CREATED );
101+
$response->assertJson(["data" => ["id" => 1]]);
102+
103+
tap( Ticket::first(), function($ticket) use($admin) {
104+
tap( Requester::first(), function($requester) use ($ticket){
105+
$this->assertEquals($requester->name, "johndoe");
106+
$this->assertEquals($requester->email, "[email protected]");
107+
$this->assertEquals( $ticket->requester_id, $requester->id);
108+
});
109+
$this->assertEquals ( $ticket->title, "App is not working >>>");
110+
$this->assertEquals ( $ticket->body, "I can't log in into the applicationalert(1)");
111+
$this->assertTrue ( $ticket->tags->pluck('name')->contains("xef") );
112+
$this->assertEquals( Ticket::STATUS_NEW, $ticket->status);
113+
114+
Notification::assertSentTo(
115+
[$admin],
116+
TicketCreated::class,
117+
function ($notification, $channels) use ($ticket) {
118+
return $notification->ticket->id === $ticket->id;
119+
}
120+
);
121+
});
122+
123+
124+
Notification::assertNotSentTo(
125+
[$nonAdmin], TicketCreated::class
126+
);
127+
}
128+
84129
/** @test */
85130
public function requester_is_required(){
86131
$response = $this->post('api/tickets',$this->validParams([
@@ -163,6 +208,25 @@ public function requester_can_comment_the_ticket(){
163208
//TODO: assert notifications
164209
}
165210

211+
/** @test */
212+
public function requester_can_comment_the_ticket_with_js_injection(){
213+
Notification::fake();
214+
$ticket = factory(Ticket::class)->create();
215+
$ticket->comments()->create(["body" => "first comment", "new_status" => 1]);
216+
217+
$response = $this->post("api/tickets/{$ticket->id}/comments", [
218+
"body" => "<script> this is a comment </script>"
219+
],["token" => 'the-api-token']);
220+
221+
$response->assertStatus ( Response::HTTP_CREATED );
222+
$response->assertJson (["data" => ["id" => 2]]);
223+
224+
$this->assertCount (2, $ticket->comments);
225+
$this->assertEquals ($ticket->comments[1]->body, " this is a comment ");
226+
227+
//TODO: assert notifications
228+
}
229+
166230
/** @test */
167231
public function commenting_a_closed_ticket_reopens_it(){
168232
Notification::fake();

0 commit comments

Comments
 (0)