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

Correct protocol on context.request.url when behind reverse proxy #246

Open
elf-pavlik opened this issue Aug 9, 2024 · 1 comment
Open

Comments

@elf-pavlik
Copy link

elf-pavlik commented Aug 9, 2024

I started running the SAI service behind a reverse proxy, which terminates HTTPS. I'm not sure how to get the correct context.request.url. I get http instead of https.

In CSS they use the 'X-Forwarded-Proto' header on the request. As shown in this snippet:

https://github.com/solid/solidproject.org/wiki/Using-NGINX-as-a-reverse-proxy#configuration

Also seen in code: https://github.com/CommunitySolidServer/CommunitySolidServer/blob/4599bf413e0ca08725d22b5e01fe43e97ef7d714/src/http/input/identifier/OriginalUrlExtractor.ts#L46-L53

I wasn't able to find how the same can be achieved in handlersjs.

After a quick look at the code, I found one place where http seems to be hardcoded

const urlObject: URL = new URL(url.replace(/^\/+/, '/'), `http://${nodeHttpStreams.requestStream.headers.host}`);

@elf-pavlik
Copy link
Author

elf-pavlik commented Aug 9, 2024

For now, I just added another layer to the 🧅

import type { Observable } from 'rxjs';
import { getLogger } from '@useid/handlersjs-logging';
import type {
  HttpHandlerResponse,
  HttpHandler ,
  HttpHandlerContext
} from '@useid/handlersjs-http';


export class XForwardedProtoHandler implements HttpHandler {

  public logger = getLogger();

  /**
   * Checks HTTP X-Forwarded-Proto header, if needed updates protocol of context.request.url
   */
  constructor(
    private nestedHandler: HttpHandler
  ) {

    if (!nestedHandler) {

      throw new Error('A HttpHandler must be provided');

    }

  }

  handle(context: HttpHandlerContext): Observable<HttpHandlerResponse> {

    const headerValue = context.request.headers[`x-forwarded-proto`]
    const proto = headerValue.trim().replace(/\s*,.*/u, '')

    this.logger.debug('X-Forwarded-Proto: ', proto);
    
    if (proto === 'https') {
      context.request.url.protocol = 'https:'
    }

    return this.nestedHandler.handle(context)
  }

}

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

1 participant