Skip to content
quarry Artifact [522cc14de0]
Login

Artifact [522cc14de0]

Artifact 522cc14de075e7c51528b3cc84f77d4c25a910fd8d66fff4949476b097483203:


package web

import "testing"

// safeNext guards the post-login redirect. Anyone who can choose ?next=
// can otherwise land a freshly-signed-in reader on their own page, which
// is how a login form becomes an open redirect — and the reader is at
// their most trusting on the hop straight after authenticating.
func TestSafeNextRefusesOffSiteTargets(t *testing.T) {
	for _, bad := range []string{
		"https://evil.example/steal",
		"//evil.example/steal", // scheme-relative: a browser treats this as off-site
		"http://evil.example",
		"/ok\nSet-Cookie: x=1", // header injection through the redirect
		"/ok\r\nLocation: https://evil.example",
		"javascript:alert(1)",
		"",
	} {
		if got := safeNext(bad); got != "/" {
			t.Errorf("safeNext(%q) = %q, want \"/\"", bad, got)
		}
	}
	for _, ok := range []string{
		"/r/alpha/wiki/Home",
		"/r/alpha/ticket/abc/edit",
		"/",
	} {
		if got := safeNext(ok); got != ok {
			t.Errorf("safeNext(%q) = %q, want it unchanged", ok, got)
		}
	}
}