Skip to content

Unable to send request URL path with dot-segments (without resolving) #8657

Open
@jacob-pro

Description

@jacob-pro

It doesn't seem to be possible to send a URL with raw / unresolved dot-segments.

The problem happens even when passing a Java String or Java URL. How to reproduce:

@Test
void testUrl() throws Exception {
    ServerSocket serverSocket = new ServerSocket(0, 50);
    try (val executor = Executors.newVirtualThreadPerTaskExecutor()) {
        executor.submit(() -> {
            try {
                val socket = serverSocket.accept();
                try (var is = socket.getInputStream(); var os = socket.getOutputStream()) {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                    var firstLine = reader.readLine(); // e.g. 'GET /http/foo HTTP/1.1'
                    System.out.println("Request Line: " + firstLine);
                    socket.close();
                }
            } catch (IOException e) {
                // ignored
            }
        });
        String url = "http://localhost:" + serverSocket.getLocalPort() + "/abc/../123";
        Request request = new Request.Builder().url(url).build();
        OkHttpClient client = new OkHttpClient().newBuilder().build();
        try (var response = client.newCall(request).execute()) {
            System.out.println(response);
        }
    }
}

Outputs:

Request Line: GET /123 HTTP/1.1

Expected:

Request Line: GET /abc/../123 HTTP/1.1

Similar problems:

  1. OkHttpClient automatically replaces a \ with a / in the URL
  2. OkHttpClient automatically escapes non-standard URL characters such as |<>"

An example reason this could be a problem is if you are using untrusted / external input to build a URL then you don't want to risk allowing access to an unexpected web-server directory via .. or an unexpected \ to / conversion.

Is there anyway to disable this URL normalisation behaviour?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugBug in existing code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions