runlight 0.4.3 → 0.5.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.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +1 -1
  3. data/README.md +1 -1
  4. data/assets/build.json +4 -4
  5. data/assets/dashboard.js +12 -12
  6. data/assets/locales.json +5 -5
  7. data/assets/picker.js +1 -1
  8. data/assets/tracker.js +1 -1
  9. data/lib/runlight/accounts/accounts.rb +40 -10
  10. data/lib/runlight/accounts/crypto.rb +6 -3
  11. data/lib/runlight/accounts/lockout.rb +98 -0
  12. data/lib/runlight/accounts/throttle.rb +70 -16
  13. data/lib/runlight/accounts/web.rb +71 -28
  14. data/lib/runlight/body.rb +13 -0
  15. data/lib/runlight/core.rb +194 -28
  16. data/lib/runlight/data/sources.rb +1 -2
  17. data/lib/runlight/db/connect.rb +2 -2
  18. data/lib/runlight/db/db.rb +52 -32
  19. data/lib/runlight/geo.rb +2 -2
  20. data/lib/runlight/http/response.rb +22 -3
  21. data/lib/runlight/icon.rb +2 -1
  22. data/lib/runlight/importers/csv_visits.rb +13 -3
  23. data/lib/runlight/importers/dub.rb +2 -1
  24. data/lib/runlight/importers/index.rb +2 -1
  25. data/lib/runlight/importers/umami.rb +3 -0
  26. data/lib/runlight/importers/visits.rb +16 -5
  27. data/lib/runlight/importers/write.rb +50 -12
  28. data/lib/runlight/links.rb +10 -1
  29. data/lib/runlight/mcp.rb +10 -1
  30. data/lib/runlight/oauth.rb +34 -23
  31. data/lib/runlight/payload.rb +26 -7
  32. data/lib/runlight/rack_app.rb +7 -1
  33. data/lib/runlight/routes.rb +51 -21
  34. data/lib/runlight/server/cli.rb +9 -0
  35. data/lib/runlight/server/db_ip.rb +287 -45
  36. data/lib/runlight/server/standalone.rb +41 -5
  37. data/lib/runlight/sources.rb +3 -2
  38. data/lib/runlight/store/sql.rb +14 -0
  39. data/lib/runlight/store/sql_store.rb +88 -59
  40. data/lib/runlight/ua.rb +14 -4
  41. data/lib/runlight/version.rb +1 -1
  42. data/lib/runlight/weak_keys.rb +31 -0
  43. data/lib/runlight.rb +2 -0
  44. metadata +6 -4
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "ipaddr"
4
+
3
5
  module Runlight
4
6
  module Accounts
5
7
  # Accounts on the web: sign-in, the code step, invites, first-run setup, and the Account and People APIs, under
@@ -17,12 +19,14 @@ module Runlight
17
19
  }.freeze
18
20
 
19
21
  DEVICE_COOKIE = "runlight_device"
22
+ # The most a sign-in form or an account change may weigh: a few fields, far smaller than this.
23
+ MAX_BODY_BYTES = 64 * 1024
20
24
  # Who made each token, kept beside it as a setting, so removing someone or making them a viewer deletes them.
21
25
  MADE_BY = "token-by:"
22
26
  # When each account was last sent a sign-in link, at most one a minute; a setting, so every process sees it.
23
27
  LINK_SENT = "login-link-sent:"
24
28
  ESCAPES = { "&" => "&amp;", "<" => "&lt;", ">" => "&gt;", '"' => "&quot;", "'" => "&#39;" }.freeze
25
- private_constant :HTML, :DEVICE_COOKIE, :MADE_BY, :LINK_SENT, :ESCAPES
29
+ private_constant :HTML, :DEVICE_COOKIE, :MAX_BODY_BYTES, :MADE_BY, :LINK_SENT, :ESCAPES
26
30
 
27
31
  attr_reader :accounts
28
32
 
@@ -52,12 +56,17 @@ module Runlight
52
56
  # cannot keep the server hashing; a request with no known address skips that one,
53
57
  # since it would hold up everyone at once.
54
58
  # Each try counts before the password is checked, and a right one is taken back.
55
- @per_address = Throttle.new(@store, "address", 10)
59
+ # The two keyed by an address are counted in this process and never written down (an IPv6 address
60
+ # counts as its /64, which is one caller's to pick from); the one per account is kept in the database,
61
+ # so every process shares it.
62
+ @per_address = Throttle.new(@store, "address", 10, memory: true)
56
63
  @per_account = Throttle.new(@store, "account", 50)
57
- @per_ip = Throttle.new(@store, "ip", 30)
58
- # Six-digit codes: five wrong tries an account every fifteen minutes, and five to confirm the first one.
59
- # Password re-checks in Account: ten.
60
- @code_tries = Throttle.new(@store, "code", 5)
64
+ @per_ip = Throttle.new(@store, "ip", 30, memory: true)
65
+ # Six-digit codes: five wrong tries an account, then a lock of fifteen minutes that doubles each time the
66
+ # tries run out again, up to a day, so guessing on and on gets about five tries a day. A browser that
67
+ # signed in to the account before counts on its own, so its owner is never held up by others' guesses.
68
+ # Five tries to confirm the first code. Password re-checks in Account: ten.
69
+ @code_tries = Lockout.new(@store, "code", 5)
61
70
  @confirm_tries = Throttle.new(@store, "confirm", 5)
