-
Notifications
You must be signed in to change notification settings - Fork 383
Expand file tree
/
Copy pathStrictModelMiddlewareFactory.ts
More file actions
73 lines (64 loc) · 2.29 KB
/
Copy pathStrictModelMiddlewareFactory.ts
File metadata and controls
73 lines (64 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { NextFunction, Request, RequestHandler, Response } from "express";
import ILogger from "../../common/ILogger";
import BlobStorageContext from "../context/BlobStorageContext";
import StrictModelNotSupportedError from "../errors/StrictModelNotSupportedError";
import Context from "../generated/Context";
import ExpressRequestAdapter from "../generated/ExpressRequestAdapter";
import IRequest from "../generated/IRequest";
import { DEFAULT_CONTEXT_PATH, HeaderConstants } from "../utils/constants";
export type StrictModelRequestValidator = (
req: IRequest,
context: Context,
logger: ILogger
) => Promise<void>;
export const UnsupportedHeadersBlocker: StrictModelRequestValidator = async (
req: IRequest,
context: Context,
logger: ILogger
): Promise<void> => {
const UnsupportedHeaderKeys = [
HeaderConstants.X_MS_RANGE_GET_CONTENT_CRC64,
HeaderConstants.X_MS_ENCRYPTION_KEY,
HeaderConstants.X_MS_ENCRYPTION_KEY_SHA256,
HeaderConstants.X_MS_ENCRYPTION_ALGORITHM
];
for (const headerKey of UnsupportedHeaderKeys) {
const value = req.getHeader(headerKey);
if (typeof value === "string") {
throw new StrictModelNotSupportedError(headerKey, context.contextId);
}
}
};
export const UnsupportedParametersBlocker: StrictModelRequestValidator = async (
req: IRequest,
context: Context,
logger: ILogger
): Promise<void> => {
const UnsupportedParameterKeys: string[] = [
];
for (const parameterKey of UnsupportedParameterKeys) {
const value = req.getQuery(parameterKey);
if (typeof value === "string") {
throw new StrictModelNotSupportedError(parameterKey, context.contextId);
}
}
};
export default class StrictModelMiddlewareFactory {
constructor(
private readonly logger: ILogger,
private readonly validators: StrictModelRequestValidator[]
) {}
public createStrictModelMiddleware(): RequestHandler {
return (req: Request, res: Response, next: NextFunction) => {
this.validate(req, res)
.then(next)
.catch(next);
};
}
private async validate(req: Request, res: Response): Promise<void> {
const context = new BlobStorageContext(res.locals, DEFAULT_CONTEXT_PATH);
for (const validator of this.validators) {
await validator(new ExpressRequestAdapter(req), context, this.logger);
}
}
}