@routr/common 2.31.2 → 2.31.4

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.
@@ -1,9 +1,9 @@
1
1
  import { SipURI } from "./types";
2
2
  /**
3
3
  * The caller identity Routr rewrites toward a Trunk travels in its own
4
- * Record-Route as parameters, so the dialog's later requests can be mapped back
5
- * without state. Shared by the processor, which writes and reads them, and the
6
- * trace sanitizer, which must rewrite inside them.
4
+ * Record-Route, as parameters or in the URI user part, so the dialog's later
5
+ * requests can be mapped back without state. Shared by the processor, which
6
+ * writes and reads them, and the trace sanitizer, which must rewrite inside them.
7
7
  */
8
8
  /**
9
9
  * The Record-Route parameter names: [r]ewritten [f]rom or [c]allee (To),
@@ -22,6 +22,7 @@ export declare const IDENTITY_PARAMETER_NAMES: string[];
22
22
  export interface Identity {
23
23
  user?: string;
24
24
  host: string;
25
+ port?: number;
25
26
  userParam?: string;
26
27
  }
27
28
  /**
@@ -33,9 +34,20 @@ export interface Identity {
33
34
  */
34
35
  export declare const encodeIdentity: (uri: SipURI) => string;
35
36
  /**
36
- * Decodes an identity parameter value.
37
+ * Decodes an identity parameter value, user@host[:port][;user=...].
37
38
  *
38
39
  * @param {string} value - The base64url value
39
40
  * @return {Identity | undefined} The identity, or undefined when it is not one
40
41
  */
41
42
  export declare const decodeIdentity: (value?: string) => Identity | undefined;
43
+ /**
44
+ * Reads the identity mapping from a Route URI user part of the form
45
+ * rfo.<b64>.rfn.<b64>.rco.<b64>.rcn.<b64>: each pair optional, in that order.
46
+ * The URI is what a user agent keeps of a Record-Route entry (RFC 3261 section
47
+ * 12.1.2), and "." is not in the base64url alphabet.
48
+ *
49
+ * @param {string} user - The URI user part
50
+ * @return {Record<string, string> | undefined} The base64url values by
51
+ * parameter name, or undefined when the user part is not of this form
52
+ */
53
+ export declare const decodeIdentityUser: (user?: string) => Record<string, string> | undefined;
package/dist/identity.js CHANGED
@@ -1,11 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.decodeIdentity = exports.encodeIdentity = exports.IDENTITY_PARAMETER_NAMES = exports.IDENTITY_PARAMETERS = void 0;
3
+ exports.decodeIdentityUser = exports.decodeIdentity = exports.encodeIdentity = exports.IDENTITY_PARAMETER_NAMES = exports.IDENTITY_PARAMETERS = void 0;
4
4
  /**
5
5
  * The caller identity Routr rewrites toward a Trunk travels in its own
6
- * Record-Route as parameters, so the dialog's later requests can be mapped back
7
- * without state. Shared by the processor, which writes and reads them, and the
8
- * trace sanitizer, which must rewrite inside them.
6
+ * Record-Route, as parameters or in the URI user part, so the dialog's later
7
+ * requests can be mapped back without state. Shared by the processor, which
8
+ * writes and reads them, and the trace sanitizer, which must rewrite inside them.
9
9
  */
10
10
  /**
11
11
  * The Record-Route parameter names: [r]ewritten [f]rom or [c]allee (To),
@@ -33,12 +33,13 @@ const encodeIdentity = (uri) => {
33
33
  };
34
34
  exports.encodeIdentity = encodeIdentity;
35
35
  /**
36
- * Decodes an identity parameter value.
36
+ * Decodes an identity parameter value, user@host[:port][;user=...].
37
37
  *
38
38
  * @param {string} value - The base64url value
39
39
  * @return {Identity | undefined} The identity, or undefined when it is not one
40
40
  */
