package mailtemplate import ( "errors" "net" "net/url" "strconv" "strings" "lincodex/backend/internal/domain" ) const ( maxPortNumber = 65535 // maxLinkPathLength keeps notification links short enough to read and to // copy from the plain-text part. maxLinkPathLength = 256 ) // tokenLink builds #token=. The token travels only // in the fragment, which browsers never send to a server, so it stays out of // access logs and Referer headers; it is never put in the query. func (renderer *Renderer) tokenLink(linkPath string, token domain.SecretToken) string { link := renderer.origin link.Path = linkPath link.Fragment = "token=" + token.Reveal() return link.String() } // appLink builds for a path inside the application. Only // a rooted path of letters, digits, "-", ".", "_", "~", and "/" is accepted, so // a scheme (javascript:, https:), a host (//evil.example), a backslash, a // percent-encoded byte, a query, or a fragment cannot reach the link, and "." // or ".." segments cannot step out of the cabinet path the caller named. func (renderer *Renderer) appLink(linkPath string) (string, error) { const location = "Renderer.appLink" if linkPath == "" || len(linkPath) > maxLinkPathLength { return "", invalidData(location, "link path must have 1 to 256 characters") } if linkPath[0] != '/' { return "", invalidData(location, "link path must be a path inside the application that starts with /") } for index := range len(linkPath) { if !isPathCharacter(linkPath[index]) { return "", invalidData(location, "link path may contain only letters, digits, -, ., _, ~, and /") } } for segment := range strings.SplitSeq(linkPath[1:], "/") { if segment == "" || segment == "." || segment == ".." { return "", invalidData(location, "link path must not contain empty, . or .. segments") } } link := renderer.origin link.Path = linkPath return link.String(), nil } func isPathCharacter(character byte) bool { isLetter := ('a' <= character && character <= 'z') || ('A' <= character && character <= 'Z') isDigit := '0' <= character && character <= '9' return isLetter || isDigit || strings.IndexByte("-._~/", character) >= 0 } // parsePublicOrigin applies the APP_PUBLIC_URL rules of the config package // again, because links in mail are a trust boundary of their own: https, or // http only for a loopback host; a valid host and port; no credentials, path, // query, or fragment. One trailing slash is dropped, as config does. func parsePublicOrigin(raw string) (url.URL, error) { const location = "mailtemplate.parsePublicOrigin" raw = strings.TrimSuffix(strings.TrimSpace(raw), "/") origin, err := url.Parse(raw) if err != nil || origin.Hostname() == "" || !validHost(origin.Hostname()) { return url.URL{}, errors.New(location + " - public origin must be an absolute URL with a valid host") } switch { case origin.Scheme == "https": case origin.Scheme == "http" && isLoopbackHost(origin.Hostname()): default: return url.URL{}, errors.New(location + " - public origin must use https; http is allowed only for localhost, 127.0.0.1, or [::1]") } if !isBareOrigin(origin, raw) { return url.URL{}, errors.New(location + " - public origin must not contain credentials, a path, query, or fragment") } if !validPort(origin) { return url.URL{}, errors.New(location + " - public origin must have a valid nonzero port") } return url.URL{Scheme: origin.Scheme, Host: origin.Host}, nil } // isBareOrigin reports a URL with nothing after the authority. raw is checked // for "#" because url.Parse drops an empty fragment. func isBareOrigin(origin *url.URL, raw string) bool { hasCredentials := origin.User != nil hasPath := origin.Path != "" || origin.RawPath != "" || origin.Opaque != "" hasQuery := origin.RawQuery != "" || origin.ForceQuery hasFragment := origin.Fragment != "" || strings.Contains(raw, "#") return !hasCredentials && !hasPath && !hasQuery && !hasFragment } func validHost(host string) bool { if net.ParseIP(host) != nil { return true } for label := range strings.SplitSeq(host, ".") { if label == "" || strings.HasPrefix(label, "-") || strings.HasSuffix(label, "-") { return false } for _, character := range label { isLetter := ('a' <= character && character <= 'z') || ('A' <= character && character <= 'Z') isDigit := '0' <= character && character <= '9' if !isLetter && !isDigit && character != '-' && character != '_' { return false } } } return true } func validPort(origin *url.URL) bool { port := origin.Port() if port == "" { return !strings.HasSuffix(origin.Host, ":") } number, err := strconv.Atoi(port) return err == nil && number >= 1 && number <= maxPortNumber } func isLoopbackHost(host string) bool { return strings.EqualFold(host, "localhost") || host == "127.0.0.1" || host == "::1" }