minisky 0.5.1 → 0.5.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 11cd25820ca8ab1f4cd6f9c94604a8229fd2f824695341ccd052aae62bc085c6
4
- data.tar.gz: ecca2bbad124246a93e7090a3be0ace4675208ad1750d36adfdfa523cf9fd420
3
+ metadata.gz: 412a75311a1f85346a0085428cfafb40ed865b9fa167c05afec75988758938da
4
+ data.tar.gz: 8a8e0bb0b328281605f8c17a2a0cfe9b6c3d08a66a4d952e4114bd2219574af2
5
5
  SHA512:
6
- metadata.gz: 2169712a4d8596840ae0aa9ab891cfccee951f73a7fce146105503a69d647545b3ee0ed8f475bf65096d88226cf66c07a8637048a076c65e9363023877b8b55d
7
- data.tar.gz: 2283843f151c6f03ce5a275195e21ff85e33085c06a6db56bc9301c205d97ef7ea86ff245b0f609e406c2d54ddaa732b2e72930aba5ad75e27dab2ef7c1bac1c
6
+ metadata.gz: c85aef6fcc92c12ce3293ccf59bdf1bdcb3dee31aa74d9e0ea4186dd0bcc878840f24bf7258027daf833bb6e5a51d8934763487182d1212ad8b128d182dee79f
7
+ data.tar.gz: 3b161229e28fe3ea1594fe2101d134addb78f5050930f869fa06fa790f2a25c6b53808777cb59a201499fdea9ee418627e51f6a0632987ac4f50ff93425d0299
data/CHANGELOG.md CHANGED
@@ -1,4 +1,13 @@
1
- ## Unreleased
1
+ ## [0.5.2] – 2026-11-09
2
+
3
+ * added `:timeout` option which is passed to `:open_timeout` & `:read_timeout` on requests
4
+ * improved error handling for JSON responses, JWT payloads, or wrong argument values
5
+ * fixed access token JWT parsing which could fail sometimes due to wrong version of Base64 being expected
6
+ * detect repeated cursors during `fetch_all` pagination, which could lead to an infinite loop when fetching from a misbehaving server
7
+ * changed `fetch_all` to not modify the received `params` object in place
8
+ * added `frozen_string_literal` comments
9
+
10
+ ## [0.5.1] – 2026-02-15
2
11
 
3
12
  The "really niche bugfix" edition:
4
13
 
@@ -13,9 +22,9 @@ The "really niche bugfix" edition:
13
22
  * fixed URL query params in POST requests on Ruby 2.x
14
23
  * marked `Minisky#active_repl?` method as private
15
24
 
