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: saveSettings endpoint allowing NaN to be stored on Int settings #32428

Merged
merged 11 commits into from
Jun 21, 2024
5 changes: 5 additions & 0 deletions .changeset/happy-windows-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rocket.chat/meteor": patch
---

Fixed an issue that allowed saveSettings method to save NaN values on numeric settings.
6 changes: 6 additions & 0 deletions apps/meteor/app/lib/server/methods/saveSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ Meteor.methods<ServerMethods>({
case 'timespan':
case 'int':
check(value, Number);
if (!Number.isInteger(value)) {
throw new Meteor.Error(`Invalid setting value ${value}`, 'Invalid setting value', {
method: 'saveSettings',
});
}

break;
case 'multiSelect':
check(value, Array);
Expand Down
4 changes: 4 additions & 0 deletions apps/meteor/tests/e2e/page-objects/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export class Admin {
return this.page.locator('button >> text="Save"');
}

get btnSaveSettings(): Locator {
return this.page.getByRole('button', { name: 'Save changes' });
}

get btnEdit(): Locator {
return this.page.locator('button >> text="Edit"');
}
Expand Down
25 changes: 25 additions & 0 deletions apps/meteor/tests/e2e/settings-int.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Users } from './fixtures/userStates';
import { Admin } from './page-objects';
import { test, expect } from './utils/test';

test.use({ storageState: Users.admin.state });

test.describe.serial('settings-int', () => {
let poAdmin: Admin;

test.beforeEach(async ({ page }) => {
poAdmin = new Admin(page);
await page.goto('/admin/settings/Message');

await expect(page.locator('[data-qa-type="PageHeader-title"]')).toHaveText('Message');
});

test('expect not being able to set int value as empty string', async ({ page }) => {
await page.locator('#Message_AllowEditing_BlockEditInMinutes').fill('');
await page.locator('#Message_AllowEditing_BlockEditInMinutes').blur();

await poAdmin.btnSaveSettings.click();

await expect(page.locator('.rcx-toastbar.rcx-toastbar--error')).toBeVisible();
});
});
65 changes: 65 additions & 0 deletions apps/meteor/tests/end-to-end/api/24-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -3108,4 +3108,69 @@ describe('Meteor.methods', function () {
});
});
});

describe('[@saveSettings]', () => {
it('should return an error when trying to save a "NaN" value', () => {
request
.post(api('method.call/saveSettings'))
.set(credentials)
.send({
message: JSON.stringify({
msg: 'method',
id: '13',
method: 'saveSettings',
params: [[{ _id: 'Message_AllowEditing_BlockEditInMinutes', value: { $InfNaN: 0 } }]],
}),
})
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
const parsedBody = JSON.parse(res.body.message);
expect(parsedBody).to.have.property('error');
expect(parsedBody.error).to.have.property('error', 'Invalid setting value NaN');
});
});

it('should return an error when trying to save a "Infinity" value', () => {
request
.post(api('method.call/saveSettings'))
.set(credentials)
.send({
message: JSON.stringify({
msg: 'method',
id: '13',
method: 'saveSettings',
params: [[{ _id: 'Message_AllowEditing_BlockEditInMinutes', value: { $InfNaN: 1 } }]],
}),
})
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
const parsedBody = JSON.parse(res.body.message);
expect(parsedBody).to.have.property('error');
expect(parsedBody.error).to.have.property('error', 'Invalid setting value Infinity');
});
});

it('should return an error when trying to save a "-Infinity" value', () => {
request
.post(api('method.call/saveSettings'))
.set(credentials)
.send({
message: JSON.stringify({
msg: 'method',
id: '13',
method: 'saveSettings',
params: [[{ _id: 'Message_AllowEditing_BlockEditInMinutes', value: { $InfNaN: -1 } }]],
}),
})
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
const parsedBody = JSON.parse(res.body.message);
expect(parsedBody).to.have.property('error');
expect(parsedBody.error).to.have.property('error', 'Invalid setting value -Infinity');
});
});
});
});
Loading