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

refactor (provider-utils): copy relevant code from secure-json-parse into codebase #5566

Closed
wants to merge 14 commits into from

Conversation

gr2m
Copy link
Contributor

@gr2m gr2m commented Apr 6, 2025

Similar to #5543.

@lgrammel and I discussed that secure-json-parse could be removed as dependency in order to further reduce the number of external dependencies.

Todos

  • add tests

gr2m added 8 commits April 5, 2025 18:46
- `@ai-sdk/secure-json-parse` is not using a custom reviver or options, so I removed the two arguments
- I simplified the regexes as we only target modern JS runtime environment and shouldn't encounter problems with unicode support. I made the change in the original package to make sure tests are passsing. I also ran the benchamrs with the original and simpilified regexes to make sure there are no regressions.

Benchmarks original code:

```
> node valid.js

secure-json-parse parse x 1,721,006 ops/sec ±0.29% (101 runs sampled)
secure-json-parse parse proto x 1,839,405 ops/sec ±0.27% (100 runs sampled)
secure-json-parse safeParse x 1,681,051 ops/sec ±1.42% (93 runs sampled)
secure-json-parse safeParse proto x 1,106,173 ops/sec ±1.47% (96 runs sampled)

> node ignore.js

secure-json-parse parse x 1,124,978 ops/sec ±1.13% (93 runs sampled)
secure-json-parse safeParse x 1,094,985 ops/sec ±0.50% (95 runs sampled)

> node no__proto__.js

secure-json-parse parse x 1,019,390 ops/sec ±0.83% (92 runs sampled)
secure-json-parse safeParse x 1,046,083 ops/sec ±0.23% (97 runs sampled)

> node remove.js

secure-json-parse parse x 618,079 ops/sec ±1.08% (97 runs sampled)
secure-json-parse safeParse x 1,071,897 ops/sec ±0.28% (94 runs sampled)

> node throw.js

secure-json-parse parse x 399,504 ops/sec ±0.34% (98 runs sampled)
secure-json-parse safeParse x 473,815 ops/sec ±0.33% (92 runs sampled)
```

Benchmarks simplified code:

```
> node valid.js

secure-json-parse parse x 1,689,387 ops/sec ±0.69% (99 runs sampled)
secure-json-parse parse proto x 1,785,980 ops/sec ±0.94% (96 runs sampled)
secure-json-parse safeParse x 1,678,833 ops/sec ±0.78% (96 runs sampled)
secure-json-parse safeParse proto x 1,119,962 ops/sec ±0.48% (96 runs sampled)

> node ignore.js

secure-json-parse parse x 1,116,215 ops/sec ±0.27% (96 runs sampled)
secure-json-parse safeParse x 1,090,075 ops/sec ±0.86% (94 runs sampled)

> node no__proto__.js

secure-json-parse parse x 1,063,566 ops/sec ±0.23% (99 runs sampled)
secure-json-parse safeParse x 1,067,426 ops/sec ±0.24% (95 runs sampled)

> node remove.js

secure-json-parse parse x 645,046 ops/sec ±0.45% (92 runs sampled)
secure-json-parse safeParse x 1,108,105 ops/sec ±1.67% (96 runs sampled)

> node throw.js

secure-json-parse parse x 389,421 ops/sec ±1.82% (94 runs sampled)
secure-json-parse safeParse x 473,508 ops/sec ±0.37% (93 runs sampled)
```
Comment on lines 28 to 31
// utf8 BOM checker (https://github.com/fastify/secure-json-parse/pull/5)
if (text && text.charCodeAt(0) === 0xfeff) {
text = text.slice(1);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this is relevant in the context of @ai-sdk/provider-utils

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're right, lets remove this.

@lgrammel
Copy link
Collaborator

lgrammel commented Apr 6, 2025

Oh didn't realize it's fairly simple. Can you add tests as well (esp regarding the security issues it resolves), in case they have them?

@gr2m
Copy link
Contributor Author

gr2m commented Apr 6, 2025

Can you add tests as well (esp regarding the security issues it resolves)

ha, good point, will do!

@gr2m gr2m marked this pull request as draft April 6, 2025 19:35
@gr2m gr2m marked this pull request as ready for review April 6, 2025 20:26
Comment on lines +83 to +89
const { stackTraceLimit } = Error;
Error.stackTraceLimit = 0;
try {
return _parse(text);
} finally {
Error.stackTraceLimit = stackTraceLimit;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gr2m do you know what this is for? I usually don't like messing w/ globals - but this might be for security reasons?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's for performance reasons, see fastify/secure-json-parse#90. Let me know if you want to keep it or not, I will add a comment with the pull request URL for future reference.

Fun fact: I know @Uzlopak, the author of that PR quite well, he is helping co-maintain @probot and @octokit for years now.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's keep it - might be worth adding a comment that this is for performance reasons

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah nevermind the comment is there

@lgrammel
Copy link
Collaborator

lgrammel commented Apr 9, 2025

Continued in #5622

@lgrammel lgrammel closed this Apr 9, 2025
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.

2 participants