16
- Also added YARD API documentation for most of the code.
25
+ Also added [YARD API documentation](https://rubydoc.info/gems/minisky/) for most of the code.
17
26
 
18
- ## [0.5.0] - 2024-12-27 🎄
27
+ ## [0.5.0] – 2024-12-27 🎄
19
28
 
20
29
  * `host` param in the initializer can be passed with a `https://` prefix (useful if you're passing it directly from a DID document, e.g. using DIDKit)
21
30
  * added validation of the `method` parameter in request calls: it needs to be either a proper NSID, or a full URL as a string or a URI object
@@ -27,7 +36,7 @@ Also added YARD API documentation for most of the code.
27
36
  * `check_access` now returns a result symbol: `:logged_in`, `:refreshed` or `:ok`
28
37
  * fixed `method_missing` setter on `User`
29
38
 
30
- ## [0.4.0] - 2024-03-31 🐣
39
+ ## [0.4.0] – 2024-03-31 🐣
31
40
 
32
41
  * allow passing non-JSON body to requests (e.g. when uploading blobs)
33
42
  * allow passing custom headers to requests, including overriding `Content-Type`
@@ -36,11 +45,11 @@ Also added YARD API documentation for most of the code.
36
45
  * aliased `default_progress` setting as `progress`
37
46
  * added `base64` dependency explicitly to the gemspec – fixes a warning in Ruby 3.3, since it will be extracted as an optional gem in 3.4
38
47
 
39
- ## [0.3.1] - 2023-10-10
48
+ ## [0.3.1] – 2023-10-10
40
49
 
41
50
  * fixed Minisky not working on Ruby 2.x
42
51
 
43
- ## [0.3.0] - 2023-10-05
52
+ ## [0.3.0] – 2023-10-05
44
53
 
45
54
  * authentication improvements & changes:
46
55
  - Minisky now automatically manages access tokens, calling `check_access` manually is not necessary (set `auto_manage_tokens` to `false` to disable this)
@@ -53,7 +62,7 @@ Also added YARD API documentation for most of the code.
53
62
  * `user` wrapper can also be used for writing fields to the config
54
63
  * improved error handling
55
64
 
56
- ## [0.2.0] - 2023-09-02
65
+ ## [0.2.0] – 2023-09-02
57
66
 
58
67
  * more consistent handling of parameters in the main methods:
59
68
  - `auth` is now a named parameter
@@ -69,7 +78,7 @@ Also added YARD API documentation for most of the code.
69
78
  * config is now accessed in `Requests` from the client object as a `config` property instead of `@config` ivar
70
79
  * config fields are exposed as a `user` wrapper object, e.g. `user.did` delegates to `@config['did']`
71
80
 
72
- ## [0.1.0] - 2023-09-01
81
+ ## [0.1.0] – 2023-09-01
73
82
 
74
83
  - extracted most code to a `Requests` module that can be included into a different client class with custom config handling
75
84
  - added `#check_access` method
@@ -77,7 +86,7 @@ Also added YARD API documentation for most of the code.
77
86
  - config file name can be passed as a parameter
78
87
  - added tests
79
88
 
80
- ## [0.0.1] - 2023-08-30
89
+ ## [0.0.1] – 2023-08-30
81
90
 
82
91
  Initial release – extracted from original gist:
83
92
 
data/README.md CHANGED
@@ -1,8 +1,10 @@
1
1
  # Minisky 🌤
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/minisky.svg?icon=si%3Arubygems&icon_color=%23ff6251)](https://rubygems.org/gems/minisky) [![YARD Docs](http://img.shields.io/badge/yard-docs-blue.svg)](https://rubydoc.info/gems/minisky)
4
+
3
5
  Minisky is a minimal client of the Bluesky (ATProto) API. It provides a simple API client class that you can use to log in to the Bluesky API and make any GET and POST requests there. It's meant to be an easy way to start playing and experimenting with the AT Protocol API.
4
6
 
5
- This is designed as a low-level XRPC client library - it purposefully does not include any convenience methods like "get posts" or "get profile" etc., it only provides base components that you could use to build a higher level API.
7
+ This is designed as a low-level XRPC client library – it purposefully does not include any convenience methods like "get posts" or "get profile" etc., it only provides base components that you could use to build a higher level API.
6
8
 
7
9
  > [!NOTE]
8
10
  > Part of ATProto Ruby SDK: [ruby.sdk.blue](https://ruby.sdk.blue)
@@ -10,7 +12,7 @@ This is designed as a low-level XRPC client library - it purposefully does not i
10
12
 
11
13
  ## Installation
12
14
 
13
- To use Minisky, you need a reasonably new version of Ruby – it should run on Ruby 2.6 and above, although it's recommended to use a version that's still getting maintainance updates, i.e. currently 3.2+. A compatible version should be preinstalled on macOS Big Sur and above and on many Linux systems. Otherwise, you can install one using tools such as [RVM](https://rvm.io), [asdf](https://asdf-vm.com), [ruby-install](https://github.com/postmodern/ruby-install) or [ruby-build](https://github.com/rbenv/ruby-build), or `rpm` or `apt-get` on Linux (see more installation options on [ruby-lang.org](https://www.ruby-lang.org/en/downloads/)).
15
+ To use Minisky, you need a reasonably new version of Ruby – it should run on Ruby 2.6 and above, although it's recommended to use a version that's still getting maintainance updates, i.e. currently 3.3+. A compatible version should be preinstalled on macOS Big Sur and above and on many Linux systems. Otherwise, you can install one using tools such as [RVM](https://rvm.io), [asdf](https://asdf-vm.com), [ruby-install](https://github.com/postmodern/ruby-install) or [ruby-build](https://github.com/rbenv/ruby-build), or `rpm` or `apt-get` on Linux (see more installation options on [ruby-lang.org](https://www.ruby-lang.org/en/downloads/)).
14
16
 
15
17
  To install the Minisky gem, run the command:
16
18
 
@@ -66,7 +68,7 @@ pass: very-secret-password
66
68
  The `id` can be either your handle, or your DID, or the email you've used to sign up. It's recommended that you use the "app password" that you can create in the settings instead of your main account password.
67
69
 
68
70
  > [!NOTE]
69
- > Bluesky has recently implemented OAuth, but Minisky doesn't support it yet - it will be added in a future version. App passwords should still be supported for a fairly long time.
71
+ > Bluesky has recently implemented OAuth, but Minisky doesn't support it yet – it will be added in a future version. App passwords should still be supported for a fairly long time.
70
72
 
71
73
  After you log in, this file will also be used to store your access & request tokens and DID. The data in the config file can be accessed through a `user` wrapper property that exposes them as methods, e.g. the password is available as `user.pass` and the DID as `user.did`.
72
74
 
@@ -78,7 +80,7 @@ require 'minisky'
78
80
  bsky = Minisky.new('bsky.social', 'bluesky.yml')
79
81
  ```
80
82
 
81
- Minisky automatically manages your access and refresh tokens - it will first log you in using the login & password, and then use the refresh token to update the access token before the request when it expires.
83
+ Minisky automatically manages your access and refresh tokens – it will first log you in using the login & password, and then use the refresh token to update the access token before the request when it expires.
82
84
 
83
85
 
84
86
  ### Making requests
@@ -135,20 +137,18 @@ This will output a line like this:
135
137
  .................
136
138
  ```
137
139
 
138
- You can find more examples on the [examples page](https://ruby.sdk.blue/examples/) on [ruby.sdk.blue](https://ruby.sdk.blue).
139
-
140
140
 
141
141
  ## Customization
142
142
 
143
143
  The `Minisky` client currently supports such configuration options:
144
144
 
145
- - `default_progress` - a progress character to automatically use for `#fetch_all` calls (default: `.` when in an interactive console, `nil` otherwise)
146
- - `send_auth_headers` - whether auth headers should be added by default (default: `true` in authenticated mode)
147
- - `auto_manage_tokens` - whether access tokens should be generated and refreshed automatically when needed (default: `true` in authenticated mode)
145
+ - `default_progress` – a progress character to automatically use for `#fetch_all` calls (default: `.` when in an interactive console, `nil` otherwise)
146
+ - `send_auth_headers` – whether auth headers should be added by default (default: `true` in authenticated mode)
147
+ - `auto_manage_tokens` – whether access tokens should be generated and refreshed automatically when needed (default: `true` in authenticated mode)
148
148
 
149
149
  In authenticated mode, you can disable the `send_auth_headers` option and then explicitly add `auth: true` to specific requests to include a header there.
150
150
 
151
- You can also disable the `auto_manage_tokens` option - in this case you will need to call the `#check_access` method before a request to refresh a token if needed, or alternatively, call either `#login` or `#perform_token_refresh`.
151
+ You can also disable the `auto_manage_tokens` option – in this case you will need to call the `#check_access` method before a request to refresh a token if needed, or alternatively, call either `#login` or `#perform_token_refresh`.
152
152
 
153
153
 
154
154
  ### Using your own class
@@ -186,10 +186,17 @@ bsky.get_request(...)
186
186
  The class needs to provide:
187
187
 
188
188
  - a `host` method or property that returns the hostname of the server
189
- - a `config` property which returns a hash or a hash-like object with the configuration and user data - it needs to support reading and writing arbitrary key-value pairs with string keys
189
+ - a `config` property which returns a hash or a hash-like object with the configuration and user data – it needs to support reading and writing arbitrary key-value pairs with string keys
190
190
  - a `save_config` method which persists the config object to the chosen storage
191
191
 
192
192
 
193
+ ## Other resources
194
+
195
+ - [YARD API documentation](https://rubydoc.info/gems/minisky) at rubydoc.info
196
+ - [ruby.sdk.blue](https://ruby.sdk.blue)
197
+ - [Example scripts](https://ruby.sdk.blue/examples/)
198
+
199
+
193
200
  ## Credits
194
201
 
195
202
  Copyright © 2026 Kuba Suder ([@mackuba.eu](https://bsky.app/profile/did:plc:oio4hkxaop4ao4wz2pp3f4cr)).
data/SECURITY.md ADDED
@@ -0,0 +1,16 @@
1
+ # Security Policy
2
+
3
+ ## How To Report a Vulnerability
4
+
5
+ If you find a potential vulnerability in one of my projects, it's recommended not to report it through a standard publicly-readable issue report, in order to avoid giving possible clues to any potential attackers who could abuse it before it's fixed (see: ["Just a rumour of a bug is enough to find a security exploit these days"](https://anil.recoil.org/notes/rumour-is-the-exploit)).
6
+
7
+ You can use one of these to contact me instead:
8
+
9
+ - [@mackuba.eu](https://bsky.app/profile/did:plc:oio4hkxaop4ao4wz2pp3f4cr) on Bluesky (my DMs should be open, or mention me discretely with a request for contact)
10
+ - [@mackuba@martianbase.net](https://martianbase.net/@mackuba) on Mastodon (my personal instance)
11
+ - email: mackuba `@` protonmail.ch (optionally with [this PGP key](https://mail-api.proton.me/pks/lookup?op=get&search=mackuba@protonmail.ch))
12
+ - ["Report a vulnerability"](https://github.com/mackuba/minisky/security/advisories/new) form on GitHub
13
+
14
+ I do not check my Twitter/X or LinkedIn accounts regularly.
15
+
16
+ Note: please don't submit AI-reported issues that have not been verified manually :)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'minisky'
2
4
 
3
5
  class Minisky
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'minisky'
2
4
 
3
5
  class Minisky
@@ -119,4 +121,16 @@ class Minisky
119
121
  super("Field parameter not provided; available fields: #{@fields.inspect}")
120
122
  end
121
123
  end
124
+
125
+ #
126
+ # Raised when the received response includes an invalid or unexpected cursor.
127
+ #
128
+ class InvalidCursorError < Error
129
+ end
130
+
131
+ #
132
+ # Raised when the received response JSON has some missing field or invalid format.
133
+ #
134
+ class InvalidJSONError < Error
135
+ end
122
136
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'yaml'
2
4
 
3
5
  #
@@ -49,9 +51,8 @@ class Minisky
49
51
  if @config_file
50
52
  @config = YAML.load(File.read(@config_file))
51
53
 
52
- if user.id.nil? || user.pass.nil?
53
- raise AuthError, "Missing user id or password in the config file #{@config_file}"
54
- end
54
+ raise AuthError, "Missing user id in the config file #{@config_file}" if user.id.nil?
55
+ raise AuthError, "Missing password in the config file #{@config_file}" if user.pass.nil?
55
56
  else
56
57
  @config = nil
57
58
  end
@@ -1,9 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'minisky'
2
4
  require_relative 'errors'
3
5
 
4
6
  require 'base64'
5
7
  require 'json'
6
8
  require 'net/http'
9
+ require 'set'
7
10
  require 'time'
8
11
  require 'uri'
9
12
 
@@ -60,6 +63,13 @@ class Minisky
60
63
  #
61
64
  attr_accessor :stop_fetch_on_empty_page
62
65
 
66
+ # If set, the `open_timeout` and `read_timeout` parameters will be set to this value
67
+ # on network requests.
68
+ #
69
+ # @return [Numeric, nil]
70
+ #
71
+ attr_accessor :timeout
72
+
63
73
  attr_writer :send_auth_headers
64
74
  attr_writer :auto_manage_tokens
65
75
 
@@ -202,7 +212,10 @@ class Minisky
202
212
  url.query = URI.encode_www_form(params)
203
213
  end
204
214
 
205
- response = Net::HTTP.post(url, body, headers)
215
+ request = Net::HTTP::Post.new(url, headers)
216
+ request.body = body
217
+
218
+ response = make_request(request)
206
219
  handle_response(response)
207
220
  end
208
221
 
@@ -276,23 +289,28 @@ class Minisky
276
289
  def fetch_all(method, params = nil, auth: default_auth_mode,
277
290
  field: nil, break_when: nil, max_pages: nil, headers: nil, progress: @default_progress)
278
291
  data = []
279
- params = {} if params.nil?
292
+ params = (params.nil?) ? {} : params.dup
280
293
  pages = 0
294
+ seen_cursors = Set.new
295
+
296
+ if max_pages && !(max_pages.is_a?(Integer) && max_pages > 0)
297
+ raise ArgumentError, "max_pages should be a positive integer"
298
+ end
281
299
 
282
300
  loop do
283
301
  print(progress) if progress
284
302
 
285
303
  response = get_request(method, params, auth: auth, headers: headers)
286
-
287
- if field.nil?
288
- raise FieldNotSetError, response.keys.select { |f| response[f].is_a?(Array) }
289
- end
304
+ validate_fetch_all_response(response, field)
290
305
 
291
306
  records = response[field]
292
307
  cursor = response['cursor']
293
308
 
309
+ raise InvalidCursorError, "Repeated cursor: #{cursor}" if seen_cursors.include?(cursor)
310
+
294
311
  data.concat(records)
295
312
  params[:cursor] = cursor
313
+ seen_cursors << cursor
296
314
  pages += 1
297
315
 
298
316
  break if !cursor || pages == max_pages || (stop_fetch_on_empty_page && records.empty?)
@@ -422,11 +440,19 @@ class Minisky
422
440
  return nil unless parts.length == 3
423
441
 
424
442
  begin
425
- payload = JSON.parse(Base64.decode64(parts[1]))
443
+ json = Base64.urlsafe_decode64(parts[1])
444
+ rescue ArgumentError
445
+ return nil
446
+ end
447
+
448
+ begin
449
+ payload = JSON.parse(json)
426
450
  rescue JSON::ParserError
427
451
  return nil
428
452
  end
429
453
 
454
+ return nil if !payload.is_a?(Hash)
455
+
430
456
  exp = payload['exp']
431
457
  return nil unless exp.is_a?(Numeric) && exp > 0
432
458
 
@@ -482,8 +508,16 @@ class Minisky
482
508
  private
483
509
 
484
510
  def make_request(request)
485
- # this long form is needed because #get_response only supports a headers param in Ruby 3.x
486
- response = Net::HTTP.start(request.uri.hostname, request.uri.port, use_ssl: (request.uri.scheme == 'https')) do |http|
511
+ options = {
512
+ use_ssl: (request.uri.scheme == 'https')
513
+ }
514
+
515
+ if @timeout
516
+ options[:open_timeout] = @timeout
517
+ options[:read_timeout] = @timeout
518
+ end
519
+
520
+ response = Net::HTTP.start(request.uri.hostname, request.uri.port, options) do |http|
487
521
  http.request(request)
488
522
  end
489
523
  end
@@ -518,6 +552,26 @@ class Minisky
518
552
  end
519
553
  end
520
554
 
555
+ def validate_fetch_all_response(response, field)
556
+ if !response.is_a?(Hash)
557
+ raise InvalidJSONError, "Received response is not in expected JSON format"
558
+ end
559
+
560
+ if field.nil?
561
+ raise FieldNotSetError, response.keys.select { |f| response[f].is_a?(Array) }
562
+ end
563
+
564
+ if !response.has_key?(field)
565
+ raise InvalidJSONError, "No such field in the response JSON: '#{field}'"
566
+ elsif response[field].nil?
567
+ raise InvalidJSONError, "Field '#{field}' is nil in the received response"
568
+ elsif !response[field].is_a?(Array)
569
+ raise InvalidJSONError, "Invalid '#{field}': expected Array, got #{response[field].class}"
570
+ else
571
+ # ok
572
+ end
573
+ end
574
+
521
575
  def handle_response(response)
522
576
  status = response.code.to_i
523
577
  message = response.message
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'minisky'
2
4
 
3
5
  class Minisky
4
- VERSION = "0.5.1"
6
+ VERSION = "0.5.2"
5
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minisky
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kuba Suder
@@ -34,6 +34,7 @@ files:
34
34
  - CHANGELOG.md
35
35
  - LICENSE.txt
36
36
  - README.md
37
+ - SECURITY.md
37
38
  - lib/minisky.rb
38
39
  - lib/minisky/compat.rb
39
40
  - lib/minisky/errors.rb
@@ -48,6 +49,8 @@ metadata:
48
49
  bug_tracker_uri: https://tangled.org/mackuba.eu/minisky/issues
49
50
  changelog_uri: https://tangled.org/mackuba.eu/minisky/blob/master/CHANGELOG.md
50
51
  source_code_uri: https://tangled.org/mackuba.eu/minisky
52
+ documentation_uri: https://rubydoc.info/gems/minisky
53
+ rubygems_mfa_required: 'true'
51
54
  rdoc_options: []
52
55
  require_paths:
53
56
  - lib
@@ -62,7 +65,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
62
65
  - !ruby/object:Gem::Version
63
66
  version: '0'
64
67
  requirements: []
65
- rubygems_version: 4.0.3
68
+ rubygems_version: 4.0.23
66
69
  specification_version: 4
67
70
  summary: A minimal client of Bluesky/ATProto API
68
71
  test_files: []