62
71
  @rechecks = Throttle.new(@store, "recheck", 10)
63
72
  end
@@ -87,6 +96,19 @@ module Runlight
87
96
  path.start_with?("//") || path.start_with?("/\\") ? @home_path : path
88
97
  end
89
98
 
99
+ # The address a caller's tries are counted under. One caller has a whole IPv6 /64 to send from, often
100
+ # more, so every address in it counts as the same; an IPv4 address written inside IPv6 counts as the
101
+ # IPv4 one. Anything that is not an address counts as it came.
102
+ def self.address_key(ip)
103
+ return "" if ip == ""
104
+
105
+ address = IPAddr.new(ip.to_s.sub(/%.*\z/m, "").delete_prefix("[").delete_suffix("]"))
106
+ address = address.native if address.ipv6? && address.ipv4_mapped?
107
+ address.ipv6? ? "#{address.mask(64)}/64" : address.to_s
108
+ rescue ArgumentError
109
+ ip.to_s
110
+ end
111
+
90
112
  def signed_in(request)
91
113
  value = read_cookie(request, Accounts::SESSION_COOKIE)
92
114
  return nil if value == ""
@@ -135,6 +157,29 @@ module Runlight
135
157
  # Answers an account page or API request at a path under the base, or nil for anything else.
136
158
  # context: {"ip" => the connection's address}, optional.
137
159
  def handle(request, path, context = {})
160
+ answer(request, path, context)
161
+ rescue Http::BodyTooLong
162
+ too_large
163
+ end
164
+
165
+ def confirm_password(request)
166
+ user = signed_in(request)
167
+ return coded("Sign in first", "sign_in", 401) if user.nil?
168
+ input = body(request)
169
+ return coded("Send JSON", "send_json", 415) if input.nil?
170
+ recheck(user, input, "password", ["Your password is not right", "password_wrong"])
171
+ rescue Http::BodyTooLong
172
+ too_large
173
+ end
174
+
175
+ private
176
+
177
+ # A body past the cap is refused here, since not every host caps a request body before it reaches the app.
178
+ def too_large
179
+ coded("Send a smaller body", "body_too_large", 413)
180
+ end
181
+
182
+ def answer(request, path, context)
138
183
  if path == "/api/account" || path.start_with?("/api/account/") || path == "/api/people" || path.start_with?("/api/people/") ||
139
184
  path.start_with?("/api/invites/")
140
185
  return api(request, path)
@@ -153,16 +198,6 @@ module Runlight
153
198
  nil
154
199
  end
155
200
 
156
- def confirm_password(request)
157
- user = signed_in(request)
158
- return coded("Sign in first", "sign_in", 401) if user.nil?
159
- input = body(request)
160
- return coded("Send JSON", "send_json", 415) if input.nil?
161
- recheck(user, input, "password", ["Your password is not right", "password_wrong"])
162
- end
163
-
164
- private
165
-
166
201
  def now
167
202
  @now.call
168
203
  end
@@ -231,10 +266,15 @@ module Runlight
231
266
  def body(request)
232
267
  return nil if media_type(request) != "application/json"
233
268
 
234
- parsed, value = Js.parse_json(request.text)
269
+ parsed, value = Js.parse_json(Body.request_text(request, MAX_BODY_BYTES))
235
270
  parsed && value.is_a?(Hash) ? value : nil
236
271
  end
237
272
 
273
+ # A posted form's fields. Raises Http::BodyTooLong for one past the cap.
274
+ def form_of(request)
275
+ Http::SearchParams.new(Body.request_text(request, MAX_BODY_BYTES))
276
+ end
277
+
238
278
  # `String(input[field] ?? "")`.
239
279
  def field(input, name)
240
280
  value = Js.get(input, name)
@@ -377,7 +417,7 @@ module Runlight
377
417
  return setup_ok(code) ? html(Pages.setup_page(base, { "code" => code }, lang)) : setup_locked(lang)
378
418
  end
379
419
  if method == "POST"
380
- form = Http::SearchParams.new(request.text)
420
+ form = form_of(request)
381
421
  code = form.get("code") || ""
382
422
  unless setup_ok(code)
383
423
  if @asks_for_token
@@ -424,13 +464,15 @@ module Runlight
424
464
 
425
465
  if path == "/login/code" && method == "POST"
426
466
  ready
427
- form = Http::SearchParams.new(request.text)
467
+ form = form_of(request)
428
468
  next_path = safe_next(form.get("next"))
429
469
  user = @accounts.from_pending(form.get("pending") || "", now)
430
470
  return redirect("#{base}/login?next=#{Js.encode_uri_component(next_path)}") if user.nil?
