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
@@ -34,10 +34,14 @@ module Runlight
34
34
  # The longest client id, which carries the app's name and redirect addresses.
35
35
  MAX_CLIENT_ID = 2048
36
36
  SCOPES = %w[read manage cms].freeze
37
+ # The most an OAuth request's body may be: a registration, a consent form, or a code swap is far smaller.
38
+ MAX_BODY_BYTES = 64 * 1024
37
39
 
38
- # Where the per-address count of registrations is kept. TypeScript keeps it in memory per install; here it
39
- # lives in the install's settings, keyed by a hash of the address, so every process shares it.
40
- REGISTRATIONS = "oauth-registrations"
40
+ # Each install's per-address count of registrations, kept in this process as TypeScript keeps it and as the
41
+ # tracker's rate limit is: under a key made at start, dropped whole at the end of each minute, and never
42
+ # written anywhere.
43
+ REGISTRATIONS = WeakKeys.new
44
+ REGISTRATIONS_LOCK = Mutex.new
41
45
 
42
46
  CORS = {
43
47
  "access-control-allow-origin" => "*",
@@ -46,7 +50,7 @@ module Runlight
46
50
  }.freeze
47
51
 
48
52
  ESCAPES = { "&" => "&amp;", "<" => "&lt;", ">" => "&gt;", '"' => "&quot;", "'" => "&#39;" }.freeze
49
- private_constant :REGISTRATIONS, :CORS, :ESCAPES, :SCOPES
53
+ private_constant :REGISTRATIONS, :REGISTRATIONS_LOCK, :CORS, :ESCAPES, :SCOPES, :MAX_BODY_BYTES
50
54
 
51
55
  module_function
52
56
 
@@ -89,6 +93,13 @@ module Runlight
89
93
  #
90
94
  # context: {"ip" => the connection's address}, optional.
91
95
  def oauth_response(ctx, request, path, url, context = {})
96
+ respond(ctx, request, path, url, context)
97
+ rescue Http::BodyTooLong
98
+ # Capped here, since not every host caps a request body before it reaches the app.
99
+ oauth_error("invalid_request", "Send a smaller body", 413)
100
+ end
101
+
102
+ def respond(ctx, request, path, url, context)
92
103
  runlight = ctx["runlight"]
93
104
  base = ctx["base"].to_s
94
105
  issuer = "#{url.origin}#{base}"
@@ -121,7 +132,7 @@ module Runlight
121
132
  return oauth_error("invalid_client_metadata", "Too many registrations from this address. Wait a minute and try again.", 429)
122
133
  end
123
134
 
124
- parsed, body = Js.parse_json(request.text)
135
+ parsed, body = Js.parse_json(Body.request_text(request, MAX_BODY_BYTES))
125
136
  body = nil unless parsed
126
137
  uris = !body.nil? && Js.object?(body) ? Js.get(body, "redirect_uris") : nil
127
138
  redirects = []
@@ -138,12 +149,15 @@ module Runlight
138
149
  h = translator["html"]
139
150
  lang = translator["lang"]
140
151
  page = ->(title, body, status = 200) { page_in(lang, title, body, status) }
141
- form = request.method == "POST" ? Http::SearchParams.new(request.text) : url.search_params
152
+ # The app's own check of who is asking reads the same request, whose body is kept as text, so it can find
153
+ # the csrf field there each time it is asked.
154
+ form = request.method == "POST" ? Http::SearchParams.new(Body.request_text(request, MAX_BODY_BYTES)) : url.search_params
142
155
  client_id = form.get("client_id") || ""
143
156
  client = client_for(runlight, client_id)&.fetch("client", nil)
144
157
  redirect = form.get("redirect_uri") || ""
145
158
  # Without a known client and one of its own addresses there is nowhere safe to send an answer.
146
- if client.nil? || !Array(client["redirects"]).include?(redirect)
159
+ # An address that does not parse, from an app registered before those were refused, is no address of its own.
160
+ if client.nil? || !Array(client["redirects"]).include?(redirect) || Http::Url.parse(redirect).nil?
147
161
  return page.call(h.call("oauth.unregisteredTitle"), "<p>#{h.call("oauth.startAgain")}</p>", 400)
148
162
  end
149
163
 
@@ -186,7 +200,8 @@ module Runlight
186
200
  unless ctx["isOwner"].call(request)
