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

structuredClone / atob / btoa should throw a TypeError without arguments #41450

Closed
zloirock opened this issue Jan 9, 2022 · 14 comments · Fixed by #41478 or #41651
Closed

structuredClone / atob / btoa should throw a TypeError without arguments #41450

zloirock opened this issue Jan 9, 2022 · 14 comments · Fixed by #41478 or #41651
Labels
buffer Issues and PRs related to the buffer subsystem. confirmed-bug Issues with confirmed bugs. errors Issues and PRs related to JavaScript errors originated in Node.js core. repro-exists Issues with reproductions.

Comments

@zloirock
Copy link

zloirock commented Jan 9, 2022

Version

17.3.0

Platform

MacOS 12.1

Subsystem

global / buffer

What steps will reproduce the bug?

structuredClone(); // => undefined, should be a TypeError
atob(); // => 'ºw^~)Þ', should be a TypeError
btoa(); // => 'dW5kZWZpbmVk', should be a TypeError

How often does it reproduce? Is there a required condition?

No response

What is the expected behavior?

No response

What do you see instead?

^

Additional information

It's an inconsistency with web standards.

@DerekNonGeneric
Copy link
Contributor

@zloirock, thanks for opening this issue. As far as I understand, these utility methods evolved w/o any standard or official specification. The nearest I can find is the link below.

https://html.spec.whatwg.org/multipage/webappapis.html#atob

By any chance, would you be able to link a more complete specification or should we derive this specification from current web browser behavior?

@zloirock zloirock changed the title atob / btoa should throw a TypeError without arguments structuredClone / atob / btoa should throw a TypeError without arguments Jan 9, 2022
@zloirock
Copy link
Author

zloirock commented Jan 9, 2022

@DerekNonGeneric the same with structuredClone. I'm not an expert in Web IDL, but looks like here specified that they should take a required argument, so they should throw an error if the argument is missed. This is the behavior of all actual browsers, so I think that's interpreted correctly.

@zloirock
Copy link
Author

zloirock commented Jan 9, 2022

For example, similarly, in the URL spec we could see that URLSearchParams#append should accept 2 params - and if not, that throws an error everywhere - in Node and all browsers:

image

And the error is not specified directly.

@DerekNonGeneric DerekNonGeneric added buffer Issues and PRs related to the buffer subsystem. confirmed-bug Issues with confirmed bugs. repro-exists Issues with reproductions. spooky labels Jan 9, 2022
@VoltrexKeyva
Copy link
Member

VoltrexKeyva commented Jan 9, 2022

As far as I understand, the WHATWG spec for both of the atob() and btoa() functions/methods states that a required argument must be taken and must be a value with the primitive type of string.

As for the structuredClone() function, the value can be anything other than specified C++ natives; so I guess it's already giving out the intended output as passing nothing falls back to the undefined value, which it's structured clone is undefined, we could also require the user to pass a value regardless of the fallback behavior by checking the amount of arguments they've passed to the function, but I'm -0 on that.

@aduh95
Copy link
Contributor

aduh95 commented Jan 9, 2022

Spec for atob and btoa: https://html.spec.whatwg.org/multipage/webappapis.html#dom-atob-dev

  • Firefox: TypeError: Window.atob: At least 1 argument required, but only 0 passed
  • Safari: TypeError: Not enough arguments
  • Chromium: TypeError: Failed to execute 'atob' on 'Window': 1 argument required, but only 0 present.
  • Deno: DOMException: Failed to decode base64.

Spec for structuredClone: https://html.spec.whatwg.org/multipage/structured-data.html#dom-structuredclone

  • Firefox: TypeError: Window.structuredClone: At least 1 argument required, but only 0 passed
  • Safari: TypeError: Not enough arguments
  • Chromium: TypeError: Failed to execute 'structuredClone' on 'Window': 1 argument required, but only 0 present.
  • Deno: TypeError: Failed to execute 'structuredClone': 1 argument required, but only 0 present.

It looks like Node.js indeed not aligned with the rest of the ecosystem.

