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] Added JSON Formatting check for custom translations field #27873

Closed
wants to merge 5 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
14 changes: 14 additions & 0 deletions apps/meteor/app/lib/server/methods/saveSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ import { hasPermission } from '../../../authorization/server';
import { getSettingPermissionId } from '../../../authorization/lib';
import { twoFactorRequired } from '../../../2fa/server/twoFactorRequired';

const parseToJSON = (customTranslations) => {
try {
JSON.parse(customTranslations);
Copy link
Member

Choose a reason for hiding this comment

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

We should also accept empty strings as valid values for settings, otherwise they can't be erased.

return true;
} catch (e) {
throw new Meteor.Error('error-action-not-allowed', 'Invalid JSON Format', {
method: 'saveSettings',
});
}
};

Meteor.methods({
saveSettings: twoFactorRequired(async function (params = []) {
const uid = Meteor.userId();
Expand Down Expand Up @@ -41,6 +52,9 @@ Meteor.methods({
case 'multiSelect':
check(value, Array);
break;
case 'code':
parseToJSON(value);
Copy link
Member

Choose a reason for hiding this comment

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

Not all code settings are JSON (eg some of them are CSS or HTML). We should check settings' code property to assure they should be a valid JSON.

break;
default:
check(value, String);
break;
Expand Down
10 changes: 9 additions & 1 deletion apps/meteor/client/lib/utils/applyCustomTranslations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ import { settings } from '../../../app/settings/client';
export const applyCustomTranslations = (): void => {
const customTranslations: string | undefined = settings.get('Custom_Translations');

if (!customTranslations) {
const parseToJSON = (customTranslations: string) => {
try {
return JSON.parse(customTranslations);
} catch (e) {
return false;
}
};

if (!customTranslations || !parseToJSON(customTranslations)) {
return;
}

Expand Down
3 changes: 1 addition & 2 deletions apps/meteor/client/providers/TranslationProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const useI18next = (lng: string): typeof i18next => {

const parse = useMutableCallback((data: string, lngs?: string | string[], namespaces: string | string[] = []): { [key: string]: any } => {
const parsedCustomTranslations = typeof customTranslations === 'string' && parseToJSON(customTranslations);

const source = JSON.parse(data);
const result: { [key: string]: any } = {};

Expand Down Expand Up @@ -89,7 +88,7 @@ const useI18next = (lng: string): typeof i18next => {
}, [i18n, lng]);

useEffect(() => {
if (!customTranslations || typeof customTranslations !== 'string') {
if (!customTranslations || typeof customTranslations !== 'string' || !parseToJSON(customTranslations)) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/packages/rocketchat-i18n/i18n/en.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -1454,7 +1454,7 @@
"Custom_Sounds": "Custom Sounds",
"Custom_Status": "Custom Status",
"Custom_Translations": "Custom Translations",
"Custom_Translations_Description": "Should be a valid JSON where keys are languages containing a dictionary of key and translations. Example:<br/><code>{\n \"en\": {\n \"Channels\": \"Rooms\"\n },\n \"pt\": {\n \"Channels\": \"Salas\"\n }\n}</code> ",
"Custom_Translations_Description": "Should be a valid JSON where keys are languages containing a dictionary of key and translations. Example:{\n \"en\": {\n \"Channels\": \"Rooms\"\n },\n \"pt\": {\n \"Channels\": \"Salas\"\n }\n}",
"Custom_User_Status": "Custom User Status",
"Custom_User_Status_Add": "Add Custom User Status",
"Custom_User_Status_Added_Successfully": "Custom User Status Added Successfully",
Expand Down