| | | 1 | | namespace Elsa.ExternalAuthentication.Validation; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Validates a browser return path supplied to the authentication broker. |
| | | 5 | | /// Only client-local absolute paths may be used to prevent open redirects. |
| | | 6 | | /// </summary> |
| | | 7 | | public static class ClientReturnPathValidator |
| | | 8 | | { |
| | | 9 | | public const string DefaultReturnPath = "/"; |
| | | 10 | | public const int MaximumLength = 2048; |
| | | 11 | | |
| | | 12 | | public static bool TryValidate(string? returnPath, out string validatedReturnPath) |
| | | 13 | | { |
| | 40 | 14 | | validatedReturnPath = DefaultReturnPath; |
| | | 15 | | |
| | 40 | 16 | | if (string.IsNullOrWhiteSpace(returnPath) || returnPath.Length > MaximumLength) |
| | 1 | 17 | | return false; |
| | | 18 | | |
| | 39 | 19 | | var candidate = returnPath; |
| | 82 | 20 | | for (var attempt = 0; attempt < 4; attempt++) |
| | | 21 | | { |
| | 41 | 22 | | if (!IsClientLocalPath(candidate)) |
| | 10 | 23 | | return false; |
| | | 24 | | |
| | | 25 | | try |
| | | 26 | | { |
| | 31 | 27 | | var decoded = Uri.UnescapeDataString(candidate); |
| | 31 | 28 | | if (decoded == candidate) |
| | 29 | 29 | | break; |
| | | 30 | | |
| | 2 | 31 | | candidate = decoded; |
| | 2 | 32 | | } |
| | 0 | 33 | | catch (UriFormatException) |
| | | 34 | | { |
| | 0 | 35 | | return false; |
| | | 36 | | } |
| | | 37 | | } |
| | | 38 | | |
| | 29 | 39 | | if (!IsClientLocalPath(candidate)) |
| | 0 | 40 | | return false; |
| | | 41 | | |
| | 29 | 42 | | validatedReturnPath = returnPath; |
| | 29 | 43 | | return true; |
| | 0 | 44 | | } |
| | | 45 | | |
| | 1 | 46 | | public static string GetSafeReturnPath(string? returnPath) => TryValidate(returnPath, out var validatedReturnPath) ? |
| | | 47 | | |
| | | 48 | | public static bool TryValidateForClient(string? returnPath, IReadOnlySet<string> allowedPrefixes, out string validat |
| | | 49 | | { |
| | 31 | 50 | | ArgumentNullException.ThrowIfNull(allowedPrefixes); |
| | | 51 | | |
| | 31 | 52 | | if (!TryValidate(returnPath, out validatedReturnPath)) |
| | 4 | 53 | | return false; |
| | | 54 | | |
| | 27 | 55 | | var path = StripQueryAndFragment(validatedReturnPath); |
| | 54 | 56 | | if (allowedPrefixes.Any(prefix => IsAllowedPrefix(path, prefix))) |
| | 23 | 57 | | return true; |
| | | 58 | | |
| | 4 | 59 | | validatedReturnPath = DefaultReturnPath; |
| | 4 | 60 | | return false; |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | private static bool IsClientLocalPath(string value) |
| | | 64 | | { |
| | 97 | 65 | | return value.StartsWith("/", StringComparison.Ordinal) |
| | 97 | 66 | | && !value.StartsWith("//", StringComparison.Ordinal) |
| | 97 | 67 | | && value.IndexOf('\\') < 0 |
| | 97 | 68 | | && !value.Any(char.IsControl); |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | private static bool IsAllowedPrefix(string path, string prefix) |
| | | 72 | | { |
| | 27 | 73 | | if (!IsClientLocalPath(prefix) || prefix.IndexOfAny(['?', '#']) >= 0) |
| | 0 | 74 | | return false; |
| | | 75 | | |
| | 27 | 76 | | var normalizedPrefix = prefix.Length > 1 ? prefix.TrimEnd('/') : prefix; |
| | 27 | 77 | | return normalizedPrefix == "/" |
| | 27 | 78 | | || string.Equals(path, normalizedPrefix, StringComparison.Ordinal) |
| | 27 | 79 | | || path.StartsWith($"{normalizedPrefix}/", StringComparison.Ordinal); |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | private static string StripQueryAndFragment(string value) |
| | | 83 | | { |
| | 27 | 84 | | var separatorIndex = value.IndexOfAny(['?', '#']); |
| | 27 | 85 | | return separatorIndex < 0 ? value : value[..separatorIndex]; |
| | | 86 | | } |
| | | 87 | | } |