base64 example will throw an error if a user happens to submit a string that gets base64 decoded to an array #3316
Send to my agentWhich project does this relate to?#
Router
Describe the bug#
I'm using the following the base64 example from the docs. If I have two parameters: string and array and submit the array i get WyIxIiwiMiJd as the value. Now if I navigate and search on a string with that array value it will try to decode the string as an array when I hit submit. In other words, no one can ever enter WyIxIiwiMiJd as a string value on a form that submits to the URL since that happens to decode to a valid array.
Is stringifySearch missing a second argument to stringifySearchWith?
import {
Router,
parseSearchWith,
stringifySearchWith,
} from '@tanstack/react-router'
const router = createRouter({
parseSearch: parseSearchWith((value) => JSON.parse(decodeFromBinary(value))),
stringifySearch: stringifySearchWith((value) =>
encodeToBinary(JSON.stringify(value)),
),
})
function decodeFromBinary(str: string): string {
return decodeURIComponent(
Array.prototype.map
.call(atob(str), function (c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)
})
.join(''),
)
}
function encodeToBinary(str: string): string {
return btoa(
encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function (match, p1) {
return String.fromCharCode(parseInt(p1, 16))
}),
)
}
Your Example Website or App#
Steps to Reproduce the Bug or Issue#
Enter "WyIxIiwiMiJd" as a value to an expected string parameter when calling navigate.search
Expected behavior#
It should get "WyIxIiwiMiJd" back instead of the the decoded array.
Screenshots or Videos#
No response
Platform#
tanstack router 1.97.14
Additional context#
No response
One thing I noted with this is that if you submit a value of 1 then it will return a string instead of a number.
Unless someone has a better strategy to the functions performing the encoding/decoding, we'll need to update the docs advising people about the dangers of just using base64.
@rostero1 if this fixes it, could you please make a PR in the docs? If not, let me know I'll add a disclaimer in the docs.
Edit: Also, let me know if you are unable to make a PR.
SeanCassiere added labelsdocumentation
SeanCassiere closed this
Sign in to join this conversation.
The following may fix it: