File tree 3 files changed +52
-4
lines changed
3 files changed +52
-4
lines changed Original file line number Diff line number Diff line change 1
1
class PagesController < ApplicationController
2
+
3
+
2
4
def show
3
5
show_pages_params = params . permit ( :page , :layout )
4
- render action : show_pages_params [ :page ] , layout : ( show_pages_params [ :layout ] || 'application' )
6
+ page_to_show = whitelist_page ( show_pages_params [ :page ] )
7
+
8
+ render action : page_to_show , layout : whitelist_layout ( show_pages_params [ :layout ] )
9
+ end
10
+
11
+ private
12
+
13
+ # Checks whether the requested_page exists in app/views/pages/*.html.haml
14
+ def whitelist_page ( requested_page )
15
+ raise "Invalid page: #{ requested_page } " unless STATIC_PAGES . include? ( requested_page )
16
+
17
+ requested_page
18
+ end
19
+
20
+ def whitelist_layout ( requested_layout )
21
+ return 'application' if requested_layout . nil?
22
+
23
+ raise "Invalid layout: #{ requested_layout } " unless STATIC_PAGE_LAYOUTS . include? ( requested_layout )
24
+
25
+ requested_layout
5
26
end
6
27
end
Original file line number Diff line number Diff line change
1
+ # Look at the *.html.haml files in the app/views/pages directory
2
+ STATIC_PAGES ||= Dir . glob ( 'app/views/pages/*.html.{erb,haml}' )
3
+ . map { |f | File . basename ( f , '.html.erb' ) }
4
+ . map { |f | File . basename ( f , '.html.haml' ) }
5
+ . reject { |f | f =~ /^_/ }
6
+ . sort
7
+ . uniq
8
+
9
+ # Look at the *.html.haml files in the app/views/pages directory
10
+ STATIC_PAGE_LAYOUTS ||= Dir . glob ( 'app/views/layouts/*.html.{erb,haml}' )
11
+ . map { |f | File . basename ( f , '.html.erb' ) }
12
+ . map { |f | File . basename ( f , '.html.haml' ) }
13
+ . reject { |f | f =~ /^_/ }
14
+ . sort
15
+ . uniq
Original file line number Diff line number Diff line change 1
1
require 'spec_helper'
2
2
3
3
describe PagesController do
4
-
5
4
it 'should be able to access privacy policy while user is logged in but not registered' do
6
5
unregisterd_user = Fabricate ( :user , state : User ::REGISTRATION )
7
6
controller . send :sign_in , unregisterd_user
8
- get :show , page : 'tos'
7
+ get :show , page : 'tos' , layout : 'application'
9
8
response . should be_success
10
9
end
11
10
12
- end
11
+ it 'fails when presented an non-whitelisted page' do
12
+ unregisterd_user = Fabricate ( :user , state : User ::REGISTRATION )
13
+ controller . send :sign_in , unregisterd_user
14
+
15
+ expect { get :show , page : 'IMNOTREAL' } . to raise_error 'Invalid page: IMNOTREAL'
16
+ end
17
+
18
+ it 'fails when presented an non-whitelisted layout' do
19
+ unregisterd_user = Fabricate ( :user , state : User ::REGISTRATION )
20
+ controller . send :sign_in , unregisterd_user
21
+
22
+ expect { get :show , page : 'tos' , layout : 'IMNOTREAL' } . to raise_error 'Invalid layout: IMNOTREAL'
23
+ end
24
+ end
You can’t perform that action at this time.
0 commit comments