Skip to content

Commit

Permalink
src: throw DOMException on cloning non-serializable objects
Browse files Browse the repository at this point in the history
Instead of TypeError, throwing DOMException in accordance to the HTML
structured serialize algorithms.
  • Loading branch information
legendecas committed May 3, 2023
1 parent f68ff9f commit 5cef3de
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 14 deletions.
13 changes: 13 additions & 0 deletions doc/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -2118,6 +2118,14 @@ urlSearchParams.has.call(buf, 'foo');

### `ERR_INVALID_TRANSFER_OBJECT`

<!-- YAML
removed: REPLACEME
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/47839
description: A DOMException is thrown instead.
-->

An invalid transfer object was passed to `postMessage()`.

<a id="ERR_INVALID_TUPLE"></a>
Expand Down Expand Up @@ -2323,6 +2331,11 @@ this error will not occur with standard builds of Node.js.

<!-- YAML
added: v15.0.0
removed: REPLACEME
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/47839
description: A DOMException is thrown instead.
-->

An object that needs to be explicitly listed in the `transferList` argument
Expand Down
4 changes: 4 additions & 0 deletions src/env_properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@
V(channel_string, "channel") \
V(chunks_sent_since_last_write_string, "chunksSentSinceLastWrite") \
V(clone_unsupported_type_str, "Cannot transfer object of unsupported type.") \
V(clone_transfer_needed_str, \
"Object that needs transfer was found in message but not listed in " \
"transferList") \
V(clone_untransferable_str, "Found invalid object in transferList.") \
V(code_string, "code") \
V(commonjs_string, "commonjs") \
V(config_string, "config") \
Expand Down
6 changes: 0 additions & 6 deletions src/node_errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,9 @@ void AppendExceptionLine(Environment* env,
V(ERR_INVALID_MODULE, Error) \
V(ERR_INVALID_STATE, Error) \
V(ERR_INVALID_THIS, TypeError) \
V(ERR_INVALID_TRANSFER_OBJECT, TypeError) \
V(ERR_MEMORY_ALLOCATION_FAILED, Error) \
V(ERR_MESSAGE_TARGET_CONTEXT_UNAVAILABLE, Error) \
V(ERR_MISSING_ARGS, TypeError) \
V(ERR_MISSING_TRANSFERABLE_IN_TRANSFER_LIST, TypeError) \
V(ERR_MISSING_PASSPHRASE, TypeError) \
V(ERR_MISSING_PLATFORM_FOR_WORKER, Error) \
V(ERR_NON_CONTEXT_AWARE_DISABLED, Error) \
Expand Down Expand Up @@ -162,15 +160,11 @@ ERRORS_WITH_CODE(V)
V(ERR_INVALID_MODULE, "No such module") \
V(ERR_INVALID_STATE, "Invalid state") \
V(ERR_INVALID_THIS, "Value of \"this\" is the wrong type") \
V(ERR_INVALID_TRANSFER_OBJECT, "Found invalid object in transferList") \
V(ERR_MEMORY_ALLOCATION_FAILED, "Failed to allocate memory") \
V(ERR_OSSL_EVP_INVALID_DIGEST, "Invalid digest used") \
V(ERR_MESSAGE_TARGET_CONTEXT_UNAVAILABLE, \
"A message object could not be deserialized successfully in the target " \
"vm.Context") \
V(ERR_MISSING_TRANSFERABLE_IN_TRANSFER_LIST, \
"Object that needs transfer was found in message but not listed " \
"in transferList") \
V(ERR_MISSING_PLATFORM_FOR_WORKER, \
"The V8 platform used by this instance of Node does not support " \
"creating Workers") \
Expand Down
4 changes: 2 additions & 2 deletions src/node_messaging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ class SerializerDelegate : public ValueSerializer::Delegate {
}

if (mode == BaseObject::TransferMode::kTransferable) {
THROW_ERR_MISSING_TRANSFERABLE_IN_TRANSFER_LIST(env_);
ThrowDataCloneError(env_->clone_transfer_needed_str());
return Nothing<bool>();
}

Expand Down Expand Up @@ -531,7 +531,7 @@ Maybe<bool> Message::Serialize(Environment* env,
}
}

THROW_ERR_INVALID_TRANSFER_OBJECT(env);
ThrowDataCloneException(context, env->clone_untransferable_str());
return Nothing<bool>();
}
if (delegate.AddNestedHostObjects().IsNothing())
Expand Down
16 changes: 12 additions & 4 deletions test/parallel/test-whatwg-webstreams-transfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ const theData = 'hello';
});

assert.throws(() => port2.postMessage(readable), {
code: 'ERR_MISSING_TRANSFERABLE_IN_TRANSFER_LIST',
constructor: DOMException,
name: 'DataCloneError',
code: 25,
});

port2.postMessage(readable, [readable]);
Expand Down Expand Up @@ -155,7 +157,9 @@ const theData = 'hello';
});

assert.throws(() => port2.postMessage(readable), {
code: 'ERR_MISSING_TRANSFERABLE_IN_TRANSFER_LIST',
constructor: DOMException,
name: 'DataCloneError',
code: 25,
});

port2.postMessage(readable, [readable]);
Expand Down Expand Up @@ -206,7 +210,9 @@ const theData = 'hello';
});

assert.throws(() => port2.postMessage(writable), {
code: 'ERR_MISSING_TRANSFERABLE_IN_TRANSFER_LIST',
constructor: DOMException,
name: 'DataCloneError',
code: 25,
});

port2.postMessage(writable, [writable]);
Expand Down Expand Up @@ -292,7 +298,9 @@ const theData = 'hello';
});

assert.throws(() => port2.postMessage(transform), {
code: 'ERR_MISSING_TRANSFERABLE_IN_TRANSFER_LIST',
constructor: DOMException,
name: 'DataCloneError',
code: 25,
});

port2.postMessage(transform, [transform]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ const { once } = require('events');
assert.throws(() => {
port1.postMessage(fh);
}, {
code: 'ERR_MISSING_TRANSFERABLE_IN_TRANSFER_LIST'
constructor: DOMException,
name: 'DataCloneError',
code: 25,
});

// Check that transferring FileHandle instances works.
Expand Down
4 changes: 3 additions & 1 deletion test/parallel/test-worker-workerdata-messageport.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ const meowScript = () => 'meow';
workerData,
transferList: []
}), {
code: 'ERR_MISSING_TRANSFERABLE_IN_TRANSFER_LIST',
constructor: DOMException,
name: 'DataCloneError',
code: 25,
message: 'Object that needs transfer was found in message but not ' +
'listed in transferList'
});
Expand Down

0 comments on commit 5cef3de

Please sign in to comment.