forked from docusealco/docuseal
-
Notifications
You must be signed in to change notification settings - Fork 0
CP-13579 - Add page limit guards to PDF uploads #90
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
6 commits
Select commit
Hold shift + click to select a range
7ac0d03
CP-13579 - Add log-only page-limit metrics endpoint
bernardodsanderson 70b86ba
CP-13579 - Add failing system spec for builder page-limit guard
bernardodsanderson 1f26920
CP-13579 - Enforce page limit in builder uploads
bernardodsanderson fc46625
CP-13579 - Enforce page limit in dashboard uploads
bernardodsanderson d1e9ab5
CP-13579 - Fix review findings on guard scope and metrics auth
bernardodsanderson 4432813
CP-13579 - Address review feedback on metrics endpoint and message copy
bernardodsanderson 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,62 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class PageLimitEventsController < ApplicationController | ||
| include IframeAuthentication | ||
|
|
||
| skip_before_action :verify_authenticity_token, only: [:create] | ||
| skip_before_action :authenticate_via_token! | ||
|
|
||
| before_action :authenticate_from_referer | ||
|
|
||
| skip_authorization_check | ||
|
|
||
| SURFACES = %w[builder dashboard].freeze | ||
| PAGE_LIMIT = 120 | ||
| MAX_INTEGER_LENGTH = 10 | ||
|
|
||
| def create | ||
| return head :unauthorized if current_user.blank? | ||
|
|
||
| page_count = parse_integer(params[:page_count]) | ||
| file_size = params[:file_size].present? ? parse_integer(params[:file_size]) : nil | ||
|
|
||
| unless valid_event_params?(page_count, file_size) | ||
| return render json: { error: 'Invalid parameters' }, status: :unprocessable_entity | ||
| end | ||
|
|
||
| Rails.logger.info({ | ||
| event: 'page_limit_blocked', | ||
| page_count:, | ||
| bucket: bucket_for(page_count), | ||
| surface: params[:surface], | ||
| file_size:, | ||
| account_id: current_account&.id, | ||
| user_id: current_user&.id | ||
| }.to_json) | ||
|
|
||
| head :no_content | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def valid_event_params?(page_count, file_size) | ||
| return false if page_count.blank? || page_count <= PAGE_LIMIT | ||
| return false unless params[:surface].in?(SURFACES) | ||
| return true if params[:file_size].blank? | ||
|
|
||
| file_size.present? && !file_size.negative? | ||
| end | ||
|
|
||
| def bucket_for(page_count) | ||
| return '121-150' if page_count <= 150 | ||
| return '151-200' if page_count <= 200 | ||
|
|
||
| '201+' | ||
| end | ||
|
|
||
| def parse_integer(value) | ||
| return nil unless value.to_s.match?(/\A\d{1,#{MAX_INTEGER_LENGTH}}\z/o) | ||
|
|
||
| Integer(value, exception: false) | ||
| 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
Oops, something went wrong.
Oops, something went wrong.
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.
[non-blocking] This endpoint has no rate limiting. Since verify_authenticity_token is skipped for create, any signed in user can call it repeatedly to flood the logs with fabricated events. Worth a throttle given its only job is metrics.
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.
Good callout. There's currently no throttle infra in the app (no Rack::Attack; cache is per-process
memory_store,null_storein test), so a hand-rolled throttle here would be weak and untestable. The endpoint requires auth and stamps every hit with account/user id, so abuse is attributable. Happy to add Rack::Attack as a follow-up if you want it.