Commit 578902fda09291a7a8059a1d50e42984c8fc10b4
- Date: Sun Jan 20 23:05:08 +0000 2008
- Committer: Johan Sørensen (johan@johansorensen.com)
- Author: Johan Sørensen (johan@johansorensen.com)
- Commit SHA1: 578902fda09291a7a8059a1d50e42984c8fc10b4
- Tree SHA1: 5cf606126f7e412de95c33e8d17efec90b8bfe56
lint key from newlines in before_validation hook
Commit diff
| |   |
| 1 | 1 | class SshKey < ActiveRecord::Base |
| 2 | 2 | belongs_to :user |
| 3 | 3 | |
| 4 | | SSH_KEY_FORMAT = /^ssh\-[a-z0-9]{3,4} [a-z0-9\+=\n\/]+ [a-z0-9_\.\-]*(@[a-z0-9\.\-]*)?$/ims |
| 4 | SSH_KEY_FORMAT = /^ssh\-[a-z0-9]{3,4} [a-z0-9\+=\/]+ [a-z0-9_\.\-]*(@[a-z0-9\.\-]*)?$/ims |
| 5 | 5 | |
| 6 | 6 | validates_presence_of :user_id, :key |
| 7 | 7 | validates_format_of :key, :with => SSH_KEY_FORMAT |
| 8 | 8 | |
| 9 | 9 | before_validation { |k| k.key.to_s.strip! } |
| 10 | | before_save :lint_key! |
| 10 | before_validation :lint_key! |
| 11 | 11 | after_create :create_new_task |
| 12 | 12 | # we only allow people to create/destroy keys after_update :create_update_task |
| 13 | 13 | after_destroy :create_delete_task |
| … | … | |
| 47 | 47 | |
| 48 | 48 | protected |
| 49 | 49 | def lint_key! |
| 50 | | key.gsub!(/\n*/m, "") |
| 50 | self.key.gsub!(/(\r|\n)*/m, "") |
| 51 | 51 | end |
| 52 | 52 | end |
| toggle raw diff |
--- a/app/models/ssh_key.rb
+++ b/app/models/ssh_key.rb
@@ -1,13 +1,13 @@
class SshKey < ActiveRecord::Base
belongs_to :user
- SSH_KEY_FORMAT = /^ssh\-[a-z0-9]{3,4} [a-z0-9\+=\n\/]+ [a-z0-9_\.\-]*(@[a-z0-9\.\-]*)?$/ims
+ SSH_KEY_FORMAT = /^ssh\-[a-z0-9]{3,4} [a-z0-9\+=\/]+ [a-z0-9_\.\-]*(@[a-z0-9\.\-]*)?$/ims
validates_presence_of :user_id, :key
validates_format_of :key, :with => SSH_KEY_FORMAT
before_validation { |k| k.key.to_s.strip! }
- before_save :lint_key!
+ before_validation :lint_key!
after_create :create_new_task
# we only allow people to create/destroy keys after_update :create_update_task
after_destroy :create_delete_task
@@ -47,6 +47,6 @@ class SshKey < ActiveRecord::Base
protected
def lint_key!
- key.gsub!(/\n*/m, "")
+ self.key.gsub!(/(\r|\n)*/m, "")
end
end |
| |   |
| 40 | 40 | key.should be_valid |
| 41 | 41 | end |
| 42 | 42 | |
| 43 | it "cant contain multiple keys" do |
| 44 | k = "ssh-rsa bXljYWtkZHlpemltd21vY2NqdGJnaHN2bXFjdG9zbXplaGlpZnZ0a3VyZWFzc2dkanB4aXNxamxieGVib3l6Z3hmb2ZxZW15Y2FrZGR5aXppbXdtb2NjanRiZ2hzdm1xY3Rvc216ZWhpaWZ2dGt1cmVhc3NnZGpweGlzcWpsYnhlYm95emd4Zm9mcWU= foo@example.com" |
| 45 | key = "#{k}\r#{k}" |
| 46 | ssh = new_key(:key => key) |
| 47 | ssh.should_not be_valid |
| 48 | end |
| 49 | |
| 43 | 50 | it "strips newlines before save" do |
| 44 | 51 | ssh = new_key(:key => "ssh-rsa bXljYWtkZHlpemltd21vY2NqdGJnaHN2bXFjdG\n9zbXplaGlpZnZ0a3VyZWFzc2dkanB4aXNxamxieGVib3l6Z3hmb2ZxZW15Y2FrZGR5aXppbXdtb2NjanRiZ2hzdm1xY3Rvc216ZWhpaWZ2dGt1cm\nVhc3NnZGpweGlzcWpsYnhlYm95emd4Zm9mcWU= foo@example.com") |
| 45 | | ssh.save |
| 52 | ssh.valid? |
| 46 | 53 | ssh.key.should_not include("\n") |
| 54 | |
| 55 | ssh = new_key(:key => "ssh-rsa bXljYWtkZHlpemltd21vY2NqdGJnaHN2bXFjdG\r\n9zbXplaGlpZnZ0a3VyZWFzc2dkanB4aXNxamxieGVib3l6Z3hmb2ZxZW15Y2FrZGR5aXppbXdtb2NjanRiZ2hzdm1xY3Rvc216ZWhpaWZ2dGt1cm\nVhc3NnZGpweGlzcWpsYnhlYm95emd4Zm9mcWU= foo@example.com") |
| 56 | ssh.valid? |
| 57 | ssh.key.should_not include("\r\n") |
| 58 | |
| 59 | ssh = new_key(:key => "ssh-rsa bXljYWtkZHlpemltd21vY2NqdGJnaHN2bXFjdG\r9zbXplaGlpZnZ0a3VyZWFzc2dkanB4aXNxamxieGVib3l6Z3hmb2ZxZW15Y2FrZGR5aXppbXdtb2NjanRiZ2hzdm1xY3Rvc216ZWhpaWZ2dGt1cm\nVhc3NnZGpweGlzcWpsYnhlYm95emd4Zm9mcWU= foo@example.com") |
| 60 | ssh.valid? |
| 61 | ssh.key.should_not include("\r") |
| 47 | 62 | end |
| 48 | 63 | |
| 49 | 64 | it "strips beginning and ending whitespace+newlines before validation" do |
| toggle raw diff |
--- a/spec/models/ssh_key_spec.rb
+++ b/spec/models/ssh_key_spec.rb
@@ -40,10 +40,25 @@ describe SshKey do
key.should be_valid
end
+ it "cant contain multiple keys" do
+ k = "ssh-rsa bXljYWtkZHlpemltd21vY2NqdGJnaHN2bXFjdG9zbXplaGlpZnZ0a3VyZWFzc2dkanB4aXNxamxieGVib3l6Z3hmb2ZxZW15Y2FrZGR5aXppbXdtb2NjanRiZ2hzdm1xY3Rvc216ZWhpaWZ2dGt1cmVhc3NnZGpweGlzcWpsYnhlYm95emd4Zm9mcWU= foo@example.com"
+ key = "#{k}\r#{k}"
+ ssh = new_key(:key => key)
+ ssh.should_not be_valid
+ end
+
it "strips newlines before save" do
ssh = new_key(:key => "ssh-rsa bXljYWtkZHlpemltd21vY2NqdGJnaHN2bXFjdG\n9zbXplaGlpZnZ0a3VyZWFzc2dkanB4aXNxamxieGVib3l6Z3hmb2ZxZW15Y2FrZGR5aXppbXdtb2NjanRiZ2hzdm1xY3Rvc216ZWhpaWZ2dGt1cm\nVhc3NnZGpweGlzcWpsYnhlYm95emd4Zm9mcWU= foo@example.com")
- ssh.save
+ ssh.valid?
ssh.key.should_not include("\n")
+
+ ssh = new_key(:key => "ssh-rsa bXljYWtkZHlpemltd21vY2NqdGJnaHN2bXFjdG\r\n9zbXplaGlpZnZ0a3VyZWFzc2dkanB4aXNxamxieGVib3l6Z3hmb2ZxZW15Y2FrZGR5aXppbXdtb2NjanRiZ2hzdm1xY3Rvc216ZWhpaWZ2dGt1cm\nVhc3NnZGpweGlzcWpsYnhlYm95emd4Zm9mcWU= foo@example.com")
+ ssh.valid?
+ ssh.key.should_not include("\r\n")
+
+ ssh = new_key(:key => "ssh-rsa bXljYWtkZHlpemltd21vY2NqdGJnaHN2bXFjdG\r9zbXplaGlpZnZ0a3VyZWFzc2dkanB4aXNxamxieGVib3l6Z3hmb2ZxZW15Y2FrZGR5aXppbXdtb2NjanRiZ2hzdm1xY3Rvc216ZWhpaWZ2dGt1cm\nVhc3NnZGpweGlzcWpsYnhlYm95emd4Zm9mcWU= foo@example.com")
+ ssh.valid?
+ ssh.key.should_not include("\r")
end
it "strips beginning and ending whitespace+newlines before validation" do |