187
201
  # Someone signed in who may only read would be sent to sign in again and again.
188
202
  if ctx["isReader"]&.call(request)
189
- return page.call(h.call("oauth.viewerTitle"), "<p>#{h.call("oauth.viewer", { "app" => esc(name) })}</p>", 403)
203
+ site_only = ctx["isSiteOnly"] ? ctx["isSiteOnly"].call(request) : false
204
+ return page.call(h.call("oauth.viewerTitle"), "<p>#{h.call(site_only ? "oauth.siteAccount" : "oauth.viewer", { "app" => esc(name) })}</p>", 403)
190
205
  end
191
206
 
192
207
  # The site stays, since on the way in it only says which one to offer first.
@@ -304,7 +319,8 @@ module Runlight
304
319
  if path == "/oauth/token" && request.method == "POST"
305
320
  runlight.init
306
321
  type = (request.headers.get("content-type") || "").split(";", -1)[0].to_s.strip
307
- form = type == "application/json" ? json_form(request.text) : Http::SearchParams.new(request.text)
322
+ text = Body.request_text(request, MAX_BODY_BYTES)
323
+ form = type == "application/json" ? json_form(text) : Http::SearchParams.new(text)
308
324
  return oauth_error("unsupported_grant_type", "Only authorization_code is supported") if form.get("grant_type") != "authorization_code"
309
325
 
310
326
  key = "oauth-code:#{Hashing.sha256(form.get("code") || "")}"
@@ -418,9 +434,10 @@ module Runlight
418
434
  json({ "error" => error, "error_description" => description }, status)
419
435
  end
420
436
 
421
- # Redirect addresses a client may register: https, or a local app's own loopback address.
437
+ # Redirect addresses a client may register: https, or a local app's own loopback address, and one that
438
+ # parses as an address at all.
422
439
  def allowed_redirect(value)
