Skip to content

Commit

Permalink
Merge pull request #347 from scouttyg/security-overhaul
Browse files Browse the repository at this point in the history
Complete security overhaul on the application due to possible attack vec...
  • Loading branch information
scouttyg committed Apr 30, 2014
2 parents 5cd6e66 + bd6c84b commit e7600d8
Show file tree
Hide file tree
Showing 14 changed files with 84 additions and 43 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ DS_Store
.rvmrc
.ruby-version
.ruby-gemset
.secret

/public/assets

Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
source 'https://rubygems.org'

# Core gems
gem 'rails', '3.2.16'
gem 'rails', '3.2.17'

# Database adapters
gem 'pg'
Expand Down
52 changes: 26 additions & 26 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
GEM
remote: https://rubygems.org/
specs:
actionmailer (3.2.16)
actionpack (= 3.2.16)
actionmailer (3.2.17)
actionpack (= 3.2.17)
mail (~> 2.5.4)
actionpack (3.2.16)
activemodel (= 3.2.16)
activesupport (= 3.2.16)
actionpack (3.2.17)
activemodel (= 3.2.17)
activesupport (= 3.2.17)
builder (~> 3.0.0)
erubis (~> 2.7.0)
journey (~> 1.0.4)
rack (~> 1.4.5)
rack-cache (~> 1.2)
rack-test (~> 0.6.1)
sprockets (~> 2.2.1)
activemodel (3.2.16)
activesupport (= 3.2.16)
activemodel (3.2.17)
activesupport (= 3.2.17)
builder (~> 3.0.0)
activerecord (3.2.16)
activemodel (= 3.2.16)
activesupport (= 3.2.16)
activerecord (3.2.17)
activemodel (= 3.2.17)
activesupport (= 3.2.17)
arel (~> 3.0.2)
tzinfo (~> 0.3.29)
activeresource (3.2.16)
activemodel (= 3.2.16)
activesupport (= 3.2.16)
activesupport (3.2.16)
activeresource (3.2.17)
activemodel (= 3.2.17)
activesupport (= 3.2.17)
activesupport (3.2.17)
i18n (~> 0.6, >= 0.6.4)
multi_json (~> 1.0)
addressable (2.3.6)
Expand Down Expand Up @@ -211,22 +211,22 @@ GEM
rack
rack-test (0.6.2)
rack (>= 1.0)
rails (3.2.16)
actionmailer (= 3.2.16)
actionpack (= 3.2.16)
activerecord (= 3.2.16)
activeresource (= 3.2.16)
activesupport (= 3.2.16)
rails (3.2.17)
actionmailer (= 3.2.17)
actionpack (= 3.2.17)
activerecord (= 3.2.17)
activeresource (= 3.2.17)
activesupport (= 3.2.17)
bundler (~> 1.0)
railties (= 3.2.16)
railties (3.2.16)
actionpack (= 3.2.16)
activesupport (= 3.2.16)
railties (= 3.2.17)
railties (3.2.17)
actionpack (= 3.2.17)
activesupport (= 3.2.17)
rack-ssl (~> 1.3.2)
rake (>= 0.8.7)
rdoc (~> 3.4)
thor (>= 0.14.6, < 2.0)
rake (10.2.2)
rake (10.3.1)
rb-fsevent (0.9.4)
rb-inotify (0.9.3)
ffi (>= 0.5.0)
Expand Down Expand Up @@ -333,7 +333,7 @@ DEPENDENCIES
poltergeist
pry-rails
quiet_assets
rails (= 3.2.16)
rails (= 3.2.17)
remotipart
rspec-rails
sass (= 3.2.13)
Expand Down
7 changes: 2 additions & 5 deletions app/controllers/channels_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ class ChannelsController < ApplicationController
before_filter :authenticate_user!
before_filter :find_channel_by_name, :only => :show
load_and_authorize_resource
before_filter :set_channel_owner, only: :create

def index
# NOTE Eager loading doesn't respect limit
Expand All @@ -27,6 +26,8 @@ def index
end

def create
@channel = Channel.new(params[:channel])
@channel.user_id = current_user.id
respond_to do |format|
if @channel.save
format.json { render :json => @channel, :status => :created }
Expand Down Expand Up @@ -63,8 +64,4 @@ def destroy
def find_channel_by_name
@channel = Channel.where("LOWER(name) = ?", params['id'].downcase).first
end

def set_channel_owner
@channel.user = current_user
end
end
1 change: 1 addition & 0 deletions app/models/activity.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class Activity < ActiveRecord::Base
belongs_to :user
belongs_to :channel
attr_accessible :content, :action

paginates_per Kandan::Config.options[:per_page]

Expand Down
4 changes: 2 additions & 2 deletions app/models/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ class Attachment < ActiveRecord::Base
belongs_to :channel
belongs_to :user

attr_accessible :file

