Skip to content

Multiple leading slashes in path causes hostname to be parsed incorrectly #3965

@BanovMiroslav

Description

@BanovMiroslav

Steps to reproduce

Urls such as http://localhost:3000//path cause incorrect parsing of the url.

Here is a catch-all route to show the result:

  server.route({
    method: 'GET',
    path: '/{p*}',
    handler: (request, h) => {
      return {
        p_param: request.params.p,
        request_url: request.url,
        request_path: request.path,
        info_hostname: request.info.hostname,
        info_host: request.info.host,
      };
    },
  });

What was the result you received?

{"p_param":"","request_url":"http://path/","request_path":"/","info_hostname":"path","info_host":"path"}

What did you expect?

{"p_param":"/path","request_url":"http://localhost:3000//path","request_path":"//path","info_hostname":"localhost","info_host":"localhost:3000"}

Context

  • node version: v10.16.2
  • hapi version: 18.3.1
  • os: Ubuntu 18.04.3 LTS

Reason

In _setUrl this code relies on native nodejs Url.URL class.

const base = (url[0] === '/' ? `${this._core.info.protocol}://${this.info.host || `${this._core.info.host}:${this._core.info.port}`}` : undefined);

url = new Url.URL(url, base);

Problem is url "//path" is protocol-relative url that has only domain component. So it is considered absolute for the purpose of the call and base is ignored.

Metadata

Metadata

Assignees

Labels

bugBug or defect

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions