soajs.controller 4.2.35 → 4.2.37

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/lib/header.js ADDED
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+
3
+ /**
4
+ * @license
5
+ * Copyright SOAJS All Rights Reserved.
6
+ *
7
+ * Use of this source code is governed by an Apache license that can be
8
+ * found in the LICENSE file at the root of this repository
9
+ */
10
+
11
+
12
+ /**
13
+ * Encodes a string to be safely used as an HTTP header value.
14
+ * This preserves all data and is the standard, recommended method.
15
+ *
16
+ * @param {string | number | boolean} value The value to encode.
17
+ * @returns {string} The URL-encoded string.
18
+ */
19
+ function encodeHeaderValue(value) {
20
+ if (value === null || typeof value === 'undefined') {
21
+ return '';
22
+ }
23
+ return encodeURIComponent(String(value));
24
+ }
25
+
26
+ /**
27
+ * Decodes a URL-encoded string from an HTTP header.
28
+ * It safely handles potential errors if the input is malformed.
29
+ *
30
+ * @param {string} encodedValue The percent-encoded header value to decode.
31
+ * @returns {string} The decoded (original) string, or the original
32
+ * string if decoding fails.
33
+ */
34
+ function decodeHeaderValue(encodedValue) {
35
+ // Return empty string or null as is, to avoid errors.
36
+ if (!encodedValue) {
37
+ return encodedValue;
38
+ }
39
+
40
+ try {
41
+ // The standard way to decode a value encoded by encodeURIComponent.
42
+ return decodeURIComponent(encodedValue);
43
+ } catch (e) {
44
+ // This catches errors from malformed URI sequences (e.g., a lone '%').
45
+ console.error(`Failed to decode header value: "${encodedValue}"`, e);
46
+ // Return the original, undecoded value so the app doesn't crash.
47
+ return encodedValue;
48
+ }
49
+ }
50
+
51
+ module.exports = { encodeHeaderValue, decodeHeaderValue };
@@ -82,6 +82,7 @@ module.exports = (configuration) => {
82
82
  req.soajs.registry.services[service_n]) {
83
83
  req.soajs.log.debug(req.soajs.registry.services[service_n]);
84
84
  }
85
+ req.soajs.log.warn(JSON.stringify({ "url": req.url, "headers": req.headers }));
85
86
  return req.soajs.controllerResponse(core.error.getError(130));
86
87
  }
87
88
 
package/mw/mt/index.js CHANGED
@@ -12,6 +12,9 @@ const async = require("async");
12
12
 
13
13
  const utils = require("./utils");
14
14
 
15
+
16
+ const { encodeHeaderValue } = require("../../lib/header.js");
17
+
15
18
  /**
16
19
  *
17
20
  * @param configuration
@@ -40,7 +43,10 @@ module.exports = (configuration) => {
40
43
 
41
44
  let serviceParam = {};
42
45
  if (!proxy) {
43
- let serviceInfo = req.soajs.controller.serviceParams.registry.versions[req.soajs.controller.serviceParams.version];
46
+ let serviceInfo = null;
47
+ if (req.soajs.controller.serviceParams.registry.versions) {
48
+ serviceInfo = req.soajs.controller.serviceParams.registry.versions[req.soajs.controller.serviceParams.version];
49
+ }
44
50
  if (!serviceInfo) {
45
51
  req.soajs.log.error("Problem accessing service [" + req.soajs.controller.serviceParams.name + "], API [" + req.soajs.controller.serviceParams.path + "] & version [" + req.soajs.controller.serviceParams.version + "]");
46
52
  return next(133);
@@ -244,8 +250,8 @@ module.exports = (configuration) => {
244
250
  injectObj.urac = {
245
251
  "_id": uracObj._id,
246
252
  "username": uracObj.username,
247
- "firstName": uracObj.firstName,
248
- "lastName": uracObj.lastName,
253
+ "firstName": encodeHeaderValue(uracObj.firstName),
254
+ "lastName": encodeHeaderValue(uracObj.lastName),
249
255
  "email": uracObj.email,
250
256
  "groups": uracObj.groups,
251
257
  "socialLogin": uracObj.socialLogin,
@@ -333,12 +339,14 @@ module.exports = (configuration) => {
333
339
  req.soajs.log.error(err.message);
334
340
  }
335
341
  let _h = JSON.stringify(injectObj);
336
- req.headers.soajsinjectobj = Buffer.from(_h, 'ascii').toString('utf-8');
342
+ // req.headers.soajsinjectobj = Buffer.from(_h, 'ascii').toString('utf-8');
343
+ req.headers.soajsinjectobj = _h; //encodeHeaderValue(_h);
337
344
  return next();
338
345
  });
339
346
  } else {
340
347
  let _h = JSON.stringify(injectObj);
341
- req.headers.soajsinjectobj = Buffer.from(_h, 'ascii').toString('utf-8');
348
+ // req.headers.soajsinjectobj = Buffer.from(_h, 'ascii').toString('utf-8');
349
+ req.headers.soajsinjectobj = _h; //encodeHeaderValue(_h);
342
350
  return next();
343
351
  }
344
352
  }
package/mw/url/index.js CHANGED
@@ -20,21 +20,21 @@ let libParseURL = require('../../lib/parseURL');
20
20
  */
