Skip to content

Commit

Permalink
fix: escape post translated JSON (#2619)
Browse files Browse the repository at this point in the history
  • Loading branch information
omBratteng authored Jan 27, 2025
1 parent f88f8ef commit cf0b9be
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
22 changes: 22 additions & 0 deletions __tests__/workers/postTranslated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,28 @@ describe('postTranslated', () => {
});
});

it('should handle titles with special characters', async () => {
expect(
(await con.getRepository(Post).findOneByOrFail({ id: 'p1' })).translation,
).toEqual({});

await expectSuccessfulTypedBackground(worker, {
id: 'p1',
language: ContentLanguage.German,
translations: {
title: `new title #"!#%&/()=?'"\`\\ƒ∂∞€€é∂ßä`,
},
});

expect(
(await con.getRepository(Post).findOneByOrFail({ id: 'p1' })).translation,
).toEqual({
de: {
title: `new title #"!#%&/()=?'"\`\\ƒ∂∞€€é∂ßä`,
},
});
});

it('should not update post translation if language is invalid', async () => {
expect(
(await con.getRepository(Post).findOneByOrFail({ id: 'p1' })).translation,
Expand Down
8 changes: 6 additions & 2 deletions src/workers/postTranslated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ export const postTranslated: TypedWorker<'kvasir.v1.post-translated'> = {
.set({
translation: () => `jsonb_set(
translation,
'{${language}}',
'${JSON.stringify(translations)}'::jsonb,
:language,
:translations::jsonb,
true
)`,
})
.setParameters({
language: [language],
translations: JSON.stringify(translations),
})
.where('id = :id', { id })
.execute();

Expand Down

0 comments on commit cf0b9be

Please sign in to comment.