File tree Expand file tree Collapse file tree 4 files changed +56
-18
lines changed Expand file tree Collapse file tree 4 files changed +56
-18
lines changed Original file line number Diff line number Diff line change
1
+ This file is here only for the sake of a spec. It's not really a README.
Original file line number Diff line number Diff line change @@ -21,16 +21,16 @@ def show
21
21
def render_page ( name , title = nil )
22
22
page = DocPage . find ( name )
23
23
24
- if page
25
- title = title || page . title
26
- @page_title = [ title , "Administrate" ] . compact . join ( " - " )
27
- # rubocop:disable Rails/OutputSafety
28
- render layout : "docs" , html : page . body . html_safe
29
- # rubocop:enable Rails/OutputSafety
30
- else
31
- render file : Rails . root . join ( "public" , "404.html" ) ,
32
- layout : false ,
33
- status : :not_found
34
- end
24
+ title = title || page . title
25
+ @page_title = [ title , "Administrate" ] . compact . join ( " - " )
26
+ # rubocop:disable Rails/OutputSafety
27
+ render layout : "docs" , html : page . body . html_safe
28
+ # rubocop:enable Rails/OutputSafety
29
+ rescue DocPage :: PageNotAllowed , DocPage :: PageNotFound
30
+ render (
31
+ file : Rails . root . join ( "public" , "404.html" ) ,
32
+ layout : false ,
33
+ status : :not_found ,
34
+ )
35
35
end
36
36
end
Original file line number Diff line number Diff line change 1
1
class DocPage
2
+ class PageNotFound < StandardError
3
+ def initialize ( page )
4
+ "Could not find page #{ page . inspect } "
5
+ end
6
+ end
7
+
8
+ class PageNotAllowed < StandardError
9
+ def initialize ( page )
10
+ "Page #{ page . inspect } is not allowed"
11
+ end
12
+ end
13
+
2
14
class << self
3
15
def find ( page )
4
16
full_path = Rails . root + "../../#{ page } .md"
17
+ raise PageNotFound . new ( page ) unless path_exists? ( full_path )
18
+
19
+ safe_path = filter_unsafe_paths ( full_path )
20
+ raise PageNotAllowed . new ( page ) unless safe_path
21
+
22
+ text = File . read ( safe_path )
23
+ new ( text )
24
+ end
25
+
26
+ private
27
+
28
+ def path_exists? ( full_path )
29
+ File . exist? ( full_path )
30
+ end
31
+
32
+ def doc_paths
33
+ [
34
+ Dir . glob ( Rails . root + "../../**/*.md" ) ,
35
+ Dir . glob ( Rails . root + "../../*.md" ) ,
36
+ ] . join
37
+ end
5
38
6
- if File . exist? ( full_path )
7
- text = File . read ( full_path )
8
- new ( text )
9
- end
39
+ def filter_unsafe_paths ( full_path )
40
+ doc_paths [ full_path . to_s ]
10
41
end
11
42
end
12
43
Original file line number Diff line number Diff line change 2
2
3
3
RSpec . describe DocPage do
4
4
describe ".find" do
5
- it "is nil if the page doesn't exist" do
6
- page = DocPage . find ( "not_a_page" )
5
+ it "raises an error if the page doesn't exist" do
6
+ expect do
7
+ DocPage . find ( "not_a_page" )
8
+ end . to raise_error ( DocPage ::PageNotFound )
9
+ end
7
10
8
- expect ( page ) . to be_nil
11
+ it "raises an error on cheeky paths" do
12
+ expect do
13
+ DocPage . find ( "docs/../spec/example_app/README" )
14
+ end . to raise_error ( DocPage ::PageNotAllowed )
9
15
end
10
16
11
17
it "renders pages without metadata" do
You can’t perform that action at this time.
0 commit comments