Skip to content

fail to proxy when starts with double slash #307

@rluvaton

Description

@rluvaton

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.17.0

Plugin version

9.2.0

Node.js version

20

Operating system

macOS

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

13.4

Description

when the URL starts with double slash the proxy fail with the following errors:

  • if only double slash: Invalid URL
  • if double slash and then route: source must be a relative path string

Steps to Reproduce

Repro

or

import Fastify from 'fastify';
import FastifyPluginHttpProxy from '@fastify/http-proxy';
import {after, before, describe, test} from "node:test";
import {deepStrictEqual} from "node:assert";

let remoteServer;
let proxy;

async function setup() {
    remoteServer = Fastify();

    remoteServer.all('*', async (req) => ({url: req.url}));

    await remoteServer.listen({host: 'localhost', port: 3333});

    proxy = Fastify();

    proxy.register(FastifyPluginHttpProxy, {
        upstream: 'http://localhost:3333',
    });

    await proxy.listen({host: 'localhost', port: 3334});
}

describe('run', () => {

    before(async () => {
        await setup();
    });

    test('should proxy with double slash at the beginning no matter what', async () => {
        const response = await fetch('http://localhost:3334//');

        const body = await response.json();
        deepStrictEqual({status: response.status, body}, {
            status: 200,
            body: {
                url: '//'
            }
        });
    });

    test('should proxy with double slash at the beginning with route after no matter what', async () => {
        const response = await fetch('http://localhost:3334//route');

        const body = await response.json();
        deepStrictEqual({status: response.status, body}, {
            status: 200,
            body: {
                url: '//route'
            }
        });
    });

    after(async () => {
        await Promise.all([remoteServer.close(), proxy.close()]);
    })

});

Expected Behavior

As I see it http-proxy should proxy the URL as is no matter what

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions