-
Notifications
You must be signed in to change notification settings - Fork 0
/
access-controller.js
335 lines (308 loc) · 11.3 KB
/
access-controller.js
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
const {constructTransformedRethinkQuery} = require('./query-transformer');
const {parseChannelResourceQuery} = require('./channel-resource-parser');
const AsyncStreamEmitter = require('async-stream-emitter');
const {validateQuery} = require('./validate');
let AccessController = function (agServer, options) {
AsyncStreamEmitter.call(this);
this.options = options || {};
this.schema = this.options.schema || {};
this.maxPageSize = this.options.maxPageSize || null;
this.rethink = this.options.rethink;
this.cache = this.options.cache;
this.agServer = agServer;
this._getModelAccessFilter = (modelType, accessPhase) => {
if (modelType == null || typeof modelType !== 'string') {
return null;
}
let modelSchema = this.schema[modelType];
if (!modelSchema) {
return null;
}
let modelAccessFilters = modelSchema.access;
if (!modelAccessFilters) {
return null;
}
return modelAccessFilters[accessPhase] || null;
};
this._getComputedModelSchema = (type) => {
return {
maxPageSize: this.maxPageSize,
...this.schema[type]
};
};
let middleware = this.options.middleware || {};
agServer.setMiddleware(agServer.MIDDLEWARE_INBOUND, async (middlewareStream) => {
for await (let action of middlewareStream) {
let middlewareFunction = middleware[action.type];
if (middlewareFunction) {
let {type, allow, block, ...simpleAction} = action;
try {
await middlewareFunction(simpleAction);
} catch (error) {
action.block(error);
continue;
}
}
if (action.type === action.INVOKE) {
if (action.procedure === 'crud') {
let query = action.data;
try {
validateQuery(query, this.schema);
} catch (validationError) {
let error = new Error(
`Query failed validation because of error: ${validationError.message}`
);
error.name = 'CRUDBlockedError';
error.type = 'pre';
action.block(error);
continue;
}
if (query.action === 'read' && query.view && typeof query.pageSize === 'number') {
let {maxPageSize} = this._getComputedModelSchema(query.type);
if (maxPageSize != null && query.pageSize > maxPageSize) {
let error = new Error(
`You are not permitted to access the ${query.view} view of the ${query.type} model - Query pageSize exceeded the maxPageSize of ${maxPageSize}`
);
error.name = 'CRUDBlockedError';
error.type = 'pre';
action.block(error);
continue;
}
}
// If socket has a valid auth token, then allow emitting get or set events
let authToken = action.socket.authToken;
let preAccessFilter = this._getModelAccessFilter(query.type, 'pre');
if (preAccessFilter) {
let crudRequest = {
r: this.rethink,
socket: action.socket,
action: query.action,
authToken,
query
};
try {
await preAccessFilter(crudRequest);
} catch (error) {
if (typeof error === 'boolean') {
error = new Error(
`You are not permitted to perform a CRUD operation on the ${query.type} resource with ID ${query.id}`
);
error.name = 'CRUDBlockedError';
error.type = 'pre';
}
action.block(error);
continue;
}
action.allow();
continue;
}
if (this.options.blockPreByDefault) {
let crudBlockedError = new Error(
`You are not permitted to perform a CRUD operation on the ${query.type} resource with ID ${query.id} - No access filters found`
);
crudBlockedError.name = 'CRUDBlockedError';
crudBlockedError.type = 'pre';
action.block(crudBlockedError);
continue;
}
action.allow();
continue;
}
action.allow();
continue;
}
if (action.type === action.PUBLISH_IN) {
let channelResourceQuery = parseChannelResourceQuery(action.channel);
if (channelResourceQuery) {
// Always block CRUD publish from outside clients.
let crudPublishNotAllowedError = new Error('Cannot publish to a CRUD resource channel');
crudPublishNotAllowedError.name = 'CRUDPublishNotAllowedError';
action.block(crudPublishNotAllowedError);
continue;
}
action.allow();
continue;
}
if (action.type === action.SUBSCRIBE) {
let authToken = action.socket.authToken;
let channelResourceQuery = parseChannelResourceQuery(action.channel);
if (!channelResourceQuery) {
action.allow();
continue;
}
channelResourceQuery.action = action.SUBSCRIBE;
// Sometimes the real viewParams may be different from what can be parsed from
// the channel name; this is because some view params don't affect the real-time
// delivery of messages but they may still be useful in constructing the view.
if (channelResourceQuery.view != null && action.data && action.data.viewParams && typeof action.data.viewParams === 'object') {
channelResourceQuery.viewParams = action.data.viewParams;
}
try {
validateQuery(channelResourceQuery, this.schema);
} catch (validationError) {
let error = new Error(
`Subscribe query failed validation because of error: ${validationError.message}`
);
error.name = 'CRUDBlockedError';
error.type = 'pre';
action.block(error);
continue;
}
let continueWithPostAccessFilter = async () => {
let subscribePostRequest = {
socket: action.socket,
action: 'subscribe',
query: channelResourceQuery,
fetchResource: true
};
let result;
try {
result = await this.applyPostAccessFilter(subscribePostRequest);
} catch (error) {
action.block(error);
return;
}
action.allow(result);
};
let preAccessFilter = this._getModelAccessFilter(channelResourceQuery.type, 'pre');
if (preAccessFilter) {
let subscribePreRequest = {
r: this.rethink,
socket: action.socket,
action: 'subscribe',
authToken,
query: channelResourceQuery
};
try {
await preAccessFilter(subscribePreRequest);
} catch (error) {
if (typeof error === 'boolean') {
error = new Error(`Cannot subscribe to the ${action.channel} channel`);
error.name = 'CRUDBlockedError';
error.type = 'pre';
}
action.block(error);
continue;
}
await continueWithPostAccessFilter();
continue;
}
if (this.options.blockPreByDefault) {
let crudBlockedError = new Error(
`Cannot subscribe to the ${action.channel} channel - No access filters found`
);
crudBlockedError.name = 'CRUDBlockedError';
crudBlockedError.type = 'pre';
action.block(crudBlockedError);
continue;
}
await continueWithPostAccessFilter();
continue;
}
action.allow();
continue;
}
});
agServer.setMiddleware(agServer.MIDDLEWARE_OUTBOUND, async (middlewareStream) => {
for await (let action of middlewareStream) {
let middlewareFunction = middleware[action.type];
if (middlewareFunction) {
let {type, allow, block, ...simpleAction} = action;
try {
await middlewareFunction(simpleAction);
} catch (error) {
action.block(error);
continue;
}
}
if (action.type === action.PUBLISH_OUT) {
let actionData = action.data;
if (actionData && typeof actionData === 'object') {
let {publisherSocketId, publisherId, ...payload} = actionData;
if (publisherSocketId === action.socket.id) {
if (publisherId) {
payload.publisherId = publisherId;
action.allow({data: payload});
continue;
}
// Block silently.
action.block(false);
continue;
}
action.allow({data: payload});
continue;
}
}
action.allow();
}
});
};
AccessController.prototype = Object.create(AsyncStreamEmitter.prototype);
AccessController.prototype.applyPostAccessFilter = async function (req) {
let {query} = req;
let postAccessFilter = this._getModelAccessFilter(query.type, 'post');
if (postAccessFilter) {
let request = {
r: this.rethink,
socket: req.socket,
action: req.action,
authToken: req.socket && req.socket.authToken,
query
};
if (req.fetchResource) {
let pageSize = query.pageSize ?? this.options.defaultPageSize;
if (!this.schema[query.type]) {
let error = new Error(`The ${query.type} model type is not supported - It is not part of the schema`);
error.name = 'CRUDInvalidModelType';
this.emit('error', {error});
throw error;
}
if (query.id) {
try {
request.resource = await this.cache.pass(query, async () => {
return await this.rethink.table(query.type).get(query.id).run();
});
} catch (error) {
this.emit('error', {error});
throw new Error('Failed to preload resource due to an unexpected error');
}
} else {
// For collections.
try {
let rethinkQuery = constructTransformedRethinkQuery(this.options, this.rethink.table(query.type), query.type, query.view, query.viewParams);
if (query.offset) {
rethinkQuery = rethinkQuery.slice(query.offset, query.offset + pageSize).pluck('id');
} else {
rethinkQuery = rethinkQuery.limit(pageSize).pluck('id');
}
request.resource = await rethinkQuery.run();
} catch (error) {
this.emit('error', {error});
throw new Error('Executed an invalid query transformation');
}
}
} else {
request.resource = req.resource;
}
try {
await postAccessFilter(request);
} catch (error) {
if (typeof error === 'boolean') {
error = new Error(`You are not permitted to perform a CRUD operation on the ${query.type} resource with ID ${query.id}`);
error.name = 'CRUDBlockedError';
error.type = 'post';
}
this.emit('error', {error});
throw error;
}
} else {
if (this.options.blockPostByDefault) {
let crudBlockedError = new Error(`You are not permitted to perform a CRUD operation on the ${query.type} resource with ID ${query.id} - No access filters found`);
crudBlockedError.name = 'CRUDBlockedError';
crudBlockedError.type = 'post';
this.emit('error', {error: crudBlockedError});
throw crudBlockedError;
}
}
};
module.exports = AccessController;