Validates return_to URLs against a whitelist of hosts.
// Source: https://github.com/Mininglamp-OSS/octo-server | License: Apache-2.0 | Copyright You must retain, in the Source form of any Derivative Works
// Provided via justclaw.it.com; keep this notice with the code.
package oidc
import (
"errors"
"fmt"
"net/url"
"strings"
)
// ErrReturnToRejected return_to 校验失败(空、非法 URL、host 不在白名单)
var ErrReturnToRejected = errors.New("oidc: return_to rejected")
// ValidateReturnTo 校验 callback 完成后跳转地址是否合法。
//
// 规则(防开放重定向):
// 1. 空字符串 -> 视为合法,调用方应回退到默认页(返 "" 表示无跳转)
// 2. 相对路径(以 / 开头,不含 //)-> 合法,host 由前端域名决定
// 3. 绝对 URL -> 必须是 http/https,且 host 命中白名单
//
// 任何其他形态(scheme-less // 形式、javascript:、data: 等)一律拒绝。
func ValidateReturnTo(raw string, allowedHosts []string) (string, error) {
if raw == "" {
return "", nil
}
// 相对路径(以单斜杠开头但不是 // 协议相对路径)
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search for more like it.
Get a free API key