Skip to content

Commit

Permalink
fix: invalid character error in cookie serialize
Browse files Browse the repository at this point in the history
  • Loading branch information
aiji42 committed Mar 9, 2022
1 parent 193cdec commit 09be2dd
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- abotsi
- ahbruns
- ahmedeldessouki
- aiji42
- airjp73
- airondumael
- Alarid
Expand Down
8 changes: 8 additions & 0 deletions packages/remix-server-runtime/__tests__/cookies-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ describe("cookies", () => {
expect(value).toMatchInlineSnapshot(`"hello michael"`);
});

it("parses/serializes string values containing utf8 characters", async () => {
let cookie = createCookie("my-cookie");
let setCookie = await cookie.serialize("日本語");
let value = await cookie.parse(getCookieFromSetCookie(setCookie));

expect(value).toBe("日本語");
});

it("fails to parses signed string values with invalid signature", async () => {
let cookie = createCookie("my-cookie", {
secrets: ["secret1"],
Expand Down
4 changes: 2 additions & 2 deletions packages/remix-server-runtime/cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,12 @@ async function decodeCookieValue(
}

function encodeData(value: any): string {
return btoa(JSON.stringify(value));
return btoa(unescape(encodeURIComponent(JSON.stringify(value))));
}

function decodeData(value: string): any {
try {
return JSON.parse(atob(value));
return JSON.parse(decodeURIComponent(escape(atob(value))));
} catch (error) {
return {};
}
Expand Down

0 comments on commit 09be2dd

Please sign in to comment.