@ti-engine/core 1.10.0 → 1.11.0
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 +62 -0
- package/package.json +1 -1
- package/types/utils/cache.d.ts +36 -0
- package/utils/cache.js +57 -8
- package/utils/tools.js +18 -2
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,68 @@
|
|
|
2
2
|
|
|
3
3
|
This document contains the list of changes made to the framework. The format is based on the [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) specification.
|
|
4
4
|
|
|
5
|
+
## Version 1.11.0
|
|
6
|
+
|
|
7
|
+
`decycle` silently lost a key named `__proto__` and corrupted the surrounding document. It built its replica as `{}`
|
|
8
|
+
and copied keys in by bracket assignment, so that one name hit the inherited prototype setter instead of becoming an
|
|
9
|
+
own property: the key vanished and its value became the replica's prototype, which `stringifyJSON` then flattened
|
|
10
|
+
back in as unrelated top-level keys. An object with an own `__proto__` key holding `{ hello: 1 }` alongside an `ada`
|
|
11
|
+
key serialized to `{"ada":{…},"hello":1}` — the key gone and `hello` promoted to a sibling. (Note the input cannot be
|
|
12
|
+
written as an object literal: in `{ __proto__: x }` that name is a prototype setter, not a key, even quoted. Only a
|
|
13
|
+
computed key or an assignment onto `Object.create( null )` produces the property this bug needed.)
|
|
14
|
+
|
|
15
|
+
This reached Redis: `cache.setJSON` serializes with `stringifyJSON`, so any caller storing such a key lost it against
|
|
16
|
+
a real cache. `decycle` also runs on the message-exchange integrity-hash path, and over an exception's `data` whenever
|
|
17
|
+
`raise` is given one. No test caught it because web-framework's in-memory cache double clones with
|
|
18
|
+
`JSON.parse( JSON.stringify( … ) )` and never reaches `stringifyJSON`.
|
|
19
|
+
|
|
20
|
+
**This is a minor rather than a patch release because it requires a consumer code change** — see the note below. The
|
|
21
|
+
fix is a bug fix, but the replica's prototype is part of the observable contract.
|
|
22
|
+
|
|
23
|
+
* fix(tools): build `decycle`'s object replica with `Object.create( null )`, so a key named `__proto__` is an ordinary
|
|
24
|
+
own key with no setter to hit. Verified against both consumers of the output — `_.isPlainObject` accepts a
|
|
25
|
+
null-prototype object, so `decomposeJSON` (message hashing) and `_.toPlainObject` (serialization) are unaffected
|
|
26
|
+
* fix(tools): the same guard in `errorToJSON`, which copied an error's own property names with the identical pattern
|
|
27
|
+
* fix(cache)!: `Cache.getValues( keys, prefix )` resolved **every key to `null`**, whatever Redis returned. Its ternary
|
|
28
|
+
inspected `results`, the accumulator being built, instead of `result`, the per-key `[ error, value ]` entry — and
|
|
29
|
+
`{}.length` is `undefined`, so the comparison was always false. The single-key `getValue()` had the correct form; the
|
|
30
|
+
two expressions were near-identical and one drifted. No first-party caller existed, which is why nothing caught it.
|
|
31
|
+
The per-entry decode and the key mapping are now two shared module-level functions (`decodeCommandValue`,
|
|
32
|
+
`mapCommandValues`, exported for testing) rather than duplicated inline expressions, so the drift cannot recur. The
|
|
33
|
+
mapping iterates the requested keys rather than the raw results, so a short response yields an entry per requested
|
|
34
|
+
key instead of silently omitting some, and its accumulator is a null-prototype object for the same reason as above —
|
|
35
|
+
cache keys come from the caller, and one named `__proto__` would otherwise repoint the map's prototype. Marked
|
|
36
|
+
breaking only because the returned map now has a null prototype (see the note below); the value behaviour is a
|
|
37
|
+
straight bug fix
|
|
38
|
+
* test(tools): pin the regression, the cycle handling it must not break, the `decomposeJSON` and `retrocycle` paths,
|
|
39
|
+
and the fact that a key named `constructor` was **never** affected — it is an ordinary writable data property, so
|
|
40
|
+
bracket assignment always shadowed it correctly, and the fix's shape should not imply otherwise
|
|
41
|
+
* build(release): bump package version from `1.10.0` to `1.11.0`
|
|
42
|
+
|
|
43
|
+
`retrocycle` was checked and is not affected: it mutates objects that already came through `JSON.parse`, which creates
|
|
44
|
+
`__proto__` as an own data property, and assignment to an existing own property shadows the inherited setter.
|
|
45
|
+
|
|
46
|
+
**Breaking for consumers who touch the returned object directly.** The objects returned by `decycle`, by `errorToJSON`
|
|
47
|
+
for an `Error`, and by `Cache.getValues` now have a **null prototype**, so they inherit nothing from `Object.prototype`. Passing
|
|
48
|
+
them to `JSON.stringify`, lodash, or `decomposeJSON` is unaffected; the following are not, and the first two throw
|
|
49
|
+
rather than merely behaving differently:
|
|
50
|
+
|
|
51
|
+
| Expression on the returned object | Before | Now |
|
|
52
|
+
|---|---|---|
|
|
53
|
+
| `` `${ result }` ``, `String( result )`, `result + ""` | `"[object Object]"` | **`TypeError: Cannot convert object to primitive value`** |
|
|
54
|
+
| `result.toString()`, `result.valueOf()` | works | **`TypeError`** |
|
|
55
|
+
| `result.hasOwnProperty( key )` | works | **`TypeError`** — use `Object.prototype.hasOwnProperty.call( result, key )` |
|
|
56
|
+
| `result instanceof Object` | `true` | `false` |
|
|
57
|
+
| `result.constructor` | `Object` | `undefined` |
|
|
58
|
+
|
|
59
|
+
The likeliest way to hit this is interpolating an `errorToJSON` result straight into a log line. Wrap such a value in
|
|
60
|
+
`_.cloneDeep( … )` — which normalizes the prototype back — or serialize it explicitly. No call site in this monorepo
|
|
61
|
+
does any of the above; every first-party `hasOwnProperty` use is already the `Object.prototype.…call` form.
|
|
62
|
+
|
|
63
|
+
**Mixed-version note:** for the rare message carrying a literal `__proto__` key, an old sender and an upgraded receiver
|
|
64
|
+
compute different `createMessageHash` values, so the receiver raises `E_SEC_MESSAGE_TAMPERING_DETECTED`. Such messages
|
|
65
|
+
were already being corrupted in transit, so this surfaces a pre-existing problem rather than creating one.
|
|
66
|
+
|
|
5
67
|
## Version 1.10.0
|
|
6
68
|
|
|
7
69
|
* feat(localization): `getLabel( label, language, fallback )` takes an optional third argument returned when the key
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ti-engine/core",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.11.0",
|
|
4
4
|
"description": "Microservice framework for Node.js: a Redis-backed message exchange with end-to-end call tracing, retries and tamper-evident message envelopes.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"microservices",
|
package/types/utils/cache.d.ts
CHANGED
|
@@ -1,6 +1,42 @@
|
|
|
1
1
|
declare const _exported: Readonly<CommonMemoryCache>;
|
|
2
2
|
export { _exported as instance };
|
|
3
|
+
export { decodeCommandValue };
|
|
4
|
+
export { mapCommandValues };
|
|
3
5
|
import ConnectionObserver = require("#connection-observer");
|
|
6
|
+
/**
|
|
7
|
+
* Decodes one entry of a `multi(...).exec()` result into the value it carries.
|
|
8
|
+
* <br/>
|
|
9
|
+
* Each entry is an ioredis `[ error, value ]` pair, so the value sits at index 1 and is a string when the key existed.
|
|
10
|
+
* Returns `undefined` for a miss, an error entry, or a malformed entry.
|
|
11
|
+
* <br/>
|
|
12
|
+
* This lives outside the class, and is shared by {@link CommonMemoryCache#getValue} and
|
|
13
|
+
* {@link CommonMemoryCache#getValues}, because it previously existed as two near-identical inline expressions and one
|
|
14
|
+
* of them drifted: `getValues` inspected its own accumulator instead of the per-key entry, so `.length` was
|
|
15
|
+
* `undefined`, the comparison was always false, and **every key resolved to `null`** whatever Redis returned.
|
|
16
|
+
*
|
|
17
|
+
* @method
|
|
18
|
+
* @param {Array} [result] One `[ error, value ]` entry.
|
|
19
|
+
* @returns {*} The parsed value, or `undefined` when there is none.
|
|
20
|
+
* @private
|
|
21
|
+
*/
|
|
22
|
+
declare function decodeCommandValue(result?: any[]): any;
|
|
23
|
+
/**
|
|
24
|
+
* Maps a set of requested keys onto the values a `multi(...).exec()` returned for them, using `null` for a miss.
|
|
25
|
+
* <br/>
|
|
26
|
+
* Iterates the requested `keys` rather than the raw results, so a short or absent response still yields one entry per
|
|
27
|
+
* requested key instead of silently omitting some — the caller's map always has the shape it asked for.
|
|
28
|
+
* <br/>
|
|
29
|
+
* The accumulator has no prototype on purpose: the key names come from the caller, and a cache key named `__proto__`
|
|
30
|
+
* written by bracket assignment onto an ordinary `{}` would repoint the accumulator's prototype instead of creating
|
|
31
|
+
* the entry. Same class as the `decycle` defect fixed in `tools.js`; see the 1.11.0 changelog entry.
|
|
32
|
+
*
|
|
33
|
+
* @method
|
|
34
|
+
* @param {string[]} keys The keys that were requested, in command order.
|
|
35
|
+
* @param {Array} [rawResults] The `multi(...).exec()` result.
|
|
36
|
+
* @returns {Object} A null-prototype map of key to value, `null` where the key was absent.
|
|
37
|
+
* @private
|
|
38
|
+
*/
|
|
39
|
+
declare function mapCommandValues(keys: string[], rawResults?: any[]): Object;
|
|
4
40
|
/**
|
|
5
41
|
* Used to create and/or return a Common Memory Cache singleton instance.
|
|
6
42
|
*
|
package/utils/cache.js
CHANGED
|
@@ -13,6 +13,55 @@ const tools = require( "#tools" );
|
|
|
13
13
|
const redis = require( "#redis-integration" );
|
|
14
14
|
const exceptions = require( "#exceptions" );
|
|
15
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Decodes one entry of a `multi(...).exec()` result into the value it carries.
|
|
18
|
+
* <br/>
|
|
19
|
+
* Each entry is an ioredis `[ error, value ]` pair, so the value sits at index 1 and is a string when the key existed.
|
|
20
|
+
* Returns `undefined` for a miss, an error entry, or a malformed entry.
|
|
21
|
+
* <br/>
|
|
22
|
+
* This lives outside the class, and is shared by {@link CommonMemoryCache#getValue} and
|
|
23
|
+
* {@link CommonMemoryCache#getValues}, because it previously existed as two near-identical inline expressions and one
|
|
24
|
+
* of them drifted: `getValues` inspected its own accumulator instead of the per-key entry, so `.length` was
|
|
25
|
+
* `undefined`, the comparison was always false, and **every key resolved to `null`** whatever Redis returned.
|
|
26
|
+
*
|
|
27
|
+
* @method
|
|
28
|
+
* @param {Array} [result] One `[ error, value ]` entry.
|
|
29
|
+
* @returns {*} The parsed value, or `undefined` when there is none.
|
|
30
|
+
* @private
|
|
31
|
+
*/
|
|
32
|
+
function decodeCommandValue( result ) {
|
|
33
|
+
// `Array.isArray` rather than a bare truthy-and-length test: a string also has a `length` and an indexable
|
|
34
|
+
// character at 1, so a malformed non-array entry would otherwise be parsed as if it were a value.
|
|
35
|
+
return ( Array.isArray( result ) && result.length > 1 && _.isString( result[ 1 ] ) ) ? tools.parseJSON( result[ 1 ] ) : undefined;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Maps a set of requested keys onto the values a `multi(...).exec()` returned for them, using `null` for a miss.
|
|
40
|
+
* <br/>
|
|
41
|
+
* Iterates the requested `keys` rather than the raw results, so a short or absent response still yields one entry per
|
|
42
|
+
* requested key instead of silently omitting some — the caller's map always has the shape it asked for.
|
|
43
|
+
* <br/>
|
|
44
|
+
* The accumulator has no prototype on purpose: the key names come from the caller, and a cache key named `__proto__`
|
|
45
|
+
* written by bracket assignment onto an ordinary `{}` would repoint the accumulator's prototype instead of creating
|
|
46
|
+
* the entry. Same class as the `decycle` defect fixed in `tools.js`; see the 1.11.0 changelog entry.
|
|
47
|
+
*
|
|
48
|
+
* @method
|
|
49
|
+
* @param {string[]} keys The keys that were requested, in command order.
|
|
50
|
+
* @param {Array} [rawResults] The `multi(...).exec()` result.
|
|
51
|
+
* @returns {Object} A null-prototype map of key to value, `null` where the key was absent.
|
|
52
|
+
* @private
|
|
53
|
+
*/
|
|
54
|
+
function mapCommandValues( keys, rawResults ) {
|
|
55
|
+
let values = Object.create( null );
|
|
56
|
+
|
|
57
|
+
_.forEach( keys, ( key, idx ) => {
|
|
58
|
+
let decoded = decodeCommandValue( rawResults ? rawResults[ idx ] : undefined );
|
|
59
|
+
values[ key ] = ( decoded === undefined ) ? null : decoded;
|
|
60
|
+
} );
|
|
61
|
+
|
|
62
|
+
return values;
|
|
63
|
+
}
|
|
64
|
+
|
|
16
65
|
/**
|
|
17
66
|
* Used to create and/or return a Common Memory Cache singleton instance.
|
|
18
67
|
*
|
|
@@ -260,8 +309,7 @@ class CommonMemoryCache extends ConnectionObserver {
|
|
|
260
309
|
if ( this.#isOperational === true ) {
|
|
261
310
|
let commandGetValue = [ redis.cacheCommands.GET_VALUE, key ];
|
|
262
311
|
this.#redisClient.executeCommands( [ commandGetValue ] ).then( ( results ) => {
|
|
263
|
-
results
|
|
264
|
-
resolve( ( results && results.length > 1 && _.isString( results[ 1 ] ) ) ? tools.parseJSON( results[ 1 ] ) : undefined );
|
|
312
|
+
resolve( decodeCommandValue( results ? results[ 0 ] : undefined ) );
|
|
265
313
|
} ).catch( ( error ) => {
|
|
266
314
|
reject( error );
|
|
267
315
|
} );
|
|
@@ -288,11 +336,7 @@ class CommonMemoryCache extends ConnectionObserver {
|
|
|
288
336
|
commands.push( [ redis.cacheCommands.GET_VALUE, ( ( prefix ) ? prefix : "" ) + key ] );
|
|
289
337
|
} );
|
|
290
338
|
this.#redisClient.executeCommands( commands ).then( ( rawResults ) => {
|
|
291
|
-
|
|
292
|
-
_.forEach( rawResults, ( result, idx ) => {
|
|
293
|
-
results[ keys[ idx ] ] = ( results && results.length > 1 && _.isString( results[ 1 ] ) ) ? tools.parseJSON( results[ 1 ] ) : null;
|
|
294
|
-
} );
|
|
295
|
-
resolve( results );
|
|
339
|
+
resolve( mapCommandValues( keys, rawResults ) );
|
|
296
340
|
} ).catch( ( error ) => {
|
|
297
341
|
reject( error );
|
|
298
342
|
} );
|
|
@@ -769,4 +813,9 @@ class CommonMemoryCache extends ConnectionObserver {
|
|
|
769
813
|
}
|
|
770
814
|
|
|
771
815
|
const instance = new CommonMemoryCache();
|
|
772
|
-
module.exports.instance = Object.freeze( instance );
|
|
816
|
+
module.exports.instance = Object.freeze( instance );
|
|
817
|
+
|
|
818
|
+
// Exported for testing. The cache singleton builds its own Redis client in its constructor, so `getValues` cannot be
|
|
819
|
+
// driven without a live server — these are the pure halves of it, and they are where the defect was.
|
|
820
|
+
module.exports.decodeCommandValue = decodeCommandValue;
|
|
821
|
+
module.exports.mapCommandValues = mapCommandValues;
|
package/utils/tools.js
CHANGED
|
@@ -182,7 +182,11 @@ module.exports.getEnumName = ( enumList, enumValue, placeholder = undefined ) =>
|
|
|
182
182
|
* @public
|
|
183
183
|
*/
|
|
184
184
|
module.exports.errorToJSON = ( value ) => {
|
|
185
|
-
|
|
185
|
+
// Null prototype for the same reason as in `decycle`: the keys come from the error's own property names, and a
|
|
186
|
+
// key named `__proto__` written by bracket assignment onto an ordinary `{}` would repoint the prototype rather
|
|
187
|
+
// than be copied. An error carrying such a property is exotic — it takes a deliberate `defineProperty` — but the
|
|
188
|
+
// pattern is identical and the guard costs nothing.
|
|
189
|
+
let error = Object.create( null );
|
|
186
190
|
|
|
187
191
|
if ( value instanceof Error ) {
|
|
188
192
|
Object.getOwnPropertyNames( value ).forEach( ( key ) => {
|
|
@@ -337,7 +341,19 @@ module.exports.decycle = ( object, replacer ) => {
|
|
|
337
341
|
} );
|
|
338
342
|
} else {
|
|
339
343
|
// If it is an object, replicate the object.
|
|
340
|
-
|
|
344
|
+
// The replica has NO prototype on purpose. A key is copied in by bracket assignment, and on an
|
|
345
|
+
// ordinary `{}` a key named `__proto__` would hit the inherited setter instead of creating an own
|
|
346
|
+
// property: the key vanished from the replica and its value silently became the replica's prototype.
|
|
347
|
+
// `stringifyJSON` then ran `_.toPlainObject` over that, flattening the lost value's own fields back
|
|
348
|
+
// in as unrelated top-level keys, so the key vanished and its contents surfaced as siblings. Silent
|
|
349
|
+
// corruption, not a crash, and it reached Redis through `cache.setJSON`. With a null prototype there
|
|
350
|
+
// is no setter to hit, so `__proto__` is an ordinary key.
|
|
351
|
+
// Both consumers of this output are safe: `_.isPlainObject` accepts a null-prototype object, so
|
|
352
|
+
// `decomposeJSON` behaves identically, and `_.toPlainObject` preserves the key — but only because
|
|
353
|
+
// lodash's own `baseAssignValue` special-cases `__proto__` with `defineProperty`. That is a dependency
|
|
354
|
+
// this fix leans on, so `test/tools-proto-keys.test.js` asserts the whole chain end to end rather than
|
|
355
|
+
// just this function.
|
|
356
|
+
newItem = Object.create( null );
|
|
341
357
|
Object.keys( value ).forEach( ( name ) => {
|
|
342
358
|
newItem[ name ] = derez(
|
|
343
359
|
value[ name ],
|