captchafox 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +6 -0
- data/LICENSE +202 -0
- data/README.md +126 -0
- data/lib/captchafox/client.rb +191 -0
- data/lib/captchafox/verification.rb +31 -0
- data/lib/captchafox/verification_unavailable_error.rb +9 -0
- data/lib/captchafox/version.rb +5 -0
- data/lib/captchafox.rb +10 -0
- metadata +54 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 248b58fcafdd05af4a7695803bb7119bc60c6d1ef15a577116c0b32844626eb6
|
|
4
|
+
data.tar.gz: 976e02256ed07a5ffa5d098746fdefda2ec9c9291ee298c9c401247839cc01c4
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 464ae76e484d91ae406dfb76cb80bae6955313bf236876911497cb184d990d1233ac38e3742de58e480e91777398e27d21d31a8e2bbea696bb91ec6bbce81d37
|
|
7
|
+
data.tar.gz: 459fcc447e8a3be7b374e451536d59b52bdf4e4f4f95f929c04a9030b26fef535ae75c847473715dd1a8b8e8fba0de2335ab161b25fc0fecac6219f1abd2299d
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
## 1.0.0
|
|
2
|
+
|
|
3
|
+
- Verify CaptchaFox tokens with siteverify: `CaptchaFox::Client#verify` returns `CaptchaFox::Verification::Passed` or
|
|
4
|
+
`Rejected`, and raises `CaptchaFox::VerificationUnavailableError` when CaptchaFox cannot be reached in time or
|
|
5
|
+
answers unexpectedly.
|
|
6
|
+
- Ruby 3.3 and later, no runtime dependencies.
|
data/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
data/README.md
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# CaptchaFox for Ruby
|
|
2
|
+
|
|
3
|
+
Official [CaptchaFox](https://captchafox.com) integration for Ruby. It verifies the answers of the widget with
|
|
4
|
+
CaptchaFox.
|
|
5
|
+
|
|
6
|
+
## Compatibility
|
|
7
|
+
|
|
8
|
+
| Ruby | Library |
|
|
9
|
+
|---|---|
|
|
10
|
+
| 3.3, 3.4, 4.0 | 1.x |
|
|
11
|
+
|
|
12
|
+
Ruby 3.2 and older are not supported: RubyGems and Bundler do not install the library there.
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
bundle add captchafox
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
or in your `Gemfile`:
|
|
21
|
+
|
|
22
|
+
```ruby
|
|
23
|
+
gem "captchafox", "~> 1.0"
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Without Bundler, run `gem install captchafox`. The library needs nothing but the libraries Ruby ships.
|
|
27
|
+
|
|
28
|
+
## Configuration
|
|
29
|
+
|
|
30
|
+
### Get your keys
|
|
31
|
+
|
|
32
|
+
In the [CaptchaFox portal](https://portal.captchafox.com), open your website under **Sites > Site Details** and copy
|
|
33
|
+
the **site key** and the **secret key**.
|
|
34
|
+
|
|
35
|
+
### Set up the library
|
|
36
|
+
|
|
37
|
+
Create a `CaptchaFox::Client` once with the secret key of your site and reuse it; it is thread-safe. Keep the secret
|
|
38
|
+
key in your configuration or environment, not in source code:
|
|
39
|
+
|
|
40
|
+
```ruby
|
|
41
|
+
require "captchafox"
|
|
42
|
+
|
|
43
|
+
CAPTCHAFOX = CaptchaFox::Client.new(ENV.fetch("CAPTCHAFOX_SECRET"))
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Options
|
|
47
|
+
|
|
48
|
+
| Option | Default | Meaning |
|
|
49
|
+
|---|---|---|
|
|
50
|
+
| `timeout` | 5 seconds | How long a verification may take, from connecting to CaptchaFox to the end of its answer: `CaptchaFox::Client.new(secret, timeout: 3)`. |
|
|
51
|
+
|
|
52
|
+
## Usage
|
|
53
|
+
|
|
54
|
+
The widget adds the token to the form as the POST parameter `cf-captcha-response`. Verify it on the server before
|
|
55
|
+
the form is processed, here in a Rails controller:
|
|
56
|
+
|
|
57
|
+
```ruby
|
|
58
|
+
begin
|
|
59
|
+
verification = CAPTCHAFOX.verify(params["cf-captcha-response"], sitekey: SITE_KEY, remote_ip: request.remote_ip)
|
|
60
|
+
unless verification.passed?
|
|
61
|
+
# Reject the form.
|
|
62
|
+
end
|
|
63
|
+
rescue CaptchaFox::VerificationUnavailableError
|
|
64
|
+
# CaptchaFox gave no usable answer: decide whether the form passes.
|
|
65
|
+
end
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
`verify(token)` sends the token alone; `verify(token, sitekey:, remote_ip:)` also sends the site key the token was
|
|
69
|
+
issued for and the visitor's IP address, each only when it is not `nil` or empty. Each of them is a `String` or
|
|
70
|
+
`nil`; anything else, such as a form field sent as an array, raises `TypeError`.
|
|
71
|
+
|
|
72
|
+
A verification has one of three outcomes:
|
|
73
|
+
|
|
74
|
+
- `CaptchaFox::Verification::Passed`: CaptchaFox accepted the token. `challenge_ts` and `hostname` hold the time of
|
|
75
|
+
the challenge and the hostname of the site, where CaptchaFox sent them.
|
|
76
|
+
- `CaptchaFox::Verification::Rejected`: CaptchaFox rejected the token; `error_codes` holds its error codes, such as
|
|
77
|
+
`timeout-or-duplicate`, possibly none. An empty or missing token is rejected with `missing-input-response` without
|
|
78
|
+
asking CaptchaFox.
|
|
79
|
+
- `CaptchaFox::VerificationUnavailableError`: CaptchaFox gave no usable answer; see
|
|
80
|
+
[If CaptchaFox is unreachable](#if-captchafox-is-unreachable).
|
|
81
|
+
|
|
82
|
+
`Passed` and `Rejected` are objects, and every object is true in a condition: check `passed?` or the class, never the
|
|
83
|
+
result of `verify` alone. Both work with pattern matching:
|
|
84
|
+
|
|
85
|
+
```ruby
|
|
86
|
+
case CAPTCHAFOX.verify(token)
|
|
87
|
+
in CaptchaFox::Verification::Passed(hostname:)
|
|
88
|
+
# ...
|
|
89
|
+
in CaptchaFox::Verification::Rejected(error_codes:)
|
|
90
|
+
# ...
|
|
91
|
+
end
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Operation
|
|
95
|
+
|
|
96
|
+
### If CaptchaFox is unreachable
|
|
97
|
+
|
|
98
|
+
When CaptchaFox cannot be reached, does not complete its answer within the timeout, answers with an HTTP status other
|
|
99
|
+
than 200, or with a body that is no valid answer, `verify` raises `CaptchaFox::VerificationUnavailableError`. Your
|
|
100
|
+
code decides whether the form passes then: letting it through keeps your forms available during an outage, rejecting
|
|
101
|
+
it keeps them protected. Tokens that CaptchaFox rejects are always `Rejected`.
|
|
102
|
+
|
|
103
|
+
### Log
|
|
104
|
+
|
|
105
|
+
The library logs and prints nothing. The messages of its errors never contain the secret key. An error names the
|
|
106
|
+
class of the failure underneath in its message and keeps no cause.
|
|
107
|
+
|
|
108
|
+
### Visitor IP
|
|
109
|
+
|
|
110
|
+
The visitor's IP address is sent only when you pass it as `remote_ip`. Behind a reverse proxy or load balancer, pass
|
|
111
|
+
the visitor's address that the proxy forwards, not the address of the proxy.
|
|
112
|
+
|
|
113
|
+
### Proxy
|
|
114
|
+
|
|
115
|
+
The library connects to CaptchaFox directly. It does not use a proxy that the environment names, such as
|
|
116
|
+
`http_proxy`.
|
|
117
|
+
|
|
118
|
+
### Privacy
|
|
119
|
+
|
|
120
|
+
CaptchaFox is operated by Scoria Labs GmbH, Germany. How CaptchaFox processes data and how to get a data processing
|
|
121
|
+
agreement is described in the [CaptchaFox Privacy Center](https://captchafox.com/privacy). A suggested paragraph for
|
|
122
|
+
your privacy policy is on the [GDPR page](https://captchafox.com/privacy/gdpr).
|
|
123
|
+
|
|
124
|
+
## License
|
|
125
|
+
|
|
126
|
+
Apache-2.0. See [LICENSE](LICENSE).
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "net/http"
|
|
5
|
+
require "timeout"
|
|
6
|
+
require "uri"
|
|
7
|
+
|
|
8
|
+
module CaptchaFox
|
|
9
|
+
# Verifies tokens of the CaptchaFox widget with the secret key of a site. Create it once and reuse it; it is
|
|
10
|
+
# thread-safe.
|
|
11
|
+
class Client
|
|
12
|
+
ENDPOINT = URI("https://api.captchafox.com/siteverify").freeze
|
|
13
|
+
private_constant :ENDPOINT
|
|
14
|
+
|
|
15
|
+
# Every string of a body, with the colon that makes it a member name, or a slash outside strings.
|
|
16
|
+
TOKENS = /"(?:[^"\\]|\\.)*"(\s*:)?|\//m
|
|
17
|
+
private_constant :TOKENS
|
|
18
|
+
|
|
19
|
+
# timeout is how long a verification may take in seconds, from connecting to siteverify to the end of its answer.
|
|
20
|
+
def initialize(secret_key, timeout: 5)
|
|
21
|
+
# Error trackers record the local variables of the frames an exception passes, so the secret key leaves the
|
|
22
|
+
# locals of this frame before an argument error can be raised here.
|
|
23
|
+
@secret_key = secret_key
|
|
24
|
+
secret_key = nil
|
|
25
|
+
|
|
26
|
+
unless @secret_key.is_a?(String)
|
|
27
|
+
raise TypeError, "the secret key is no String"
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
if @secret_key.empty?
|
|
31
|
+
raise ArgumentError, "the secret key is empty"
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
unless timeout.is_a?(Numeric) && timeout.real? && timeout.finite? && timeout.positive?
|
|
35
|
+
raise ArgumentError, "the timeout is not a positive number of seconds"
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
@timeout = timeout
|
|
39
|
+
@endpoint = ENDPOINT
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# Verifies a token, issued for sitekey to a visitor at remote_ip; each of them is sent only when it is not nil or
|
|
43
|
+
# empty. A nil or empty token is rejected with "missing-input-response" without a request. Returns a
|
|
44
|
+
# Verification::Passed or Verification::Rejected; raises VerificationUnavailableError if siteverify cannot be
|
|
45
|
+
# evaluated.
|
|
46
|
+
def verify(token, sitekey: nil, remote_ip: nil)
|
|
47
|
+
{ token: token, sitekey: sitekey, remote_ip: remote_ip }.each do |name, value|
|
|
48
|
+
unless value.nil? || value.is_a?(String)
|
|
49
|
+
raise TypeError, "#{name} is no String"
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
if token.nil? || token.empty?
|
|
54
|
+
return Verification::Rejected.new(error_codes: ["missing-input-response"])
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
evaluate(exchange(request(token, sitekey, remote_ip)))
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def inspect
|
|
61
|
+
"#<#{self.class.name} timeout=#{@timeout}>"
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
private
|
|
65
|
+
|
|
66
|
+
# Error trackers record the local variables of the frames an exception passes, so the form with the secret key is
|
|
67
|
+
# built in this frame, which ends before the network is used. The inspect form of the request leaves its body out.
|
|
68
|
+
def request(token, sitekey, remote_ip)
|
|
69
|
+
fields = { "secret" => @secret_key, "response" => token }
|
|
70
|
+
if sitekey && !sitekey.empty?
|
|
71
|
+
fields["sitekey"] = sitekey
|
|
72
|
+
end
|
|
73
|
+
if remote_ip && !remote_ip.empty?
|
|
74
|
+
fields["remoteIp"] = remote_ip
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
request = Net::HTTP::Post.new(@endpoint, "User-Agent" => "captchafox-ruby/#{VERSION}")
|
|
78
|
+
request.set_form_data(fields)
|
|
79
|
+
request
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def exchange(request)
|
|
83
|
+
options = {
|
|
84
|
+
use_ssl: @endpoint.scheme == "https",
|
|
85
|
+
# Net::HTTP of Ruby 3.3 and 3.4 bounds the connect with a Timeout of its own, which can fire after the
|
|
86
|
+
# deadline below ended the verification, in the caller's next call; the deadline alone bounds the connect.
|
|
87
|
+
open_timeout: nil,
|
|
88
|
+
read_timeout: @timeout,
|
|
89
|
+
write_timeout: @timeout,
|
|
90
|
+
# By default Net::HTTP takes a body that ends before its Content-Length as complete.
|
|
91
|
+
ignore_eof: false
|
|
92
|
+
}
|
|
93
|
+
# The timeouts of Net::HTTP bound each operation alone, so an answer that trickles in would never end; one
|
|
94
|
+
# deadline bounds the whole exchange.
|
|
95
|
+
Timeout.timeout(@timeout) do
|
|
96
|
+
# nil: no proxy from the environment.
|
|
97
|
+
Net::HTTP.start(@endpoint.hostname, @endpoint.port, nil, options) do |http|
|
|
98
|
+
http.request(request)
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
rescue Timeout::Error
|
|
102
|
+
raise_unavailable("siteverify did not answer within #{@timeout} seconds")
|
|
103
|
+
rescue StandardError => e
|
|
104
|
+
raise_unavailable("siteverify could not be reached or gave no valid HTTP answer (#{e.class})")
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def evaluate(response)
|
|
108
|
+
unless response.code == "200"
|
|
109
|
+
raise_unavailable("siteverify answered with HTTP status #{response.code}")
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
body = response.body.to_s.dup.force_encoding(Encoding::UTF_8)
|
|
113
|
+
unless body.valid_encoding?
|
|
114
|
+
raise_unavailable("siteverify answered with a body that is no UTF-8")
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
begin
|
|
118
|
+
# strict? refuses repeated member names and comments for every version of json. Unpatched json 2.13 to 2.18.2
|
|
119
|
+
# refuse repeated names through a format string flaw (CVE-2026-33210); later versions warn about both.
|
|
120
|
+
answer = JSON.parse(body, allow_duplicate_key: true, allow_comments: false)
|
|
121
|
+
rescue JSON::ParserError
|
|
122
|
+
raise_unavailable("siteverify answered with a body that is no JSON")
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
unless answer.is_a?(Hash)
|
|
126
|
+
raise_unavailable("siteverify answered with JSON that is no object")
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
unless strict?(body, answer)
|
|
130
|
+
raise_unavailable("siteverify answered with a comment or a repeated member name")
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
case answer["success"]
|
|
134
|
+
when true
|
|
135
|
+
Verification::Passed.new(
|
|
136
|
+
challenge_ts: string_or_nil(answer["challenge_ts"]),
|
|
137
|
+
hostname: string_or_nil(answer["hostname"])
|
|
138
|
+
)
|
|
139
|
+
when false
|
|
140
|
+
Verification::Rejected.new(error_codes: strings(answer["error-codes"]))
|
|
141
|
+
else
|
|
142
|
+
raise_unavailable("siteverify answered without a boolean success")
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
# The error keeps no cause: causes are raised in frames of Net::HTTP, Timeout and json, whose local variables,
|
|
147
|
+
# which error trackers record, can hold the request with the secret key.
|
|
148
|
+
def raise_unavailable(message)
|
|
149
|
+
raise VerificationUnavailableError, message, cause: nil
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
# json accepts comments, and json 2.11 and 2.12 keep the last of repeated member names, such as "success" twice,
|
|
153
|
+
# without an option to refuse either. Of a body that json parsed, a slash outside strings starts a comment, and
|
|
154
|
+
# json keeps fewer members than the body names when a name repeats.
|
|
155
|
+
def strict?(body, answer)
|
|
156
|
+
names = 0
|
|
157
|
+
body.scan(TOKENS) do
|
|
158
|
+
match = Regexp.last_match
|
|
159
|
+
if match[0] == "/"
|
|
160
|
+
return false
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
if match[1]
|
|
164
|
+
names += 1
|
|
165
|
+
end
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
names == members(answer)
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
def members(value)
|
|
172
|
+
case value
|
|
173
|
+
when Hash
|
|
174
|
+
value.size + value.each_value.sum { |member| members(member) }
|
|
175
|
+
when Array
|
|
176
|
+
value.sum { |element| members(element) }
|
|
177
|
+
else
|
|
178
|
+
0
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
def string_or_nil(value)
|
|
183
|
+
value.is_a?(String) ? value : nil
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
# A rejection stays a rejection when its error codes are malformed; only the entries that are strings are kept.
|
|
187
|
+
def strings(value)
|
|
188
|
+
value.is_a?(Array) ? value.grep(String) : []
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CaptchaFox
|
|
4
|
+
# The outcomes of a verification that siteverify answered. When it cannot be evaluated, Client#verify raises
|
|
5
|
+
# VerificationUnavailableError instead.
|
|
6
|
+
module Verification
|
|
7
|
+
# siteverify answered "success": true. challenge_ts, an ISO 8601 timestamp, and hostname are nil where it sent
|
|
8
|
+
# none.
|
|
9
|
+
Passed = Data.define(:challenge_ts, :hostname) do
|
|
10
|
+
def initialize(challenge_ts: nil, hostname: nil)
|
|
11
|
+
super(challenge_ts: challenge_ts.dup.freeze, hostname: hostname.dup.freeze)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def passed?
|
|
15
|
+
true
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# siteverify answered "success": false, or the token was empty. error_codes holds its error codes, such as
|
|
20
|
+
# "timeout-or-duplicate", possibly none.
|
|
21
|
+
Rejected = Data.define(:error_codes) do
|
|
22
|
+
def initialize(error_codes:)
|
|
23
|
+
super(error_codes: error_codes.map { |code| code.dup.freeze }.freeze)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def passed?
|
|
27
|
+
false
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CaptchaFox
|
|
4
|
+
# siteverify could not be evaluated: it was not reached, did not answer in time, answered with an HTTP status other
|
|
5
|
+
# than 200, or with a body that is no JSON object with a boolean "success". Whether the protected request passes
|
|
6
|
+
# then is the caller's decision. The error never contains the secret key and keeps no cause.
|
|
7
|
+
class VerificationUnavailableError < StandardError
|
|
8
|
+
end
|
|
9
|
+
end
|
data/lib/captchafox.rb
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "captchafox/version"
|
|
4
|
+
require_relative "captchafox/verification"
|
|
5
|
+
require_relative "captchafox/verification_unavailable_error"
|
|
6
|
+
require_relative "captchafox/client"
|
|
7
|
+
|
|
8
|
+
# Verifies tokens of the CaptchaFox widget with siteverify.
|
|
9
|
+
module CaptchaFox
|
|
10
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: captchafox
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Scoria Labs GmbH
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-12-02 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Official CaptchaFox integration for Ruby. It verifies the answers of
|
|
14
|
+
the widget with CaptchaFox.
|
|
15
|
+
email:
|
|
16
|
+
executables: []
|
|
17
|
+
extensions: []
|
|
18
|
+
extra_rdoc_files: []
|
|
19
|
+
files:
|
|
20
|
+
- CHANGELOG.md
|
|
21
|
+
- LICENSE
|
|
22
|
+
- README.md
|
|
23
|
+
- lib/captchafox.rb
|
|
24
|
+
- lib/captchafox/client.rb
|
|
25
|
+
- lib/captchafox/verification.rb
|
|
26
|
+
- lib/captchafox/verification_unavailable_error.rb
|
|
27
|
+
- lib/captchafox/version.rb
|
|
28
|
+
homepage: https://captchafox.com
|
|
29
|
+
licenses:
|
|
30
|
+
- Apache-2.0
|
|
31
|
+
metadata:
|
|
32
|
+
homepage_uri: https://captchafox.com
|
|
33
|
+
source_code_uri: https://github.com/CaptchaFox/captchafox-ruby
|
|
34
|
+
changelog_uri: https://github.com/CaptchaFox/captchafox-ruby/blob/main/CHANGELOG.md
|
|
35
|
+
post_install_message:
|
|
36
|
+
rdoc_options: []
|
|
37
|
+
require_paths:
|
|
38
|
+
- lib
|
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
40
|
+
requirements:
|
|
41
|
+
- - ">="
|
|
42
|
+
- !ruby/object:Gem::Version
|
|
43
|
+
version: '3.3'
|
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
45
|
+
requirements:
|
|
46
|
+
- - ">="
|
|
47
|
+
- !ruby/object:Gem::Version
|
|
48
|
+
version: '0'
|
|
49
|
+
requirements: []
|
|
50
|
+
rubygems_version: 3.5.22
|
|
51
|
+
signing_key:
|
|
52
|
+
specification_version: 4
|
|
53
|
+
summary: Verifies CaptchaFox tokens with siteverify.
|
|
54
|
+
test_files: []
|