Skip to content

Commit 62515d3

Browse files
alzeihashmaroli
andauthored
Prevent unexpected file overwrites for collections, drafts and pages (#582)
For collections, drafts and pages, return HTTP 404 if a resource being created or renamed into will overwrite an existing item. Additionally, log a warning in the terminal as to why the front-end dispatched an error-notification. Co-authored-by: Ashwin Maroli <ashmaroli@users.noreply.github.com>
1 parent ef813e4 commit 62515d3

File tree

6 files changed

+31
-4
lines changed

6 files changed

+31
-4
lines changed

lib/jekyll-admin/file_helper.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ def ensure_written_file
5252
ensure_file(written_file)
5353
end
5454

55+
def ensure_not_overwriting_existing_file
56+
ensure_not_file(written_file)
57+
end
58+
5559
def find_by_path(path)
5660
files = case namespace
5761
when "collections"
@@ -72,6 +76,14 @@ def ensure_file(file)
7276
render_404 if file.nil?
7377
end
7478

79+
def ensure_not_file(file)
80+
return if file.nil?
81+
82+
Jekyll.logger.warn "Jekyll Admin:", "Could not create file."
83+
Jekyll.logger.warn "", "Path #{file.relative_path.inspect} already exists!"
84+
render_404
85+
end
86+
7587
def ensure_directory
7688
render_404 unless Dir.exist?(directory_path)
7789
end

lib/jekyll-admin/path_helper.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ def renamed?
5151
ensure_leading_slash(request_payload["path"]) != relative_path
5252
end
5353

54+
# Is this request creating a new file?
55+
def new?
56+
!request_payload["path"]
57+
end
58+
5459
private
5560

5661
# Returns the path to the requested file's containing directory

lib/jekyll-admin/server/collections.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@ class Server < Sinatra::Base
2525
put "/:collection_id/*?/?:path.:ext" do
2626
ensure_collection
2727

28-
if renamed?
28+
if new?
29+
ensure_not_overwriting_existing_file
30+
elsif renamed?
2931
ensure_requested_file
32+
ensure_not_overwriting_existing_file
3033
delete_file_without_process path
3134
end
3235

lib/jekyll-admin/server/drafts.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ class Server < Sinatra::Base
1616
put "/*?/?:path.:ext" do
1717
ensure_html_content
1818

19-
if renamed?
19+
if new?
20+
ensure_not_overwriting_existing_file
21+
elsif renamed?
2022
ensure_requested_file
23+
ensure_not_overwriting_existing_file
2124
delete_file_without_process path
2225
end
2326

lib/jekyll-admin/server/pages.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ class Server < Sinatra::Base
1616
put "/*?/?:path.:ext" do
1717
ensure_html_content
1818

19-
if renamed?
19+
if new?
20+
ensure_not_overwriting_existing_file
21+
elsif renamed?
2022
ensure_requested_file
23+
ensure_not_overwriting_existing_file
2124
delete_file_without_process path
2225
end
2326

2427
write_file write_path, page_body
25-
2628
json written_file.to_api(:include_content => true)
2729
end
2830

spec/jekyll-admin/server/collection_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ def app
267267
write_file "_posts/2016-01-01-test2.md"
268268

269269
request = {
270+
:path => "_posts/2016-01-01-test2.md",
270271
:front_matter => { :foo => "bar2" },
271272
:raw_content => "test",
272273
}
@@ -283,6 +284,7 @@ def app
283284
write_file "_posts/more posts/2016-01-01-test2.md"
284285

285286
request = {
287+
:path => "_posts/more posts/2016-01-01-test2.md",
286288
:front_matter => { :foo => "bar2" },
287289
:raw_content => "test",
288290
}

0 commit comments

Comments
 (0)