Skip to content

Commit 74c95f5

Browse files
committed
fix(security): reject CRLF and normalize URLs in TurboHttpResponse.Redirect()
1 parent 178c966 commit 74c95f5

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

src/TurboHTTP/Server/Context/TurboHttpResponse.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,22 @@ public override void Redirect(string location, bool permanent = false)
7474
{
7575
ArgumentNullException.ThrowIfNull(location);
7676

77-
if (!location.StartsWith('/') &&
78-
Uri.TryCreate(location, UriKind.Absolute, out var uri) &&
79-
uri.Scheme is not ("http" or "https"))
77+
if (location.AsSpan().ContainsAny('\r', '\n'))
78+
{
79+
throw new ArgumentException("Redirect location must not contain CR or LF characters.", nameof(location));
80+
}
81+
82+
if (!Uri.TryCreate(location, UriKind.RelativeOrAbsolute, out var parsed))
83+
{
84+
throw new ArgumentException("Redirect location is not a valid URI.", nameof(location));
85+
}
86+
87+
if (parsed.IsAbsoluteUri && parsed.Scheme is not ("http" or "https"))
8088
{
8189
throw new ArgumentException("Redirect location must be a relative path or an HTTP/HTTPS URL.", nameof(location));
8290
}
8391

8492
StatusCode = permanent ? 301 : 302;
85-
Headers["Location"] = location;
93+
Headers["Location"] = parsed.IsAbsoluteUri ? parsed.AbsoluteUri : location;
8694
}
8795
}

0 commit comments

Comments
 (0)