Zenn
Open8

Rails (というかRack::Request) で `X-Forwarded-Host` に空文字を指定してリクエストを行うとエラーになる場合があるっぽい?

oieioioieioi

x-forwarded-host を 空文字でリクエストすると起きるっぽい。
使用するHTTPクライアントによっては空文字のヘッダはそもそもリクエストに含めなかったりするので、HTTPクライアントを確認する必要あり。

curl は 空ヘッダは自動で削除するっぽい

$ curl -v "http://localhost/test" --header "x-forwarded-host: "

JavaScriptのfetchはしないっぽい

let headers = new Headers
headers.set('x-forwarded-host', ``)

let res = await fetch('http://localhost/test', { method: 'GET', headers: headers})
res.status
let text = await res.text()
oieioioieioi

RailsとRackの最小構成アプリを作って、request.authority をコールして再現させる

アプリケーションが request.authority をコールしなくても、Datadog を使ってる場合、instrumentation の過程で上記のnilアクセスが発生するメソッド(Rack::Request#base_url)を呼ぶっぽい

oieioioieioi

↑のPRで curl でも再現可能ということを教えてもらった。 comment
-H で : をつかわずに ; で終わると 空で送信できる。

$ curl -s -v -H 'x-forwarded-host;' localhost:8888

man にも書いてあった。

$ man curl
...
If you send the custom header with no-value then its header must
be terminated with a semicolon, such as -H "X-Custom-Header;" to send "X-Custom-Header:".
ログインするとコメントできます