Skip to content

passport strategy currentUrl does not consider port number #767

Closed as not planned

Description

@charleshetier

What happened?

I am working on a localhost:8086 express server.

The implementation of currentUrl is creating an url such as http://localhost/xxx instead of http://localhost:8086/xxx

from https://github.com/panva/openid-client/blob/main/src/passport.ts#L412

  currentUrl(req: express.Request): URL {
    return new URL(`${req.protocol}://${req.host}${req.originalUrl ?? req.url}`)
  }

could you consider changing implementation like so instead ? :

  currentUrl(req: express.Request): URL {
    return new URL(`${req.protocol}://${req.get("host")}${req.originalUrl ?? req.url}`)
  }

note the req.get("host")instead of req.host

Or if any compatibility issue with older versions, mayby

  currentUrl(req: express.Request): URL {
    return new URL(`${req.protocol}://${req.get?.("host") ?? req.host}${req.originalUrl ?? req.url}`)
  }

?

For now, I have monkey patched the code this way:

Strategy.prototype.currentUrl = (req) =>
  new URL(`${req.protocol}://${req.get("host")}${req.originalUrl}`);

(it's working)

Charles

Version

6.3.4

Runtime

Node.js

Runtime Details

node v22.13.1, express@4.21.2

Code to reproduce

passport.use(new Strategy(
    {
      config,
      name: "oidc",
      callbackURL: "http://localhost:8086/auth/callback",
    },
    (tokens, verified) => verified(null, tokens.claims()),
  )); 


 // Authentication endpoint
// The sso server complains about http://localhost/auth/callback redirection instead of http://localhost:8086/auth/callback
  app.use(passport.authenticate("oidc", {
      assignProperty: "user",
    }),
    (req, res) => {
      console.log("OIDC Authenticated successfully.");
      res.json({
        user: req.user,
        authInfo: req.authInfo,
      });
    },
  );

Required

  • I have searched the issues tracker and discussions for similar topics and couldn't find anything related.
  • I agree to follow this project's Code of Conduct

Activity

  1. panva commented on Mar 24, 2025

    @panva
    Owner

    Hello @charleshetier

    For now, I have monkey patched the code this way:

    That's fine. Altho you probably only want to do that for localhost development, it is not appropriate to do so in production.

    * This function may need to be overridden by users if its return value does
    * not match the actual URL the authorization server redirected the user to.

    Otherwise, please see #743, #733, #746, and #713. Those are all similar issues that I've already answered on the subject that may explain why the existing implementation is the way it is considering that

    • express@5 is the latest version you should be able to upgrade to (and it fixes the issue on the account of express fixing its req.host getter)
    • I do not intend to work around express@4 bugged behaviours
    • none of your suggestions work in production behind a tls-offloading proxy and do not hook into express behind proxies setups
    • nor do I wish to supplement express' request URL resolution
  2. reopened this on Mar 24, 2025
  3. charleshetier commented on Mar 25, 2025

    @charleshetier
    Author

    Hello @panva , thank you for you clear answer. That's make sense. I'll consider migrating to expressjs@5 as soon as possible. Sorry for the duplication then, and thank you for your great work !

  4. panva commented on Mar 25, 2025

    @panva
    Owner

    No worries.

  5. locked and limited conversation to collaborators on Jun 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions