Skip to content
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

Merged
merged 2 commits into from
Oct 6, 2021

Conversation

ashmckenzie
Copy link
Contributor

This PR ensures that when attempting to view or delete a letter, the provided :id is within the letters base path.

end

private

def base_dir
"#{LetterOpenerWeb.config.letters_location}/#{id}"
LetterOpenerWeb.config.letters_location.join(id).cleanpath
Copy link
Contributor Author

@ashmckenzie ashmckenzie Sep 29, 2021

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 of Pathname so we can leverage its powers 🙂.

#cleanpath is key here because it removes dots and slashes.

@@ -77,6 +79,14 @@ def style_exists?(style)
File.exist?("#{base_dir}/#{style}.html")
end

def exists?
File.exist?(base_dir)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought: As #base_dir is actually an instance of Pathname, we could also change this to:

Suggested change
File.exist?(base_dir)
base_dir.exist?

But I wanted to keep the diff as small as possible.

@@ -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 }
Copy link
Contributor Author

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 of Pathname so we can leverage its powers 🙂

end

def base_dir_within_letters_location?
base_dir.to_s.start_with?(LetterOpenerWeb.config.letters_location.to_s)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought: This is the main change to ensure base_dir starts with LetterOpenerWeb.config.letters_location. It leans on #base_dir returning a 'clean path'.

@ashmckenzie
Copy link
Contributor Author

@fgrehm for your review, please 🙂 🤞

@fgrehm
Copy link
Owner

fgrehm commented Oct 6, 2021

Thank you!

@fgrehm fgrehm merged commit ae7e362 into fgrehm:master Oct 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants