-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improving Table Filter parameter validation
- Loading branch information
Showing
16 changed files
with
431 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/Microsoft.Azure.WebJobs.Host/Tables/TableFilterFormatter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Text; | ||
using Microsoft.Azure.WebJobs.Host.Bindings; | ||
using Microsoft.Azure.WebJobs.Host.Bindings.Path; | ||
|
||
namespace Microsoft.Azure.WebJobs.Host.Tables | ||
{ | ||
internal static class TableFilterFormatter | ||
{ | ||
private static char[] _specialChars; | ||
|
||
static TableFilterFormatter() | ||
{ | ||
_specialChars = new char[] { ' ', '\'', ')', '(' }; | ||
} | ||
|
||
public static string Format(BindingTemplate template, IReadOnlyDictionary<string, object> bindingData) | ||
{ | ||
if (!template.ParameterNames.Any()) | ||
{ | ||
return template.Pattern; | ||
} | ||
|
||
var convertedBindingData = BindingDataPathHelper.ConvertParameters(bindingData); | ||
|
||
// each distinct parameter can occur one or more times in the template | ||
var parameterGroups = template.ParameterNames.GroupBy(p => p); | ||
|
||
// for each parameter, classify it as a string literal or other | ||
// and perform value validation | ||
foreach (var parameterGroup in parameterGroups) | ||
{ | ||
// to classify as a string literal, ALL occurrences in the template | ||
// must be string literals (e.g. of the form '{p}') | ||
// note that this will also capture OData expressions of the form | ||
// datetime'{p}', guid'{p}', X'{p}' | ||
bool isStringLiteral = true; | ||
string parameterName = parameterGroup.Key; | ||
string stringParameterFormat = $"'{{{parameterName}}}'"; | ||
int count = 0, idx = 0; | ||
while (idx >= 0 && idx < template.Pattern.Length && count++ < parameterGroup.Count()) | ||
{ | ||
idx = template.Pattern.IndexOf(stringParameterFormat, idx, StringComparison.OrdinalIgnoreCase); | ||
if (idx < 0) | ||
{ | ||
isStringLiteral = false; | ||
break; | ||
} | ||
idx++; | ||
} | ||
|
||
// validate and format the value based on its classification | ||
string value = null; | ||
if (convertedBindingData.TryGetValue(parameterName, out value)) | ||
{ | ||
if (isStringLiteral) | ||
{ | ||
// ensure we escape any single quotes | ||
convertedBindingData[parameterName] = value.Replace("'", "''"); | ||
} | ||
else if (!TryValidateNonStringLiteral(value)) | ||
{ | ||
throw new InvalidOperationException($"An invalid parameter value was specified for filter parameter '{parameterName}'."); | ||
} | ||
} | ||
} | ||
|
||
return template.Bind(convertedBindingData); | ||
} | ||
|
||
internal static bool TryValidateNonStringLiteral(string value) | ||
{ | ||
// verify that there are no special characters | ||
if (_specialChars.Any(p => value.Contains(p))) | ||
{ | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.