diff options
author | Kenta Moriuchi <moriken@kimamass.com> | 2024-01-04 13:12:38 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-04 09:42:38 +0530 |
commit | b2cd254c35b6b1b128beea0eacdb8e814d91e003 (patch) | |
tree | d55fa5910e32d8a664aff5b680e07debea93181e /ext/web/09_file.js | |
parent | 48556748577ba46db5f9212d14a0fcaa90d632f6 (diff) |
fix: strict type check for cross realms (#21669)
Deno v1.39 introduces `vm.runInNewContext`. This may cause problems when
using `Object.prototype.isPrototypeOf` to check built-in types.
```js
import vm from "node:vm";
const err = new Error();
const crossErr = vm.runInNewContext(`new Error()`);
console.assert( !(crossErr instanceof Error) );
console.assert( Object.getPrototypeOf(err) !== Object.getPrototypeOf(crossErr) );
```
This PR changes to check using internal slots solves them.
---
current:
```
> import vm from "node:vm";
undefined
> vm.runInNewContext(`new Error("message")`)
Error {}
> vm.runInNewContext(`new Date("2018-12-10T02:26:59.002Z")`)
Date {}
```
this PR:
```
> import vm from "node:vm";
undefined
> vm.runInNewContext(`new Error("message")`)
Error: message
at <anonymous>:1:1
> vm.runInNewContext(`new Date("2018-12-10T02:26:59.002Z")`)
2018-12-10T02:26:59.002Z
```
---------
Co-authored-by: Bartek IwaĆczuk <biwanczuk@gmail.com>
Diffstat (limited to 'ext/web/09_file.js')
-rw-r--r-- | ext/web/09_file.js | 72 |
1 files changed, 33 insertions, 39 deletions
diff --git a/ext/web/09_file.js b/ext/web/09_file.js index 4288802af..c38fbd101 100644 --- a/ext/web/09_file.js +++ b/ext/web/09_file.js @@ -16,10 +16,9 @@ import * as webidl from "ext:deno_webidl/00_webidl.js"; import { ReadableStream } from "ext:deno_web/06_streams.js"; import { URL } from "ext:deno_url/00_url.js"; const { - ArrayBufferPrototype, - ArrayBufferPrototypeSlice, - ArrayBufferPrototypeGetByteLength, ArrayBufferIsView, + ArrayBufferPrototypeGetByteLength, + ArrayBufferPrototypeSlice, ArrayPrototypePush, AsyncGeneratorPrototypeNext, DataViewPrototypeGetBuffer, @@ -31,23 +30,26 @@ const { MathMin, ObjectPrototypeIsPrototypeOf, RegExpPrototypeTest, - // TODO(lucacasonato): add SharedArrayBuffer to primordials - // SharedArrayBufferPrototype SafeFinalizationRegistry, SafeRegExp, StringPrototypeCharAt, - StringPrototypeToLowerCase, StringPrototypeSlice, + StringPrototypeToLowerCase, Symbol, SymbolFor, - TypedArrayPrototypeSet, + TypeError, TypedArrayPrototypeGetBuffer, TypedArrayPrototypeGetByteLength, TypedArrayPrototypeGetByteOffset, - TypedArrayPrototypeGetSymbolToStringTag, - TypeError, + TypedArrayPrototypeSet, Uint8Array, } = primordials; +const { + isAnyArrayBuffer, + isArrayBuffer, + isDataView, + isTypedArray, +} = core; import { createFilteredInspectProxy } from "ext:deno_console/01_console.js"; const { op_blob_read_part, @@ -129,34 +131,30 @@ function processBlobParts(parts, endings) { let size = 0; for (let i = 0; i < parts.length; ++i) { const element = parts[i]; - if (ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, element)) { + if (isArrayBuffer(element)) { const chunk = new Uint8Array(ArrayBufferPrototypeSlice(element, 0)); ArrayPrototypePush(processedParts, BlobReference.fromUint8Array(chunk)); size += ArrayBufferPrototypeGetByteLength(element); - } else if (ArrayBufferIsView(element)) { - if (TypedArrayPrototypeGetSymbolToStringTag(element) !== undefined) { - // TypedArray - const chunk = new Uint8Array( - TypedArrayPrototypeGetBuffer(/** @type {Uint8Array} */ (element)), - TypedArrayPrototypeGetByteOffset(/** @type {Uint8Array} */ (element)), - TypedArrayPrototypeGetByteLength(/** @type {Uint8Array} */ (element)), - ); - size += TypedArrayPrototypeGetByteLength( - /** @type {Uint8Array} */ (element), - ); - ArrayPrototypePush(processedParts, BlobReference.fromUint8Array(chunk)); - } else { - // DataView - const chunk = new Uint8Array( - DataViewPrototypeGetBuffer(/** @type {DataView} */ (element)), - DataViewPrototypeGetByteOffset(/** @type {DataView} */ (element)), - DataViewPrototypeGetByteLength(/** @type {DataView} */ (element)), - ); - size += DataViewPrototypeGetByteLength( - /** @type {DataView} */ (element), - ); - ArrayPrototypePush(processedParts, BlobReference.fromUint8Array(chunk)); - } + } else if (isTypedArray(element)) { + const chunk = new Uint8Array( + TypedArrayPrototypeGetBuffer(/** @type {Uint8Array} */ (element)), + TypedArrayPrototypeGetByteOffset(/** @type {Uint8Array} */ (element)), + TypedArrayPrototypeGetByteLength(/** @type {Uint8Array} */ (element)), + ); + size += TypedArrayPrototypeGetByteLength( + /** @type {Uint8Array} */ (element), + ); + ArrayPrototypePush(processedParts, BlobReference.fromUint8Array(chunk)); + } else if (isDataView(element)) { + const chunk = new Uint8Array( + DataViewPrototypeGetBuffer(/** @type {DataView} */ (element)), + DataViewPrototypeGetByteOffset(/** @type {DataView} */ (element)), + DataViewPrototypeGetByteLength(/** @type {DataView} */ (element)), + ); + size += DataViewPrototypeGetByteLength( + /** @type {DataView} */ (element), + ); + ArrayPrototypePush(processedParts, BlobReference.fromUint8Array(chunk)); } else if (ObjectPrototypeIsPrototypeOf(BlobPrototype, element)) { ArrayPrototypePush(processedParts, element); size += element.size; @@ -446,11 +444,7 @@ webidl.converters["BlobPart"] = (V, prefix, context, opts) => { if (ObjectPrototypeIsPrototypeOf(BlobPrototype, V)) { return webidl.converters["Blob"](V, prefix, context, opts); } - if ( - ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, V) || - // deno-lint-ignore prefer-primordials - ObjectPrototypeIsPrototypeOf(SharedArrayBuffer.prototype, V) - ) { + if (isAnyArrayBuffer(V)) { return webidl.converters["ArrayBuffer"](V, prefix, context, opts); } if (ArrayBufferIsView(V)) { |