Skip to content

Commit

Permalink
fix(core): don't crash on escaped unicode symbols in messages (#1692)
Browse files Browse the repository at this point in the history
  • Loading branch information
timofei-iatsenko authored Jun 7, 2023
1 parent 6034c3a commit 461c2fc
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
{
"path": "./packages/core/dist/index.mjs",
"import": "{ i18n }",
"limit": "2 kB"
"limit": "3 kB"
},
{
"path": "./packages/detect-locale/dist/index.mjs",
Expand Down
3 changes: 2 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@
],
"dependencies": {
"@babel/runtime": "^7.20.13",
"@lingui/message-utils": "4.2.0"
"@lingui/message-utils": "4.2.0",
"unraw": "^2.0.1"
},
"devDependencies": {
"@lingui/jest-mocks": "*",
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/interpolate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,11 @@ describe("interpolate", () => {
})
})
})

it("should not crash on a unicode sequences", () => {
const cache = compile("Hey {name}!")
expect(interpolate(cache, "en", [])({ name: "Joe\\xaa" })).toEqual(
"Hey Joeª!"
)
})
})
9 changes: 7 additions & 2 deletions packages/core/src/interpolate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CompiledMessage, Formats, Locales, Values } from "./i18n"
import { date, number, plural } from "./formats"
import { isString } from "./essentials"
import unraw from "unraw"

export const UNICODE_REGEX = /\\u[a-fA-F0-9]{4}|\\x[a-fA-F0-9]{2}/g

Expand Down Expand Up @@ -92,8 +93,12 @@ export function interpolate(
}

const result = formatMessage(translation)
if (isString(result) && UNICODE_REGEX.test(result))
return JSON.parse(`"${result.trim()}"`)
if (isString(result) && UNICODE_REGEX.test(result)) {
// convert raw unicode sequences back to normal strings
// note JSON.parse hack is not working as you might expect https://stackoverflow.com/a/57560631/2210610
// that's why special library for that purpose is used
return unraw(result.trim())
}
if (isString(result)) return result.trim()
return result
}
Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2729,6 +2729,7 @@ __metadata:
"@lingui/jest-mocks": "*"
"@lingui/message-utils": 4.2.0
unbuild: ^1.1.2
unraw: ^2.0.1
languageName: unknown
linkType: soft

Expand Down Expand Up @@ -14363,6 +14364,13 @@ __metadata:
languageName: node
linkType: hard

"unraw@npm:^2.0.1":
version: 2.0.1
resolution: "unraw@npm:2.0.1"
checksum: af9a9d2f6e420cb4f52fe2f1f5982e6b0be95da640d6ae8d6d9ff631d864771793cb9fe7e2a16ef1ce631b94065f4438e7bd7f1701076fc69296edc4e704d42f
languageName: node
linkType: hard

"untyped@npm:^1.2.2":
version: 1.2.2
resolution: "untyped@npm:1.2.2"
Expand Down

1 comment on commit 461c2fc

@vercel
Copy link

@vercel vercel bot commented on 461c2fc Jun 7, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.