Reputation: 201
In terminal, typing:
> irb
> require('open-uri')
> open("| curl http://www.haosou.com").read
can execute a system command. How can I avoid this?
Upvotes: 1
Views: 66
Reputation: 2998
Executing this kind of command is a serious security issue.
You can use a regex to validate the format:
/^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix
The validation can be done in a model:
validates_format_of :url, :with => /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix
Or elsewhere:
if url =~ /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix
require('open-uri')
open(url).read
end
Upvotes: 1