Skip to content

Commit 9b6d08f

Browse files
committed
security: Restrict targets and handles to be valid strings
1 parent 3b5dc1c commit 9b6d08f

1 file changed

Lines changed: 27 additions & 4 deletions

File tree

Util/Controller/RequestDataLoader.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ public function load(): array
2626
throw new RuntimeException('Not a valid request');
2727
}
2828

29-
$this->sanityCheck($data);
30-
31-
return $data;
29+
return $this->sanitize($data);
3230
}
3331

3432
public function mergeRequestParams(): void
@@ -54,7 +52,7 @@ public function mergeRequestParams(): void
5452
$this->request->setParams($params);
5553
}
5654

57-
private function sanityCheck(array $requestData = []): void
55+
private function sanitize(array $requestData = []): array
5856
{
5957
if (false === $this->ajax->isAjax()) {
6058
throw new RuntimeException('Not an Alpine request');
@@ -66,5 +64,30 @@ private function sanityCheck(array $requestData = []): void
6664
throw new RuntimeException('No '.$requiredField.' in request');
6765
}
6866
}
67+
68+
if (isset($requestData['targets'])) {
69+
$requestData['targets'] = $this->sanitizeTargets($requestData['targets']);
70+
}
71+
72+
if (isset($requestData['handles'])) {
73+
$requestData['handles'] = $this->sanitizeHandlers($requestData['handles']);
74+
}
75+
76+
77+
return $requestData;
78+
}
79+
80+
private function sanitizeTargets(array $targets): array
81+
{
82+
return array_map(function ($target) {
83+
return preg_replace('/([^a-zA-Z0-9\-_.]+)/', '', (string)$target);
84+
}, $targets);
85+
}
86+
87+
private function sanitizeHandlers(array $handlers): array
88+
{
89+
return array_map(function ($handler) {
90+
return preg_replace('/([^a-zA-Z0-9\-_]+)/', '', (string)$handler);
91+
}, $handlers);
6992
}
7093
}

0 commit comments

Comments
 (0)