Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trim = from Base64UrlSafeEncode #691

Merged
merged 4 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Fluid.Tests/IncludeStatementTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class IncludeStatementTests
#endif

[Fact]
public async Task IncludeSatement_ShouldThrowFileNotFoundException_IfTheFileProviderIsNotPresent()
public async Task IncludeStatement_ShouldThrowFileNotFoundException_IfTheFileProviderIsNotPresent()
{
var expression = new LiteralExpression(new StringValue("_Partial.liquid"));
var sw = new StringWriter();
Expand All @@ -39,7 +39,7 @@ public async Task IncludeSatement_ShouldThrowFileNotFoundException_IfTheFileProv
}

[Fact]
public async Task IncludeSatement_ShouldLoadPartial_IfThePartialsFolderExist()
public async Task IncludeStatement_ShouldLoadPartial_IfThePartialsFolderExist()
{

var expression = new LiteralExpression(new StringValue("_Partial.liquid"));
Expand All @@ -64,7 +64,7 @@ public async Task IncludeSatement_ShouldLoadPartial_IfThePartialsFolderExist()
}

[Fact]
public async Task IncludeSatement_ShouldLoadCorrectTemplate_IfTheMemberExpressionValueChanges()
public async Task IncludeStatement_ShouldLoadCorrectTemplate_IfTheMemberExpressionValueChanges()
{
var expression = new MemberExpression(new IdentifierSegment("Firstname"));
var sw = new StringWriter();
Expand Down Expand Up @@ -109,7 +109,7 @@ public async Task IncludeSatement_ShouldLoadCorrectTemplate_IfTheMemberExpressio
}

[Fact]
public async Task IncludeSatement_WithInlinevariableAssignment_ShouldBeEvaluated()
public async Task IncludeStatement_WithInlinevariableAssignment_ShouldBeEvaluated()
{
var expression = new LiteralExpression(new StringValue("_Partial.liquid"));
var assignStatements = new List<AssignStatement>
Expand Down Expand Up @@ -138,7 +138,7 @@ public async Task IncludeSatement_WithInlinevariableAssignment_ShouldBeEvaluated
}

[Fact]
public async Task IncludeSatement_WithTagParams_ShouldBeEvaluated()
public async Task IncludeStatement_WithTagParams_ShouldBeEvaluated()
{
var pathExpression = new LiteralExpression(new StringValue("color"));
var withExpression = new LiteralExpression(new StringValue("blue"));
Expand All @@ -163,7 +163,7 @@ public async Task IncludeSatement_WithTagParams_ShouldBeEvaluated()
}

[Fact]
public async Task IncludeSatement_ShouldLimitRecursion()
public async Task IncludeStatement_ShouldLimitRecursion()
{
var expression = new LiteralExpression(new StringValue("_Partial.liquid"));
var sw = new StringWriter();
Expand Down
8 changes: 8 additions & 0 deletions Fluid.Tests/MiscFiltersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ public async Task DecodeUrl()

[Theory]
[InlineData("a<>:a?", "YTw+OmE/")]
[InlineData("Hell", "SGVsbA==")]
[InlineData("Hello", "SGVsbG8=")]
public async Task Base64Encode(string value, string expected)
{
var input = new StringValue(value);
Expand All @@ -115,6 +117,8 @@ public async Task Base64Encode(string value, string expected)

[Theory]
[InlineData("YTw+OmE/", "a<>:a?")]
[InlineData("SGVsbA==", "Hell")]
[InlineData("SGVsbG8=", "Hello")]
public async Task Base64Decode(string value, string expected)
{
var input = new StringValue(value);
Expand All @@ -129,6 +133,8 @@ public async Task Base64Decode(string value, string expected)

[Theory]
[InlineData("a<>:a?", "YTw-OmE_")]
[InlineData("Hell", "SGVsbA")]
[InlineData("Hello", "SGVsbG8")]
public async Task Base64UrlSafeEncode(string value, string expected)
{
// Arrange
Expand All @@ -145,6 +151,8 @@ public async Task Base64UrlSafeEncode(string value, string expected)

[Theory]
[InlineData("YTw-OmE_", "a<>:a?")]
[InlineData("SGVsbA", "Hell")]
[InlineData("SGVsbG8", "Hello")]
public async Task Base64UrlSafeDecode(string value, string expected)
{
// Arrange
Expand Down
41 changes: 39 additions & 2 deletions Fluid/Filters/MiscFilters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,18 @@ public static ValueTask<FluidValue> Base64UrlSafeEncode(FluidValue input, Filter
encodedBase64StringBuilder.Replace('+', '-');
encodedBase64StringBuilder.Replace('/', '_');

if (encodedBase64StringBuilder[^1] == '=')
{
if (encodedBase64StringBuilder[^2] == '=')
{
encodedBase64StringBuilder.Length -= 2;
}
else
{
encodedBase64StringBuilder.Length--;
}
}

return new StringValue(encodedBase64StringBuilder.ToString());
}
}
Expand All @@ -209,13 +221,38 @@ public static ValueTask<FluidValue> Base64UrlSafeDecode(FluidValue input, Filter
}
else
{
var paddingCharsToAdd = (value.Length % 4) switch
{
0 => 0,
2 => 2,
3 => 1,
_ => -1
};

if (paddingCharsToAdd == -1)
{
return StringValue.Empty;
}

var encodedBase64StringBuilder = new StringBuilder(value);
encodedBase64StringBuilder.Replace('-', '+');
encodedBase64StringBuilder.Replace('_', '/');

var decodedBase64 = Encoding.UTF8.GetString(Convert.FromBase64String(encodedBase64StringBuilder.ToString()));
// Add the padding characters back.
for (; paddingCharsToAdd > 0; paddingCharsToAdd--)
{
encodedBase64StringBuilder.Append('=');
}

return new StringValue(decodedBase64);
try
{
var decodedBase64 = Encoding.UTF8.GetString(Convert.FromBase64String(encodedBase64StringBuilder.ToString()));
return new StringValue(decodedBase64);
}
catch
{
return StringValue.Empty;
}
}
}

Expand Down