question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

OpenAPI URL ignores X-Forwarded headers

See original GitHub issue

I am using 1.3.2.Final and trying to run the registry behind an Nginx proxy. Setting the X-Forwarded header variables per #513 does not work for two reasons. The first is that the application sends a 302 redirect for “/ui” to “/ui/”. That redirect ignores the X-Forwarded variables and sets the scheme back to http:

Location: http://xxxxxxxx/ui/

Adding a redirect (rewrite ^/ui$ /ui/ redirect;) to the Nginx config gets past that. I don’t see any issues with doing so.

The second reason is that browsing to https://xxxxxxxx/api fails because the response includes the HTTP scheme in one of its URLs (http://xxxxxxxx/openapi?format=JSON). The error is similar to the Mixed-Content error described in #513.

Mixed Content: The page at ‘https://xxxxxxxx/api’ was loaded over HTTPS, but requested an insecure resource ‘http://xxxxxxxx/openapi?format=JSON’. This request has been blocked; the content must be served over HTTPS.

It seems like generateSpecUrl in app/src/main/java/io/apicurio/registry/ui/servlets/SpecUrlFilter.java is forcing the http prefix.

/**
 * Generates a URL that the caller can use to access the API.
 * @param request
 */
private String generateSpecUrl(HttpServletRequest request) {
    try {
        String url = request.getRequestURL().toString();
        url = new URI(url).resolve("/openapi?format=JSON").toString();
        return url;
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }

The url creation doesn’t test for the scheme like generateApiUrl in app/src/main/java/io/apicurio/registry/ui/servlets/ConfigJsServlet.java. Adding that test and update logic to generateSpecUrl doesn’t work because request.isSecure() is returning false in this method. I whacked it just to confirm that updating the scheme in this method does resolve the Mixed Content error:

/**
 * Generates a URL that the caller can use to access the API.
 * @param request
 */
private String generateSpecUrl(HttpServletRequest request) {
    try {
        String url = request.getRequestURL().toString();
        url = new URI(url).resolve("/openapi?format=JSON").toString();
        if (url.startsWith("http:") /*&& request.isSecure()*/) {
            url = url.replaceFirst("http", "https");
        }
        return url;
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }

This works in my environment, but obviously would break environments using HTTP only. It should find out why the request.isSecure() is returning false.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
EricWittmanncommented, Mar 2, 2021

But it might be better to wait for 2.0.0.CR1 which is schedule for Thursday.

Read more comments on GitHub >

github_iconTop Results From Across the Web

X-Forwarded Headers not used #1612 - GitHub
Describe the bug Getting "Failed to load remote configuration." when accessing swagger-ui behind api gateway. Have gone through #215, ...
Read more >
Springdoc OpenAPI ui does not honor context-path in "location"
Okay so the issue is that springdoc-openapi-ui is unaware of your app context path unless it is defined in spring boot, which may...
Read more >
F.A.Q - Springdoc-openapi
How can I ignore @AuthenticationPrincipal parameter from spring-security ? ... There are also non-standard headers, like X-Forwarded-Host , X-Forwarded-Port ...
Read more >
OpenAPI extensions | Cloud Endpoints with OpenAPI
The backend requires the original Authorization header from the API client and cannot use X-Forwarded-Authorization (described in the jwt_audience section). In ...
Read more >
Proxy configuration | Sanic Framework
All other proxy headers are ignored once a trusted forwarded element ... X-Forwarded-For ... Sanic uses the following headers for URL parts:.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found