423
- value.match?(%r{\Ahttps://[^/]+}) || value.match?(%r{\Ahttp://(localhost|127\.0\.0\.1|\[::1\])(:\d+)?/})
440
+ (value.match?(%r{\Ahttps://[^/]+}) || value.match?(%r{\Ahttp://(localhost|127\.0\.0\.1|\[::1\])(:\d+)?/})) && !Http::Url.parse(value).nil?
424
441
  end
425
442
 
426
443
  # `new URLSearchParams(Object.entries(await request.json() ?? {}))`, each value as String() writes it.
@@ -438,18 +455,12 @@ module Runlight
438
455
  end
439
456
 
440
457
  # Counts a registration from an address and says whether it is under the limit for this minute. The count
441
- # is kept in the install's settings, keyed by an HMAC of the address, so no address is ever stored.
458
+ # is kept in this process, keyed by a hash of the address, so no address is ever kept or written down.
442
459
  def allow_registration(runlight, ip)
443
- # No address cannot be told apart, so it is not limited.
444
- return true if ip == ""
445
-
446
- window = runlight.now.div(60_000)
447
- id = Hashing.hmac(client_key(runlight), "register:#{ip}")[0, 16]
448
- saved = Json.try_decode(runlight.store.setting(REGISTRATIONS).to_s)
449
- counts = saved.is_a?(Hash) && saved["window"] == window && saved["counts"].is_a?(Hash) ? saved["counts"] : {}
450
- counts[id] = Js.number(counts[id] || 0).to_i + 1
451
- runlight.store.set_setting(REGISTRATIONS, Json.encode({ "window" => window, "counts" => counts }))
452
- counts[id] <= REGISTRATIONS_PER_MINUTE
460
+ limit = REGISTRATIONS_LOCK.synchronize do
461
+ REGISTRATIONS[runlight] ||= RateLimit.new(REGISTRATIONS_PER_MINUTE, -> { runlight.now })
462
+ end
463
+ limit.allow(ip)
453
464
  end
454
465
 
455
466
  # Registers a client by signing its name and addresses into its id, so
@@ -494,7 +505,7 @@ module Runlight
494
505
  )
495
506
  end
496
507
 
497
- private_class_method :base64url, :from_base64url, :client_key, :client_for, :site_host, :constant_time_equal, :esc, :json, :oauth_error,
508
+ private_class_method :respond, :base64url, :from_base64url, :client_key, :client_for, :site_host, :constant_time_equal, :esc, :json, :oauth_error,
498
509
  :allowed_redirect, :json_form, :allow_registration, :register, :page_in
499
510
  end
500
511
  end
@@ -21,10 +21,29 @@ module Runlight
21
21
 
22
22
  module_function
23
23
 
24
+ # A string as it came, cut to `max` UTF-16 units: for a URL, which the parser then encodes.
24
25
  def str(value, max)
25
26
  value.is_a?(String) ? Js.slice(value, 0, max) : ""
26
27
  end
27
28
 
29
+ # Text as every database can keep it: without U+0000, which Postgres refuses. Half a surrogate pair, which
30
+ # is no character, is U+FFFD by the time text gets here: parse reads a lone escape as one, and bytes that
31
+ # are not UTF-8 are scrubbed to it.
32
+ def tidy(text)
33
+ Js.scrub(text).delete("\0")
34
+ end
35
+
36
+ # Tidied text cut to `max` characters (code points), so a character such as an emoji is never cut in half.
37
+ def clean_text(text, max)
38
+ clean = tidy(text)
39
+ clean.length <= max ? clean : clean[0, max]
40
+ end
41
+
42
+ # A string the tracker sent, as it is stored.
43
+ def stored(value, max)
44
+ value.is_a?(String) ? clean_text(value, max) : ""
45
+ end
46
+
28
47
  def int(value, min, max)
29
48
  return nil unless value.is_a?(Integer) || value.is_a?(Float)
30
49
 
@@ -42,11 +61,11 @@ module Runlight
42
61
  entries(value).each do |key, raw|
43
62
  break if count >= MAX_PROPS
44
63
 
45
- k = Js.slice(Js.trim(key), 0, 60)
64
+ k = clean_text(Js.trim(tidy(key)), 60)
46
65
  next if k.empty?
47
66
 
48
67
  if raw.is_a?(String)
49
- text = Js.slice(raw, 0, 500)
68
+ text = clean_text(raw, 500)
50
69
  elsif (raw.is_a?(Integer) || raw.is_a?(Float)) && raw.to_f.finite?
51
70
  # JSON.parse reads every number as a double, so a long integer is rounded as it is there.
52
71
  text = Js.string(raw.to_f)
@@ -120,7 +139,7 @@ module Runlight
120
139
  url = Http::Url.parse(str(get.call("u"), 2048))
121
140
  return nil if url.nil? || (url.protocol != "http:" && url.protocol != "https:")
122
141
 
123
- name = Js.trim(str(get.call("n"), 120))
142
+ name = Js.trim(stored(get.call("n"), 120))
124
143
  return nil if kind == "event" && name.empty?
125
144
 
126
145
  pageview_id = str(get.call("i"), 32)
@@ -129,13 +148,13 @@ module Runlight
129
148
 
130
149
  {
131
150
  "kind" => kind,
132
- "site" => str(get.call("s"), 64),
151
+ "site" => stored(get.call("s"), 64),
133
152
  "url" => url,
134
153
  "referrer" => str(get.call("r"), 2048),
135
- "title" => str(get.call("t"), 500),
154
+ "title" => stored(get.call("t"), 500),
136
155
  "screenWidth" => int(get.call("w"), 0, 20_000),
137
156
  "screenHeight" => int(get.call("h"), 0, 20_000),
138
- "language" => str(get.call("l"), 35),
157
+ "language" => stored(get.call("l"), 35),
139
158
  "name" => name,
140
159
  "props" => kind == "event" ? link_props(name, props(get.call("p"))) : nil,
141
160
  "pageviewId" => pageview_id,
@@ -144,6 +163,6 @@ module Runlight
144
163
  }
145
164
  end
146
165
 
147
- private_class_method :str, :int, :props, :link_props, :entries, :parse
166
+ private_class_method :str, :stored, :int, :props, :link_props, :entries, :parse
148
167
  end
149
168
  end
@@ -20,9 +20,11 @@ module Runlight
20
20
  # passed through. Once the answer is sent, the work TS does after answering (a
21
21
  # retention change's deletions) runs in the Core's idle.
22
22
  class RackApp
23
+ # Methods no route or link answers, so they are the app's.
24
+ UNSUPPORTED = %w[TRACE TRACK CONNECT].freeze
23
25
  INTERNAL = Json.encode({ "error" => "Internal error", "code" => "internal" })
24
26
  TOO_LARGE = Json.encode({ "error" => "That request is too large" })
25
- private_constant :INTERNAL, :TOO_LARGE
27
+ private_constant :INTERNAL, :TOO_LARGE, :UNSUPPORTED
26
28
 
27
29
  attr_reader :runlight
28
30
 
@@ -49,6 +51,10 @@ module Runlight
49
51
  def call(env)
50
52
  # A request the app behind will answer keeps its body for the app; Runlight reads only its headers.
51
53
  path = "#{env["SCRIPT_NAME"]}#{env["PATH_INFO"]}"
54
+ if UNSUPPORTED.include?((env["rack.methodoverride.original_method"] || env["REQUEST_METHOD"]).to_s.upcase)
55
+ return @app.nil? ? [405, {}, []] : @app.call(env)
56
+ end
57
+
52
58
  begin
53
59
  request = Http::Request.from_rack(env, read_body: @app.nil? || mine?(path))
54
60
  rescue Http::Request::TooLarge
@@ -97,7 +97,10 @@ module Runlight
97
97
  EMAIL = /\A[^#{Js::SPACE}@<>"]+@[^#{Js::SPACE}@<>"]+\.[^#{Js::SPACE}@<>"]+\z/
98
98
  # A dashboard's origin, which a picker ticket names.
99
99
  ORIGIN = %r{\Ahttps?://[^/?\##{Js::SPACE}]+\z}
100
- private_constant :EMAIL, :ORIGIN
100
+ # The longest email address and domain name there can be. Anything longer is refused before a pattern reads it.
101
+ MAX_EMAIL = 320
102
+ MAX_DOMAIN = 253
103
+ private_constant :EMAIL, :ORIGIN, :MAX_EMAIL, :MAX_DOMAIN
101
104
 
102
105
  # The files in assets/, read once per process.
103
106
  @assets = {}
@@ -105,8 +108,6 @@ module Runlight
105
108
 
106
109
  # Requests from a manage token, already checked against its one site, act as the owner's; requests from a
107
110
  # member have full access apart from the install-wide controls. Both are keyed by the request object.
108
- WEAK_MAP = defined?(ObjectSpace::WeakKeyMap) ? ObjectSpace::WeakKeyMap : ObjectSpace::WeakMap
109
- private_constant :WEAK_MAP
110
111
 
111
112
  attr_reader :base
112
113
 
@@ -114,8 +115,8 @@ module Runlight
114
115
  options = Options.normalize(options)
115
116
  @rl = runlight
116
117
  @options = options
117
- @managed = WEAK_MAP.new
118
- @members = WEAK_MAP.new
118
+ @managed = WeakKeys.new
119
+ @members = WeakKeys.new
119
120
  # When each report's last sample went out.
120
121
  @sample_sent = {}
121
122
  # Each person's questions to the assistant in the last hour, and how many are being answered now.
@@ -181,7 +182,11 @@ module Runlight
181
182
  "runlight" => runlight,
182
183
  "base" => @base,
183
184
  "isOwner" => ->(r) { can_read(r) == true },
184
- "isReader" => ->(r) { authorize.nil? ? (web ? web.access(r) == "read" : false) : authorize.call(r) == "read" },
185
+ # An account kept to one site connects no apps either, so it is told so rather than sent to sign in again.
186
+ "isReader" => lambda do |r|
187
+ !session_scope(r)&.dig("site").nil? || (authorize.nil? ? (web ? web.access(r) == "read" : false) : authorize.call(r) == "read")
188
+ end,
189
+ "isSiteOnly" => ->(r) { !session_scope(r)&.dig("site").nil? },
185
190
  }
186
191
  oauth["signIn"] = @sign_in if !@sign_in.nil? && @sign_in != ""
187
192
  oauth["accountOf"] = @account_of unless @account_of.nil?
@@ -249,7 +254,14 @@ module Runlight
249
254
  else
250
255
  first.sub(/:\d*\z/, "")
251
256
  end
252
- name.sub(/\.+\z/, "").sub(/\Awww\./, "")
257
+ without_final_dots(name).sub(/\Awww\./, "")
258
+ end
259
+
260
+ # Text without its final dots, read from the end, so a long run of dots costs no more than its length.
261
+ def without_final_dots(text)
262
+ finish = text.length
263
+ finish -= 1 while finish.positive? && text[finish - 1] == "."
264
+ text[0, finish]
253
265
  end
254
266
 
255
267
  # Whether a domain name is one kept for private networks or tests, or has
@@ -263,7 +275,8 @@ module Runlight
263
275
  end
264
276
 
265
277
  def email?(value)
266
- Js.scrub(value).match?(EMAIL)
278
+ text = Js.scrub(value)
279
+ Js.length(text) <= MAX_EMAIL && text.match?(EMAIL)
267
280
  end
268
281
 
269
282
  # A plain page in a visitor's language, for unsubscribing, for a share link that is gone, and for the picker's
@@ -607,6 +620,12 @@ module Runlight
607
620
  @web.signed_in(request) if @web
608
621
  end
609
622
 
623
+ # A hub's manage token and an account kept to one site reach one site's settings and nothing of the install's,
624
+ # so neither is taken at its word about where this app lives: the Host of a request is the caller's to choose.
625
+ def restricted?(request)
626
+ @managed.key?(request) || !session_scope(request)&.dig("site").nil?
627
+ end
628
+
610
629
  def can_read(request)
611
630
  return true if @managed.key?(request)
612
631
 
@@ -681,7 +700,8 @@ module Runlight
681
700
  @rl.site(url.search_params.get("site")) || coded("Unknown site", "unknown_site", 404)
682
701
  end
683
702
 
684
- def read_query(url, site)
703
+ # `clicks` is for the short link reports, whose "all" reaches back to the oldest click as well as the oldest visit.
704
+ def read_query(url, site, clicks = false)
685
705
  params = url.search_params
686
706
  filters = []
687
707
  if params.get_all("filter").length > Query::MAX_FILTERS
@@ -697,7 +717,10 @@ module Runlight
697
717
  now = @rl.now
698
718
  first_date = nil
699
719
  if params.get("period") == "all"
700
- first = store.first_seen(site["id"])
720
+ seen = store.first_seen(site["id"])
721
+ # Clicks imported as daily counts belong to no visit, and may be older than any.
722
+ clicked = clicks ? store.first_click(site["id"]) : nil
723
+ first = [seen, clicked].compact.min
701
724
  first_date = Dates.local_date(first.to_i, site["timezone"]) unless first.nil?
702
725
  end
703
726
  range = Dates.resolve_range({ "period" => params.get("period"), "from" => params.get("from"), "to" => params.get("to"),
@@ -819,10 +842,12 @@ module Runlight
819
842
  # The checks a link domain passes before a site gets it, from Settings or from an import: the name as it
820
843
  # is kept, or the refusal to send.
821
844
  def check_link_domain(request, url, site, value)
822
- domain = Js.lower(Js.trim(value))
823
- domain = domain.sub(%r{\Ahttps?://}, "")
845
+ given = Js.lower(Js.trim(value))
846
+ return coded("That is not a domain name", "domain_invalid", 400) if Js.length(given) > MAX_DOMAIN
847
+
848
+ domain = given.sub(%r{\Ahttps?://}, "")
824
849
  domain = domain.sub(%r{/.*\z}, "")
825
- domain = domain.sub(/\.+\z/, "")
850
+ domain = without_final_dots(domain)
826
851
  domain = domain.sub(/\Awww\./, "")
827
852
  return coded("That is not a domain name", "domain_invalid", 400) unless domain.match?(DOMAIN_NAME)
828
853
 
@@ -833,8 +858,8 @@ module Runlight
833
858
  # A link domain answers every path on it, so it must never be where the dashboard or a counted site lives.
834
859
  # The request's own Host is the caller's to choose, so the configured address and the names people
835
860
  # signed in from count too. A hub cannot know every name this app answers on, so it adds none until
836
- # the app knows its own address.
837
- return origin_needed if @managed.key?(request) && @origin.nil?
861
+ # the app knows its own address, and neither does an account kept to one site, which has a hub's reach.
862
+ return origin_needed if restricted?(request) && @origin.nil?
838
863
 
839
864
  here = [request.headers.get("host"), request.headers.get("x-forwarded-host"), url.host].select { |h| Js.truthy?(h) }
840
865
  own = [*(@origin.nil? ? [] : [Http::Url.new(@origin).host]), *here]
@@ -930,7 +955,7 @@ module Runlight
930
955
 
931
956
  if path == "/api/links"
932
957
  if request.method == "GET"
933
- read = read_query(url, site)
958
+ read = read_query(url, site, true)
934
959
  return read if read.is_a?(Http::Response)
935
960
 
936
961
  links = store.links(site["id"], read["range"]["from"], read["range"]["to"])
@@ -993,7 +1018,7 @@ module Runlight
993
1018
  link = store.link_by_id(id)
994
1019
  return coded("Unknown link", "unknown_link", 404) if link.nil? || link["site"] != site["id"]
995
1020
 
996
- read = read_query(url, site)
1021
+ read = read_query(url, site, true)
997
1022
  return read if read.is_a?(Http::Response)
998
1023
 
999
1024
  range = read["range"]
@@ -1297,8 +1322,9 @@ module Runlight
1297
1322
  # Links in the email point back to the configured address, or else to this dashboard at the address
1298
1323
  # the request came to. An address in the body is never used, so whoever adds a report cannot point
1299
1324
  # its links somewhere else. A report made from a hub needs the configured address, where its
1300
- # unsubscribe link answers, since the Host its request names is the hub's to choose.
1301
- return origin_needed if @managed.key?(request) && @origin.nil?
1325
+ # unsubscribe link answers, since the Host its request names is the hub's to choose. So does one
1326
+ # made by an account kept to one site.
1327
+ return origin_needed if restricted?(request) && @origin.nil?
1302
1328
 
1303
1329
  home = "#{@origin || url.origin}#{@base}"
1304
1330
  # A period already due counts as sent, so a report added mid-week first goes out on the next Monday, as the form says.
@@ -2003,6 +2029,11 @@ module Runlight
2003
2029
  if !hub.nil? && store.setting("token-origin:#{hub["id"]}") != origin
2004
2030
  return coded("This hub's address is not the one it connected from. Connect the site again from here.", "pick_hub", 403)
2005
2031
  end
2032
+ # An account kept to one site gets a ticket only for this dashboard: the configured address, or without
2033
+ # one the address its request came to.
2034
+ if hub.nil? && !session_scope(request)&.dig("site").nil? && origin != (@origin || url.origin)
2035
+ return coded("Your account can only pick from this dashboard's own address", "pick_own", 403)
2036
+ end
2006
2037
 
2007
2038
  return json({ "ticket" => pick_ticket(origin, site["id"], back) })
2008
2039
  end
@@ -2131,8 +2162,7 @@ module Runlight
2131
2162
  recorded = 0
2132
2163
  # A fetch reported without its address cannot be checked; it is never checked against the reporter's own.
2133
2164
  keep.each do |p|
2134
- request = Http::Request.new(p["page"].href, method: "GET", headers: { "user-agent" => p["userAgent"] })
2135
- recorded += 1 if rl.observe(request, p["at"], { "agentIp" => p["ip"] })
2165
+ recorded += 1 if rl.observe(Http::Request.new(p["page"].href, method: "GET"), p["at"], { "agentIp" => p["ip"], "userAgent" => p["userAgent"] })
2136
2166
  end
2137
2167
  # A single report, as the CMS plugins send, needs no answer; a batch learns what was kept.
2138
2168
  return Http::Response.new("", status: 204) unless batch
@@ -124,6 +124,11 @@ module Runlight
124
124
  host = config.get("HOST") || "0.0.0.0"
125
125
  server = config.standalone({ "now" => now })
126
126
  server.runlight.init
127
+ # Said at every start, loudly, and the server still runs: an install with a short one must keep working.
128
+ %w[RUNLIGHT_SECRET RUNLIGHT_TOKEN].each do |name|
129
+ warning = Standalone.short_warning(name, config.get(name))
130
+ err.write("\n#{warning}\n\n") unless warning.nil?
131
+ end
127
132
  shown = ["0.0.0.0", "::"].include?(host) ? "localhost" : host
128
133
  options = { Host: host, Port: port }
129
134
  if name == "puma"
@@ -151,6 +156,10 @@ module Runlight
151
156
  end
152
157
  out.flush if out.respond_to?(:flush)
153
158
  end
159
+ rescue Errno::EADDRINUSE, Errno::EADDRNOTAVAIL, Errno::EACCES => e
160
+ # A port already taken, or an address that cannot be listened on, is said in a line, like any other
161
+ # trouble at start.
162
+ raise RuntimeError, "could not listen on #{host}:#{port}: #{e.message}"
154
163
  ensure
155
164
  server.stop
156
165
  config.store.close if config.store.respond_to?(:close)