tina4ruby 3.13.165 → 3.13.166

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.
data/lib/tina4/mcp.rb CHANGED
@@ -888,6 +888,11 @@ module Tina4
888
888
  sensitive = %w[secret password token key credential api_key]
889
889
  if sensitive.any? { |s| key.downcase.include?(s) }
890
890
  "***REDACTED***"
891
+ elsif key.match?(/(?:_URL|_URI|_DSN)\z/i)
892
+ # A connection URL carries its password inline (TINA4_DATABASE_URL,
893
+ # TINA4_CACHE_URL, TINA4_GRAPH_URL...), and no key-name test catches
894
+ # it. Masked by the shared primitive (GHSA-h2mr-m3v3-cq8c).
895
+ Tina4::DatabaseUrl.redact(value)
891
896
  else
892
897
  value
893
898
  end
data/lib/tina4/mqtt.rb CHANGED
@@ -62,6 +62,8 @@ module Tina4
62
62
  DEFAULT_PORT = 1883
63
63
  DEFAULT_TLS_PORT = 8883
64
64
  DEFAULT_URL = "mqtt://127.0.0.1:1883"
65
+ # "scheme:/x" or "scheme//x": a scheme that lost its ":" or a "/".
66
+ MANGLED_SCHEME = %r{\A[A-Za-z][A-Za-z0-9+.\-]*(?::/(?!/)|//)}.freeze
65
67
  DEFAULT_KEEPALIVE = 60
66
68
  SUBSCRIPTION_REFUSED = 0x80
67
69
  MAX_REMAINING_LENGTH = 268_435_455 # 4 varint bytes
@@ -170,6 +172,8 @@ module Tina4
170
172
  raw = url.to_s.strip
171
173
  raise ArgumentError, "MQTT url is empty — set TINA4_MQTT_URL (e.g. #{DEFAULT_URL})" if raw.empty?
172
174
 
175
+ refuse_mangled_scheme!(raw)
176
+
173
177
  scheme = raw[%r{\A([A-Za-z][A-Za-z0-9+.-]*)://}, 1]&.downcase
174
178
  unless scheme.nil? || %w[mqtt tcp mqtts].include?(scheme)
175
179
  raise ArgumentError,
@@ -181,15 +185,7 @@ module Tina4
181
185
  tls = scheme == "mqtts"
182
186
  rest = scheme ? raw.sub(%r{\A[A-Za-z][A-Za-z0-9+.-]*://}, "") : raw
183
187
 
184
- # Split on the LAST "@" so a password containing an un-encoded "@" still
185
- # leaves the host intact.
186
- username = password = nil
187
- if rest.include?("@")
188
- userinfo, _, rest = rest.rpartition("@")
189
- raw_username, has_password, raw_password = userinfo.partition(":")
190
- username = percent_decode(raw_username)
191
- password = percent_decode(raw_password) unless has_password.empty?
192
- end
188
+ username, password, rest = split_credentials(rest)
193
189
 
194
190
  match = rest.match(%r{\A(?<host>\[[^\]]+\]|[^:/]+)(?::(?<port>\d+))?(?:/.*)?\z})
195
191
  raise ArgumentError, "malformed MQTT url #{Tina4::DatabaseUrl.redact(raw).inspect} — expected mqtt://host:port" unless match
@@ -203,6 +199,27 @@ module Tina4
203
199
  }
204
200
  end
205
201
 
202
+ # A scheme that lost its ":" or a "/" (`mqtt:/u:pw@h`, `mqtt//u:pw@h`) is
203
+ # a typo, not a bare host: refused without echoing it
204
+ # (GHSA-h2mr-m3v3-cq8c), since the credential sits where the host is read.
205
+ def self.refuse_mangled_scheme!(raw)
206
+ return unless raw.match?(MANGLED_SCHEME)
207
+
208
+ raise ArgumentError.new("MQTT url: #{Tina4::DatabaseUrl::AMBIGUOUS_URL}"), cause: nil
209
+ end
210
+
211
+ # [username, password, host-and-rest]. Userinfo ends at the LAST "@", so a
212
+ # password containing an un-encoded "@" still leaves the host intact; the
213
+ # shared split also refuses an "@" that cannot be ending a userinfo (one in
214
+ # the path or query).
215
+ def self.split_credentials(rest)
216
+ userinfo, rest = Tina4::DatabaseUrl.split_userinfo(rest, "MQTT url")
217
+ return [nil, nil, rest] if userinfo.nil?
218
+
219
+ raw_username, has_password, raw_password = userinfo.partition(":")
220
+ [percent_decode(raw_username), has_password.empty? ? nil : percent_decode(raw_password), rest]
221
+ end
222
+
206
223
  # %XX in url userinfo. Not CGI.unescape / URI.decode_www_form_component:
207
224
  # those also turn "+" into a space, which silently corrupts a password.
208
225
  def self.percent_decode(value)
data/lib/tina4/queue.rb CHANGED
@@ -558,8 +558,11 @@ module Tina4
558
558
  config = {}
559
559
  url = url.sub("amqp://", "").sub("amqps://", "")
560
560
 
561
+ # Userinfo ends at the LAST "@" (GHSA-h2mr-m3v3-cq8c). Splitting at the
562
+ # first one turned `u:p@ss@host` into password "p" and host "ss@host",
563
+ # which Bunny then printed in its connection error.
561
564
  if url.include?("@")
562
- creds, rest = url.split("@", 2)
565
+ creds, _, rest = url.rpartition("@")
563
566
  if creds.include?(":")
564
567
  config[:username], config[:password] = creds.split(":", 2)
565
568
  else
@@ -596,6 +599,12 @@ module Tina4
596
599
 
597
600
  if hostport.include?(":")
598
601
  host, port = hostport.split(":", 2)
602
+ # A non-numeric port is the password of a URL that lost its host
603
+ # (`amqp://u:secret`) - and whatever followed a "/" in it became the
604
+ # vhost. Refused without echoing the value (GHSA-h2mr-m3v3-cq8c).
605
+ unless port.match?(/\A\d*\z/)
606
+ raise ArgumentError.new("TINA4_QUEUE_URL: #{Tina4::DatabaseUrl::AMBIGUOUS_URL}"), cause: nil
607
+ end
599
608
  config[:host] = host
600
609
  config[:port] = port.to_i
601
610
  elsif hostport && !hostport.empty?
@@ -1260,7 +1260,8 @@ module Tina4
1260
1260
  end
1261
1261
 
1262
1262
  def self.enforce_route_auth(env, route)
1263
- return nil unless route.auth_required
1263
+ # A bare ANY route is public for reads and secured for writes (ADR-0019).
1264
+ return nil unless route.auth_required_for?(env["REQUEST_METHOD"])
1264
1265
 
1265
1266
  # ERR-DEC-02 (#342): a browser gets errors/401.twig, an API client the JSON
1266
1267
  # envelope — the same negotiation every other framework error uses. Was an
data/lib/tina4/router.rb CHANGED
@@ -14,6 +14,9 @@ module Tina4
14
14
  attr_reader :method, :path, :handler, :auth_handler, :swagger_meta,
15
15
  :path_regex, :param_names, :template
16
16
  attr_accessor :auth_required, :cached
17
+ # True on a bare ANY route: reads are public, writes need a token. See
18
+ # #auth_required_for?.
19
+ attr_reader :auth_write_methods
17
20
  # RBAC guard groups (Feature 138 / ADR-0058). Each is an array of arrays:
18
21
  # OR within a group, AND across groups (stacking).
19
22
  attr_reader :roles, :perms
@@ -31,6 +34,13 @@ module Tina4
31
34
  # Middleware is additive, never an auth bypass. tina4-book#141
32
35
  # PY-10-02. Call .no_auth on the route to opt out explicitly.
33
36
  @auth_required = %w[POST PUT PATCH DELETE].include?(@method)
37
+ # An ANY route serves reads AND writes from one entry, so one boolean
38
+ # cannot say "public GET, secured POST". With no explicit choice it is
39
+ # write-secured: the gate requires a token when the request method is a
40
+ # write, exactly as if each method had its own route (ADR-0019; tina4-php
41
+ # and tina4-nodejs register any() as one route per method). .secure /
42
+ # .role / .can lock every method; .no_auth opens every method.
43
+ @auth_write_methods = @method == "ANY"
34
44
  @cached = false
35
45
  # CSRF is enforced on a write route even when it is public (#265): a form
36
46
  # token proves ORIGIN, not identity. Call .no_csrf to opt a server-to-
@@ -54,9 +64,17 @@ module Tina4
54
64
  # Returns self for chaining: Router.post("/login") { ... }.no_auth
55
65
  def no_auth
56
66
  @auth_required = false
67
+ @auth_write_methods = false
57
68
  self
58
69
  end
59
70
 
71
+ # Whether a request with +http_method+ must carry a token on this route.
72
+ def auth_required_for?(http_method)
73
+ return true if @auth_required
74
+
75
+ @auth_write_methods && !%w[GET HEAD OPTIONS].include?(http_method.to_s.upcase)
76
+ end
77
+
60
78
  # Opt a route OUT of CSRF form-token validation (webhooks / server-to-server
61
79
  # callbacks that carry no browser session). .no_auth no longer skips CSRF
62
80
  # (#265): a public form stays CSRF-protected. CSRF (origin) and auth
@@ -8,6 +8,14 @@
8
8
  module Tina4
9
9
  # Internal credential persistence: validate and read/write the same descriptor.
10
10
  module SecretFile
11
+ # O_NONBLOCK keeps an open from hanging on a FIFO planted at a credential
12
+ # path. Windows has no FIFOs at file paths, and Ruby on Windows rejects the
13
+ # flag outright: File.open answers EACCES for a read-write open and IOError
14
+ # for a read-only one. Passing it there broke every secret write (RSA keys
15
+ # from Auth.setup, the dev TINA4_SECRET in .env.local, dev-admin .env edits)
16
+ # and the dev-admin file viewer. Zero on Windows; the flag elsewhere.
17
+ OPEN_NONBLOCK = defined?(File::NONBLOCK) && !Gem.win_platform? ? File::NONBLOCK : 0
18
+
11
19
  def self.update(path)
12
20
  begin
13
21
  before = File.lstat(path)
@@ -17,7 +25,7 @@ module Tina4
17
25
  end
18
26
  flags = File::RDWR | File::CREAT
19
27
  flags |= File::NOFOLLOW if defined?(File::NOFOLLOW)
20
- flags |= File::NONBLOCK if defined?(File::NONBLOCK)
28
+ flags |= OPEN_NONBLOCK
21
29
  File.open(path, flags, 0o600) do |file|
22
30
  file.flock(File::LOCK_EX)
23
31
  stat = file.stat
data/lib/tina4/seeder.rb CHANGED
@@ -998,13 +998,31 @@ module Tina4
998
998
  seed_dir(seed_folder: seed_folder, clear: clear)
999
999
  end
1000
1000
 
1001
- # Run all seed files in the given folder.
1001
+ # Raised when a seed file raises, so the seed run stopped at it (ADR-0108).
1002
+ # +seed_file+ is the failing file, +completed+ the files that ran before it,
1003
+ # and +cause+ (set by Ruby on a raise inside rescue) the original error.
1004
+ class SeedError < StandardError
1005
+ attr_reader :seed_file, :completed
1006
+
1007
+ def initialize(seed_file, completed, error)
1008
+ @seed_file = seed_file
1009
+ @completed = completed.dup
1010
+ super("Seed file failed: #{File.basename(seed_file)}: #{error.message}")
1011
+ end
1012
+ end
1013
+
1014
+ # Run the seed files in +seed_folder+ in name order, skipping files that
1015
+ # start with "_". Returns the files executed, in order ([] when the folder is
1016
+ # missing). A seed file that raises stops the run: it is logged and a
1017
+ # Tina4::SeedError naming it is raised, and no later file runs (ADR-0108).
1002
1018
  #
1003
1019
  # @param seed_folder [String] path to seed files (default: "seeds")
1020
+ # @return [Array<String>] the seed files executed, in order
1021
+ # @raise [Tina4::SeedError] when a seed file raises
1004
1022
  def self.seed_dir(seed_folder: "seeds", clear: false)
1005
1023
  unless Dir.exist?(seed_folder)
1006
1024
  Tina4::Log.info("Seeder: No seeds folder found at #{seed_folder}")
1007
- return
1025
+ return []
1008
1026
  end
1009
1027
 
1010
1028
  files = Dir.glob(File.join(seed_folder, "*.rb")).sort
@@ -1012,19 +1030,23 @@ module Tina4
1012
1030
 
1013
1031
  if files.empty?
1014
1032
  Tina4::Log.info("Seeder: No seed files found in #{seed_folder}")
1015
- return
1033
+ return []
1016
1034
  end
1017
1035
 
1018
1036
  Tina4::Log.info("Seeder: Found #{files.length} seed file(s) in #{seed_folder}")
1019
1037
 
1038
+ executed = []
1020
1039
  files.each do |filepath|
1021
1040
  begin
1022
1041
  Tina4::Log.info("Seeder: Running #{File.basename(filepath)}...")
1023
1042
  load filepath
1024
1043
  Tina4::Log.info("Seeder: Completed #{File.basename(filepath)}")
1025
- rescue => e
1026
- Tina4::Log.error("Seeder: Failed to run #{File.basename(filepath)}: #{e.message}")
1044
+ rescue StandardError, ScriptError => e
1045
+ Tina4::Log.error("Seeder: #{File.basename(filepath)} failed, seeding stopped: #{e.message}")
1046
+ raise SeedError.new(filepath, executed, e)
1027
1047
  end
1048
+ executed << filepath
1028
1049
  end
1050
+ executed
1029
1051
  end
1030
1052
  end
@@ -304,7 +304,7 @@ module Tina4
304
304
  # server, which is far worse than any parse error.
305
305
  def parse_host_port(uri)
306
306
  remainder = uri.to_s.sub(%r{\Amongodb://}, "")
307
- remainder = remainder.split("@", 2).last.to_s # credentials
307
+ remainder = remainder.rpartition("@").last.to_s # credentials end at the LAST "@" (GHSA-h2mr-m3v3-cq8c)
308
308
  remainder = remainder.split("/", 2).first.to_s # path + query
309
309
  remainder = remainder.split(",", 2).first.to_s # first seed host
310
310
  host, port = remainder.split(":", 2)
data/lib/tina4/version.rb CHANGED
@@ -7,5 +7,5 @@
7
7
 
8
8
 
9
9
  module Tina4
10
- VERSION = "3.13.165"
10
+ VERSION = "3.13.166"
11
11
  end
@@ -89,17 +89,12 @@ module Tina4
89
89
  end
90
90
 
91
91
  @url = url || ENV.fetch("TINA4_WS_BACKPLANE_URL", "redis://localhost:6379")
92
- begin
93
- @redis = Redis.new(url: @url)
94
- @subscriber = Redis.new(url: @url)
95
- rescue StandardError => e
96
- # A URL the client cannot parse raises URI::InvalidURIError quoting the
97
- # WHOLE raw URL, password included, and WebSocket#ensure_backplane logs
98
- # e.message. Re-raise naming only the redacted form.
99
- raise ArgumentError,
100
- "Invalid TINA4_WS_BACKPLANE_URL for the Redis backplane: " \
101
- "#{Tina4::DatabaseUrl.redact(@url)} (#{e.class})"
102
- end
92
+ # GHSA-h2mr-m3v3-cq8c: the redis gem re-parses the URL with URI.parse,
93
+ # which reads `redis://u:12?q@h` as host "u", port 12 and then quotes that
94
+ # host in its connection errors. Refuse (generically) any URL whose
95
+ # userinfo URI.parse would not take the way the shared redaction does.
96
+ Tina4::DatabaseUrl.assert_client_url!(@url, "Invalid TINA4_WS_BACKPLANE_URL for the Redis backplane")
97
+ @redis, @subscriber = open_clients
103
98
  @threads = {}
104
99
  @running = true
105
100
  end
@@ -137,6 +132,28 @@ module Tina4
137
132
  @subscriber.close
138
133
  @redis.close
139
134
  end
135
+
136
+ private
137
+
138
+ # The publisher and subscriber clients. A URL the client cannot parse
139
+ # raises URI::InvalidURIError quoting the WHOLE raw URL, password included,
140
+ # and WebSocket#ensure_backplane logs e.message. Re-raised naming only the
141
+ # redacted form - outside the rescue and with cause: nil, because a chained
142
+ # cause carries that raw message into every crash report and full_message.
143
+ def open_clients
144
+ clients = new_clients
145
+ return clients if clients.is_a?(Array)
146
+
147
+ raise ArgumentError.new("Invalid TINA4_WS_BACKPLANE_URL for the Redis backplane: " \
148
+ "#{Tina4::DatabaseUrl.redact(@url)} (#{clients.class})"), cause: nil
149
+ end
150
+
151
+ # Both clients, or the error building them raised (returned, not raised).
152
+ def new_clients
153
+ [Redis.new(url: @url), Redis.new(url: @url)]
154
+ rescue StandardError => e
155
+ e
156
+ end
140
157
  end
141
158
 
142
159
  # NATS pub/sub backplane.
@@ -163,6 +180,13 @@ module Tina4
163
180
  @running = true
164
181
  @mutex = Mutex.new
165
182
 
183
+ # Same rule as RedisBackplane (GHSA-h2mr-m3v3-cq8c): refuse a URL whose
184
+ # userinfo the client would read differently from the shared redaction.
185
+ # nats-pure takes a comma-separated server list, so each is checked.
186
+ @url.to_s.split(",").each do |server|
187
+ Tina4::DatabaseUrl.assert_client_url!(server.strip, "Invalid TINA4_WS_BACKPLANE_URL for the NATS backplane")
188
+ end
189
+
166
190
  # Connect to NATS in a background thread with its own event loop
167
191
  @nats = NATS::IO::Client.new
168
192
  @nats.connect(@url)
data/lib/tina4.rb CHANGED
@@ -346,6 +346,7 @@ module Tina4
346
346
  autoload :IMAP_CONNECTION_ERRORS, File.expand_path("tina4/messenger", __dir__)
347
347
  autoload :DocStore, File.expand_path("tina4/docstore", __dir__)
348
348
  autoload :FakeData, File.expand_path("tina4/seeder", __dir__)
349
+ autoload :SeedError, File.expand_path("tina4/seeder", __dir__)
349
350
  autoload :WSDL, File.expand_path("tina4/wsdl", __dir__)
350
351
  # Validator is a counted framework feature and belongs on the main
351
352
  # `require "tina4"` surface like every other subsystem — PHP reaches
@@ -1017,8 +1018,12 @@ module Tina4
1017
1018
  # A successful single connection was already logged, with this URL, by
1018
1019
  # Database#connect - one "Database connected" line, not two.
1019
1020
  rescue => e
1020
- @database_setup_error = e.message.to_s.gsub(db_url) { safe_url }.gsub(%r{://[^/\s@]*@}, "://")
1021
- Tina4::Log.error("Database connection failed: #{e.message}")
1021
+ # GHSA-h2mr-m3v3-cq8c: this logged e.message RAW. Whatever raised here
1022
+ # (a driver, the sqlite path resolver) can quote the URL or a part of
1023
+ # it, so the logged line is the scrubbed one too.
1024
+ @database_setup_error = Tina4::DatabaseUrl.scrub_message(e.message, db_url, safe_url)
1025
+ .gsub(%r{://[^/\s@]*@}, "://")
1026
+ Tina4::Log.error("Database connection failed: #{@database_setup_error}")
1022
1027
  end
1023
1028
  end
1024
1029
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tina4ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.13.165
4
+ version: 3.13.166
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tina4 Team
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2026-11-22 00:00:00.000000000 Z
11
+ date: 2026-11-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sqlite3
@@ -195,6 +195,7 @@ files:
195
195
  - lib/tina4/auto_crud.rb
196
196
  - lib/tina4/background.rb
197
197
  - lib/tina4/base64.rb
198
+ - lib/tina4/batch_spawn.rb
198
199
  - lib/tina4/cache.rb
199
200
  - lib/tina4/cache_backends.rb
200
201
  - lib/tina4/cache_backends/base_backend.rb