if ENV['S3_BUCKET']
has_attached_file(:file, {
:storage => :s3,
Expand All @@ -19,8 +21,6 @@ class Attachment < ActiveRecord::Base
has_attached_file :file
do_not_validate_attachment_file_type :file
end

attr_accessible :file

def url
file.to_s
Expand Down
4 changes: 2 additions & 2 deletions app/models/attachment_observer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ class AttachmentObserver < ActiveRecord::Observer

def after_save(attachment)
activity = Activity.new(
:channel_id => attachment.channel_id,
:user_id => attachment.user_id,
:action => "upload",
:content => attachment.url
)
activity.channel_id = attachment.channel_id
activity.user_id = attachment.user_id
activity.save
end

Expand Down
11 changes: 9 additions & 2 deletions app/models/channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class Channel < ActiveRecord::Base
has_many :activities, :dependent => :destroy
has_many :attachments, :dependent => :destroy
belongs_to :user
attr_accessible :name

validates :name, :presence => { :message => "Room name cannot be blank"}, :uniqueness => { :message => "Room name is already taken" }
validates :user, :presence => { :message => "Room must belong to a user"}
Expand All @@ -22,11 +23,17 @@ def primary
end

def user_connect(user)
activity = Channel.primary.activities.create!(:user_id => user.id, :action => "connect")
activity = Channel.primary.activities.new
activity.user_id = user.id
activity.action = "connect"
activity.save!
end

def user_disconnect(user)
activity = Channel.primary.activities.create!(:user_id => user.id, :action => "disconnect")
activity = Channel.primary.activities.new
activity.user_id = user.id
activity.action = "disconnect"
activity.save!
end
end
end
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class User < ActiveRecord::Base
devise devise *Kandan.devise_modules

# Setup accessible (or protected) attributes for your model
attr_accessible :id, :username, :email, :password, :password_confirmation, :remember_me, :first_name, :last_name, :locale, :gravatar_hash, :registration_status, :avatar_url
attr_accessible :username, :email, :password, :password_confirmation, :remember_me, :first_name, :last_name, :locale, :gravatar_hash, :avatar_url

def full_name
"#{self.first_name.to_s} #{self.last_name.to_s}".titlecase
Expand Down
3 changes: 2 additions & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ class Application < Rails::Application
# This will create an empty whitelist of attributes available for mass-assignment for all models
# in your app. As such, your models will need to explicitly whitelist or blacklist accessible
# parameters by using an attr_accessible or attr_protected declaration.
# config.active_record.whitelist_attributes = true
config.active_record.whitelist_attributes = true
config.active_record.mass_assignment_sanitizer = :strict

# Enable the asset pipeline
config.assets.enabled = true
Expand Down
2 changes: 1 addition & 1 deletion config/initializers/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# note that it will be overwritten if you use your own mailer class with default "from" parameter.
config.mailer_sender = "no-reply@kandan.com"

config.secret_key = '2a9ce1b0e9c5637175f692e9bcf57100fda5eb903a9540679f44f19ec7835eae95851c0f5854f6b0eadaae6b2f55b1a8ba6e304a5c738b9f4b9e4f37462d1eb0'
#config.secret_key = '2a9ce1b0e9c5637175f692e9bcf57100fda5eb903a9540679f44f19ec7835eae95851c0f5854f6b0eadaae6b2f55b1a8ba6e304a5c738b9f4b9e4f37462d1eb0'
# Configure the class responsible to send e-mails.
# config.mailer = "Devise::Mailer"

Expand Down
21 changes: 20 additions & 1 deletion config/initializers/secret_token.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
require 'securerandom'
# Be sure to restart your server when you modify this file.

# Your secret key for verifying the integrity of signed cookies.
# If you change this key, all old signed cookies will become invalid!
# Make sure the secret is at least 30 characters and all random,
# no regular words or you'll be exposed to dictionary attacks.
Kandan::Application.config.secret_token = 'bb04f17da6af7d0c441a12973ce4594dc562fdf035987fa39d1034dffe2708ed1791dee4e09bd3ebd06769699bd063821fa40881724e2d3ebbb57cfe56f32c12'

def find_secure_token
token_file = Rails.root.join('.secret')
if ENV.key?('SECRET_KEY_BASE')
ENV['SECRET_KEY_BASE']
elsif File.exist? token_file
# Use the existing token.
File.read(token_file).chomp
else
# Generate a new token of 64 random hexadecimal characters and store it in token_file.
token = SecureRandom.hex(64)
File.write(token_file, token)
token
end
end

Kandan::Application.config.secret_token = find_secure_token
Kandan::Application.config.secret_key_base = find_secure_token

11 changes: 11 additions & 0 deletions spec/factories/attachment_factory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FactoryGirl.define do
sequence :file_name do |n|
"file#{n}.jpg"
end

factory :attachment do
file { generate(:file_name) }
user
channel
end
end
6 changes: 5 additions & 1 deletion spec/models/attachment_observer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

describe AttachmentObserver do
it "should create an activity on save" do

Activity.any_instance.should_receive(:save)
Attachment.new.save
a = Attachment.new
c = FactoryGirl.create(:channel)
a.stub(:channel).and_return(c)
a.save
end
end

0 comments on commit e7600d8

Please sign in to comment.