431
471
 
432
- # Counted before the check, so a burst cannot get past five.
433
- unless @code_tries.take(user["id"], now)
472
+ # Counted before the check, so a burst cannot get past five. A browser the account signed in from
473
+ # before has its own count, which only someone holding that browser's mark can use up.
474
+ tries = @accounts.trusts_device(read_cookie(request, DEVICE_COOKIE), user) ? "#{user["id"]}\ndevice" : user["id"]
475
+ unless @code_tries.take(tries, now)
434
476
  return html(Pages.code_page(base, { "pending" => form.get("pending") || "", "next" => next_path, "error" => t.call("error.too_many_tries") }, lang), 429)
435
477
  end
436
478
  unless @accounts.check_second_factor(user["id"], form.get("code") || "", now)
@@ -438,7 +480,7 @@ module Runlight
438
480
  "error" => t.call("auth.codeWrong") }, lang), 401)
439
481
  end
440
482
 
441
- @code_tries.clear(user["id"], now)
483
+ @code_tries.clear(tries)
442
484
  return signed_in_to(request, user, next_path)
443
485
  end
444
486
 
@@ -456,7 +498,7 @@ module Runlight
456
498
  return html(Pages.invite_page(base, { "code" => code, "email" => invite["email"], "role" => invite["role"], "site" => invite["site"], "host" => url.host }, lang))
457
499
  end
458
500
  if method == "POST"
459
- form = Http::SearchParams.new(request.text)
501
+ form = form_of(request)
460
502
  code = form.get("code") || ""
461
503
  invite = @accounts.invite_by_code(code, now)
462
504
  return html(Pages.invite_gone_page(base, lang), 410) if invite.nil?
@@ -483,12 +525,12 @@ module Runlight
483
525
  def login(request, context)
484
526
  ready
485
527
  base = @base
486
- form = Http::SearchParams.new(request.text)
528
+ form = form_of(request)
487
529
  email = form.get("email") || ""
488
530
  password = form.get("password") || ""
489
531
  next_path = safe_next(form.get("next"))
490
532
  account = Js.lower(Js.trim(email))
491
- ip = @rl.client_ip(request, context)
533
+ ip = self.class.address_key(@rl.client_ip(request, context))
492
534
  pair = "#{account}\n#{ip == "" ? "unknown" : ip}"
493
535
  lang = Messages.accepted_language(request)
494
536
  t = Messages.translator(lang)["t"]
@@ -509,7 +551,7 @@ module Runlight
509
551
  home = home_origin
510
552
  return too_many.call if !@rl.mail_settings || home.nil? || home == ""
511
553
 
512
- user = @accounts.sign_in(email, password)
554
+ user = @accounts.sign_in(email, password, known: trusted)
513
555
  unless user.nil?
514
556
  # Sent once the answer is out, as TypeScript does, so a right password takes no longer to answer than a
515
557
  # wrong one.
@@ -521,7 +563,7 @@ module Runlight
521
563
  end
522
564
  return html(login_page.call({ "error" => t.call("auth.tooManyLink"), "email" => email }), 429)
523
565
  end
524
- user = @accounts.sign_in(email, password)
566
+ user = @accounts.sign_in(email, password, known: trusted)
525
567
  return html(login_page.call({ "error" => t.call("auth.wrongPassword"), "email" => email }), 401) if user.nil?
526
568
 
527
569
 
@@ -542,7 +584,8 @@ module Runlight
542
584
  def recheck(user, input, name, wrong)
543
585
  return coded("Too many tries. Wait fifteen minutes and try again.", "too_many_tries", 429) unless @rechecks.take(user["id"], now)
544
586
  begin
545
- right = @accounts.sign_in(user["email"], field(input, name))
587
+ # Someone signed in already: their check is not held up by sign-in tries from strangers.
588
+ right = @accounts.sign_in(user["email"], field(input, name), known: true)
546
589
  rescue AccountError => e
547
590
  raise unless e.code == "busy"
548
591
 
data/lib/runlight/body.rb CHANGED
@@ -22,6 +22,19 @@ module Runlight
22
22
  utf8(text)
23
23
  end
24
24
 
25
+ # A request's body as text, up to max_bytes; past that, by its declared length or its own, Http::BodyTooLong.
26
+ # A Rack adapter has read the body whole by now (up to its own, far larger, limit), so this is where a
27
+ # route that takes only a small one refuses the rest.
28
+ def request_text(request, max_bytes)
29
+ declared = Js.number(request.headers.get("content-length"))
30
+ raise Http::BodyTooLong, "Body over #{max_bytes} bytes" if declared > max_bytes
31
+
32
+ text = request.text
33
+ raise Http::BodyTooLong, "Body over #{max_bytes} bytes" if text.bytesize > max_bytes
34
+
35
+ text
36
+ end
37
+
25
38
  # The body as JSON, up to max_bytes, as read_text_capped reads it.
26
39
  def read_json_capped(response, max_bytes)
27
40
  Json.decode(read_text_capped(response, max_bytes))