c.redirect() merges the request's query into same-site paths, with no opt-out

Aperta
#648 0 commenti 1 reazione 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
35/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
typescript

Direzione di ricerca

Start in runtime/routing.mjs at the c.redirect wrapper, then trace redirectLocation() and mergeQueryIntoLocation(). First determine which proposal to implement: the issue lists changing config redirects, adding an opt-out, or documenting a workaround. Done means the chosen behavior is covered by tests for same-site paths and existing config redirects.

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

Descrizione

Summary

c.redirect('/path') adds the incoming request's query string to the destination. This is documented and makes sense for config redirects, where a moved page should keep its query. In handler, loader and middleware code, though, the destination is usually built on purpose, and the extra params are unexpected. There's currently no way to turn it off.

Reproduction
// pages/sign-in.server.ts
import { defineHandler } from 'void';
import { getUser } from 'void/auth';

export const loader = defineHandler((c) => {
  // Signed-in user visiting /sign-in?redirectTo=/notes
  if (getUser()) return c.redirect('/notes');
  return {};
});
Request Code Expected Location Actual Location
GET /sign-in?redirectTo=/notes c.redirect('/notes') /notes /notes?redirectTo=/notes
GET /notes?x=1 c.redirect('/sign-in?redirectTo=%2Fnotes') /sign-in?redirectTo=%2Fnotes /sign-in?redirectTo=%2Fnotes&x=1
GET /any?token=abc (middleware) c.redirect('/banned') /banned /banned?token=abc

The merge happens in runtime/routing.mjs (c.redirect wrapper → redirectLocation() → mergeQueryIntoLocation()) for any string that starts with a single /.

Why it matters
  • Return-to-page flows break. The "send the user back after sign-in" pattern keeps the return path in the query, and then the redirect back carries that query along.
  • Values leak into the next URL. Whatever was in the query (tokens, error codes, tracking params) follows the user into the next URL, its history entry and the request logs.
  • It's easy to miss. It's documented under the c.rewrite() reference, not where someone looking up redirects would expect it. Hono's own c.redirect() doesn't do this.
Proposal

In order of preference:

  1. Merge only for config redirects (routing.redirects). c.redirect() in code sends exactly the location it's given.
  2. Add an opt-out, e.g. c.redirect('/notes', { query: false }) or a separate c.redirectExact(). This doesn't break existing apps.
  3. At minimum, document the workaround: an absolute URL is sent unchanged, so c.redirect(new URL('/notes', c.req.url).href) skips the merge. Mention it on the redirect docs, not only under rewrites.
Prior art

Next.js makes the split in option 1:

  • redirects in next.config.js pass the request's query through to the destination.
  • redirect() from next/navigation and NextResponse.redirect() in middleware go to exactly the given URL.
Workaround
export function redirect(c: CloudContext, path: string) {
  return c.redirect(new URL(path, c.req.url).href);
}
Environment
  • void 1.4.0
Lingua principale
TypeScript
Stelle
627
Fork
66
Merge medio
12h 56m
PR unite (30g)
162

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 voidzero-dev/void

Tutte le issue di voidzero-dev/void

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.