Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Treat ArrayBuffers from another context as valid #170

Merged
merged 1 commit into from
Aug 5, 2017

Conversation

feross
Copy link
Owner

@feross feross commented Aug 4, 2017

Fixes: #166

ArrayBuffers from another context (i.e. an iframe) do not pass the
instanceof check but they should be treated as valid.

Fixes: #166

ArrayBuffers from another context (i.e. an iframe) do not pass the
`instanceof` check but they should be treated as valid.
@feross
Copy link
Owner Author

feross commented Aug 4, 2017

cc @jvilk

@jvilk
Copy link

jvilk commented Aug 4, 2017

@feross do you want to add a test for this case? It looks like you run browser tests. If you can access the DOM during these tests (& only run the test in the browser environment), we can inject an iframe and construct an arraybuffer using its constructor.

@jvilk
Copy link

jvilk commented Aug 4, 2017

Here's an (mostly untested) example demonstrating how this might be accomplished. Due to cross-origin issues, you need to create a data URI iframe that creates another data URI iframe and returns the result to the test context via a postMessage.

const childFrameHTML = `<head>
<!-- Needs to be the proper URL to buffer. -->
<script type="text/javascript" src="buffer.js"></script>
</head>
<body>
<script type="text/javascript">
  const iframe = document.createElement("iframe");
  iframe.src = 'data:text/html;base64,';
  document.body.appendChild(iframe);
  const ab = new iframe.contentWindow.ArrayBuffer(10);
  const u8arr = new Uint8Array(ab);
  const b = Buffer.from(ab);
  u8arr[0] = 3;
  window.top.postMessage('' + (b[0] === 3), "*");
</script></body>`;
const iframe = document.createElement("iframe");
iframe.onmessage = function(e) {
  console.assert(e.data === "true");
};
iframe.src = 'data:text/html;base64,' + Buffer.from(childFrameHTML, 'utf8').toString('base64');
document.body.appendChild(iframe);
// In cleanup, make sure you document.body.removeChild(iframe);

@feross feross merged commit 39941b1 into master Aug 5, 2017
@feross feross deleted the arraybuffer-context branch August 5, 2017 01:37
@feross
Copy link
Owner Author

feross commented Aug 5, 2017

I think testing for this would probably be pretty brittle. I'm satisfied with just pushing out the fix and relying on code review to ensure that isArrayBuffer is always used instead of instanceof ArrayBuffer. But if you want to send a PR that works, I would merge it :)

@feross
Copy link
Owner Author

feross commented Aug 5, 2017

Released as 5.0.7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants