fingerly 0.4.1 → 0.5.1

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.
@@ -61,6 +61,25 @@ module Fingerly
61
61
 
62
62
  Result = Struct.new(:status, :headers, :body, keyword_init: true)
63
63
 
64
+ # How far one call got; see {TimeoutError#connected?} and
65
+ # {ConnectionError#sent?}.
66
+ Progress = Struct.new(:connected, :written)
67
+
68
+ # Marks a request written once Net::HTTP has sent all of it, so a
69
+ # connection lost after that can be told from one lost before. Should
70
+ # Net::HTTP stop calling it, nothing is marked and every lost connection
71
+ # reads as before sending.
72
+ module WriteTracking
73
+ attr_accessor :fingerly_progress
74
+
75
+ def exec(sock, ver, path)
76
+ result = super
77
+ fingerly_progress.written = true
78
+ result
79
+ end
80
+ end
81
+ private_constant :WriteTracking
82
+
64
83
  # @param origin [String] scheme and authority, without a trailing slash
65
84
  # @param timeout [Numeric] the deadline for one exchange, in seconds
66
85
  # @param configure_http [Proc, nil] receives the Net::HTTP before it starts
@@ -83,8 +102,13 @@ module Fingerly
83
102
  # @raise [TimeoutError] if the deadline passes at any point
84
103
  # @raise [ConnectionError] if the API cannot be reached
85
104
  def call(method, path, headers, body = nil)
105
+ # Whether a connection was open when time ran out: a timeout before one
106
+ # was sent nothing, and a caller that must never send twice can retry it.
107
+ progress = Progress.new(false, false)
86
108
  request = method.new(path, headers)
87
109
  request.body = body if body
110
+ request.extend(WriteTracking)
111
+ request.fingerly_progress = progress
88
112
  # Net::HTTP's own retry would resend on a connection error; reuse is
89
113
  # handled here instead, where an idempotent request can be told from one
90
114
  # that must never be sent twice.
@@ -93,19 +117,23 @@ module Fingerly
93
117
  # One budget for the whole call, so reopening a closed connection cannot
94
118
  # quietly buy a second timeout's worth of time.
95
119
  Timeout.timeout(@timeout, DeadlineExceeded) do
96
- perform(request, deadline: now + @timeout, idempotent: idempotent)
120
+ perform(request, deadline: now + @timeout, idempotent: idempotent, progress: progress)
97
121
  end
98
122
  rescue DeadlineExceeded
99
123
  # perform has already discarded the interrupted connection.
100
- raise TimeoutError, "The Fingerly request timed out"
124
+ raise TimeoutError.new("The Fingerly request timed out", connected: progress.connected)
125
+ rescue Net::OpenTimeout => e
126
+ raise TimeoutError.new("The Fingerly request timed out (#{e.class})", connected: false)
101
127
  rescue Timeout::Error => e
102
- # Net::OpenTimeout, ReadTimeout and WriteTimeout all derive from it.
103
- raise TimeoutError, "The Fingerly request timed out (#{e.class})"
128
+ # Net::ReadTimeout and WriteTimeout derive from it.
129
+ raise TimeoutError.new("The Fingerly request timed out (#{e.class})",
130
+ connected: progress.connected)
104
131
  rescue SocketError, IOError, SystemCallError, OpenSSL::OpenSSLError, Zlib::Error,
105
132
  Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
106
133
  # Every network failure arrives as the SDK's own error, never as a raw
107
134
  # runtime one. The class names the cause; nothing from the body does.
108
- raise ConnectionError, "Could not reach the Fingerly API (#{e.class})"
135
+ raise ConnectionError.new("Could not reach the Fingerly API (#{e.class})",
136
+ sent: progress.written)
109
137
  end
110
138
 
111
139
  # Closes the idle connections kept for this destination.
@@ -206,10 +234,10 @@ module Fingerly
206
234
  [["origin", "#{@uri.scheme}://#{@uri.host}:#{@uri.port}"], ["timeout", @timeout]]
207
235
  end
208
236
 
209
- def perform(request, deadline:, idempotent:)
237
+ def perform(request, deadline:, idempotent:, progress:)
210
238
  entry, reused = connection
211
239
  begin
212
- result = exchange(entry.http, request, deadline)
240
+ result = exchange(entry.http, request, deadline, progress)
213
241
  rescue IOError, SystemCallError, Net::HTTPBadResponse
214
242
  discard(entry.http)
215
243
  # A connection the other end closed while it was idle fails on its
@@ -221,7 +249,7 @@ module Fingerly
221
249
 
222
250
  entry = fresh
223
251
  begin
224
- result = exchange(entry.http, request, deadline)
252
+ result = exchange(entry.http, request, deadline, progress)
225
253
  rescue Exception # rubocop:disable Lint/RescueException
226
254
  discard(entry.http)
227
255
  raise
@@ -236,9 +264,11 @@ module Fingerly
236
264
  result
237
265
  end
238
266
 
239
- def exchange(http, request, deadline)
267
+ def exchange(http, request, deadline, progress)
240
268
  apply_timeouts(http, deadline)
241
269
  http.start unless http.started?
270
+ progress.connected = true
271
+ progress.written = false
242
272
  apply_timeouts(http, deadline)
243
273
 
244
274
  # The result is captured rather than returned from the block. Net::HTTP
@@ -262,7 +292,7 @@ module Fingerly
262
292
  end
263
293
  declared = response.content_length
264
294
  if declared && body.bytesize != declared
265
- raise ConnectionError, "The Fingerly API response was cut short"
295
+ raise ConnectionError.new("The Fingerly API response was cut short", sent: true)
266
296
  end
267
297
 
