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
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion Fluid/Filters/MiscFilters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public static ValueTask<FluidValue> Base64UrlSafeEncode(FluidValue input, Filter
encodedBase64StringBuilder.Replace('+', '-');
encodedBase64StringBuilder.Replace('/', '_');

return new StringValue(encodedBase64StringBuilder.ToString());
return new StringValue(encodedBase64StringBuilder.ToString().TrimEnd('='));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the string builder have something like it? That would prevent the extra string allocation.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we know that base64 encoded version ends with == (it should) it should suffice to encodedBase64StringBuilder.Length -= 2;

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good option too

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not always the case, could be zero, one or two chars. See the test cases I shared for instance.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I see what you mean now, sorry.

Copy link
Collaborator Author

@hishamco hishamco Aug 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue that might be raise during decoding how we know there was zero, one or two "=" on the encoded URL?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI there's Base64.IsValid() introduced in .NET 8.0 but it will not solve the problem in our case

}
}

Expand Down