chore: Add max length validation to text fields (#7073)

Introduces a default max length validation for all string and text fields to prevent processing large payloads.
This commit is contained in:
Sojan Jose
2023-05-12 22:12:21 +05:30
committed by GitHub
parent 198cd9b28d
commit 385eab6b96
6 changed files with 89 additions and 6 deletions

View File

@@ -23,6 +23,23 @@ RSpec.describe Account do
it { is_expected.to have_many(:categories).dependent(:destroy_async) }
it { is_expected.to have_many(:teams).dependent(:destroy_async) }
# This validation happens in ApplicationRecord
describe 'length validations' do
let(:account) { create(:account) }
it 'validates name length' do
account.name = 'a' * 256
account.valid?
expect(account.errors[:name]).to include('is too long (maximum is 255 characters)')
end
it 'validates domain length' do
account.domain = 'a' * 150
account.valid?
expect(account.errors[:domain]).to include('is too long (maximum is 100 characters)')
end
end
describe 'usage_limits' do
let(:account) { create(:account) }

View File

@@ -1,6 +1,11 @@
require 'rails_helper'
RSpec.describe Article, type: :model do
let!(:account) { create(:account) }
let(:user) { create(:user, account_ids: [account.id], role: :agent) }
let!(:portal_1) { create(:portal, account_id: account.id, config: { allowed_locales: %w[en es] }) }
let!(:category_1) { create(:category, slug: 'category_1', locale: 'en', portal_id: portal_1.id) }
context 'with validations' do
it { is_expected.to validate_presence_of(:account_id) }
it { is_expected.to validate_presence_of(:author_id) }
@@ -13,12 +18,30 @@ RSpec.describe Article, type: :model do
it { is_expected.to belong_to(:author) }
end
# This validation happens in ApplicationRecord
describe 'length validations' do
let(:article) do
create(:article, category_id: category_1.id, content: 'This is the content', description: 'this is the description',
slug: 'this-is-title', title: 'this is title',
portal_id: portal_1.id, author_id: user.id)
end
context 'when it validates content length' do
it 'valid when within limit' do
article.content = 'a' * 1000
expect(article.valid?).to be true
end
it 'invalid when crossed the limit' do
article.content = 'a' * 25_001
article.valid?
expect(article.errors[:content]).to include('is too long (maximum is 20000 characters)')
end
end
end
describe 'search' do
let!(:account) { create(:account) }
let(:user) { create(:user, account_ids: [account.id], role: :agent) }
let!(:portal_1) { create(:portal, account_id: account.id, config: { allowed_locales: %w[en es] }) }
let!(:portal_2) { create(:portal, account_id: account.id, config: { allowed_locales: %w[en es] }) }
let!(:category_1) { create(:category, slug: 'category_1', locale: 'en', portal_id: portal_1.id) }
let!(:category_2) { create(:category, slug: 'category_2', locale: 'es', portal_id: portal_1.id) }
let!(:category_3) { create(:category, slug: 'category_3', locale: 'es', portal_id: portal_2.id) }

View File

@@ -10,6 +10,23 @@ RSpec.describe Message, type: :model do
it { is_expected.to validate_presence_of(:account_id) }
end
describe 'length validations' do
let(:message) { create(:message) }
context 'when it validates name length' do
it 'valid when within limit' do
message.content = 'a' * 120_000
expect(message.valid?).to be true
end
it 'invalid when crossed the limit' do
message.content = 'a' * 150_001
message.valid?
expect(message.errors[:content]).to include('is too long (maximum is 150000 characters)')
end
end
end
describe 'concerns' do
it_behaves_like 'liqudable'
end