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

fix(remix-server-runtime): fix invalid character error in cookie serialize #1290

Closed
wants to merge 4 commits into from
Closed
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
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,4 @@
- weavdale
- zachdtaylor
- zainfathoni
- nimaa77
4 changes: 2 additions & 2 deletions docs/contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ Running `yarn build` from the root directory will run the build.

### Testing

Before running the tests, you need to run a build. After you build, running `yarn test` from the root directory will run **every** package's tests. If you want to run tests for a specific package, use `yarn test --projects packages/<package-name>`:
Before running the tests, you need to run a build. After you build, running `yarn test` from the root directory will run **every** package's tests. If you want to run tests for a specific package, use `yarn test --selectProjects <project-displayName>` (you can find project displayName from `jest.config.js` file):

```bash
# Test all packages
yarn test

# Test only @remix-run/express
yarn test --projects packages/remix-express
yarn test --selectProjects express
```

## Repository Branching
Expand Down
32 changes: 32 additions & 0 deletions packages/remix-server-runtime/__tests__/cookies-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,36 @@ describe("cookies", () => {
let setCookie2 = await cookie.serialize(value);
expect(setCookie).not.toEqual(setCookie2);
});

it("parses/serializes unsigned UTF8 string values", async () => {
let cookie = createCookie("my-cookie");
let setCookie = await cookie.serialize("سلام دنیا");
let value = await cookie.parse(getCookieFromSetCookie(setCookie));

expect(value).toEqual("سلام دنیا");
});

it("parses/serializes signed UTF8 string values", async () => {
let cookie = createCookie("my-cookie", {
secrets: ["secret1"]
});
let setCookie = await cookie.serialize("سلام ریمیکس");
let value = await cookie.parse(getCookieFromSetCookie(setCookie));

expect(value).toMatchInlineSnapshot(`"سلام ریمیکس"`);
});

it("parses/serializes signed object UTF8 values", async () => {
let cookie = createCookie("my-cookie", {
secrets: ["secret1"]
});
let setCookie = await cookie.serialize({ hello: "مایکل" });
let value = await cookie.parse(getCookieFromSetCookie(setCookie));

expect(value).toMatchInlineSnapshot(`
Object {
"hello": "مایکل",
}
`);
});
});
34 changes: 32 additions & 2 deletions packages/remix-server-runtime/cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,43 @@ async function decodeCookieValue(
}

function encodeData(value: any): string {
return btoa(JSON.stringify(value));
let stringified = JSON.stringify(value);
let converted = toBinary(stringified);
return btoa(converted);
}

function decodeData(value: string): any {
try {
return JSON.parse(atob(value));
let decodedData = atob(value);
let coverted = fromBinary(decodedData);
return JSON.parse(coverted);
} catch (error) {
return {};
}
}

function toBinary(string: string): string {
let codeUnits = new Uint16Array(string.length);
for (let i = 0; i < codeUnits.length; i++) {
codeUnits[i] = string.charCodeAt(i);
}
let charCodes = new Uint8Array(codeUnits.buffer);
let result = "";
for (let i = 0; i < charCodes.byteLength; i++) {
result += String.fromCharCode(charCodes[i]);
}
return result;
}

function fromBinary(binary: string): string {
let bytes = new Uint8Array(binary.length);
for (let i = 0; i < bytes.length; i++) {
bytes[i] = binary.charCodeAt(i);
}
let charCodes = new Uint16Array(bytes.buffer);
let result = "";
for (let i = 0; i < charCodes.length; i++) {
result += String.fromCharCode(charCodes[i]);
}
return result;
}