Skip to content

Commit

Permalink
Merge pull request #761 from Janek91/UrlValidationFix
Browse files Browse the repository at this point in the history
Url validation fix
  • Loading branch information
tmenier authored Sep 6, 2023
2 parents 9b77721 + dae613a commit e35478c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions Test/Flurl.Test/Http/RealHttpTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public async Task decompresses_automatically(string encoding, string jsonKey) {
[TestCase("https://httpbin.org/response-headers", "attachment", null, "response-headers")]
public async Task can_download_file(string url, string contentDisposition, string suppliedFilename, string expectedFilename) {
var folder = Path.Combine(Path.GetTempPath(), $"flurl-test-{Guid.NewGuid()}"); // random so parallel tests don't trip over each other
Directory.CreateDirectory(folder);

try {
var path = await url.SetQueryParam("Content-Disposition", contentDisposition).DownloadFileAsync(folder, suppliedFilename);
Expand Down
6 changes: 5 additions & 1 deletion Test/Flurl.Test/UrlBuilder/UtilityMethodTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand Down Expand Up @@ -74,6 +74,10 @@ public void can_encode_and_decode_very_long_value() {
[TestCase("blah", false)]
[TestCase("http:/www.mysite.com", false)]
[TestCase("www.mysite.com", false)]
[TestCase("https://example.com/%C3%A5%2A", true)]
[TestCase("https://example.com/%E0%B8%95%E0%B8%B1%2A", true)]
[TestCase("https://example.com:999/api/path.cgi?u=uname&p=P%C3%B3%5E78", true)]
[TestCase("https://example.com:999/api/path.cgi?u=uname&p=P%C378", true)]
public void IsValid_works(string s, bool isValid) {
Assert.AreEqual(isValid, Url.IsValid(s));
}
Expand Down
14 changes: 13 additions & 1 deletion src/Flurl/Url.cs
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,19 @@ public static string EncodeIllegalCharacters(string s, bool encodeSpaceAsPlus =
/// </summary>
/// <param name="url">The string to check</param>
/// <returns>true if the string is a well-formed absolute URL</returns>
public static bool IsValid(string url) => url != null && Uri.IsWellFormedUriString(url, UriKind.Absolute);
public static bool IsValid(string url) {
if (url is null)
return false;

if (!Uri.TryCreate(url, UriKind.Absolute, out var result))
return false;

//Non-Ascii characters combined with URI reserved characters make IsWellFormedUriString return false
//even though url is correct https://stackoverflow.com/a/62411610/2380881
//IsWellFormedUriString is only needed to check if there is no path and query in url
return !string.IsNullOrEmpty(result?.PathAndQuery) || Uri.IsWellFormedUriString(url, UriKind.Absolute);
}

#endregion
}
}

0 comments on commit e35478c

Please sign in to comment.