268
298
  result = Result.new(
@@ -276,10 +306,10 @@ module Fingerly
276
306
  raise Error, "The Fingerly API returned more than #{MAX_BODY_BYTES} bytes"
277
307
  end
278
308
 
279
- # @raise [TimeoutError] when nothing is left of the deadline
309
+ # @raise [DeadlineExceeded] when nothing is left of the deadline
280
310
  def apply_timeouts(http, deadline)
281
311
  left = deadline - now
282
- raise TimeoutError, "The Fingerly request timed out" if left <= 0
312
+ raise DeadlineExceeded if left <= 0
283
313
 
284
314
  http.open_timeout = left
285
315
  http.read_timeout = left
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Fingerly
4
4
  # The SDK version, reported to the API on every request.
5
- VERSION = "0.4.1"
5
+ VERSION = "0.5.1"
6
6
  end
data/lib/fingerly.rb CHANGED
@@ -10,6 +10,10 @@ require_relative "fingerly/transport"
10
10
  require_relative "fingerly/client"
11
11
  require_relative "fingerly/webhooks"
12
12
  require_relative "fingerly/proxy"
13
+ require_relative "fingerly/agent_sightings"
14
+ require_relative "fingerly/agent_sightings_store"
15
+ require_relative "fingerly/agent_sightings_reader"
16
+ require_relative "fingerly/agent_sightings_middleware"
13
17
 
14
18
  # Server-side Fingerly SDK.
15
19
  #
data/sig/fingerly.rbs CHANGED
@@ -20,13 +20,17 @@ module Fingerly
20
20
  attr_reader details: untyped
21
21
  attr_reader request_id: String?
22
22
  attr_reader retry_after: Integer?
23
+ attr_reader rate_limit_scope: String?
24
+
25
+ RATE_LIMIT_SCOPE_DAILY: String
23
26
 
24
27
  def initialize: (
25
28
  status: Integer,
26
29
  ?code: String?,
27
30
  ?details: untyped,
28
31
  ?request_id: String?,
29
- ?retry_after: Integer?
32
+ ?retry_after: Integer?,
33
+ ?rate_limit_scope: String?
30
34
  ) -> void
31
35
  def event_not_found?: () -> bool
32
36
  def pending?: () -> bool
@@ -69,9 +73,13 @@ module Fingerly
69
73
  end
70
74
 
71
75
  class TimeoutError < Error
76
+ def initialize: (?String message, ?connected: bool) -> void
77
+ def connected?: () -> bool
72
78
  end
73
79
 
74
80
  class ConnectionError < Error
81
+ def initialize: (?String message, ?sent: bool) -> void
82
+ def sent?: () -> bool
75
83
  end
76
84
 
77
85
  module Signal
@@ -88,7 +96,6 @@ module Fingerly
88
96
  RESIDENTIAL_PROXY_MEDIUM_CONFIDENCE: String
89
97
  RESIDENTIAL_PROXY_HIGH_CONFIDENCE: String
90
98
  VPN_PUBLIC: String
91
- VPN_TIMEZONE: String
92
99
  VPN_RELAY: String
93
100
  VPN_MOBILE: String
94
101
  VPN_OS_MISMATCH: String
@@ -103,6 +110,7 @@ module Fingerly
103
110
  ANYCAST_IP: String
104
111
  INCOGNITO: String
105
112
  BAD_BOT: String
113
+ AI_SPOOFED: String
106
114
  AUTOMATION: String
107
115
  FETCH_METADATA_MISSING: String
108
116
  WEB_TICKET_MISSING: String
@@ -110,11 +118,13 @@ module Fingerly
110
118
  ANTIDETECT_BROWSER: String
111
119
  VIRTUAL_MACHINE: String
112
120
  FINGERPRINT_SUPPRESSED: String
121
+ IDENTITY_OBFUSCATED: String
113
122
  PRIVACY_SETTINGS: String
114
123
  EMBEDDED_WEB_VIEW: String
115
124
  REMOTE_CONTROL: String
116
125
  EMULATOR: String
117
126
  ROOT_APPS: String
127
+ BOOTLOADER_UNLOCKED: String
118
128
  APP_CLONER: String
119
129
  JAILBREAK: String
120
130
  SIMULATOR: String
@@ -262,9 +272,60 @@ module Fingerly
262
272
  AI_BOT: String
263
273
  AUTOMATION: String
264
274
  SEARCH_CRAWLER: String
275
+ UNKNOWN: String
276
+ ALL: Array[String]
277
+ end
278
+
279
+ module AiPurpose
280
+ TRAINING: String
281
+ SEARCH: String
282
+ USER_FETCH: String
283
+ AGENT: String
284
+ ALL: Array[String]
285
+ end
286
+
287
+ module AiRuntime
288
+ PROVIDER_CLOUD: String
289
+ HOSTED_BROWSER: String
290
+ USER_DEVICE: String
265
291
  ALL: Array[String]
266
292
  end
267
293
 
294
+ module AiVerificationMethod
295
+ SIGNATURE: String
296
+ IP_LIST: String
297
+ REVERSE_DNS: String
298
+ NONE: String
299
+ ALL: Array[String]
300
+ end
301
+
302
+ module AiVerificationResult
303
+ PASS: String
304
+ FAIL: String
305
+ NOT_ATTEMPTED: String
306
+ ALL: Array[String]
307
+ end
308
+
309
+ class AiVerification
310
+ attr_reader method: String
311
+ attr_reader result: String
312
+
313
+ def initialize: (Hash[String, untyped]) -> void
314
+ end
315
+
316
+ class AiAgent
317
+ attr_reader name: String
318
+ attr_reader provider: String
319
+ attr_reader purpose: String
320
+ attr_reader runtime: String
321
+ attr_reader declared: bool
322
+ attr_reader verification: AiVerification
323
+
324
+ def self.from: (untyped) -> AiAgent?
325
+ def initialize: (Hash[String, untyped]) -> void
326
+ def declared?: () -> bool
327
+ end
328
+
268
329
  class EventTrigger
269
330
  SIGNAL_ATTESTATION_FAILED: String
270
331
  SIGNAL_ATTESTATION_MISSING: String
@@ -321,6 +382,7 @@ module Fingerly
321
382
  attr_reader occurred_at: Time?
322
383
  attr_reader suspect_score: Integer?
323
384
  attr_reader triggers: Array[EventTrigger]
385
+ attr_reader ai: AiAgent?
324
386
  attr_reader document: Hash[String, untyped]?
325
387
  attr_reader raw: Hash[String, untyped]
326
388
 
@@ -413,6 +475,7 @@ module Fingerly
413
475
 
414
476
  attr_reader events: Events
415
477
  attr_reader device_check: DeviceCheck
478
+ attr_reader agents: Agents
416
479
 
417
480
  def initialize: (
418
481
  secret_key: String,
@@ -426,8 +489,137 @@ module Fingerly
426
489
  def get: (String) -> Hash[String, untyped]
427
490
  def spend_device_check: (untyped, String, String?) -> Hash[String, untyped]
428
491
  def post_consume: (String) -> Hash[String, untyped]
492
+ def post_json: (String, String) -> Hash[String, untyped]
493
+
494
+ def inspect: () -> String
495
+ end
496
+
497
+ class Agents
498
+ MAX_SIGHTINGS: Integer
499
+
500
+ def initialize: (Client) -> void
501
+ def report_sightings: (Array[AgentSightingInput]) -> AgentSightingsReport
502
+ def self.wire: (AgentSightingInput) -> Hash[String, untyped]
503
+
504
+ def inspect: () -> String
505
+ end
506
+
507
+ class AgentSightingInput
508
+ attr_reader user_agent: String
509
+ attr_reader client_ip: String
510
+ attr_reader host: String?
511
+ attr_reader request_method: String?
512
+ attr_reader signature: String?
513
+ attr_reader signature_input: String?
514
+ attr_reader signature_agent: String?
515
+ attr_reader count: Integer?
516
+
517
+ def initialize: (
518
+ user_agent: String,
519
+ client_ip: String,
520
+ ?host: String?,
521
+ ?request_method: String?,
522
+ ?signature: String?,
523
+ ?signature_input: String?,
524
+ ?signature_agent: String?,
525
+ ?count: Integer?
526
+ ) -> void
527
+ def to_h: () -> Hash[String, untyped]
528
+ end
529
+
530
+ class AgentSighting
531
+ attr_reader ttl: Integer
532
+ attr_reader visitor_kind: String
533
+ attr_reader agent: String?
534
+ attr_reader ai: AiAgent?
535
+
536
+ def initialize: (Hash[String, untyped]) -> void
537
+ end
538
+
539
+ class AgentSightingsReport
540
+ attr_reader rules_version: String
541
+ attr_reader items: Array[AgentSighting]
542
+ attr_reader raw: Hash[String, untyped]
543
+
544
+ def initialize: (Hash[String, untyped]) -> void
545
+ end
546
+
547
+ class AgentSightings
548
+ UNKNOWN: AgentSighting
549
+ DEFAULT_CAPACITY: Integer
550
+ MAX_CAPACITY: Integer
551
+ MAX_PENDING: Integer
552
+ FLUSH_ITEMS: Integer
553
+ FLUSH_BYTES: Integer
554
+ FLUSH_AFTER: Integer
555
+ FLUSH_DEADLINE: Integer
556
+ IN_FLIGHT_WAIT: Integer
557
+ TRAP_WAIT: Integer
558
+ MAX_TTL: Integer
559
+ MAX_HEADER_BYTES: Integer
560
+ MAX_COUNT: Integer
561
+ TICK: Float
562
+ AUTH_PAUSE: Integer
563
+ RATE_LIMIT_PAUSE: Integer
564
+ RETRY_AFTER_MIN: Integer
565
+ RETRY_AFTER_MAX: Integer
566
+ BACKOFF_MIN: Integer
567
+ BACKOFF_MAX: Integer
568
+ LOG_INTERVAL: Integer
569
+ WELL_KNOWN_PATHS: Array[String]
570
+
571
+ type request = Hash[String, untyped] | _HasEnv
572
+
573
+ interface _HasEnv
574
+ def env: () -> Hash[String, untyped]
575
+ end
576
+
577
+ attr_reader capacity: Integer
578
+
579
+ def initialize: (
580
+ secret_key: String,
581
+ client_ip: ^(ProxyRequest) -> String?,
582
+ ?capacity: Integer,
583
+ ?endpoint: String?,
584
+ ?allow_insecure_loopback: bool,
585
+ ?configure_http: (^(untyped) -> void)?,
586
+ ?logger: untyped,
587
+ ?clock: (^() -> Numeric)?
588
+ ) -> void
589
+ def lookup: (request) -> AgentSighting
590
+ def report: (request) -> nil
591
+ def flush: () -> nil
592
+ def close: () -> nil
593
+ def dropped_count: () -> Integer
594
+ def self.key: (
595
+ user_agent: String,
596
+ client_ip: String,
597
+ ?signature_agent: String,
598
+ ?signed: bool
599
+ ) -> String?
600
+ def self.canonical_ip: (untyped) -> String?
601
+ def self.monotonic: () -> Float
602
+ def self.trap_context?: () -> bool
603
+ def self.rate_limit_pause: (APIError) -> Integer
604
+ def self.outside_trap: (Array[Thread::Mutex]) { () -> void } -> nil
429
605
 
430
606
  def inspect: () -> String
607
+
608
+ class Middleware
609
+ DEFAULT_CONTENT_TYPES: Array[String]
610
+
611
+ def initialize: (
612
+ untyped app,
613
+ AgentSightings sightings,
614
+ ?content_types: Array[String],
615
+ ?include_path_prefixes: Array[String],
616
+ ?exclude_path_prefixes: Array[String]
617
+ ) -> void
618
+ def call: (Hash[String, untyped]) -> untyped
619
+ def reported?: (String, untyped) -> bool
620
+
621
+ def inspect: () -> String
622
+ end
431
623
  end
432
624
 
433
625
  class DeviceCheck
@@ -447,7 +639,10 @@ module Fingerly
447
639
  ?page: Integer?,
448
640
  ?limit: Integer?,
449
641
  ?visitor: String?,
450
- ?level: String?
642
+ ?level: String?,
643
+ ?kind: String?,
644
+ ?ai_provider: String?,
645
+ ?ai_purpose: String?
451
646
  ) -> EventPage
452
647
  DEFAULT_MAX_AGE_SECONDS: Integer
453
648
 
@@ -507,6 +702,8 @@ module Fingerly
507
702
  MISSING_ORIGIN_JSON: String
508
703
  ALLOWED_PLATFORMS: Regexp
509
704
  FORWARDED_CLIENT_HEADERS: Array[String]
705
+ SIGNATURE_HEADERS: Array[String]
706
+ MAX_SIGNATURE_HEADER_BYTES: Integer
510
707
  RELAYABLE_STATUS: Range[Integer]
511
708
  BODILESS: Array[Integer]
512
709
  RELAYED_HEADERS: Array[String]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fingerly
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fingerly
@@ -22,6 +22,10 @@ files:
22
22
  - LICENSE
23
23
  - README.md
24
24
  - lib/fingerly.rb
25
+ - lib/fingerly/agent_sightings.rb
26
+ - lib/fingerly/agent_sightings_middleware.rb
27
+ - lib/fingerly/agent_sightings_reader.rb
28
+ - lib/fingerly/agent_sightings_store.rb
25
29
  - lib/fingerly/client.rb
26
30
  - lib/fingerly/config.rb
27
31
  - lib/fingerly/enums.rb