41
41
  const decodeIdentity = (value) => {
42
+ var _a;
42
43
  if (!value)
43
44
  return undefined;
44
45
  const decoded = Buffer.from(value, "base64url").toString("utf8");
@@ -47,10 +48,32 @@ const decodeIdentity = (value) => {
47
48
  if (at < 0)
48
49
  return undefined;
49
50
  const user = address.slice(0, at);
50
- return {
51
- user: user || undefined,
52
- host: address.slice(at + 1),
53
- userParam: userParam || undefined
54
- };
51
+ const hostPort = address.slice(at + 1);
52
+ // An IPv6 host keeps its brackets, so its port follows the last "]:"
53
+ const [, host = hostPort, port] = (_a = hostPort.match(/^(\[[^\]]*\]|[^:]*)(?::(\d+))?$/)) !== null && _a !== void 0 ? _a : [];
54
+ return Object.assign(Object.assign({ user: user || undefined, host }, (port ? { port: Number(port) } : {})), { userParam: userParam || undefined });
55
55
  };
56
56
  exports.decodeIdentity = decodeIdentity;
57
+ /**
58
+ * Reads the identity mapping from a Route URI user part of the form
59
+ * rfo.<b64>.rfn.<b64>.rco.<b64>.rcn.<b64>: each pair optional, in that order.
60
+ * The URI is what a user agent keeps of a Record-Route entry (RFC 3261 section
61
+ * 12.1.2), and "." is not in the base64url alphabet.
62
+ *
63
+ * @param {string} user - The URI user part
64
+ * @return {Record<string, string> | undefined} The base64url values by
65
+ * parameter name, or undefined when the user part is not of this form
66
+ */
67
+ const decodeIdentityUser = (user) => {
68
+ const parts = (user !== null && user !== void 0 ? user : "").split(".");
69
+ if (parts.length < 2 || parts.length % 2)
70
+ return undefined;
71
+ const pairs = parts
72
+ .filter((_, i) => i % 2 === 0)
73
+ .map((name, i) => [name, parts[i * 2 + 1]]);
74
+ const order = pairs.map(([name]) => exports.IDENTITY_PARAMETER_NAMES.indexOf(name));
75
+ const valid = order.every((o, i) => o >= 0 && (i === 0 || o > order[i - 1])) &&
76
+ pairs.every(([, value]) => /^[A-Za-z0-9_-]+$/.test(value));
77
+ return valid ? Object.fromEntries(pairs) : undefined;
78
+ };
79
+ exports.decodeIdentityUser = decodeIdentityUser;
@@ -87,6 +87,8 @@ message MessageRequest {
87
87
  string edge_port_version = 10;
88
88
  // Local socket the EdgePort received the message on
89
89
  NetInterface received_on = 11;
90
+ // Hostnames this EdgePort answers to (spec.aliases)
91
+ repeated string aliases = 12;
90
92
  }
91
93
 
