Repository navigation
Description
rawhttp version:
v0.3.1, the latest release. The pipeline client on main (17eb452) has the same code.
- OS: macOS 26.7.1
- Go: go1.27.4
- Nuclei: dev (9bd87c689)
Current Behavior:
PipelineClient sends a request body with neither Content-Length nor Transfer-Encoding. RFC 9112 §6.3 gives such a request no body: "If this is a request message and none of the above are true, then the message body length is zero". The server then reads the body as the start of the next request on the connection. Post with a strings.Reader body sends it this way, and so does Dor with a retryablehttp.Request whose ContentLength is 3. In the program below, Go's http.ReadRequest reads the first request with an empty body and rejects the second with invalid method "a=1POST".
Nuclei sends pipelined body: requests, and raw: requests without unsafe: true, through Dor, so they reach the server without Content-Length too. A pipelined template with 3 payloads and a body reports Matched: 0 | Errors: 1. Without pipeline: true, the same template reports Matched: 3 | Errors: 0.
Expected Behavior:
Each request with a body carries a Content-Length that matches the body, so the server reads a=1 and a=2 as the bodies of two requests. A Content-Length that the caller sets is sent once, as given.
Steps To Reproduce:
- In an empty directory, run
go mod init reproand save this asmain.go. It accepts one connection, prints each chunk the server reads, and frames the requests withhttp.ReadRequest:package main import ( "bufio" "fmt" "io" "net" "net/http" "strings" "github.com/projectdiscovery/rawhttp" "github.com/projectdiscovery/retryablehttp-go" ) // logReader prints every chunk the server reads from the connection. type logReader struct{ r io.Reader } func (l logReader) Read(p []byte) (int, error) { n, err := l.r.Read(p) if n > 0 { fmt.Printf("server read %q\n", p[:n]) } return n, err } // serve frames requests with net/http's parser and answers each one. func serve(ln net.Listener) { conn, err := ln.Accept() if err != nil { return } defer conn.Close() br := bufio.NewReader(logReader{conn}) for { req, err := http.ReadRequest(br) if err != nil { fmt.Printf("server error: %v\n", err) _, _ = io.WriteString(conn, "HTTP/1.1 400 Bad Request\r\nContent-Length: 0\r\nConnection: close\r\n\r\n") return } body, _ := io.ReadAll(req.Body) fmt.Printf("server request: %s %s, ContentLength=%d, body=%q\n", req.Method, req.URL, req.ContentLength, body) _, _ = io.WriteString(conn, "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n") } } func main() { ln, err := net.Listen("tcp", "127.0.0.1:0") if err != nil { panic(err) } defer ln.Close() go serve(ln) options := rawhttp.DefaultPipelineOptions options.Host = ln.Addr().String() options.MaxConnections = 1 client := rawhttp.NewPipelineClient(options) url := "http://" + options.Host + "/" // Post with a body of known length. resp, err := client.Post(url, "application/x-www-form-urlencoded", strings.NewReader("a=1")) if err != nil { fmt.Printf("client Post: %v\n", err) } else { fmt.Printf("client Post: %d\n", resp.StatusCode) } // Dor with a request whose ContentLength is set, the way nuclei sends it. req, err := retryablehttp.NewRequest(http.MethodPost, url, "a=2") if err != nil { panic(err) } fmt.Printf("client Dor: req.ContentLength=%d\n", req.ContentLength) resp, err = client.Dor(req) if err != nil { fmt.Printf("client Dor: %v\n", err) } else { fmt.Printf("client Dor: %d\n", resp.StatusCode) } }
- Run
go get github.com/projectdiscovery/[email protected], thengo mod tidy, thengo run .:# the same in 2 of 2 runs, and against main (17eb452); PORT is the listener's port $ go run . server read "POST / HTTP/1.1\r\nHost: 127.0.0.1:PORT\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\na=1" server request: POST /, ContentLength=0, body="" client Post: 200 client Dor: req.ContentLength=3 server read "POST / HTTP/1.1\r\nHost: 127.0.0.1:PORT\r\n\r\na=2" server error: invalid method "a=1POST" client Dor: 400
Anything else:
Dor passes req.Header and req.Body to do and drops req.ContentLength (pipelineclient.go:79-86). A retryablehttp.Request keeps the body length in that field, as net/http does, so req.Header has no Content-Length. do builds the request with clientpipeline.ToRequest (pipelineclient.go:119), which leaves Request.AutomaticContentLength false, and nothing in the pipeline client sets it. Request.Write writes Content-Length only when that flag is set and ContentLength() knows the length (clientpipeline/request.go:85-92). ContentLength() knows it only for a *bytes.Buffer or *strings.Reader body (clientpipeline/request.go:49-62), and the header line it would write lacks the trailing \r\n.
The flag has stayed unset since 5b1dd52 added the pipeline client. That commit sent a body of unknown length with Transfer-Encoding: chunked. Commit a5c7bd0 ("fixing read body from pipelined responses") commented that branch out, so from v0.0.2 on, the pipeline client sends every request body without Content-Length or chunked encoding.
In nuclei, a raw: request with unsafe: true goes through DoRaw with a Content-Length that nuclei adds to its headers itself, so it isn't affected. The reproduction in projectdiscovery/nuclei#8256 sets Content-Length in its template for this reason.
How it shows in nuclei
The templates set Connection: keep-alive, because nuclei otherwise sends Connection: close on pipelined requests (projectdiscovery/nuclei#8254).
- Save this as
serve.pyand runpython3 serve.py. It serves 127.0.0.1:18170, prints the bytes it reads on each connection, and frames requests by RFC 9112:import itertools import re import socket import sys import threading # Prints the bytes read on each connection, then frames requests by RFC 9112: # a request without Content-Length has no body. Answers 200 with the body it # read, or 400 and closes the connection when a request line isn't valid. TOKEN = re.compile(rb"[!#$%&'*+\-.^_`|~0-9A-Za-z]+") def read_more(conn, cid): chunk = conn.recv(65536) if chunk: print(f"conn {cid} read {chunk!r}", flush=True) return chunk def handle(conn, cid): buf = b"" with conn: while True: while b"\r\n\r\n" not in buf: chunk = read_more(conn, cid) if not chunk: return buf += chunk head, buf = buf.split(b"\r\n\r\n", 1) lines = head.split(b"\r\n") parts = lines[0].split(b" ") if len(parts) != 3 or not TOKEN.fullmatch(parts[0]): print(f"conn {cid} 400 for request line {lines[0]!r}", flush=True) conn.sendall(b"HTTP/1.1 400 Bad Request\r\nContent-Length: 0\r\nConnection: close\r\n\r\n") return headers = {} for line in lines[1:]: key, _, value = line.partition(b":") headers[key.strip().lower()] = value.strip() length = headers.get(b"content-length", b"none") size = int(length) if length != b"none" else 0 while len(buf) < size: chunk = read_more(conn, cid) if not chunk: return buf += chunk body, buf = buf[:size], buf[size:] print(f"conn {cid} request {parts[1].decode()} Content-Length: {length.decode()} body={body!r}", flush=True) reply = b"body=" + body + b";" conn.sendall(b"HTTP/1.1 200 OK\r\nContent-Length: %d\r\n\r\n%s" % (len(reply), reply)) if headers.get(b"connection", b"").lower() == b"close": return port = int(sys.argv[1]) if len(sys.argv) > 1 else 18170 server = socket.create_server(("127.0.0.1", port)) for cid in itertools.count(1): conn, _ = server.accept() threading.Thread(target=handle, args=(conn, cid), daemon=True).start()
- Save this as
pipe-post.yaml. It matches when the server read the request's own body:id: pipe-post info: name: pipe-post author: repro severity: info http: - method: POST path: - "{{BaseURL}}/?p={{p}}" payloads: p: - "1" - "2" - "3" body: "p={{p}}" headers: Connection: keep-alive pipeline: true matchers: - type: word words: - "body=p={{p}};"
- Run
nuclei -duc -stats -v -t pipe-post.yaml -u http://127.0.0.1:18170twice:$ nuclei -duc -stats -v -t pipe-post.yaml -u http://127.0.0.1:18170 [0:00:00] | Templates: 1 | Hosts: 1 | RPS: 11 | Matched: 0 | Errors: 1 | Requests: 3/3 | Tasks: 1/1 (100%) [WRN] [pipe-post] Could not execute request for http://127.0.0.1:18170: ReadStatusLine: EOF # server, with the User-Agent values cut: conn 1 read b'POST /?p=3 HTTP/1.1\r\nHost: 127.0.0.1:18170\r\nAccept: */*\r\nAccept-Language: en\r\nConnection: keep-alive\r\nUser-Agent: ...\r\n\r\np=3POST /?p=1 HTTP/1.1\r\nHost: 127.0.0.1:18170\r\nAccept-Language: en\r\nConnection: keep-alive\r\nUser-Agent: ...\r\nAccept: */*\r\n\r\np=1POST /?p=2 HTTP/1.1\r\nHost: 127.0.0.1:18170\r\nConnection: keep-alive\r\nUser-Agent: ...\r\nAccept: */*\r\nAccept-Language: en\r\n\r\np=2' conn 1 request /?p=3 Content-Length: none body=b'' conn 1 400 for request line b'p=3POST /?p=1 HTTP/1.1' # run 2: the same stats line and warning; the server reads /?p=2 with an empty body, # then answers 400 for b'p=2POST /?p=3 HTTP/1.1'
Variants of the same block, 2 of 2 runs each:
| Template | Content-Length the server reads |
Matched | Errors |
|---|---|---|---|
pipe-post.yaml |
none | 0 | 1 |
pipe-post.yaml with Content-Length: "3" under headers |
3 | 3 | 0 |
pipe-post.yaml without pipeline: true |
3 | 3 | 0 |
raw: request below, with pipeline: true |
none | 0 | 1 |
raw: request below, with pipeline: true and unsafe: true |
3 | 3 | 0 |
The raw: request replaces method, path, body, and headers:
- raw:
- |-
POST /?p={{p}} HTTP/1.1
Host: {{Hostname}}
Connection: keep-alive
p={{p}}With -debug-req, nuclei prints Content-Length: 3 in each pipelined request it dumps, although the server reads none. Nuclei renders the dump with net/http, which writes the header from ContentLength.
Activity