21
21
  module.exports = (configuration) => {
22
22
  let core = configuration.core;
23
-
23
+
24
24
  return (req, res, next) => {
25
25
  if (!req.soajs) {
26
26
  throw new TypeError('soajs mw is not started');
27
27
  }
28
-
28
+
29
29
  if (!req.soajs.controller) {
30
30
  req.soajs.controller = {};
31
31
  }
32
-
32
+
33
33
  let parsedUrl = url.parse(req.url, true);
34
34
  if (!req.query && parsedUrl.query) {
35
35
  req.query = parsedUrl.query;
36
36
  }
37
-
37
+
38
38
  if (!req.query) {
39
39
  req.query = {};
40
40
  }
@@ -42,6 +42,18 @@ module.exports = (configuration) => {
42
42
  if (!req.soajs.controller.serviceParams.service_n || req.soajs.controller.serviceParams.service_n === '') {
43
43
  return req.soajs.controllerResponse(core.error.getError(136));
44
44
  }
45
+ if (
46
+ req.soajs.controller.serviceParams.service_n !== "soajs" &&
47
+ (
48
+ !req.soajs.registry ||
49
+ !req.soajs.registry.services ||
50
+ !req.soajs.registry.services[req.soajs.controller.serviceParams.service_n]
51
+ )) {
52
+ req.soajs.log.fatal("Service [" + req.soajs.controller.serviceParams.service_n + "] URL [" + req.url + "] couldn't be matched to a service or the service entry in registry is missing [port || hosts]");
53
+ req.soajs.log.warn(JSON.stringify({ "url": req.url, "headers": req.headers }));
54
+ return req.soajs.controllerResponse(core.error.getError(130));
55
+ }
56
+
45
57
  let key = req.headers.key || req.query.key;
46
58
  if (!req.headers.key) {
47
59
  req.headers.key = key;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "soajs.controller",
3
3
  "description": "soajs multi tenant API gateway",
4
- "version": "4.2.35",
4
+ "version": "4.2.37",
5
5
  "author": {
6
6
  "name": "soajs team",
7
7
  "email": "team@soajs.org"
@@ -94,6 +94,8 @@ Controller.prototype.init = function (callback) {
94
94
  app.use(ip2ban_mw());
95
95
  app.use(maintenanceMode_mw()); //NOTE: this is for global maintenance mode
96
96
 
97
+ app.use(url_mw({ "core": core }));
98
+
97
99
  log.info("Loading Provision ...");
98
100
  let dbConfig = registry.coreDB.provision;
99
101
  if (registry.coreDB.oauth) {
@@ -135,7 +137,7 @@ Controller.prototype.init = function (callback) {
135
137
 
136
138
  // app.use(enhancer_mw());
137
139
 
138
- app.use(url_mw({ "core": core }));
140
+ // app.use(url_mw({ "core": core }));
139
141
 
140
142
  app.use(key_mw({ "provision": provision, "regEnvironment": regEnvironment }));
141
143
 
@@ -27,7 +27,7 @@ let service = {
27
27
  },
28
28
  oauth: true,
29
29
  extKeyRequired: true,
30
- urac: false,
30
+ urac: true,
31
31
  urac_Profile: false,
32
32
  urac_ACL: false,
33
33
  provision_ACL: false,
@@ -9,7 +9,7 @@ let token = {
9
9
  _id: "5c8d0c505653de3985aa0ffd",
10
10
  locked: true,
11
11
  username: "owner",
12
- firstName: "owner3",
12
+ firstName: "owner3'sèį",
13
13
  lastName: "owner",
14
14
  email: "me@localhost.com",
15
15
  "ts": new Date().getTime(),
@@ -9,7 +9,7 @@ let token = {
9
9
  _id: "5c8d0c505653de3985aa0ffd",
10
10
  locked: true,
11
11
  username: "owner",
12
- firstName: "owner3",
12
+ firstName: "owner3'sèį",
13
13
  lastName: "owner",
14
14
  email: "me@localhost.com",
15
15
  "ts": new Date().getTime(),
@@ -4,7 +4,7 @@ let user = {
4
4
  locked: true,
5
5
  username: "owner",
6
6
  password: "$2a$12$geJJfv33wkYIXEAlDkeeuOgiQ6y6MjP/YxbqLdHdDSK7LDG.7n7Pq",
7
- firstName: "owner3",
7
+ firstName: "owner3'sèį",
8
8
  lastName: "owner",
9
9
  email: "me@localhost.com",
10
10
  ts: 1552747600152,
@@ -20,15 +20,17 @@ function startServer(serverConfig, callback) {
20
20
  'lastname': "hage"
21
21
  }
22
22
  };
23
-
24
-
25
- sApp.get('/passport/login', (req, res) => res.json(sReply));
26
-
23
+
24
+
25
+ sApp.get('/passport/login', (req, res) => {
26
+ res.json(sReply);
27
+ });
28
+
27
29
  mApp.get('/heartbeat', (req, res) => res.json(mReply));
28
-
30
+
29
31
  let sAppServer = sApp.listen(serverConfig.s.port, () => console.log(`${serverConfig.name} service mock listening on port ${serverConfig.s.port}!`));
30
32
  let mAppServer = mApp.listen(serverConfig.m.port, () => console.log(`${serverConfig.name} service mock listening on port ${serverConfig.m.port}!`));
31
-
33
+
32
34
  return callback(
33
35
  {
34
36
  "sAppServer": sAppServer,
@@ -40,11 +42,11 @@ function startServer(serverConfig, callback) {
40
42
 
41
43
  function killServer(config) {
42
44
  console.log("killing server ....");
43
-
45
+
44
46
  config.mAppServer.close((err) => {
45
47
  console.log("...sAppServer: " + config.name);
46
48
  });
47
-
49
+
48
50
  config.sAppServer.close((err) => {
49
51
  console.log("...mAppServer: " + config.name);
50
52
  });
@@ -44,7 +44,7 @@
44
44
  "requestTimeoutRenewal": 5,
45
45
  "extKeyRequired": true,
46
46
  "oauth": true,
47
- "urac": false,
47
+ "urac": true,
48
48
  "urac_Profile": false,
49
49
  "urac_ACL": false,
50
50
  "tenant_Profile": false,