-
Notifications
You must be signed in to change notification settings - Fork 113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ensure letter is within letters base path #110
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -56,17 +56,19 @@ def attachments | |||||
end | ||||||
|
||||||
def delete | ||||||
FileUtils.rm_rf("#{LetterOpenerWeb.config.letters_location}/#{id}") | ||||||
return unless valid? | ||||||
|
||||||
FileUtils.rm_rf(base_dir.to_s) | ||||||
end | ||||||
|
||||||
def exists? | ||||||
File.exist?(base_dir) | ||||||
def valid? | ||||||
exists? && base_dir_within_letters_location? | ||||||
end | ||||||
|
||||||
private | ||||||
|
||||||
def base_dir | ||||||
"#{LetterOpenerWeb.config.letters_location}/#{id}" | ||||||
LetterOpenerWeb.config.letters_location.join(id).cleanpath | ||||||
end | ||||||
|
||||||
def read_file(style) | ||||||
|
@@ -77,6 +79,14 @@ def style_exists?(style) | |||||
File.exist?("#{base_dir}/#{style}.html") | ||||||
end | ||||||
|
||||||
def exists? | ||||||
File.exist?(base_dir) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thought: As
Suggested change
But I wanted to keep the diff as small as possible. |
||||||
end | ||||||
|
||||||
def base_dir_within_letters_location? | ||||||
base_dir.to_s.start_with?(LetterOpenerWeb.config.letters_location.to_s) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thought: This is the main change to ensure |
||||||
end | ||||||
|
||||||
def adjust_link_targets(contents) | ||||||
# We cannot feed the whole file to an XML parser as some mails are | ||||||
# "complete" (as in they have the whole <html> structure) and letter_opener | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
describe LetterOpenerWeb::Letter do | ||
let(:location) { File.expand_path('../../tmp', __dir__) } | ||
let(:location) { Pathname.new(__dir__).join('..', '..', 'tmp').cleanpath } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thought: |
||
|
||
def rich_text(mail_id) | ||
|
@@ -128,13 +128,24 @@ def rich_text(mail_id) | |
|
||
describe '#delete' do | ||
let(:id) { '1111_1111' } | ||
|
||
subject { described_class.new(id: id).delete } | ||
|
||
it'removes the letter with given id' do | ||
it 'removes the letter with given id' do | ||
subject | ||
directories = Dir["#{location}/*"] | ||
expect(directories.count).to eql(1) | ||
expect(directories.first).not_to match(id) | ||
end | ||
|
||
context 'when the id is outside of the letters base path' do | ||
let(:id) { '../3333_3333' } | ||
|
||
it 'does not remove the letter' do | ||
expect(FileUtils).not_to receive(:rm_rf).with(location.join(id).cleanpath.to_s) | ||
|
||
expect(subject).to be_nil | ||
end | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thought:
LetterOpenerWeb.config.letters_location
is actually an instance ofPathname
so we can leverage its powers 🙂.#cleanpath
is key here because it removes dots and slashes.