-
Notifications
You must be signed in to change notification settings - Fork 166
Add server-side validation for frontend errors filtering #10104
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| class FrontendErrorForm | ||
| include ActiveModel::Model | ||
| include ActionView::Helpers::TranslationHelper | ||
|
|
||
| validate :validate_filename_extension | ||
| validate :validate_filename_host | ||
|
|
||
| attr_reader :filename | ||
|
|
||
| def submit(filename:) | ||
| @filename = filename | ||
|
|
||
| FormResponse.new(success: valid?, errors:, serialize_error_details_only: true) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def validate_filename_extension | ||
| return if File.extname(filename.to_s) == '.js' | ||
| errors.add(:filename, :invalid_extension, message: t('errors.general')) | ||
| end | ||
|
|
||
| def validate_filename_host | ||
| begin | ||
| return if URI(filename.to_s).host == IdentityConfig.store.domain_name | ||
| rescue URI::InvalidURIError; end | ||
|
|
||
| errors.add(:filename, :invalid_host, message: t('errors.general')) | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe FrontendErrorForm do | ||
| let(:filename) { 'https://example.com/foo.js' } | ||
|
|
||
| subject(:form) { described_class.new } | ||
|
|
||
| before do | ||
| allow(IdentityConfig.store).to receive(:domain_name).and_return('example.com') | ||
| end | ||
|
|
||
| describe '#submit' do | ||
| subject(:result) { form.submit(filename:) } | ||
|
|
||
| context 'with valid filename' do | ||
| let(:filename) { 'https://example.com/foo.js' } | ||
|
|
||
| it 'is successful' do | ||
| expect(result.success?).to eq(true) | ||
| expect(result.errors).to eq({}) | ||
| end | ||
| end | ||
|
|
||
| context 'without filename' do | ||
| let(:filename) { nil } | ||
|
|
||
| it 'is unsuccessful' do | ||
| expect(result.success?).to eq(false) | ||
| expect(result.errors).to eq(filename: [t('errors.general'), t('errors.general')]) | ||
| end | ||
| end | ||
|
|
||
| context 'with filename without extension' do | ||
| let(:filename) { 'https://example.com/foo' } | ||
|
|
||
| it 'is unsuccessful' do | ||
| expect(result.success?).to eq(false) | ||
| expect(result.errors).to eq(filename: [t('errors.general')]) | ||
| end | ||
| end | ||
|
|
||
| context 'with filename having extension other than js' do | ||
| let(:filename) { 'https://example.com/foo.txt' } | ||
|
|
||
| it 'is unsuccessful' do | ||
| expect(result.success?).to eq(false) | ||
| expect(result.errors).to eq(filename: [t('errors.general')]) | ||
| end | ||
| end | ||
|
|
||
| context 'with filename from a different host' do | ||
| let(:filename) { 'https://wrong.example.com/foo.js' } | ||
|
|
||
| it 'is unsuccessful' do | ||
| expect(result.success?).to eq(false) | ||
| expect(result.errors).to eq(filename: [t('errors.general')]) | ||
| end | ||
| end | ||
|
|
||
| context 'with filename that cannot be parsed as url' do | ||
| let(:filename) { '{' } | ||
|
|
||
| it 'is unsuccessful' do | ||
| expect(result.success?).to eq(false) | ||
| expect(result.errors).to eq(filename: [t('errors.general'), t('errors.general')]) | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
do we 100% always get the filename? my guess is we'd want to allow a
nilfilename too just in case?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.
The spec implies that it must be a string, but that it could (at least by the defaults defined there) be empty. The behavior here matches the behavior on the client-side, which would ignore anything not explicitly
.js. Whether we should want to log those, I'd have a mild curiosity what (if anything) would be logged, but generally think we're interested in errors known to be originating from our own scripts.