@tiledesk/tiledesk-server 3.3.5 → 3.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +20 -0
- package/app.js +7 -0
- package/deploynew.sh +7 -1
- package/docs/activities-client.md +27 -0
- package/docs/mcp-oauth2-it.md +279 -0
- package/docs/mcp-oauth2.md +279 -0
- package/docs/numeric-input-validation.md +91 -0
- package/docs/superpowers/plans/2026-11-10-mcp-oauth2-server-it.md +155 -0
- package/docs/superpowers/plans/2026-11-10-mcp-oauth2-server.md +155 -0
- package/docs/superpowers/plans/2026-11-10-mcp-oauth2-tybot-it.md +63 -0
- package/docs/superpowers/plans/2026-11-10-mcp-oauth2-tybot.md +63 -0
- package/docs/superpowers/specs/2026-11-10-mcp-oauth2-design-it.md +207 -0
- package/docs/superpowers/specs/2026-11-10-mcp-oauth2-design.md +207 -0
- package/jobs.js +15 -0
- package/models/mcpOAuthConnection.js +131 -0
- package/models/request.js +8 -0
- package/package.json +1 -1
- package/pubmodules/activities/activityMessageUtil.js +137 -0
- package/pubmodules/activities/models/activity.js +2 -0
- package/pubmodules/activities/routes/activity.js +39 -11
- package/pubmodules/activities/test/execution-filter.test.js +133 -0
- package/pubmodules/activities/test/execution-messages.test.js +158 -0
- package/pubmodules/canned/cannedResponseRoute.js +6 -9
- package/pubmodules/events/eventRoute.js +6 -9
- package/pubmodules/executions-publisher/index.js +457 -0
- package/pubmodules/executions-publisher/test/audit-consumer.test.js +384 -0
- package/pubmodules/executions-publisher/test/executions-publisher.test.js +204 -0
- package/pubmodules/pubModulesManager.js +12 -0
- package/routes/answered.js +7 -3
- package/routes/campaigns.js +7 -3
- package/routes/department.js +14 -3
- package/routes/executions-refire.js +148 -0
- package/routes/faq.js +6 -14
- package/routes/filesp.js +9 -4
- package/routes/kb.js +40 -15
- package/routes/lead.js +23 -28
- package/routes/logs.js +8 -1
- package/routes/mcp.js +11 -8
- package/routes/mcpOAuth.js +133 -0
- package/routes/mcpOAuthCallback.js +75 -0
- package/routes/message.js +3 -1
- package/routes/messagesRoot.js +5 -18
- package/routes/project.js +57 -12
- package/routes/project_user.js +21 -4
- package/routes/property.js +18 -20
- package/routes/request.js +36 -45
- package/routes/roles.js +8 -18
- package/routes/segment.js +8 -18
- package/routes/subscription.js +4 -5
- package/routes/tag.js +6 -9
- package/routes/unanswered.js +7 -3
- package/routes/user-request.js +29 -34
- package/routes/webhook.js +120 -54
- package/services/mcpOAuthDiscovery.js +248 -0
- package/services/mcpOAuthGoogleScopes.js +55 -0
- package/services/mcpOAuthService.js +732 -0
- package/services/mcpService.js +111 -15
- package/services/requestService.js +4 -2
- package/services/webhookService.js +4 -2
- package/test/executions-refire.test.js +362 -0
- package/test/kbRoute.js +4 -7
- package/test/mcpOAuthDiscovery.test.js +331 -0
- package/test/mcpOAuthGoogleScopes.test.js +25 -0
- package/test/mcpOAuthRoute.test.js +455 -0
- package/test/mcpOAuthService.test.js +693 -0
- package/test/mcpOAuthUtils.test.js +348 -0
- package/test/mcpServiceOAuth.test.js +267 -0
- package/test/numericInput.js +137 -0
- package/test/requestRoute.js +57 -0
- package/test/webhookConversationRoute.js +51 -0
- package/utils/logRedactor.js +4 -2
- package/utils/mcpOAuthCrypto.js +69 -0
- package/utils/mcpOAuthPkce.js +20 -0
- package/utils/numericInput.js +200 -0
- package/utils/outboundUrlGuard.js +131 -0
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
process.env.NODE_ENV = 'test';
|
|
2
|
+
|
|
3
|
+
var assert = require('assert');
|
|
4
|
+
var express = require('express');
|
|
5
|
+
var chai = require('chai');
|
|
6
|
+
var chaiHttp = require('chai-http');
|
|
7
|
+
var numericInput = require('../utils/numericInput');
|
|
8
|
+
|
|
9
|
+
chai.use(chaiHttp);
|
|
10
|
+
|
|
11
|
+
describe('numeric input validation', function () {
|
|
12
|
+
|
|
13
|
+
it('rejects a rating outside 1-5, including negatives', function () {
|
|
14
|
+
assert.strictEqual(numericInput.parseBoundedInteger(-1, 1, 5), null);
|
|
15
|
+
assert.strictEqual(numericInput.parseBoundedInteger(-100, 1, 5), null);
|
|
16
|
+
assert.strictEqual(numericInput.parseBoundedInteger('-3', 1, 5), null);
|
|
17
|
+
assert.strictEqual(numericInput.parseBoundedInteger(0, 1, 5), null);
|
|
18
|
+
assert.strictEqual(numericInput.parseBoundedInteger(6, 1, 5), null);
|
|
19
|
+
assert.strictEqual(numericInput.parseBoundedInteger(1.5, 1, 5), null);
|
|
20
|
+
assert.strictEqual(numericInput.parseBoundedInteger(true, 1, 5), null);
|
|
21
|
+
assert.strictEqual(numericInput.parseBoundedInteger([1], 1, 5), null);
|
|
22
|
+
assert.strictEqual(numericInput.parseBoundedInteger('1e2', 1, 5), null);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('accepts an integer rating from 1 to 5', function () {
|
|
26
|
+
assert.strictEqual(numericInput.parseBoundedInteger(1, 1, 5), 1);
|
|
27
|
+
assert.strictEqual(numericInput.parseBoundedInteger(5, 1, 5), 5);
|
|
28
|
+
assert.strictEqual(numericInput.parseBoundedInteger('4', 1, 5), 4);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('allows -1 only for fields whose domain includes that sentinel', function () {
|
|
32
|
+
assert.strictEqual(numericInput.parseBoundedInteger(-1, -1, 100000), -1);
|
|
33
|
+
assert.strictEqual(numericInput.parseBoundedInteger(-2, -1, 100000), null);
|
|
34
|
+
assert.strictEqual(numericInput.parseBoundedInteger(0, -1, 100000), 0);
|
|
35
|
+
assert.strictEqual(numericInput.parseAllowedInteger(-1, numericInput.DEPARTMENT_STATUSES), -1);
|
|
36
|
+
assert.strictEqual(numericInput.parseAllowedInteger(-2, numericInput.DEPARTMENT_STATUSES), null);
|
|
37
|
+
assert.strictEqual(numericInput.parseAllowedInteger(-100, numericInput.MESSAGE_STATUSES), -100);
|
|
38
|
+
assert.strictEqual(numericInput.parseAllowedInteger(-1, numericInput.MESSAGE_STATUSES), null);
|
|
39
|
+
assert.strictEqual(numericInput.parseAllowedInteger(-1, numericInput.REQUEST_STATUSES), null);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('rejects negative page and limit and keeps an over-max limit on the previous default', function () {
|
|
43
|
+
var negativePage = numericInput.readPagination({ page: -1, limit: 10 }, { defaultLimit: 40, maxLimit: 100, overMax: 'default' });
|
|
44
|
+
assert.ok(negativePage.error);
|
|
45
|
+
|
|
46
|
+
var negativeLimit = numericInput.readPagination({ limit: -5 }, { defaultLimit: 40, maxLimit: 100, overMax: 'default' });
|
|
47
|
+
assert.ok(negativeLimit.error);
|
|
48
|
+
|
|
49
|
+
var overMax = numericInput.readPagination({ limit: 200 }, { defaultLimit: 40, maxLimit: 100, overMax: 'default' });
|
|
50
|
+
assert.strictEqual(overMax.limit, 40);
|
|
51
|
+
assert.strictEqual(overMax.error, undefined);
|
|
52
|
+
|
|
53
|
+
var ok = numericInput.readPagination({ page: '2', limit: '10' }, { defaultLimit: 40, maxLimit: 100, overMax: 'default' });
|
|
54
|
+
assert.strictEqual(ok.page, 2);
|
|
55
|
+
assert.strictEqual(ok.limit, 10);
|
|
56
|
+
assert.strictEqual(ok.skip, 20);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('accepts sort direction -1 and 1 only', function () {
|
|
60
|
+
assert.strictEqual(numericInput.parseBoundedInteger(-1, -1, 1), -1);
|
|
61
|
+
assert.strictEqual(numericInput.parseBoundedInteger(1, -1, 1), 1);
|
|
62
|
+
assert.strictEqual(numericInput.parseBoundedInteger(-5, -1, 1), null);
|
|
63
|
+
assert.strictEqual(numericInput.parseBoundedInteger(0, -1, 1), 0);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
describe('PATCH /:requestid/rating', function () {
|
|
67
|
+
var app;
|
|
68
|
+
var Request;
|
|
69
|
+
var originalFindOneAndUpdate;
|
|
70
|
+
var lastUpdate;
|
|
71
|
+
|
|
72
|
+
before(function () {
|
|
73
|
+
Request = require('../models/request');
|
|
74
|
+
originalFindOneAndUpdate = Request.findOneAndUpdate;
|
|
75
|
+
Request.findOneAndUpdate = function (query, update) {
|
|
76
|
+
lastUpdate = update;
|
|
77
|
+
var chain = {
|
|
78
|
+
populate: function () { return chain; },
|
|
79
|
+
exec: function (callback) {
|
|
80
|
+
callback(null, Object.assign({ request_id: 'req-1' }, update.$set));
|
|
81
|
+
return chain;
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
return chain;
|
|
85
|
+
};
|
|
86
|
+
var userRequest = require('../routes/user-request');
|
|
87
|
+
app = express();
|
|
88
|
+
app.use(express.json());
|
|
89
|
+
app.use(userRequest);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
after(function () {
|
|
93
|
+
Request.findOneAndUpdate = originalFindOneAndUpdate;
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
beforeEach(function () {
|
|
97
|
+
lastUpdate = undefined;
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
function patch(body) {
|
|
101
|
+
return chai.request(app).patch('/req-1/rating').send(body);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
it('returns 422 for a negative rating', function (done) {
|
|
105
|
+
patch({ rating: -1, rating_message: 'bad' }).end(function (err, res) {
|
|
106
|
+
if (err) return done(err);
|
|
107
|
+
assert.strictEqual(res.status, 422);
|
|
108
|
+
assert.ok(res.body.msg.indexOf('rating') !== -1);
|
|
109
|
+
assert.strictEqual(lastUpdate, undefined);
|
|
110
|
+
done();
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('returns 422 for zero, above 5, floats and non-numeric values', function () {
|
|
115
|
+
return Promise.all([0, 6, 1.5, 'no', true, [-1]].map(function (rating) {
|
|
116
|
+
return new Promise(function (resolve, reject) {
|
|
117
|
+
patch({ rating: rating }).end(function (err, res) {
|
|
118
|
+
if (err) return reject(err);
|
|
119
|
+
if (res.status !== 422) return reject(new Error('expected 422 for ' + JSON.stringify(rating) + ', got ' + res.status));
|
|
120
|
+
resolve();
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
}));
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it('stores an in-range rating', function (done) {
|
|
127
|
+
patch({ rating: '4', rating_message: 'Good' }).end(function (err, res) {
|
|
128
|
+
if (err) return done(err);
|
|
129
|
+
assert.strictEqual(res.status, 200);
|
|
130
|
+
assert.strictEqual(res.body.rating, 4);
|
|
131
|
+
assert.strictEqual(lastUpdate.$set.rating, 4);
|
|
132
|
+
assert.strictEqual(lastUpdate.$set.rating_message, 'Good');
|
|
133
|
+
done();
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
});
|
package/test/requestRoute.js
CHANGED
|
@@ -1659,6 +1659,63 @@ describe('RequestRoute', () => {
|
|
|
1659
1659
|
|
|
1660
1660
|
});
|
|
1661
1661
|
|
|
1662
|
+
// mocha test/requestRoute.js --grep 'replace'
|
|
1663
|
+
describe('/replace', () => {
|
|
1664
|
+
|
|
1665
|
+
function replaceAndCapture(reasonInBody, cb) {
|
|
1666
|
+
var requestEvent = require('../event/requestEvent');
|
|
1667
|
+
var email = "test-request-replace-" + Date.now() + "@email.com";
|
|
1668
|
+
var pwd = "pwd";
|
|
1669
|
+
var captured = [];
|
|
1670
|
+
var onUpdate = function (data) { captured.push(data); };
|
|
1671
|
+
|
|
1672
|
+
userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
|
|
1673
|
+
projectService.create("request-replace", savedUser._id).then(function (savedProject) {
|
|
1674
|
+
chai.request(server)
|
|
1675
|
+
.post('/' + savedProject._id + '/requests/')
|
|
1676
|
+
.auth(email, pwd)
|
|
1677
|
+
.set('content-type', 'application/json')
|
|
1678
|
+
.send({ "first_text": "first_text" })
|
|
1679
|
+
.end(function (err, res) {
|
|
1680
|
+
res.should.have.status(200);
|
|
1681
|
+
requestEvent.on('request.participants.update', onUpdate);
|
|
1682
|
+
var body = { id: "5f0000000000000000000001" };
|
|
1683
|
+
if (reasonInBody !== undefined) { body.reason = reasonInBody; }
|
|
1684
|
+
chai.request(server)
|
|
1685
|
+
.put('/' + savedProject._id + '/requests/' + res.body.request_id + '/replace')
|
|
1686
|
+
.auth(email, pwd)
|
|
1687
|
+
.set('content-type', 'application/json')
|
|
1688
|
+
.send(body)
|
|
1689
|
+
.end(function (err, res2) {
|
|
1690
|
+
requestEvent.removeListener('request.participants.update', onUpdate);
|
|
1691
|
+
cb(res2, captured);
|
|
1692
|
+
});
|
|
1693
|
+
});
|
|
1694
|
+
});
|
|
1695
|
+
});
|
|
1696
|
+
}
|
|
1697
|
+
|
|
1698
|
+
it('replaceCarriesSubagentReason', function (done) {
|
|
1699
|
+
replaceAndCapture('subagent_call', function (res2, captured) {
|
|
1700
|
+
res2.should.have.status(200);
|
|
1701
|
+
expect(captured).to.have.lengthOf(1);
|
|
1702
|
+
expect(captured[0].reason).to.equal('subagent_call');
|
|
1703
|
+
expect(captured[0].addedParticipants).to.deep.equal(['bot_5f0000000000000000000001']);
|
|
1704
|
+
done();
|
|
1705
|
+
});
|
|
1706
|
+
});
|
|
1707
|
+
|
|
1708
|
+
it('replaceIgnoresUnknownReason', function (done) {
|
|
1709
|
+
replaceAndCapture('whatever', function (res2, captured) {
|
|
1710
|
+
res2.should.have.status(200);
|
|
1711
|
+
expect(captured).to.have.lengthOf(1);
|
|
1712
|
+
expect(captured[0].reason).to.equal(undefined);
|
|
1713
|
+
done();
|
|
1714
|
+
});
|
|
1715
|
+
});
|
|
1716
|
+
|
|
1717
|
+
});
|
|
1718
|
+
|
|
1662
1719
|
});
|
|
1663
1720
|
|
|
1664
1721
|
|
|
@@ -345,3 +345,54 @@ describe('WebhookConversationRoute - run', function () {
|
|
|
345
345
|
expect(statuses.filter((s) => s !== 429 && s !== 202)).to.deep.equal([]);
|
|
346
346
|
});
|
|
347
347
|
});
|
|
348
|
+
|
|
349
|
+
// Executions lineage (contract 8): retry_of is set only by the server's own refire, never by a public caller.
|
|
350
|
+
describe('WebhookConversationRoute - retry_of from public callers', function () {
|
|
351
|
+
|
|
352
|
+
this.timeout(20000);
|
|
353
|
+
const webhookService = require('../services/webhookService');
|
|
354
|
+
|
|
355
|
+
it('drops a body retry_of on the public conversation route', async () => {
|
|
356
|
+
const ctx = await setupProjectWithBot(server);
|
|
357
|
+
const webhook = await createConversationWebhook(ctx);
|
|
358
|
+
const res = await chai.request(server).post('/webhook/' + webhook.webhook_id).send({ alert: 'a', retry_of: 'exec-spoofed' });
|
|
359
|
+
expect(res).to.have.status(202);
|
|
360
|
+
const request = await Request.findOne({ request_id: res.body.request_id }).lean().exec();
|
|
361
|
+
expect(request.attributes.payload.alert).to.equal('a');
|
|
362
|
+
expect(request.attributes.payload).to.not.have.property('retry_of');
|
|
363
|
+
expect(request.attributes).to.not.have.property('retry_of');
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
it('drops a body retry_of on the dev conversation route', async () => {
|
|
367
|
+
const ctx = await setupProjectWithBot(server);
|
|
368
|
+
const webhook = await createConversationWebhook(ctx);
|
|
369
|
+
const preload = await chai.request(server).post('/' + ctx.project._id + '/webhooks/preload/' + webhook.webhook_id).auth(ctx.email, ctx.pwd);
|
|
370
|
+
expect(preload).to.have.status(200);
|
|
371
|
+
await new Promise((resolve) => setTimeout(resolve, 200)); // preload writes to Redis without awaiting
|
|
372
|
+
const res = await chai.request(server).post('/webhook/' + webhook.webhook_id + '/dev').send({ alert: 'd', retry_of: 'exec-spoofed' });
|
|
373
|
+
expect(res).to.have.status(202);
|
|
374
|
+
const request = await Request.findOne({ request_id: res.body.request_id }).lean().exec();
|
|
375
|
+
expect(request.attributes.payload).to.not.have.property('retry_of');
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
it('drops a body retry_of on the public and dev automation routes', async () => {
|
|
379
|
+
const ctx = await setupProjectWithBot(server);
|
|
380
|
+
const webhook = await createWebhook(ctx, { block_id: 'legacy-block' });
|
|
381
|
+
const originalRun = webhookService.run;
|
|
382
|
+
const calls = [];
|
|
383
|
+
webhookService.run = function (...args) {
|
|
384
|
+
calls.push(args[1]);
|
|
385
|
+
return originalRun.apply(webhookService, args);
|
|
386
|
+
};
|
|
387
|
+
try {
|
|
388
|
+
const res = await chai.request(server).post('/webhook/' + webhook.webhook_id).send({ alert: 'x', retry_of: 'exec-spoofed' });
|
|
389
|
+
expect(res).to.have.status(200);
|
|
390
|
+
await chai.request(server).post('/webhook/' + webhook.webhook_id + '/dev').send({ alert: 'y', retry_of: 'exec-spoofed' });
|
|
391
|
+
} finally {
|
|
392
|
+
webhookService.run = originalRun;
|
|
393
|
+
}
|
|
394
|
+
expect(calls).to.have.length(2);
|
|
395
|
+
expect(calls[0].alert).to.equal('x');
|
|
396
|
+
for (const p of calls) expect(p).to.not.have.property('retry_of');
|
|
397
|
+
});
|
|
398
|
+
});
|
package/utils/logRedactor.js
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
17
|
// Object keys whose value is a secret and must never be logged.
|
|
18
|
-
const SENSITIVE_KEY_RE = /^(api_?key|gptkey|llm_key|apikey|embedding_key|token|password|passwd|secret|client_secret|authorization|x-auth-token|kb_webhook_token)$/i;
|
|
18
|
+
const SENSITIVE_KEY_RE = /^(api_?key|gptkey|llm_key|apikey|embedding_key|token|password|passwd|secret|client_secret|authorization|x-auth-token|kb_webhook_token|access_token|refresh_token|code_verifier|access_token_enc|refresh_token_enc|client_secret_enc)$/i;
|
|
19
19
|
|
|
20
20
|
const REDACTED = '[REDACTED]';
|
|
21
21
|
const MAX_DEPTH = 8;
|
|
@@ -30,7 +30,9 @@ function redactString(str) {
|
|
|
30
30
|
.replace(/pcsk_[A-Za-z0-9_-]{12,}/g, 'pcsk_[REDACTED]')
|
|
31
31
|
.replace(/AIza[A-Za-z0-9_-]{20,}/g, 'AIza[REDACTED]')
|
|
32
32
|
// JSON-encoded secret fields inside a serialized body: "api_key":"..."
|
|
33
|
-
.replace(/("(?:api_?key|gptkey|llm_key|apikey|embedding_key|token|password|secret|client_secret)"\s*:\s*)"[^"]*"/gi, '$1"[REDACTED]"')
|
|
33
|
+
.replace(/("(?:api_?key|gptkey|llm_key|apikey|embedding_key|token|password|secret|client_secret|access_token|refresh_token|code_verifier)"\s*:\s*)"[^"]*"/gi, '$1"[REDACTED]"')
|
|
34
|
+
// form-encoded OAuth bodies and callback query strings: refresh_token=...&code=...
|
|
35
|
+
.replace(/(^|[?&])(access_token|refresh_token|code_verifier|client_secret|code)=[^&\s"]*/gi, '$1$2=[REDACTED]')
|
|
34
36
|
// bearer tokens
|
|
35
37
|
.replace(/Bearer\s+[A-Za-z0-9._-]+/gi, 'Bearer [REDACTED]');
|
|
36
38
|
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const crypto = require('crypto');
|
|
4
|
+
|
|
5
|
+
const VERSION = 'v1';
|
|
6
|
+
const ALGORITHM = 'aes-256-gcm';
|
|
7
|
+
const IV_BYTES = 12;
|
|
8
|
+
const KEY_BYTES = 32;
|
|
9
|
+
|
|
10
|
+
function getKey() {
|
|
11
|
+
const raw = process.env.MCP_OAUTH_ENCRYPTION_KEY;
|
|
12
|
+
if (!raw) {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
const key = Buffer.from(raw, 'base64');
|
|
16
|
+
return key.length === KEY_BYTES ? key : null;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function isConfigured() {
|
|
20
|
+
return getKey() !== null;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function requireKey() {
|
|
24
|
+
const key = getKey();
|
|
25
|
+
if (!key) {
|
|
26
|
+
const error = new Error('MCP OAuth is not available on this installation');
|
|
27
|
+
error.status = 503;
|
|
28
|
+
error.code = 'not_configured';
|
|
29
|
+
throw error;
|
|
30
|
+
}
|
|
31
|
+
return key;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Encrypts a secret as `v1:<iv>:<tag>:<ciphertext>` (base64 parts).
|
|
36
|
+
* null and undefined are returned unchanged.
|
|
37
|
+
*/
|
|
38
|
+
function encrypt(plain) {
|
|
39
|
+
if (plain === null || plain === undefined) {
|
|
40
|
+
return plain;
|
|
41
|
+
}
|
|
42
|
+
const key = requireKey();
|
|
43
|
+
const iv = crypto.randomBytes(IV_BYTES);
|
|
44
|
+
const cipher = crypto.createCipheriv(ALGORITHM, key, iv);
|
|
45
|
+
const ciphertext = Buffer.concat([cipher.update(String(plain), 'utf8'), cipher.final()]);
|
|
46
|
+
const tag = cipher.getAuthTag();
|
|
47
|
+
return [VERSION, iv.toString('base64'), tag.toString('base64'), ciphertext.toString('base64')].join(':');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Decrypts a value produced by encrypt. Throws when the value was tampered with
|
|
52
|
+
* or was encrypted with another key.
|
|
53
|
+
*/
|
|
54
|
+
function decrypt(encrypted) {
|
|
55
|
+
if (encrypted === null || encrypted === undefined) {
|
|
56
|
+
return encrypted;
|
|
57
|
+
}
|
|
58
|
+
const key = requireKey();
|
|
59
|
+
const parts = String(encrypted).split(':');
|
|
60
|
+
if (parts.length !== 4 || parts[0] !== VERSION) {
|
|
61
|
+
throw new Error('Unsupported encrypted value');
|
|
62
|
+
}
|
|
63
|
+
const [, iv, tag, ciphertext] = parts;
|
|
64
|
+
const decipher = crypto.createDecipheriv(ALGORITHM, key, Buffer.from(iv, 'base64'));
|
|
65
|
+
decipher.setAuthTag(Buffer.from(tag, 'base64'));
|
|
66
|
+
return Buffer.concat([decipher.update(Buffer.from(ciphertext, 'base64')), decipher.final()]).toString('utf8');
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
module.exports = { isConfigured, encrypt, decrypt };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const crypto = require('crypto');
|
|
4
|
+
|
|
5
|
+
/** Random OAuth `state`: 32 bytes, base64url (43 chars). */
|
|
6
|
+
function randomState() {
|
|
7
|
+
return crypto.randomBytes(32).toString('base64url');
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** PKCE code_verifier: 48 random bytes, base64url (64 chars, RFC 7636 allows 43..128). */
|
|
11
|
+
function createVerifier() {
|
|
12
|
+
return crypto.randomBytes(48).toString('base64url');
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** PKCE S256 code_challenge of a verifier. */
|
|
16
|
+
function challengeOf(verifier) {
|
|
17
|
+
return crypto.createHash('sha256').update(verifier, 'ascii').digest('base64url');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
module.exports = { randomState, createVerifier, challengeOf };
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
var MessageConstants = require('../models/messageConstants');
|
|
2
|
+
var RequestConstants = require('../models/requestConstants');
|
|
3
|
+
var LeadConstants = require('../models/leadConstants');
|
|
4
|
+
|
|
5
|
+
var MESSAGE_STATUSES = Object.values(MessageConstants.CHAT_MESSAGE_STATUS);
|
|
6
|
+
var REQUEST_STATUSES = Object.values(RequestConstants);
|
|
7
|
+
var LEAD_STATUSES = Object.values(LeadConstants);
|
|
8
|
+
var DEPARTMENT_STATUSES = [-1, 0, 1];
|
|
9
|
+
var PROPERTY_STATUSES = [100, 1000];
|
|
10
|
+
var SCRAPE_TYPES = [0, 1, 2, 4];
|
|
11
|
+
|
|
12
|
+
function parseBoundedInteger(value, min, max) {
|
|
13
|
+
if (typeof value === 'boolean' || value === null || value === undefined) {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
if (Array.isArray(value) || typeof value === 'object') {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
var n;
|
|
21
|
+
if (typeof value === 'number') {
|
|
22
|
+
n = value;
|
|
23
|
+
} else if (typeof value === 'string') {
|
|
24
|
+
var trimmed = value.trim();
|
|
25
|
+
if (!/^-?\d+$/.test(trimmed)) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
n = Number(trimmed);
|
|
29
|
+
} else {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (!Number.isSafeInteger(n) || n < min || n > max) {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
return n;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function parseAllowedInteger(value, allowed) {
|
|
40
|
+
if (!allowed || allowed.length === 0) {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
var min = allowed[0];
|
|
44
|
+
var max = allowed[0];
|
|
45
|
+
for (var i = 1; i < allowed.length; i++) {
|
|
46
|
+
if (allowed[i] < min) min = allowed[i];
|
|
47
|
+
if (allowed[i] > max) max = allowed[i];
|
|
48
|
+
}
|
|
49
|
+
var n = parseBoundedInteger(value, min, max);
|
|
50
|
+
if (n === null || allowed.indexOf(n) === -1) {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
return n;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function sendInvalidNumber(res, msg) {
|
|
57
|
+
res.status(422).json({ success: false, msg: msg });
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function isAbsent(value) {
|
|
62
|
+
return value === undefined || value === null || value === '';
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function takeOptionalBoundedInteger(res, value, min, max, label) {
|
|
66
|
+
if (isAbsent(value)) {
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
var n = parseBoundedInteger(value, min, max);
|
|
70
|
+
if (n === null) {
|
|
71
|
+
return sendInvalidNumber(res, label + ' must be an integer between ' + min + ' and ' + max);
|
|
72
|
+
}
|
|
73
|
+
return n;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function takeOptionalAllowedInteger(res, value, allowed, label) {
|
|
77
|
+
if (isAbsent(value)) {
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
80
|
+
var n = parseAllowedInteger(value, allowed);
|
|
81
|
+
if (n === null) {
|
|
82
|
+
return sendInvalidNumber(res, label + ' must be an integer and one of: ' + allowed.join(', '));
|
|
83
|
+
}
|
|
84
|
+
return n;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function takeStatusList(res, raw, allowed) {
|
|
88
|
+
if (typeof raw !== 'string') {
|
|
89
|
+
return sendInvalidNumber(res, 'status must be "all" or a comma-separated list of: ' + allowed.join(', '));
|
|
90
|
+
}
|
|
91
|
+
var parts = raw.split(',').map(function (part) { return part.trim(); }).filter(Boolean);
|
|
92
|
+
if (parts.length === 0) {
|
|
93
|
+
return sendInvalidNumber(res, 'status must be "all" or a comma-separated list of: ' + allowed.join(', '));
|
|
94
|
+
}
|
|
95
|
+
var out = [];
|
|
96
|
+
for (var i = 0; i < parts.length; i++) {
|
|
97
|
+
var n = parseAllowedInteger(parts[i], allowed);
|
|
98
|
+
if (n === null) {
|
|
99
|
+
return sendInvalidNumber(res, 'status must be an integer and one of: ' + allowed.join(', '));
|
|
100
|
+
}
|
|
101
|
+
out.push(n);
|
|
102
|
+
}
|
|
103
|
+
return out;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* overMax:
|
|
108
|
+
* - 'default': values above maxLimit fall back to defaultLimit (existing list endpoints)
|
|
109
|
+
* - 'clamp': values above maxLimit become maxLimit
|
|
110
|
+
* - 'reject': values above maxLimit are rejected
|
|
111
|
+
*/
|
|
112
|
+
function readPagination(query, options) {
|
|
113
|
+
var defaultLimit = options.defaultLimit;
|
|
114
|
+
var maxLimit = options.maxLimit;
|
|
115
|
+
var overMax = options.overMax || 'reject';
|
|
116
|
+
var errors = [];
|
|
117
|
+
var limit = defaultLimit;
|
|
118
|
+
var page = 0;
|
|
119
|
+
var source = query || {};
|
|
120
|
+
|
|
121
|
+
if (!isAbsent(source.limit)) {
|
|
122
|
+
var parsedLimit = parseBoundedInteger(source.limit, 1, Number.MAX_SAFE_INTEGER);
|
|
123
|
+
if (parsedLimit === null) {
|
|
124
|
+
errors.push('limit must be an integer greater than or equal to 1');
|
|
125
|
+
} else if (maxLimit != null && parsedLimit > maxLimit) {
|
|
126
|
+
if (overMax === 'default') {
|
|
127
|
+
limit = defaultLimit;
|
|
128
|
+
} else if (overMax === 'clamp') {
|
|
129
|
+
limit = maxLimit;
|
|
130
|
+
} else {
|
|
131
|
+
errors.push('limit must be an integer between 1 and ' + maxLimit);
|
|
132
|
+
}
|
|
133
|
+
} else {
|
|
134
|
+
limit = parsedLimit;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (!isAbsent(source.page)) {
|
|
139
|
+
var parsedPage = parseBoundedInteger(source.page, 0, 1000000);
|
|
140
|
+
if (parsedPage === null) {
|
|
141
|
+
errors.push('page must be an integer greater than or equal to 0');
|
|
142
|
+
} else {
|
|
143
|
+
page = parsedPage;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (errors.length) {
|
|
148
|
+
return { error: errors.join('; ') };
|
|
149
|
+
}
|
|
150
|
+
return { limit: limit, page: page, skip: page * limit };
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function takePagination(res, query, options) {
|
|
154
|
+
var paging = readPagination(query, options);
|
|
155
|
+
if (paging.error) {
|
|
156
|
+
return sendInvalidNumber(res, paging.error);
|
|
157
|
+
}
|
|
158
|
+
return paging;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function takeSortDirection(res, value) {
|
|
162
|
+
if (isAbsent(value)) {
|
|
163
|
+
return -1;
|
|
164
|
+
}
|
|
165
|
+
var n = parseBoundedInteger(value, -1, 1);
|
|
166
|
+
if (n !== -1 && n !== 1) {
|
|
167
|
+
return sendInvalidNumber(res, 'direction must be -1 or 1');
|
|
168
|
+
}
|
|
169
|
+
return n;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function takeMessageStatus(res, value) {
|
|
173
|
+
var parsed = takeOptionalAllowedInteger(res, value, MESSAGE_STATUSES, 'status');
|
|
174
|
+
if (parsed === null) {
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
if (parsed === undefined) {
|
|
178
|
+
return MessageConstants.CHAT_MESSAGE_STATUS.SENDING;
|
|
179
|
+
}
|
|
180
|
+
return parsed;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
module.exports = {
|
|
184
|
+
MESSAGE_STATUSES: MESSAGE_STATUSES,
|
|
185
|
+
REQUEST_STATUSES: REQUEST_STATUSES,
|
|
186
|
+
LEAD_STATUSES: LEAD_STATUSES,
|
|
187
|
+
DEPARTMENT_STATUSES: DEPARTMENT_STATUSES,
|
|
188
|
+
PROPERTY_STATUSES: PROPERTY_STATUSES,
|
|
189
|
+
SCRAPE_TYPES: SCRAPE_TYPES,
|
|
190
|
+
parseBoundedInteger: parseBoundedInteger,
|
|
191
|
+
parseAllowedInteger: parseAllowedInteger,
|
|
192
|
+
sendInvalidNumber: sendInvalidNumber,
|
|
193
|
+
takeOptionalBoundedInteger: takeOptionalBoundedInteger,
|
|
194
|
+
takeOptionalAllowedInteger: takeOptionalAllowedInteger,
|
|
195
|
+
takeStatusList: takeStatusList,
|
|
196
|
+
readPagination: readPagination,
|
|
197
|
+
takePagination: takePagination,
|
|
198
|
+
takeSortDirection: takeSortDirection,
|
|
199
|
+
takeMessageStatus: takeMessageStatus
|
|
200
|
+
};
|