namespace OpenWaf.Domain.Security; /// /// A per-entry redirect target, judged once. /// /// This string is written straight into a Location header, and the /// shipped image used to accept any string at all: POST /admin-api/blocks /// answered 204 for every one of these, and the next request from that source /// then paid for it — /// /// "https://example.com/ok" -> 301 Location: https://example.com/ok /// "http://bad target" -> 301 Location: http://bad target (unparseable) /// "http://x.invalid/\r\nX-Injected: 1"-> 500 Internal Server Error, Content-Length: 0 /// "javascript:alert(1)" -> 301 Location: javascript:alert(1) /// "not-a-url" -> 301 Location: not-a-url /// /// The CRLF one is the reason this type exists: Kestrel refuses to write the /// header, so the block that was supposed to protect the site turns into a 500 /// for that visitor — the operator's own entry, accepted with 204, breaks the /// response. No header was injected (the server validates), which is the only /// part of this that went right. /// /// http(s) only. javascript: and data: are absolute URIs and would /// be written into Location happily; a redirect is a navigation, and a /// navigation to a script: URL is an execution request made on the operator's /// behalf by a string somebody typed into a form. /// public static class RedirectTarget { public static bool IsSendable(string? target) => target is not null && IsSendableUri(target); private static bool IsSendableUri(string target) { if (string.IsNullOrWhiteSpace(target)) return false; if (target.Trim() != target) return false; foreach (var c in target) { if (c <= ' ' || c == '\u007f') return false; } if (!Uri.TryCreate(target, UriKind.Absolute, out var uri)) return false; return uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps; } }