Skip to content

Commit 34f6e9c

Browse files
committed
Validate lengths of string attributes
When using the MySQL database, Rails automatically sets a limit of 255 characters for string columns. This changes ensures this limit is also enforced in the validations. For several columns with a different limit, that different limit is enforced instead. For the PostgreSQL database, there is no limit in the database, but it is useful to set a limit anyway to prevent absurdly large values from being submitted.
1 parent 298b452 commit 34f6e9c

20 files changed

+177
-2
lines changed

Manifest.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ app/models/article.rb
160160
app/models/article/factory.rb
161161
app/models/blog.rb
162162
app/models/comment.rb
163+
app/models/concerns/string_length_limit.rb
163164
app/models/config_manager.rb
164165
app/models/content.rb
165166
app/models/content_base.rb

app/models/blog.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#
1010
class Blog < ApplicationRecord
1111
include ConfigManager
12+
include StringLengthLimit
13+
1214
include Rails.application.routes.url_helpers
1315

1416
has_many :contents
@@ -139,6 +141,7 @@ class Blog < ApplicationRecord
139141

140142
validate :permalink_has_identifier
141143
# validates :base_url, presence: true
144+
validates_default_string_length :base_url
142145

143146
# Find the Blog that matches a specific base URL. If no Blog object is found
144147
# that matches, then grab the first blog. If *that* fails, then create a new
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
module StringLengthLimit
4+
# Default string length limit for model attributes. When running on MySQL,
5+
# this is equal to the default string length in the database as set by Rails.
6+
STRING_LIMIT = 255
7+
8+
extend ActiveSupport::Concern
9+
10+
class_methods do
11+
def validates_default_string_length(*names)
12+
names.each do |name|
13+
validates name, length: { maximum: STRING_LIMIT }
14+
end
15+
end
16+
end
17+
end

app/models/content.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
class Content < ApplicationRecord
77
include ContentBase
8+
include StringLengthLimit
89

910
belongs_to :user, optional: true, touch: true
1011
belongs_to :blog
@@ -38,6 +39,9 @@ class Content < ApplicationRecord
3839

3940
serialize :whiteboard
4041

42+
validates_default_string_length :title, :author, :permalink, :name,
43+
:post_type, :text_filter_name
44+
4145
def author=(user)
4246
if user.respond_to?(:login)
4347
self[:author] = user.login

app/models/feedback.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,16 @@ class Feedback < ApplicationRecord
1010

1111
include PublifyGuid
1212
include ContentBase
13+
include StringLengthLimit
1314

1415
validate :feedback_allowed, on: :create
1516
validates :article, presence: true
1617

18+
validates_default_string_length :title, :author, :email, :url, :blog_name,
19+
:user_agent, :text_filter_name
20+
21+
validates :ip, length: { maximum: 40 }
22+
1723
before_save :correct_url, :classify_content
1824
before_create :create_guid
1925

app/models/ping.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# frozen_string_literal: true
22

33
class Ping < ApplicationRecord
4+
include StringLengthLimit
5+
46
belongs_to :article
7+
validates_default_string_length :url
58
end

app/models/post_type.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
# frozen_string_literal: true
22

33
class PostType < ApplicationRecord
4+
include StringLengthLimit
5+
46
validates :name, uniqueness: true
57
validates :name, presence: true
68
validate :name_is_not_read
9+
validates_default_string_length :name, :permalink, :description
10+
711
before_save :sanitize_title
812

913
def name_is_not_read

app/models/redirect.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
# frozen_string_literal: true
22

33
class Redirect < ApplicationRecord
4+
include StringLengthLimit
5+
46
belongs_to :content, optional: true, touch: true
57
belongs_to :blog
68

79
validates :from_path, uniqueness: true
810
validates :to_path, presence: true
911
validates :blog, presence: true
1012

13+
validates_default_string_length :from_path, :to_path
14+
1115
def full_to_path
1216
path = to_path
1317
# FIXME: Unify HTTP URI matchers

app/models/resource.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
require "carrierwave/orm/activerecord"
55

66
class Resource < ApplicationRecord
7+
include StringLengthLimit
78
belongs_to :blog
89
belongs_to :content, optional: true
910

1011
mount_uploader :upload, ResourceUploader
1112
validates :upload, presence: true
13+
14+
validates_default_string_length :mime
1215
end

app/models/tag.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
# frozen_string_literal: true
22

33
class Tag < ApplicationRecord
4+
include StringLengthLimit
5+
46
belongs_to :blog
57
has_and_belongs_to_many :contents, order: "created_at DESC"
68

79
validates :name, uniqueness: { scope: :blog_id }
810
validates :blog, presence: true
911
validates :name, presence: true
12+
validates_default_string_length :display_name
1013

1114
before_validation :ensure_naming_conventions
1215

0 commit comments

Comments
 (0)