|
|
@@ -2,14 +2,12 @@ import type { Request } from 'express';
|
|
|
|
|
|
import type { AccessTokenParserReq } from '~/server/middlewares/access-token-parser/interfaces';
|
|
|
|
|
|
-// 1. Check if the request method is allowed
|
|
|
const allowedMethods = ['GET', 'HEAD', 'POST'] as const;
|
|
|
type AllowedMethod = typeof allowedMethods[number];
|
|
|
function isAllowedMethod(method: string): method is AllowedMethod {
|
|
|
return allowedMethods.includes(method as AllowedMethod);
|
|
|
}
|
|
|
|
|
|
-// 2. Check if the request headers are safe
|
|
|
const safeRequestHeaders = [
|
|
|
'accept',
|
|
|
'accept-language',
|
|
|
@@ -25,7 +23,10 @@ const safeRequestHeaders = [
|
|
|
] as const;
|
|
|
type SafeRequestHeader = typeof safeRequestHeaders[number];
|
|
|
|
|
|
-// 3. Content-Type is
|
|
|
+function isSafeRequestHeader(header: string): header is SafeRequestHeader {
|
|
|
+ return safeRequestHeaders.includes(header.toLowerCase() as SafeRequestHeader);
|
|
|
+}
|
|
|
+
|
|
|
const allowedContentTypes = [
|
|
|
'application/x-www-form-urlencoded',
|
|
|
'multipart/form-data',
|
|
|
@@ -33,22 +34,27 @@ const allowedContentTypes = [
|
|
|
] as const;
|
|
|
type AllowedContentType = typeof allowedContentTypes[number];
|
|
|
|
|
|
+function isAllowedContentType(contentType: string): contentType is AllowedContentType {
|
|
|
+ return allowedContentTypes.some(allowed => contentType.toLowerCase().startsWith(allowed));
|
|
|
+}
|
|
|
+
|
|
|
const isSimpleRequest = (req: Request | AccessTokenParserReq): boolean => {
|
|
|
+ // 1. Check if the request method is allowed
|
|
|
if (!isAllowedMethod(req.method)) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
+ // 2. Check if the request headers are safe
|
|
|
const nonSafeHeaders = Object.keys(req.headers).filter((header) => {
|
|
|
- const headerLower = header.toLowerCase();
|
|
|
- return !safeRequestHeaders.includes(headerLower as SafeRequestHeader);
|
|
|
+ return !isSafeRequestHeader(header);
|
|
|
});
|
|
|
-
|
|
|
if (nonSafeHeaders.length > 0) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
+ // 3. Content-Type is
|
|
|
const contentType = req.headers['content-type'];
|
|
|
- if (contentType != null && !allowedContentTypes.includes(contentType.toLowerCase() as AllowedContentType)) {
|
|
|
+ if (contentType != null && !isAllowedContentType(contentType)) {
|
|
|
return false;
|
|
|
}
|
|
|
|