xlock 0.1.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/LICENSE +21 -0
- data/README.md +64 -0
- data/lib/xlock/rack.rb +54 -0
- data/lib/xlock/rails.rb +65 -0
- data/lib/xlock/verify.rb +49 -0
- data/lib/xlock/version.rb +5 -0
- data/lib/xlock.rb +6 -0
- metadata +53 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 1420010c44730cf9ba48ffab54e85fcd358de4eb89a3e5611731552d4755c8a4
|
|
4
|
+
data.tar.gz: 7ee393ae2ba3a0fd4339d1fd6334c8e51368cdd26c8e02e27a86f70e04e7746e
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 0befae735395708c8fcdaf61d9b814b27cd745b698a3ae4087e5e873cd82ee963fd099ba2740624edbff932ced338eb5ab5d2fc1e8bb426a86e7636f9fc4351f
|
|
7
|
+
data.tar.gz: 23a5b02aa8ae5f0ba63f08527473f2bfbc90e3ad551a07030b80fd02c8ad30bff4665f4cdb4fad9adfa9289fd0e99d6b8fbecb9f5d81de2026146fce19697fa8
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 x-lock
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# xlock
|
|
2
|
+
|
|
3
|
+
Server-side bot protection middleware for Ruby. Works with Rack, Rails, or standalone.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add to your Gemfile:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem "xlock"
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Then run `bundle install`.
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
### Rails Controller
|
|
18
|
+
|
|
19
|
+
```ruby
|
|
20
|
+
class ApplicationController < ActionController::Base
|
|
21
|
+
include XLock::Rails
|
|
22
|
+
|
|
23
|
+
protect_with_xlock only: [:create, :login],
|
|
24
|
+
site_key: ENV["XLOCK_SITE_KEY"]
|
|
25
|
+
end
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Options: `site_key`, `api_url`, `fail_open` (default `true`).
|
|
29
|
+
|
|
30
|
+
### Rack Middleware
|
|
31
|
+
|
|
32
|
+
```ruby
|
|
33
|
+
use XLock::Rack,
|
|
34
|
+
site_key: ENV["XLOCK_SITE_KEY"],
|
|
35
|
+
protected_paths: ["/api/auth", "/api/login"]
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Only POST requests to the listed paths are checked. If `protected_paths` is empty, all POST requests are checked.
|
|
39
|
+
|
|
40
|
+
### Direct Verification
|
|
41
|
+
|
|
42
|
+
```ruby
|
|
43
|
+
result = XLock.verify(
|
|
44
|
+
token: "...",
|
|
45
|
+
site_key: ENV["XLOCK_SITE_KEY"],
|
|
46
|
+
path: "/api/login"
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
if result.blocked
|
|
50
|
+
puts "Blocked: #{result.reason}"
|
|
51
|
+
elsif result.error
|
|
52
|
+
puts "Error: #{result.error}"
|
|
53
|
+
else
|
|
54
|
+
puts "Allowed"
|
|
55
|
+
end
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Configuration
|
|
59
|
+
|
|
60
|
+
Set `XLOCK_SITE_KEY` and optionally `XLOCK_API_URL` as environment variables, or pass them directly as options.
|
|
61
|
+
|
|
62
|
+
## License
|
|
63
|
+
|
|
64
|
+
MIT
|
data/lib/xlock/rack.rb
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module XLock
|
|
4
|
+
# Rack middleware for x-lock bot protection.
|
|
5
|
+
#
|
|
6
|
+
# Usage:
|
|
7
|
+
# use XLock::Rack,
|
|
8
|
+
# site_key: ENV["XLOCK_SITE_KEY"],
|
|
9
|
+
# protected_paths: ["/api/auth"]
|
|
10
|
+
class Rack
|
|
11
|
+
def initialize(app, **options)
|
|
12
|
+
@app = app
|
|
13
|
+
@site_key = options[:site_key] || ENV["XLOCK_SITE_KEY"]
|
|
14
|
+
@api_url = options[:api_url] || XLock::API_URL
|
|
15
|
+
@fail_open = options.fetch(:fail_open, true)
|
|
16
|
+
@protected_paths = options[:protected_paths] || []
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def call(env)
|
|
20
|
+
request = ::Rack::Request.new(env)
|
|
21
|
+
|
|
22
|
+
unless @site_key && request.post? && matches?(request.path)
|
|
23
|
+
return @app.call(env)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
token = env["HTTP_X_LOCK"]
|
|
27
|
+
unless token
|
|
28
|
+
return [403, { "Content-Type" => "application/json" },
|
|
29
|
+
['{"error":"Blocked by x-lock: missing token"}']]
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
result = XLock.verify(token: token, site_key: @site_key, path: request.path, api_url: @api_url)
|
|
33
|
+
|
|
34
|
+
if result.blocked
|
|
35
|
+
return [403, { "Content-Type" => "application/json" },
|
|
36
|
+
[{ error: "Blocked by x-lock", reason: result.reason }.to_json]]
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
if result.error && !@fail_open
|
|
40
|
+
return [403, { "Content-Type" => "application/json" },
|
|
41
|
+
['{"error":"x-lock verification failed"}']]
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
@app.call(env)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
private
|
|
48
|
+
|
|
49
|
+
def matches?(path)
|
|
50
|
+
return true if @protected_paths.empty?
|
|
51
|
+
@protected_paths.any? { |p| path.start_with?(p) }
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
data/lib/xlock/rails.rb
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module XLock
|
|
4
|
+
# Rails controller concern for x-lock bot protection.
|
|
5
|
+
#
|
|
6
|
+
# Usage:
|
|
7
|
+
# class ApplicationController < ActionController::Base
|
|
8
|
+
# include XLock::Rails
|
|
9
|
+
# protect_with_xlock only: [:create, :login],
|
|
10
|
+
# site_key: ENV["XLOCK_SITE_KEY"]
|
|
11
|
+
# end
|
|
12
|
+
module Rails
|
|
13
|
+
def self.included(base)
|
|
14
|
+
base.extend(ClassMethods)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
module ClassMethods
|
|
18
|
+
def protect_with_xlock(**options)
|
|
19
|
+
before_action :enforce_xlock!, **options.except(:site_key, :api_url, :fail_open)
|
|
20
|
+
|
|
21
|
+
define_method(:xlock_config) do
|
|
22
|
+
{
|
|
23
|
+
site_key: options[:site_key] || ENV["XLOCK_SITE_KEY"],
|
|
24
|
+
api_url: options[:api_url] || XLock::API_URL,
|
|
25
|
+
fail_open: options.fetch(:fail_open, true)
|
|
26
|
+
}
|
|
27
|
+
end
|
|
28
|
+
private :xlock_config
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
private
|
|
33
|
+
|
|
34
|
+
def enforce_xlock!
|
|
35
|
+
config = xlock_config
|
|
36
|
+
return unless config[:site_key]
|
|
37
|
+
|
|
38
|
+
token = request.headers["x-lock"]
|
|
39
|
+
unless token
|
|
40
|
+
render json: { error: "Blocked by x-lock: missing token" }, status: 403
|
|
41
|
+
return
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
result = XLock.verify(
|
|
45
|
+
token: token,
|
|
46
|
+
site_key: config[:site_key],
|
|
47
|
+
path: request.path,
|
|
48
|
+
api_url: config[:api_url]
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
if result.blocked
|
|
52
|
+
render json: { error: "Blocked by x-lock", reason: result.reason }, status: 403
|
|
53
|
+
return
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
if result.error
|
|
57
|
+
if config[:fail_open]
|
|
58
|
+
::Rails.logger.error("[x-lock] Enforcement error: #{result.error}") if defined?(::Rails.logger)
|
|
59
|
+
else
|
|
60
|
+
render json: { error: "x-lock verification failed" }, status: 403
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
data/lib/xlock/verify.rb
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "net/http"
|
|
4
|
+
require "json"
|
|
5
|
+
require "uri"
|
|
6
|
+
require "ostruct"
|
|
7
|
+
|
|
8
|
+
module XLock
|
|
9
|
+
API_URL = ENV.fetch("XLOCK_API_URL", "https://api.x-lock.dev")
|
|
10
|
+
|
|
11
|
+
# Verify an x-lock token against the API.
|
|
12
|
+
#
|
|
13
|
+
# @param token [String] the x-lock token from the client
|
|
14
|
+
# @param site_key [String] your site key
|
|
15
|
+
# @param path [String] the request path being protected
|
|
16
|
+
# @param api_url [String] override the API endpoint
|
|
17
|
+
# @return [OpenStruct] with :blocked (Boolean), :reason (String|nil), :error (String|nil)
|
|
18
|
+
def self.verify(token:, site_key:, path: "/", api_url: API_URL)
|
|
19
|
+
if token.start_with?("v3.")
|
|
20
|
+
session_id = token.split(".")[1]
|
|
21
|
+
uri = URI("#{api_url}/v3/session/enforce")
|
|
22
|
+
body = { sessionId: session_id, siteKey: site_key, path: path }
|
|
23
|
+
else
|
|
24
|
+
uri = URI("#{api_url}/v1/enforce")
|
|
25
|
+
body = { token: token, siteKey: site_key, path: path }
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
29
|
+
http.use_ssl = uri.scheme == "https"
|
|
30
|
+
http.open_timeout = 5
|
|
31
|
+
http.read_timeout = 5
|
|
32
|
+
|
|
33
|
+
req = Net::HTTP::Post.new(uri, "Content-Type" => "application/json")
|
|
34
|
+
req.body = body.to_json
|
|
35
|
+
|
|
36
|
+
res = http.request(req)
|
|
37
|
+
|
|
38
|
+
if res.code == "403"
|
|
39
|
+
data = JSON.parse(res.body) rescue {}
|
|
40
|
+
OpenStruct.new(blocked: true, reason: data["reason"], error: nil)
|
|
41
|
+
elsif res.is_a?(Net::HTTPSuccess)
|
|
42
|
+
OpenStruct.new(blocked: false, reason: nil, error: nil)
|
|
43
|
+
else
|
|
44
|
+
OpenStruct.new(blocked: false, reason: nil, error: "Unexpected status #{res.code}")
|
|
45
|
+
end
|
|
46
|
+
rescue => e
|
|
47
|
+
OpenStruct.new(blocked: false, reason: nil, error: e.message)
|
|
48
|
+
end
|
|
49
|
+
end
|
data/lib/xlock.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: xlock
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- x-lock
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-04-05 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Server-side token verification and Rack/Rails middleware for x-lock bot
|
|
14
|
+
protection.
|
|
15
|
+
email:
|
|
16
|
+
- support@x-lock.dev
|
|
17
|
+
executables: []
|
|
18
|
+
extensions: []
|
|
19
|
+
extra_rdoc_files: []
|
|
20
|
+
files:
|
|
21
|
+
- LICENSE
|
|
22
|
+
- README.md
|
|
23
|
+
- lib/xlock.rb
|
|
24
|
+
- lib/xlock/rack.rb
|
|
25
|
+
- lib/xlock/rails.rb
|
|
26
|
+
- lib/xlock/verify.rb
|
|
27
|
+
- lib/xlock/version.rb
|
|
28
|
+
homepage: https://x-lock.dev
|
|
29
|
+
licenses:
|
|
30
|
+
- MIT
|
|
31
|
+
metadata:
|
|
32
|
+
homepage_uri: https://x-lock.dev
|
|
33
|
+
source_code_uri: https://github.com/x-lock/xlock-ruby
|
|
34
|
+
post_install_message:
|
|
35
|
+
rdoc_options: []
|
|
36
|
+
require_paths:
|
|
37
|
+
- lib
|
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
39
|
+
requirements:
|
|
40
|
+
- - ">="
|
|
41
|
+
- !ruby/object:Gem::Version
|
|
42
|
+
version: '2.7'
|
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - ">="
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '0'
|
|
48
|
+
requirements: []
|
|
49
|
+
rubygems_version: 3.0.3.1
|
|
50
|
+
signing_key:
|
|
51
|
+
specification_version: 4
|
|
52
|
+
summary: x-lock bot protection middleware for Ruby
|
|
53
|
+
test_files: []
|