From 1915f292ada78cc773075e9646405016f7a01368 Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 30 Dec 2013 14:05:44 +1100 Subject: [PATCH] BUGFIX: invalid referer would cause 500 errors --- app/models/incoming_link.rb | 8 ++++++-- spec/models/incoming_link_spec.rb | 6 ++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/app/models/incoming_link.rb b/app/models/incoming_link.rb index c9656f0ac..1d0cc48ae 100644 --- a/app/models/incoming_link.rb +++ b/app/models/incoming_link.rb @@ -18,8 +18,12 @@ class IncomingLink < ActiveRecord::Base end if request.referer.present? - host = URI.parse(request.referer).host - referer = request.referer[0..999] + begin + host = URI.parse(request.referer).host + referer = request.referer[0..999] + rescue URI::InvalidURIError + # bad uri, skip + end end if host != request.host && (user_id || referer) diff --git a/spec/models/incoming_link_spec.rb b/spec/models/incoming_link_spec.rb index a6cf4c6b3..0e672949b 100644 --- a/spec/models/incoming_link_spec.rb +++ b/spec/models/incoming_link_spec.rb @@ -62,6 +62,12 @@ describe IncomingLink do IncomingLink.add(req('http://somesite.com', 'http://somesite.com')) end + it "tracks visits for invalid referers" do + IncomingLink.add(req('http://somesite.com', 'bang bang bang')) + # no current user, don't track + IncomingLink.count.should == 0 + end + it "expects to be called with referer and user id" do IncomingLink.expects(:create).once.returns(true) IncomingLink.add(req('http://somesite.com', 'http://some.other.site.com'), build(:user))