build_url_for_request drops the leading slash: logged URL becomes host+path run together (e.g. amazonaws.comv1/messages)

Aperta Adatta ai principianti
#232 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
82/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
rails, ruby
Ambito
devtools

Direzione di ricerca

Inizia da lib/coolhand/net_http_interceptor.rb, in build_url_for_request, dove req.path viene aggiunto a scheme://host senza verificare la presenza di una barra iniziale. Esegui la spec dell'interceptor Net::HTTP e aggiungi casi per un percorso senza barra, un percorso che inizia con ?, una porta non predefinita e un percorso assoluto https:// che deve passare invariato. È completato quando ogni URL registrato ha esattamente una / tra l'authority e il percorso e le spec sugli URL esistenti continuano a passare.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Problem

build_url_for_request appends req.path to scheme://host without making sure the path starts with /. When a request reaches Net::HTTP with a slash-less path, the URL that gets logged has the host and path run together:

https://bedrock-runtime.us-east-1.amazonaws.comv1/messages

Observed in production on coolhand-ruby 0.2.0 with an app calling Anthropic models on Amazon Bedrock (the Anthropic SDK's Bedrock client appears to hand Net::HTTP the pre-rewrite path v1/messages, before it is rewritten to /model/<id>/invoke). The malformed URL is uploaded as raw_request.url. Anything downstream that classifies a request by URL can no longer recognize the provider, so every call from that app is rejected as an unsupported API (thousands per day).

Root cause

lib/coolhand/net_http_interceptor.rb:

def build_url_for_request(http, req)
  return req.path if %r{\Ahttps?://}.match?(req.path)

  scheme = http.use_ssl? ? "https" : "http"
  host = http.address
  ...
  url = "#{scheme}://#{host}"
  url << ":#{port}" if port != default
  url << req.path      # <- no leading-slash guarantee
  url
end

Still the same on main (0.12.0), so upgrading does not fix it.

Proposed fix

path = req.path.to_s
path = "/#{path}" unless path.start_with?("/")
url << path

A path starting with ? becomes /?..., which is the correct URL. Note the same value feeds intercept? / address_matches? path matching via capturable_url, so the normalized URL also makes path-anchored intercept patterns behave consistently.

Tests

Add to the Net::HTTP interceptor spec:

  • Net::HTTP::Post.new("v1/messages") against bedrock-runtime.us-east-1.amazonaws.com → logged URL https://bedrock-runtime.us-east-1.amazonaws.com/v1/messages
  • "/v1/messages" → unchanged (no double slash)
  • "?a=1" → https://host/?a=1
  • non-default port with a slash-less path → https://host:8443/v1/messages
  • absolute https://… path (the early-return branch) → unchanged

Acceptance

  • The logged URL always has exactly one / between authority and path.
  • New specs pass; existing URL specs unchanged.
  • Patch release.
Lingua principale
Ruby
Stelle
11
Fork
2
Merge medio
3g 13h
PR unite (30g)
21

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di Coolhand-Labs/coolhand-ruby

Tutte le issue di Coolhand-Labs/coolhand-ruby

Issue simili

Altre issue su Ruby

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.