This repository has been archived by the owner on Dec 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
service.js
157 lines (148 loc) · 5.38 KB
/
service.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
'use strict';
process.setMaxListeners(0);
require('events').EventEmitter.prototype._maxListeners = 100;
const newrelic = process.env.NEW_RELIC_ENABLED === 'true' ? require('newrelic') : undefined;
const senecaNR = require('seneca-newrelic');
var config = require('./config/config.js')();
var seneca = require('seneca')(config);
var _ = require('lodash');
var store = require('seneca-postgresql-store');
var storeQuery = require('seneca-store-query');
var service = 'cp-users-service';
var log = require('cp-logs-lib')({name: service, level: 'warn'});
config.log = log.log;
var util = require('util');
var dgram = require('dgram');
if (process.env.NODE_ENV !== 'production') {
seneca.log.info('using config', JSON.stringify(config, null, 4));
}
seneca.options(config);
seneca.decorate('customValidatorLogFormatter', require('./lib/custom-validator-log-formatter'));
seneca.use(store, config['postgresql-store']);
seneca.use(storeQuery);
if (process.env.MAILDEV_ENABLED === 'true') {
seneca.use('mail', config.maildev);
} else {
seneca.use('mail', config.email);
}
function shutdown (err) {
if (err !== undefined) {
var error = {
date: new Date().toString(),
msg: err.stack !== undefined
? 'FATAL: UncaughtException, please report: ' + util.inspect(err.stack)
: 'FATAL: UncaughtException, no stack trace',
err: util.inspect(err)
};
console.error(JSON.stringify(error));
process.exit(1);
}
process.exit(0);
}
require('./migrate-psql-db.js')(function (err) {
if (err) {
console.error(err);
process.exit(-1);
}
console.log('Migrations ok');
seneca.use(require('./email-notifications.js'));
seneca.use(require('./lib/agreements'));
seneca.use(require('./profiles.js'),
{ postgresql: config['postgresql-store'],
logger: log.logger
});
seneca.use(require('./oauth2.js'), {clients: config.oauth2.clients});
seneca.use('user');
seneca.use('auth');
seneca.use(require('./users.js'),
{ 'email-notifications': config['email-notifications'],
'postgresql': config['postgresql-store'],
'users': config['users'],
'logger': log.logger
});
seneca.use(require('./user-profile.js'),
{ postgresql: config['postgresql-store'],
logger: log.logger
});
seneca.use(require('./nodebb-api.js'), config.nodebb);
seneca.use(require('cp-permissions-plugin'), {
config: __dirname + '/config/permissions'
});
if (!_.isUndefined(newrelic)) {
seneca.use(senecaNR, {
newrelic,
roles: ['cd-users', 'cd-profiles', 'cd-oauth2', 'cd-user-profile'],
filter (p) {
p.user = p.user ? p.user.id : undefined;
p.login = p.login ? p.login.id : undefined;
return p;
}
});
}
process.on('SIGINT', shutdown);
process.on('SIGTERM', shutdown);
process.on('uncaughtException', shutdown);
process.on('SIGUSR2', shutdown);
require('./network.js')(seneca);
seneca.ready(function (err) {
if (err) return shutdown(err);
var message = new Buffer(service);
var client = dgram.createSocket('udp4');
client.send(message, 0, message.length, 11404, 'localhost', function (err, bytes) {
if (err) return shutdown(err);
client.close();
});
var escape = require('seneca-standard-query/lib/relational-util').escapeStr;
['load', 'list'].forEach(function (cmd) {
seneca.wrap('role: entity, cmd: ' + cmd, function filterFields (args, cb) {
try {
['limit$', 'skip$'].forEach(function (field) {
if (args.q[field] && args.q[field] !== 'NULL' && !/^[0-9]+$/g.test(args.q[field] + '')) {
throw new Error('Expect limit$, skip$ to be a number');
}
});
if (args.q.sort$) {
if (args.q.sort$ && typeof args.q.sort$ === 'object') {
var order = args.q.sort$;
_.each(order, function (ascdesc, column) {
if (!/^[a-zA-Z0-9_]+$/g.test(column)) {
throw new Error('Unexpect characters in sort$');
}
});
} else {
throw new Error('Expect sort$ to be an object');
}
}
if (args.q.fields$) {
args.q.fields$.forEach(function (field, index) {
args.q.fields$[index] = '\"' + escape(field) + '\"';
});
}
// Loop over each props
Object.values(args.q).forEach((value, key) => {
if (_.isObject(value)) {
const insecureProp = ['nin$', 'in$'];
const detected = Object.keys(value).filter((val) => insecureProp.indexOf(val) > -1);
if (detected.length > 0) {
// Loop over each detected insecureProp being used (nin or in)
detected.forEach((col, key) => {
const ids = value[col];
// Loop over each value of the array of the dangerous field
ids.forEach((id) => {
if (!/^[a-zA-Z0-9-]+$/g.test(id)) {
throw new Error(`Unexpected characters in ${col}`);
}
});
});
}
}
});
this.prior(args, cb);
} catch (err) {
// cb to avoid seneca-transport to hang while waiting for timeout error
return cb(err);
}
});
});
});
});