Description
Consider the simple example:
class Article < ActiveRecord::Base
acts_as_url :title
end
If you try to do...
Article.new(title: nil).valid?
...it will load all of the records from the article table (see this condition: https://github.com/rsl/stringex/blob/master/lib/stringex/acts_as_url/adapter/base.rb#L118).
Additionally, if you did for example Article.new(title: "a").valid?
, it will try to load all records that start with the letter 'a'. Both of these examples are very bad for large production databases, and unknowingly expose DOS exploits in your app.
I think the intention for doing a LIKE
query and loading them with to_a
is to avoid N queries when incrementing the number at the end of the slug to make it unique. Maybe it could be improved with pagination? I would think for an empty field though, that trying to create a unique slug is pointless for most use-cases (ie we have a validation on the presence of that field anyway). Even with pagination, I think the 'a' example above could still have potential for a very slow request as it still paginates through every 'a' record in the database. Maybe if we could specify an option like acts_as_url :title, if: :valid_title?
? In which case the developer would impose some restrictions on the sluggable field.