-
Notifications
You must be signed in to change notification settings - Fork 9k
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
Swagger UI should work without Node.js polyfills - rewrite generateCodeVerifier to avoid transitive dependency on Buffer (Node.js API) #7898
base: master
Are you sure you want to change the base?
Conversation
The old code used to rely on the "randombytes" package, which depends on Buffer being present. This requires a polyfill to work in browser. By doing bit of hand coding, we can solve this ourselves.
I added a unittest that ensures that we don't throw when decoding base64 - which passes. |
@teodorlu Thanks for the PR! SwaggerUI had a recent patch update that changed the build system to webpack@5. One of the benefits to this change was to remove extraneous Node.js dependencies and polyfills. However, the caveat is that Node.js polyfills are no longer included by default by webpack@5. This is true for any project based on webpack@5, not just SwaggerUI.
All that said, I think that it can't be guaranteed that SwaggerUI could and would remain free of any Node.js polyfills or fallbacks moving forward. So while I'm open to replacing I'm not familiar with Vite.js or its requirements, but I can say that I've been able to add dependencies to CreateReactApp@5-based projects. CreateReactApp@5 was released a couple months ago, and is also built with webpack@5. I hope this helps. Let me know, thanks. |
Thanks for taking the time to give a solid response. It looks like polyfills in Swagger UI is a bigger piece of work than I imagined. If we merged this PR, released and I tried again, we'd probably just hit another polyfill issue. A first question is whether Swagger UI should be supported from a Vite.js application. If yes, how? I decided to migrate my application from react-scripts 4 to Vite for build performance. I saw build times decrease by a factor of about 10. Under the hood, Vite relies on esbuild rather than webpack. So something that works out of the box with Webpack doesn't necessarily work with Vite. And you have to think about backwards compatibility with a large user base too. You'll also have to think about testing, and if you decide to support Vite, I guess you'll want CI checks in place to ensure that Swagger UI keeps on working with Vite. I think a better place to start is a minimal working example of my build issues with Swagger UI on Vite. Then perhaps some docs for how to configure the correct polyfills. I'll see what I can do. Thanks again -- Swagger UI is of great help to me and my team! Teodor |
Here's a minimal example that reproduces the load issues for SwaggerUI on a Vite app - with stacktrace pointing to I'm going to see what I can do on my end with polyfills. I'm still not super faimilar with Node.js javascript development, so I have some things to learn. I'll report back if I have any meaningful things to contribute to docs. |
@teodorlu While we wouldn't directly support Vite.js at this time, I would happy to merge in additional documentation for Vite.js users. |
Hi guys, I've looked into the Vite.js briefly and here are my observations:
It's just another build systems that does the same thing but in different way. @teodorlu I've invested couple of minutes trying to amend your vite config to support
|
I agree that Swagger UI needs to keep its scope narrow enough to maintain. Supporting everything in the world doesn't work. Yet - I'd argue that removing dependencies is a good thing. If we don't really need the dependency on Caveat: I haven't worked through removing the dependency to uncover whether that produces more problems. Meaning that this PR isn't done (yet). Landing it and removing the dependency on |
Sure, it's hard to oppose this argument - so I do agree. If we can replace vendor library with a native JavaScript alternative or replace the library with simple custom implementation (let's do it).
Right, go ahead with your PR and let's see where we get. |
@char0n - thanks for the response. Possible further steps:
I have a few other things I need to do as well at work - but I'll see what I can do. It would help us if we can remove some vulnerable Swagger UI dependencies, and fixing that upstream (here) is better for everyone. |
Hi 🙂 We're getting the exact same problem in our Vue 2 webpack 5 app with versions newer than 4.5.2. Adding the buffer polyfill to the webpack config in vue.config.js doesn't fix the issue, so if you can remove it without issues, that'd be great! |
Hi @BeMoreDog! I haven't had the time to move this further. In the meantime, here's a snippet from our // Polyfill the Node.js Buffer API for use in Swagger UI's transitive dependencies
import { Buffer } from "buffer";
declare global {
interface Window {
Buffer: any;
}
}
window.Buffer = Buffer; |
I've produces #7946 that deals with Node.js polyfills and other problems in main SwaggerUI fragment. If you continue with this PR, please pay attention also to webpack config in the PR which needs to be compensated after dependency changes. |
const arr = [] | ||
const maxValue = Math.pow(2,8) - 1; | ||
for (var i = 0; i < 32; ++i) { | ||
const randomByte = Math.floor(Math.random() * maxValue) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentionned here randomBytes() should be preferred over Math.random() for any use related to security .
This PR rewrites generateCodeVerifier to not require the "randombytes" package.
Motivation and Context
As of today, I cannot use
swagger-ui-react
in my Vite.js application without polyfills. When I try to build, I error out on a missingBuffer
insafe-buffer
.src/core/utils.js
depends onrandombytes
, which depends onsafebuffer
.safebuffer
usesBuffer
- which is a Node.js API, and requires polyfilling outside of Node.js.I'd like to avoid having to polyfill.
Additional changes may be required to make
swagger-ui-react
work out of the box with a non-polyfilled application, but this is a start. I'm making this as a PR to make a specific suggestion -- and hear whether avoiding polyfills is something you're interested in.There are some references to
randombytes
in model-example.jsx, but that looks like example code to me.How Has This Been Tested?
generateCodeVerifier
is still passinggenerateCodeVerifier
is a valid base64 string.My PR contains...
src/
is unmodified: changes to documentation, CI, metadata, etc.)package.json
)randombytes
fromsrc/core/utils.js
src/core/components/model-example.jsx
still usesrandombytes
. That looks like example code to me. I'm hoping it's possible to replace the sample code with something else, so that we can remove the dependency onrandombytes
. Suggestions welcome from maintainers that are more familiar with the Swagger UI codebase than I.randombytes
in package.json (yet)My changes...
Documentation
Automated tests