Measured on the shipped image (openwaf:ci). POST /admin-api/blocks answered 204 for every one of these, and the next request from that source paid for it: "https://example.com/ok" 301 Location: https://example.com/ok "http://bad target" 301 Location: http://bad target "http://x.invalid/<CRLF>X-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 row is the defect rather than the wart: Kestrel refuses to write the header, so the operator's own block — written with a 204 — became a server error for that visitor. Nothing was injected; the server validated the header, which is the only part of the old behaviour that was safe. javascript: and data: are absolute URIs too, and a redirect is a navigation, so those are an execution request made on the operator's behalf by a string somebody typed into a form. RedirectTarget.IsSendable is now the one judge: absolute, http(s), no control characters or surrounding whitespace. The API answers 400 naming the field, and DecisionEngine ignores a target it cannot send and answers the block's own default instead — for a row that arrived by a restored backup or a hand edit, where the API guard never ran. The console gate also asserts the other half of the same boundary: every value that came from the API is rendered through esc(), and two of those values are not authored by the person reading the page — the path in an observation row is whatever an anonymous visitor asked for, and a block reason is a form field. The seed now sends a percent-encoded path carrying <img onerror=...> and an operator block whose reason carries <svg onload=...>, and the page must show both as text and create no elements from them. unit 105 -> 126 (+21: the target table, and the decision point's fallback) integration 55 -> 62 (+7: 400 by name for five targets, a valid target reaching the wire as 301, and a hand-written row with CRLF answering 301 to the default target instead of the 500 it used to answer) console 33 -> 37 assertions Teeth, each measured by removing the thing: esc() reduced to identity in the shipped page makes the console gate go red with 6 failures, and the page title becomes "XSS" — the anonymous visitor's path executed script in the operator's browser. Dropping the API guard fails exactly 5 integration tests; dropping the decision-point guard fails exactly 6 unit tests and the 1 hand-written-row test.
45 lines
2.0 KiB
C#
45 lines
2.0 KiB
C#
namespace OpenWaf.Domain.Security;
|
|
|
|
/// <summary>
|
|
/// A per-entry redirect target, judged once.
|
|
///
|
|
/// This string is written straight into a <c>Location</c> header, and the
|
|
/// shipped image used to accept any string at all: <c>POST /admin-api/blocks</c>
|
|
/// 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. <c>javascript:</c> and <c>data:</c> 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.
|
|
/// </summary>
|
|
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;
|
|
}
|
|
}
|