public static void TryCheckTooManyParameters(TransformRouteValidationContext context, IReadOnlyDictionary <string, string> rawTransform, int expected) { if (rawTransform.Count > expected) { context.Errors.Add(new InvalidOperationException("The transform contains more parameters than expected: " + string.Join(';', rawTransform.Keys))); } }
private static void ValidateAction(TransformRouteValidationContext context, string key, string?headerValue) { if (!Enum.TryParse <ForwardedTransformActions>(headerValue, out var _)) { context.Errors.Add(new ArgumentException($"Unexpected value for {key}: {headerValue}. Expected one of {nameof(ForwardedTransformActions)}")); } }
public bool Validate(TransformRouteValidationContext context, IReadOnlyDictionary <string, string> transformValues) { if (transformValues.TryGetValue(RequestHeadersCopyKey, out var copyHeaders)) { TransformHelpers.TryCheckTooManyParameters(context, transformValues, expected: 1); if (!bool.TryParse(copyHeaders, out var _)) { context.Errors.Add(new ArgumentException($"Unexpected value for RequestHeaderCopy: {copyHeaders}. Expected 'true' or 'false'")); } } else if (transformValues.TryGetValue(RequestHeaderOriginalHostKey, out var originalHost)) { TransformHelpers.TryCheckTooManyParameters(context, transformValues, expected: 1); if (!bool.TryParse(originalHost, out var _)) { context.Errors.Add(new ArgumentException($"Unexpected value for RequestHeaderOriginalHost: {originalHost}. Expected 'true' or 'false'")); } } else if (transformValues.TryGetValue(RequestHeaderKey, out var _)) { TransformHelpers.TryCheckTooManyParameters(context, transformValues, expected: 2); if (!transformValues.TryGetValue(SetKey, out var _) && !transformValues.TryGetValue(AppendKey, out var _)) { context.Errors.Add(new ArgumentException($"Unexpected parameters for RequestHeader: {string.Join(';', transformValues.Keys)}. Expected 'Set' or 'Append'")); } } else { return(false); } return(true); }
public bool Validate(TransformRouteValidationContext context, IReadOnlyDictionary <string, string> transformValues) { if (transformValues.TryGetValue(QueryValueParameterKey, out var queryValueParameter)) { TransformHelpers.TryCheckTooManyParameters(context, transformValues, expected: 2); if (!transformValues.TryGetValue(AppendKey, out var _) && !transformValues.TryGetValue(SetKey, out var _)) { context.Errors.Add(new ArgumentException($"Unexpected parameters for QueryValueParameter: {string.Join(';', transformValues.Keys)}. Expected 'Append' or 'Set'.")); } } else if (transformValues.TryGetValue(QueryRouteParameterKey, out var queryRouteParameter)) { TransformHelpers.TryCheckTooManyParameters(context, transformValues, expected: 2); if (!transformValues.TryGetValue(AppendKey, out var _) && !transformValues.TryGetValue(SetKey, out var _)) { context.Errors.Add(new ArgumentException($"Unexpected parameters for QueryRouteParameter: {string.Join(';', transformValues.Keys)}. Expected 'Append' or 'Set'.")); } } else if (transformValues.TryGetValue(QueryRemoveParameterKey, out var removeQueryParameter)) { TransformHelpers.TryCheckTooManyParameters(context, transformValues, expected: 1); } else { return(false); } return(true); }
public bool Validate(TransformRouteValidationContext context, IReadOnlyDictionary <string, string> transformValues) { if (transformValues.TryGetValue(PathSetKey, out var pathSet)) { TransformHelpers.TryCheckTooManyParameters(context, transformValues, expected: 1); } else if (transformValues.TryGetValue(PathPrefixKey, out var pathPrefix)) { TransformHelpers.TryCheckTooManyParameters(context, transformValues, expected: 1); } else if (transformValues.TryGetValue(PathRemovePrefixKey, out var pathRemovePrefix)) { TransformHelpers.TryCheckTooManyParameters(context, transformValues, expected: 1); } else if (transformValues.TryGetValue(PathPatternKey, out var pathPattern)) { TransformHelpers.TryCheckTooManyParameters(context, transformValues, expected: 1); // TODO: Validate the pattern format. Does it build? } else { return(false); } return(true); }
private void CheckPathNotNull(TransformRouteValidationContext context, string fieldName, string?path) { if (path == null) { context.Errors.Add(new ArgumentNullException(fieldName)); } }
public bool Validate(TransformRouteValidationContext context, IReadOnlyDictionary <string, string> transformValues) { Assert.NotNull(context.Services); Assert.NotNull(context.Route); Assert.NotNull(context.Errors); ValidationCalls++; return(transformValues.TryGetValue(_v, out var _)); }
public void ValidateRoute(TransformRouteValidationContext context) { // Check all routes for a custom property and validate the associated transform data. if (context.Route.Metadata?.TryGetValue("CustomMetadata", out var value) ?? false) { if (string.IsNullOrEmpty(value)) { context.Errors.Add(new ArgumentException("A non-empty CustomMetadata value is required")); } } }
public bool Validate(TransformRouteValidationContext context, IReadOnlyDictionary <string, string> transformValues) { if (transformValues.TryGetValue("CustomTransform", out var value)) { if (string.IsNullOrEmpty(value)) { context.Errors.Add(new ArgumentException("A non-empty CustomTransform value is required")); } return(true); // Matched } return(false); }
public bool Validate(TransformRouteValidationContext context, IReadOnlyDictionary <string, string> transformValues) { if (transformValues.TryGetValue(HttpMethodChangeKey, out var _)) { TransformHelpers.CheckTooManyParameters(transformValues, expected: 2); if (!transformValues.TryGetValue(SetKey, out var _)) { context.Errors.Add(new ArgumentException($"Unexpected parameters for HttpMethod: {string.Join(';', transformValues.Keys)}. Expected 'Set'")); } } else { return(false); } return(true); }
/// <inheritdoc/> public IReadOnlyList <Exception> ValidateRoute(ProxyRoute route) { var context = new TransformRouteValidationContext() { Services = _services, Route = route, }; var rawTransforms = route?.Transforms; if (rawTransforms?.Count > 0) { foreach (var rawTransform in rawTransforms) { var handled = false; foreach (var factory in _factories) { if (factory.Validate(context, rawTransform)) { handled = true; break; } } if (!handled) { context.Errors.Add(new ArgumentException($"Unknown transform: {string.Join(';', rawTransform.Keys)}")); } } } // Let the app add any more validation it wants. foreach (var transformProvider in _providers) { transformProvider.ValidateRoute(context); } // We promise not to modify the list after we return it. return((IReadOnlyList <Exception>)context.Errors); }
public bool Validate(TransformRouteValidationContext context, IReadOnlyDictionary <string, string> transformValues) { if (transformValues.TryGetValue(XForwardedKey, out var xforwardedHeaders)) { var expected = 1; if (transformValues.TryGetValue(AppendKey, out var appendValue)) { expected++; if (!bool.TryParse(appendValue, out var _)) { context.Errors.Add(new ArgumentException($"Unexpected value for X-Forwarded:Append: {appendValue}. Expected 'true' or 'false'")); } } if (transformValues.TryGetValue(PrefixForwardedKey, out var _)) { expected++; } TransformHelpers.TryCheckTooManyParameters(context, transformValues, expected); // for, host, proto, Prefix var tokens = xforwardedHeaders.Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries); foreach (var token in tokens) { if (!string.Equals(token, ForKey, StringComparison.OrdinalIgnoreCase) && !string.Equals(token, HostKey, StringComparison.OrdinalIgnoreCase) && !string.Equals(token, ProtoKey, StringComparison.OrdinalIgnoreCase) && !string.Equals(token, PrefixKey, StringComparison.OrdinalIgnoreCase)) { context.Errors.Add(new ArgumentException($"Unexpected value for X-Forwarded: {token}. Expected 'for', 'host', 'proto', or 'Prefix'")); } } } else if (transformValues.TryGetValue(ForwardedKey, out var forwardedHeader)) { var expected = 1; if (transformValues.TryGetValue(AppendKey, out var appendValue)) { expected++; if (!bool.TryParse(appendValue, out var _)) { context.Errors.Add(new ArgumentException($"Unexpected value for Forwarded:Append: {appendValue}. Expected 'true' or 'false'")); } } var enumValues = "Random,RandomAndPort,Unknown,UnknownAndPort,Ip,IpAndPort"; if (transformValues.TryGetValue(ForFormatKey, out var forFormat)) { expected++; if (!Enum.TryParse <NodeFormat>(forFormat, ignoreCase: true, out var _)) { context.Errors.Add(new ArgumentException($"Unexpected value for Forwarded:ForFormat: {forFormat}. Expected: {enumValues}")); } } if (transformValues.TryGetValue(ByFormatKey, out var byFormat)) { expected++; if (!Enum.TryParse <NodeFormat>(byFormat, ignoreCase: true, out var _)) { context.Errors.Add(new ArgumentException($"Unexpected value for Forwarded:ByFormat: {byFormat}. Expected: {enumValues}")); } } TransformHelpers.TryCheckTooManyParameters(context, transformValues, expected); // for, host, proto, by var tokens = forwardedHeader.Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries); foreach (var token in tokens) { if (!string.Equals(token, ByKey, StringComparison.OrdinalIgnoreCase) && !string.Equals(token, HostKey, StringComparison.OrdinalIgnoreCase) && !string.Equals(token, ProtoKey, StringComparison.OrdinalIgnoreCase) && !string.Equals(token, ForKey, StringComparison.OrdinalIgnoreCase)) { context.Errors.Add(new ArgumentException($"Unexpected value for X-Forwarded: {token}. Expected 'for', 'host', 'proto', or 'by'")); } } } else if (transformValues.TryGetValue(ClientCertKey, out var clientCertHeader)) { TransformHelpers.TryCheckTooManyParameters(context, transformValues, expected: 1); } else { return(false); } return(true); }
public bool Validate(TransformRouteValidationContext context, IReadOnlyDictionary <string, string> transformValues) { if (transformValues.TryGetValue(ResponseHeadersCopyKey, out var copyHeaders)) { TransformHelpers.TryCheckTooManyParameters(context, transformValues, expected: 1); if (!bool.TryParse(copyHeaders, out var _)) { context.Errors.Add(new ArgumentException($"Unexpected value for ResponseHeadersCopy: {copyHeaders}. Expected 'true' or 'false'")); } } else if (transformValues.TryGetValue(ResponseTrailersCopyKey, out copyHeaders)) { TransformHelpers.TryCheckTooManyParameters(context, transformValues, expected: 1); if (!bool.TryParse(copyHeaders, out var _)) { context.Errors.Add(new ArgumentException($"Unexpected value for ResponseTrailersCopy: {copyHeaders}. Expected 'true' or 'false'")); } } else if (transformValues.TryGetValue(ResponseHeaderKey, out var _)) { if (transformValues.TryGetValue(WhenKey, out var whenValue)) { TransformHelpers.TryCheckTooManyParameters(context, transformValues, expected: 3); if (!string.Equals(AlwaysValue, whenValue, StringComparison.OrdinalIgnoreCase) && !string.Equals(SuccessValue, whenValue, StringComparison.OrdinalIgnoreCase)) { context.Errors.Add(new ArgumentException($"Unexpected value for ResponseHeader:When: {whenValue}. Expected 'Always' or 'Success'")); } } else { TransformHelpers.TryCheckTooManyParameters(context, transformValues, expected: 2); } if (!transformValues.TryGetValue(SetKey, out var _) && !transformValues.TryGetValue(AppendKey, out var _)) { context.Errors.Add(new ArgumentException($"Unexpected parameters for ResponseHeader: {string.Join(';', transformValues.Keys)}. Expected 'Set' or 'Append'")); } } else if (transformValues.TryGetValue(ResponseTrailerKey, out var _)) { if (transformValues.TryGetValue(WhenKey, out var whenValue)) { TransformHelpers.TryCheckTooManyParameters(context, transformValues, expected: 3); if (!string.Equals(AlwaysValue, whenValue, StringComparison.OrdinalIgnoreCase) && !string.Equals(SuccessValue, whenValue, StringComparison.OrdinalIgnoreCase)) { context.Errors.Add(new ArgumentException($"Unexpected value for ResponseTrailer:When: {whenValue}. Expected 'Always' or 'Success'")); } } else { TransformHelpers.TryCheckTooManyParameters(context, transformValues, expected: 2); } if (!transformValues.TryGetValue(SetKey, out var _) && !transformValues.TryGetValue(AppendKey, out var _)) { context.Errors.Add(new ArgumentException($"Unexpected parameters for ResponseTrailer: {string.Join(';', transformValues.Keys)}. Expected 'Set' or 'Append'")); } } else if (transformValues.TryGetValue(ResponseHeaderRemoveKey, out var _)) { if (transformValues.TryGetValue(WhenKey, out var whenValue)) { TransformHelpers.TryCheckTooManyParameters(context, transformValues, expected: 2); if (!string.Equals(AlwaysValue, whenValue, StringComparison.OrdinalIgnoreCase) && !string.Equals(SuccessValue, whenValue, StringComparison.OrdinalIgnoreCase)) { context.Errors.Add(new ArgumentException($"Unexpected value for ResponseHeaderRemove:When: {whenValue}. Expected 'Always' or 'Success'")); } } else { TransformHelpers.TryCheckTooManyParameters(context, transformValues, expected: 1); } } else if (transformValues.TryGetValue(ResponseTrailerRemoveKey, out var _)) { if (transformValues.TryGetValue(WhenKey, out var whenValue)) { TransformHelpers.TryCheckTooManyParameters(context, transformValues, expected: 2); if (!string.Equals(AlwaysValue, whenValue, StringComparison.OrdinalIgnoreCase) && !string.Equals(SuccessValue, whenValue, StringComparison.OrdinalIgnoreCase)) { context.Errors.Add(new ArgumentException($"Unexpected value for ResponseTrailerRemove:When: {whenValue}. Expected 'Always' or 'Success'")); } } else { TransformHelpers.TryCheckTooManyParameters(context, transformValues, expected: 1); } } else if (transformValues.TryGetValue(ResponseHeadersAllowedKey, out var _)) { TransformHelpers.TryCheckTooManyParameters(context, transformValues, expected: 1); } else if (transformValues.TryGetValue(ResponseTrailersAllowedKey, out var _)) { TransformHelpers.TryCheckTooManyParameters(context, transformValues, expected: 1); } else { return(false); } return(true); }
public void ValidateRoute(TransformRouteValidationContext context) { }