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

Aberta
#648 0 comentários 1 reação 0 responsáveis Ver no GitHub

Mantenedores costumam responder em até 1 dia

Ninguém assumiu esta issue ainda.

Avaliação

Dificuldade
5/5
Tempo estimado
Mais de uma semana
Facilidade para iniciantes
35/100
Tipo de issue
Funcionalidade
Clareza
Razoavelmente clara
Status de atividade
Ativa
Stack de tecnologia
typescript

Direção de pesquisa

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.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

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
Linguagem predominante
TypeScript
Estrelas
627
Forks
66
Merge médio
12h 56min
PRs com merge (30d)
162

Preparar o ambiente

Este projeto não oferece contêiner de desenvolvimento, Dockerfile nem guia de contribuição, então a configuração fica por sua conta: comece pelo README e veja nosso guia da primeira contribuição para os passos gerais.

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de voidzero-dev/void

Todas as issues de voidzero-dev/void

Issues semelhantes

Mais issues de TypeScript

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.