Skip to content

Commit c9a8e0e

Browse files
fix(Drivers): check auhorization before accepting files
1 parent faf0f15 commit c9a8e0e

File tree

2 files changed

+31
-28
lines changed

2 files changed

+31
-28
lines changed

src/driver/express/ExpressDriver.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,33 +87,21 @@ export class ExpressDriver extends BaseDriver implements Driver {
8787

8888
// middlewares required for this action
8989
const defaultMiddlewares: any[] = [];
90+
9091
if (actionMetadata.isBodyUsed) {
9192
if (actionMetadata.isJsonTyped) {
9293
defaultMiddlewares.push(this.loadBodyParser().json(actionMetadata.bodyExtraOptions));
9394
} else {
9495
defaultMiddlewares.push(this.loadBodyParser().text(actionMetadata.bodyExtraOptions));
9596
}
9697
}
97-
if (actionMetadata.isFileUsed || actionMetadata.isFilesUsed) {
98-
const multer = this.loadMulter();
99-
actionMetadata.params
100-
.filter(param => param.type === "file")
101-
.forEach(param => {
102-
defaultMiddlewares.push(multer(param.extraOptions).single(param.name));
103-
});
104-
actionMetadata.params
105-
.filter(param => param.type === "files")
106-
.forEach(param => {
107-
defaultMiddlewares.push(multer(param.extraOptions).array(param.name));
108-
});
109-
}
11098

11199
if (actionMetadata.isAuthorizedUsed) {
112100
defaultMiddlewares.push((request: any, response: any, next: Function) => {
113101
if (!this.authorizationChecker)
114102
throw new AuthorizationCheckerNotDefinedError();
115103

116-
const action: Action = {request, response, next};
104+
const action: Action = { request, response, next };
117105
const checkResult = this.authorizationChecker(action, actionMetadata.authorizedRoles);
118106

119107
const handleError = (result: any) => {
@@ -135,6 +123,20 @@ export class ExpressDriver extends BaseDriver implements Driver {
135123
});
136124
}
137125

126+
if (actionMetadata.isFileUsed || actionMetadata.isFilesUsed) {
127+
const multer = this.loadMulter();
128+
actionMetadata.params
129+
.filter(param => param.type === "file")
130+
.forEach(param => {
131+
defaultMiddlewares.push(multer(param.extraOptions).single(param.name));
132+
});
133+
actionMetadata.params
134+
.filter(param => param.type === "files")
135+
.forEach(param => {
136+
defaultMiddlewares.push(multer(param.extraOptions).array(param.name));
137+
});
138+
}
139+
138140
// user used middlewares
139141
const uses = [...actionMetadata.controllerMetadata.uses, ...actionMetadata.uses];
140142
const beforeMiddlewares = this.prepareMiddlewares(uses.filter(use => !use.afterAction));

src/driver/koa/KoaDriver.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,26 +69,13 @@ export class KoaDriver extends BaseDriver implements Driver {
6969

7070
// middlewares required for this action
7171
const defaultMiddlewares: any[] = [];
72-
if (actionMetadata.isFileUsed || actionMetadata.isFilesUsed) {
73-
const multer = this.loadMulter();
74-
actionMetadata.params
75-
.filter(param => param.type === "file")
76-
.forEach(param => {
77-
defaultMiddlewares.push(multer(param.extraOptions).single(param.name));
78-
});
79-
actionMetadata.params
80-
.filter(param => param.type === "files")
81-
.forEach(param => {
82-
defaultMiddlewares.push(multer(param.extraOptions).array(param.name));
83-
});
84-
}
8572

8673
if (actionMetadata.isAuthorizedUsed) {
8774
defaultMiddlewares.push((context: any, next: Function) => {
8875
if (!this.authorizationChecker)
8976
throw new AuthorizationCheckerNotDefinedError();
9077

91-
const action: Action = {request: context.request, response: context.response, context, next};
78+
const action: Action = { request: context.request, response: context.response, context, next };
9279
const checkResult = actionMetadata.authorizedRoles instanceof Function ?
9380
getFromContainer<RoleChecker>(actionMetadata.authorizedRoles).check(action) :
9481
this.authorizationChecker(action, actionMetadata.authorizedRoles);
@@ -110,6 +97,20 @@ export class KoaDriver extends BaseDriver implements Driver {
11097
});
11198
}
11299

100+
if (actionMetadata.isFileUsed || actionMetadata.isFilesUsed) {
101+
const multer = this.loadMulter();
102+
actionMetadata.params
103+
.filter(param => param.type === "file")
104+
.forEach(param => {
105+
defaultMiddlewares.push(multer(param.extraOptions).single(param.name));
106+
});
107+
actionMetadata.params
108+
.filter(param => param.type === "files")
109+
.forEach(param => {
110+
defaultMiddlewares.push(multer(param.extraOptions).array(param.name));
111+
});
112+
}
113+
113114
// user used middlewares
114115
const uses = actionMetadata.controllerMetadata.uses.concat(actionMetadata.uses);
115116
const beforeMiddlewares = this.prepareMiddlewares(uses.filter(use => !use.afterAction));

0 commit comments

Comments
 (0)