92
94
  message MessageResponse {
@@ -26,6 +26,16 @@ export declare const isExternalHost: (req: MessageRequest, host?: string) => boo
26
26
  */
27
27
  export declare const isInternalHost: (req: MessageRequest, host?: string) => boolean;
28
28
  export declare const isOwnHost: (req: MessageRequest, host?: string) => boolean;
29
+ /**
30
+ * Whether a host is one of the names the proxy answers to (spec.aliases),
31
+ * compared case-insensitively. Aliases are never advertised, so only rules
32
+ * about Route entries pointing at the proxy use this.
33
+ *
34
+ * @param {MessageRequest} req - The recorded request
35
+ * @param {string} host - Host to test
36
+ * @return {boolean} True for an alias
37
+ */
38
+ export declare const isAliasHost: (req: MessageRequest, host?: string) => boolean;
29
39
  export declare const requestsIn: (call: TraceRecord[]) => TraceRecord[];
30
40
  /**
31
41
  * Identifies the party that sent a request: its From URI and tag. Both are
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.uriOf = exports.isE164 = exports.directionOf = exports.requestsIn = exports.isOwnHost = exports.isInternalHost = exports.isExternalHost = exports.topBranch = exports.topVia = exports.cseqOf = exports.headerValues = exports.statusOf = exports.isInDialog = exports.isRelayed = exports.isForwarded = exports.isResponse = exports.isRequest = exports.outbound = exports.inbound = void 0;
3
+ exports.uriOf = exports.isE164 = exports.directionOf = exports.requestsIn = exports.isAliasHost = exports.isOwnHost = exports.isInternalHost = exports.isExternalHost = exports.topBranch = exports.topVia = exports.cseqOf = exports.headerValues = exports.statusOf = exports.isInDialog = exports.isRelayed = exports.isForwarded = exports.isResponse = exports.isRequest = exports.outbound = exports.inbound = void 0;
4
4
  const types_1 = require("../types");
5
5
  const status_1 = require("../status");
6
6
  /*
@@ -64,6 +64,18 @@ const isInternalHost = (req, host) => {
64
64
  exports.isInternalHost = isInternalHost;
65
65
  const isOwnHost = (req, host) => (0, exports.isExternalHost)(req, host) || (0, exports.isInternalHost)(req, host);
66
66
  exports.isOwnHost = isOwnHost;
67
+ /**
68
+ * Whether a host is one of the names the proxy answers to (spec.aliases),
69
+ * compared case-insensitively. Aliases are never advertised, so only rules
70
+ * about Route entries pointing at the proxy use this.
71
+ *
72
+ * @param {MessageRequest} req - The recorded request
73
+ * @param {string} host - Host to test
74
+ * @return {boolean} True for an alias
75
+ */
76
+ const isAliasHost = (req, host) => !!host &&
77
+ (req.aliases || []).some((a) => a.toLowerCase() === host.toLowerCase());
78
+ exports.isAliasHost = isAliasHost;
67
79
  const inNet = (host, net) => {
68
80
  const [base, bitsRaw] = net.split("/");
69
81
  const bits = bitsRaw === undefined ? 32 : parseInt(bitsRaw, 10);
@@ -31,7 +31,11 @@ exports.default = (0, types_1.defineRule)({
31
31
  var _a;
32
32
  if (!(0, facts_1.isForwarded)(record))
33
33
  return;
34
- const own = (((_a = (0, facts_1.outbound)(record)) === null || _a === void 0 ? void 0 : _a.route) || []).find((r) => { var _a, _b; return (0, facts_1.isOwnHost)(record.request, (_b = (_a = r.address) === null || _a === void 0 ? void 0 : _a.uri) === null || _b === void 0 ? void 0 : _b.host); });
34
+ const own = (((_a = (0, facts_1.outbound)(record)) === null || _a === void 0 ? void 0 : _a.route) || []).find((r) => {
35
+ var _a, _b, _c, _d;
36
+ return (0, facts_1.isOwnHost)(record.request, (_b = (_a = r.address) === null || _a === void 0 ? void 0 : _a.uri) === null || _b === void 0 ? void 0 : _b.host) ||
37
+ (0, facts_1.isAliasHost)(record.request, (_d = (_c = r.address) === null || _c === void 0 ? void 0 : _c.uri) === null || _d === void 0 ? void 0 : _d.host);
38
+ });
35
39
  if (own) {
36
40
  return fail(`outgoing request still carries a Route to ${own.address.uri.host}, one of the proxy's interfaces`, "out", "Route");
37
41
  }
@@ -99,7 +99,29 @@ function applyRewrites(record, rewrites) {
99
99
  // (";rfo=...;rfn=..." in a hop's diff); base64url has no quote, so they are
100
100
  // safe to match in the JSON text
101
101
  const encodedInText = new RegExp(`\\b(${ENCODED_IDENTITY_PARAMETERS.join("|")})=([A-Za-z0-9_-]+)`, "g");
102
- return JSON.parse(rewrite(text.replace(encodedInText, (_m, name, value) => `${name}=${reencode(value, rewrite)}`)));
102
+ // And in a Route or Record-Route URI user part kept as text
103
+ // ("<sip:rfo....rcn...@host>")
104
+ const names = ENCODED_IDENTITY_PARAMETERS.join("|");
105
+ const encodedUserInText = new RegExp(`\\b((?:${names})\\.[A-Za-z0-9_-]+(?:\\.(?:${names})\\.[A-Za-z0-9_-]+)*)@`, "g");
106
+ return JSON.parse(rewrite(text
107
+ .replace(encodedInText, (_m, name, value) => `${name}=${reencode(value, rewrite)}`)
108
+ .replace(encodedUserInText, (_m, user) => `${reencodeUser(user, rewrite)}@`)));
109
+ }
110
+ /**
111
+ * Rewrites the identities inside a URI user part that holds Routr's identity
112
+ * mapping (rfo.<b64>.rfn.<b64>...); any other user part is returned as is.
113
+ *
114
+ * @param {string} user - The URI user part
115
+ * @param {Function} rewrite - The literal rewrites
116
+ * @return {string} The rewritten user part
117
+ */
118
+ function reencodeUser(user, rewrite) {
119
+ const values = (0, identity_1.decodeIdentityUser)(user);
120
+ if (!values)
121
+ return user;
122
+ return Object.entries(values)
123
+ .map(([name, value]) => `${name}.${reencode(value, rewrite)}`)
124
+ .join(".");
103
125
  }
104
126
  /**
105
127
  * Decodes a base64url identity parameter, rewrites it and encodes it again.
@@ -123,7 +145,8 @@ function reencode(value, rewrite) {
123
145
  // survive sanitizing; they are decoded, rewritten and encoded again.
124
146
  const ENCODED_IDENTITY_PARAMETERS = identity_1.IDENTITY_PARAMETER_NAMES;
125
147
  /**
126
- * Rewrites the identity parameters wherever they are a field of the record.
148
+ * Rewrites the identity parameters wherever they are a field of the record, or
149
+ * a URI user part.
127
150
  *
128
151
  * @param {T} value - Any part of the record
129
152
  * @param {Function} rewrite - The literal rewrites
@@ -139,6 +162,10 @@ function rewriteEncodedIdentities(value, rewrite) {
139
162
  if (ENCODED_IDENTITY_PARAMETERS.includes(k) && typeof v === "string") {
140
163
  return [k, reencode(v, rewrite)];
141
164
  }
165
+ // A Route or Record-Route URI whose user part holds the mapping
166
+ if (k === "user" && typeof v === "string") {
167
+ return [k, reencodeUser(v, rewrite)];
168
+ }
142
169
  return [k, rewriteEncodedIdentities(v, rewrite)];
143
170
  }));
144
171
  }
@@ -100,6 +100,12 @@ export declare const traceRecordSchema: {
100
100
  type: string;
101
101
  };
102
102
  };
103
+ aliases: {
104
+ type: string;
105
+ items: {
106
+ type: string;
107
+ };
108
+ };
103
109
  message: {
104
110
  type: string;
105
111
  properties: {
@@ -108,6 +108,7 @@ const request = {
108
108
  listeningPoints: { type: "array" },
109
109
  externalAddrs: { type: "array", items: { type: "string" } },
110
110
  localnets: { type: "array", items: { type: "string" } },
111
+ aliases: { type: "array", items: { type: "string" } },
111
112
  message: sipMessage,
112
113
  metadata: stringMap,
113
114
  edgePortVersion: { type: "string" },
package/dist/types.d.ts CHANGED
@@ -69,6 +69,7 @@ export interface MessageRequest {
69
69
  metadata?: Record<string, string>;
70
70
  edgePortVersion?: string;
71
71
  receivedOn?: NetInterface;
72
+ aliases?: string[];
72
73
  }
73
74
  export interface MessageResponse {
74
75
  message: Record<string, unknown>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@routr/common",
3
- "version": "2.31.2",
3
+ "version": "2.31.4",
4
4
  "description": "Common package",
5
5
  "author": "Pedro Sanders <psanders@fonoster.com>",
6
6
  "homepage": "https://github.com/fonoster/routr#readme",
@@ -61,5 +61,5 @@
61
61
  "devDependencies": {
62
62
  "@types/js-yaml": "^4.0.5"
63
63
  },
64
- "gitHead": "170b3eff626b866f5ce79807a6748258da687e82"
64
+ "gitHead": "11c1ca25e5892aeea6f5b836b35cf9f90f020279"
65
65
  }