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

validators.isObject() uses unreliable toString global function #405

Closed
corvidism opened this issue Apr 9, 2024 · 3 comments
Closed

validators.isObject() uses unreliable toString global function #405

corvidism opened this issue Apr 9, 2024 · 3 comments

Comments

@corvidism
Copy link

Hi,

this is bug report is mostly a PSA because you've already fixed it in the current dev branch, but maybe I'll save someone some time:

The validation added in tough-cookie@4.1.0, specifically the isObject function, fails in an environment that overrides the global toString method - for example, in a Jest test running in the jsdom environment.

What happens

Trying to create a new jar with new CookieJar() throws error [object Object].

Why

When you adapted the validator functions from check-types, you didn't bring over the locally defined toString function. That's fine in theory, the global object has a toString method, but it breaks if something redefines it - for example the jsdom environment, which replaces the default node global with a mocked window, where the toString method always returns undefined.

Temporary fix

Before creating the jar, define window.toString again:

window.toString = Object.prototype.toString;

The upcoming TS refactor should fix this - the new objectToString function uses uses Object.prototype.toString as expected) - but I'd be happy to submit a pull request. I mean. It's literally one line of code :D.

@colincasey
Copy link
Contributor

Thanks for opening this issue and providing a temporary workaround for those who may be affected @corvidism ❤️

We'd gladly accept a PR fix for this. Most of our maintainer focus has been on preparing for the upcoming TS refactor so fixing this in 4.1.x is less of a priority at the moment.

@corvidism
Copy link
Author

Great, I'll do that.

@wjhsf
Copy link
Contributor

wjhsf commented Jun 21, 2024

Fixed in #409.

@wjhsf wjhsf closed this as completed Jun 21, 2024
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

No branches or pull requests

3 participants