fastify / fastify-swagger-ui

Serve Swagger-UI for Fastify
MIT License
142 stars 42 forks source link

Regression in v4.0.0 when hosted behind a path rewriting reverse proxy #162

Closed Hornwitser closed 3 months ago

Hornwitser commented 3 months ago

Prerequisites

Last working version

3.1.0

Stopped working in version

4.0.0

Node.js version

18.x

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

Debian 11

💥 Regression Report

When a reverse proxy rewrites the URL path before forwarding the request to the Node.js server running swagger-ui the interface gets confused and the Swagger UI becomes inoperable.

For example when a GET /api/documentation request is remapped to GET /documentation before being passed on to swagger-ui v4.0.0 the resulting html page it responds to contains resources links to /documentation/static/<resource>, which will not load as the server hosts these resources under /api/documentation/static/<resource>. Trying to access /api/documentation/static/index.html which used to work causes swagger-ui to redirect the browser to /documentation.

In swagger-ui v3.1.0 all links were relative. A GET /api/documentation request remapped to GET /documentation would correctly reply with a relative redirect to ./documentation/static/index.html sending the browser to /api/documentation/static/index.html. This page only used relative links of the form ./<resource> so it wasn't sensitive to the path prefix being remapped.

Steps to Reproduce

Configure a reverse proxy in front of swagger-ui, for example with an Apache ProxyPass directive.

<Location /api/>
    ProxyPass http://localhost:3000/
</Location>

Observe that in v3.1.0 you can access the swagger UI under /api/documentation, while in v4.0.0 it doesn't work at all.

Expected Behavior

Upgrading from 3.1.0 shouldn't cause the swagger UI to break when hosted behind a reverse proxy where the path has been rewritten. Or alternatively, this breakage/new requirement should be clearly documented.

mcollina commented 3 months ago

This wasn't expected, but I guess we should have caught it.

Would you like to send a PR to fix?

mcollina commented 3 months ago

cc @jdhollander

jdhollander commented 3 months ago

@mcollina Thanks for mentioning me, I've made a fix, see what you think...