Skip to content

Commit 101eac3

Browse files
committed
Fix: move request asserting to from core to v1
1 parent 29160aa commit 101eac3

File tree

2 files changed

+30
-31
lines changed

2 files changed

+30
-31
lines changed

library/Notifications/Api/ApiCore.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,6 @@ public function handle(ServerRequestInterface $request): ResponseInterface
6767
->setHeader('Allow', $this->getAllowedMethods());
6868
}
6969
$request = $request->withAttribute('httpMethod', $httpMethod);
70-
$identifier = $request->getAttribute('identifier');
71-
$filterStr = $request->getUri()->getQuery();
72-
7370

7471
if (! method_exists($this, $httpMethod->lowercase())) {
7572
throw (new HttpException(
@@ -79,33 +76,6 @@ public function handle(ServerRequestInterface $request): ResponseInterface
7976
->setHeader('Allow', $this->getAllowedMethods());
8077
}
8178

82-
if ($httpMethod !== HttpMethod::GET && ! empty($filterStr)) {
83-
throw new HttpBadRequestException(
84-
'Unexpected query parameter: Filter is only allowed for GET requests'
85-
);
86-
}
87-
if ($httpMethod === HttpMethod::GET && ! empty($identifier) && ! empty($filterStr)) {
88-
throw new HttpBadRequestException(
89-
'Invalid request: ' . $httpMethod->uppercase() . ' with identifier and query parameters,'
90-
. " it's not allowed to use both together."
91-
);
92-
}
93-
if (
94-
in_array($httpMethod, [HttpMethod::PUT, HttpMethod::POST])
95-
&& $request->getHeaderLine('Content-Type') !== 'application/json'
96-
) {
97-
throw new HttpBadRequestException('Invalid request header: Content-Type must be application/json');
98-
}
99-
if (
100-
! in_array($httpMethod, [HttpMethod::PUT, HttpMethod::POST])
101-
&& (! empty($request->getBody()->getSize()) || ! empty($request->getParsedBody()))
102-
) {
103-
throw new HttpBadRequestException('Invalid request: Body is only allowed for POST and PUT requests');
104-
}
105-
if (in_array($httpMethod, [HttpMethod::PUT, HttpMethod::DELETE]) && empty($identifier)) {
106-
throw new HttpBadRequestException("Invalid request: Identifier is required");
107-
}
108-
10979
$this->assertValidRequest($request);
11080

11181
return $this->handleRequest($request);

library/Notifications/Api/V1/ApiV1.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,32 @@ public function handleRequest(ServerRequestInterface $request): ResponseInterfac
109109

110110
protected function assertValidRequest(ServerRequestInterface $request): void
111111
{
112-
if (! empty($identifier = $request->getAttribute('identifier')) && ! Uuid::isValid($identifier)) {
112+
$httpMethod = $request->getAttribute('httpMethod');
113+
$identifier = $request->getAttribute('identifier');
114+
$filterStr = $request->getUri()->getQuery();
115+
116+
if ($httpMethod !== HttpMethod::GET && ! empty($filterStr)) {
117+
throw new HttpBadRequestException(
118+
'Unexpected query parameter: Filter is only allowed for GET requests'
119+
);
120+
}
121+
if ($httpMethod === HttpMethod::GET && ! empty($identifier) && ! empty($filterStr)) {
122+
throw new HttpBadRequestException(
123+
'Invalid request: ' . $httpMethod->uppercase() . ' with identifier and query parameters,'
124+
. " it's not allowed to use both together."
125+
);
126+
}
127+
if (
128+
! in_array($httpMethod, [HttpMethod::PUT, HttpMethod::POST])
129+
&& (! empty($request->getBody()->getSize()) || ! empty($request->getParsedBody()))
130+
) {
131+
throw new HttpBadRequestException('Invalid request: Body is only allowed for POST and PUT requests');
132+
}
133+
if (in_array($httpMethod, [HttpMethod::PUT, HttpMethod::DELETE]) && empty($identifier)) {
134+
throw new HttpBadRequestException("Invalid request: Identifier is required");
135+
}
136+
137+
if (! empty($identifier) && ! Uuid::isValid($identifier)) {
113138
throw new HttpBadRequestException('The given identifier is not a valid UUID');
114139
}
115140
}
@@ -173,6 +198,10 @@ function (Condition $condition) use ($allowedColumns, $idColumnName) {
173198
*/
174199
private function getValidRequestBody(ServerRequestInterface $request): array
175200
{
201+
if ($request->getHeaderLine('Content-Type') !== 'application/json') {
202+
throw new HttpBadRequestException('Invalid request header: Content-Type must be application/json');
203+
}
204+
176205
if (! empty($parsedBody = $request->getParsedBody()) && is_array($parsedBody)) {
177206
return $parsedBody;
178207
}

0 commit comments

Comments
 (0)