@targos
Copy link
Member

targos commented Jan 10, 2022

I think this is the part of Web IDL that defines what should happen: https://webidl.spec.whatwg.org/#es-overloads

If there is no valid overload for the type of value passed in here, then we throw a TypeError.

@DerekNonGeneric DerekNonGeneric added the errors Issues and PRs related to JavaScript errors originated in Node.js core. label Jan 10, 2022
@benjamingr
Copy link
Member

Sure I'll open a quick PR that throws ERR_INVALID_ARG_TYPE and adds a test good catch

@benjamingr
Copy link
Member

I can also add a fix for structuredClone to the PR but I'd rather make a good-first-issue out of it if everyone is OK with it given how straightforward it is maybe?

@DerekNonGeneric
Copy link
Contributor

DerekNonGeneric commented Jan 11, 2022

@benjamingr, do you think it would be best to add a new ERR_INVALID_ARGS_NUMBER code first, or do you plan to go w/ ERR_MISSING_ARGS? Without the name of this missing argument, it might be tough to determine what to provide to the error for the message text.

If you want me to open a PR to include this ERR_INVALID_ARGS_NUMBER first, it might be best since adding a whole new error code seems a bit much for a GFI, and it can be tricky.

@benjamingr
Copy link
Member

@DerekNonGeneric I think it's important to understand what .code is used for and its importance and I don't think it matters in this case. I just think it's important we should raise a TypeError and atob is for browser compatibility anyway so I doubt anyone is checking .code on anything atob related.

That said, if you feel strongly about this feel free to push whatever code you want on that branch or ask that I change it to ERR_MISSING_ARGS both are fine by me :)

@gioragutt
Copy link
Contributor

I'll be happy to take the structuredClone fix. What would be the error of choice in this case? ERR_MISSING_OPTION('value') or ERR_INVALID_ARG_TYPE('value', [...'everything specified in #1'], value)?

@benjamingr
Copy link
Member

ERR_MISSING_ARGS? I honestly just care that it's a TypeError and is spec compliant :)

@gioragutt
Copy link
Contributor

Oh right, ERR_MISSING_OPTION sounds like an error for a missing key in an options object, ERR_MISSING_ARGS makes more sense 👍🏻

nodejs-github-bot pushed a commit that referenced this issue Jan 24, 2022
PR-URL: #41651
Fixes: #41450
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
Reviewed-By: Mestery <mestery@protonmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
@benjamingr
Copy link
Member

Only a subset was fixed in the PR - so reopening.

@benjamingr benjamingr reopened this Jan 24, 2022
Linkgoron pushed a commit to Linkgoron/node that referenced this issue Jan 31, 2022
PR-URL: nodejs#41651
Fixes: nodejs#41450
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
Reviewed-By: Mestery <mestery@protonmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
nodejs-github-bot pushed a commit that referenced this issue Feb 3, 2022
PR-URL: #41478
Fixes: #41450
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
VoltrexKeyva pushed a commit to VoltrexKeyva/node that referenced this issue Feb 3, 2022
PR-URL: nodejs#41478
Fixes: nodejs#41450
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
ruyadorno pushed a commit that referenced this issue Feb 8, 2022
PR-URL: #41651
Fixes: #41450
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
Reviewed-By: Mestery <mestery@protonmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
ruyadorno pushed a commit that referenced this issue Feb 8, 2022
PR-URL: #41478
Fixes: #41450
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
danielleadams pushed a commit that referenced this issue Mar 2, 2022
PR-URL: #41478
Fixes: #41450
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
danielleadams pushed a commit that referenced this issue Mar 3, 2022
PR-URL: #41478
Fixes: #41450
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
danielleadams pushed a commit that referenced this issue Mar 14, 2022
PR-URL: #41478
Fixes: #41450
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
buffer Issues and PRs related to the buffer subsystem. confirmed-bug Issues with confirmed bugs. errors Issues and PRs related to JavaScript errors originated in Node.js core. repro-exists Issues with reproductions.
Projects
None yet
7 participants