-
Notifications
You must be signed in to change notification settings - Fork 5
Add turnstile token to subscription #810
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
10 commits
Select commit
Hold shift + click to select a range
3c87df7
Config turnstile screte and add test
cocomarine 35fe29d
linting
cocomarine d7426c4
update controller
cocomarine dc09c57
Merge branch 'main' into 1353-add-turnstile-token-to-subscription
cocomarine 04866c6
add turnstile key to env
cocomarine e7a835f
update errors and test
cocomarine e2e4784
update params and add timeouts to faraday
cocomarine d6044f9
fix faraday connection
cocomarine 21ee612
Merge branch 'main' into 1353-add-turnstile-token-to-subscription
cocomarine ff6615a
extract out turntile verifier and update tests
cocomarine 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
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,50 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Subscriptions | ||
| class TurnstileVerifier | ||
| API_URL = 'https://challenges.cloudflare.com/turnstile/v0/siteverify' | ||
|
|
||
| def initialize(token:, remote_ip:, secret_key:) | ||
| @token = token | ||
| @remote_ip = remote_ip | ||
| @secret_key = secret_key | ||
| end | ||
|
|
||
| def passed? | ||
| return false if @token.blank? | ||
|
|
||
| response = faraday.post( | ||
| API_URL, | ||
| { | ||
| secret: secret_key, | ||
| response: token, | ||
| remoteip: remote_ip | ||
| } | ||
| ) | ||
| unless response.success? | ||
| Rails.logger.warn("[subscriptions#create] turnstile verification skipped: HTTP #{response.status}") | ||
| return true # fail open | ||
| end | ||
|
|
||
| JSON.parse(response.body)['success'] == true | ||
| rescue Faraday::Error, JSON::ParserError => e | ||
| Sentry.capture_exception(e) | ||
| Rails.logger.warn("[subscriptions#create] turnstile verification error: #{e.message}") | ||
| # Fail open to allow the request through if verification is unavailable | ||
| # due to network issues, Cloudflare downtime or malformed responses etc. | ||
| true | ||
| end | ||
|
|
||
| private | ||
|
|
||
| attr_reader :secret_key, :remote_ip, :token | ||
|
|
||
| def faraday | ||
| @faraday ||= Faraday.new do |f| | ||
| f.request :url_encoded | ||
| f.options.timeout = 5 | ||
| f.options.open_timeout = 2 | ||
| 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
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,86 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe Subscriptions::TurnstileVerifier do | ||
| let(:secret_key) { 'test-secret' } | ||
| let(:remote_ip) { '127.0.0.1' } | ||
| let(:token) { 'test-token' } | ||
| let(:verifier) { described_class.new(token:, remote_ip:, secret_key:) } | ||
| let(:connection) { instance_double(Faraday::Connection) } | ||
| let(:response) do | ||
| instance_double(Faraday::Response, success?: true, status: 200, body: { success: true }.to_json) | ||
| end | ||
|
|
||
| before do | ||
| allow(verifier).to receive(:faraday).and_return(connection) | ||
| allow(connection).to receive(:post).and_return(response) | ||
| allow(Sentry).to receive(:capture_exception) | ||
| end | ||
|
|
||
| describe '#passed?' do | ||
| it 'posts to Cloudflare siteverify with the correct params' do | ||
| verifier.passed? | ||
|
|
||
| expect(connection).to have_received(:post).with( | ||
| described_class::API_URL, | ||
| { secret: secret_key, response: token, remoteip: remote_ip } | ||
| ) | ||
| end | ||
|
|
||
| context 'when turnstile token is missing' do | ||
| let(:token) { '' } | ||
|
|
||
| it 'returns false without calling Cloudflare' do | ||
| expect(verifier.passed?).to be(false) | ||
| expect(connection).not_to have_received(:post) | ||
| end | ||
| end | ||
|
|
||
| context 'when turnstile token is valid' do | ||
| it { expect(verifier.passed?).to be(true) } | ||
| end | ||
|
|
||
| context 'when Cloudflare rejects the token' do | ||
| let(:response) do | ||
| instance_double(Faraday::Response, success?: true, status: 200, body: { success: false }.to_json) | ||
| end | ||
|
|
||
| it { expect(verifier.passed?).to be(false) } | ||
| end | ||
|
|
||
| context 'when Cloudflare returns a server error' do | ||
| let(:response) do | ||
| instance_double(Faraday::Response, success?: false, status: 500, body: 'Internal Server Error') | ||
| end | ||
|
|
||
| it { expect(verifier.passed?).to be(true) } | ||
| end | ||
|
|
||
| context 'when Cloudflare returns malformed JSON' do | ||
| let(:response) do | ||
| instance_double(Faraday::Response, success?: true, status: 200, body: 'not-json') | ||
| end | ||
|
|
||
| it { expect(verifier.passed?).to be(true) } | ||
|
|
||
| it 'reports the error to Sentry' do | ||
| verifier.passed? | ||
| expect(Sentry).to have_received(:capture_exception).with(be_a(JSON::ParserError)) | ||
| end | ||
| end | ||
|
|
||
| context 'when the Cloudflare connection fails' do | ||
| before do | ||
| allow(connection).to receive(:post).and_raise(Faraday::ConnectionFailed.new('connection failed')) | ||
| end | ||
|
|
||
| it { expect(verifier.passed?).to be(true) } | ||
|
|
||
| it 'reports the error to Sentry' do | ||
| verifier.passed? | ||
| expect(Sentry).to have_received(:capture_exception).with(be_a(Faraday::Error)) | ||
| end | ||
| end | ||
| end | ||
| end |
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.
Uh oh!
There was an error while loading. Please reload this page.