fingerly 0.4.0 → 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.
@@ -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.0"
5
+ VERSION = "0.5.0"
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,6 +118,7 @@ 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
@@ -262,9 +271,60 @@ module Fingerly
262
271
  AI_BOT: String
263
272
  AUTOMATION: String
264
273
  SEARCH_CRAWLER: String
274
+ UNKNOWN: String
275
+ ALL: Array[String]
276
+ end
277
+
278
+ module AiPurpose
279
+ TRAINING: String
280
+ SEARCH: String
281
+ USER_FETCH: String
282
+ AGENT: String
283
+ ALL: Array[String]
284
+ end
285
+
286
+ module AiRuntime
287
+ PROVIDER_CLOUD: String
288
+ HOSTED_BROWSER: String
289
+ USER_DEVICE: String
265
290
  ALL: Array[String]
266
291
  end
267
292
 
293
+ module AiVerificationMethod
294
+ SIGNATURE: String
295
+ IP_LIST: String
296
+ REVERSE_DNS: String
297
+ NONE: String
298
+ ALL: Array[String]
299
+ end
300
+
301
+ module AiVerificationResult
302
+ PASS: String
303
+ FAIL: String
304
+ NOT_ATTEMPTED: String
305
+ ALL: Array[String]
306
+ end
307
+
308
+ class AiVerification
309
+ attr_reader method: String
310
+ attr_reader result: String
311
+
312
+ def initialize: (Hash[String, untyped]) -> void
313
+ end
314
+
315
+ class AiAgent
316
+ attr_reader name: String
317
+ attr_reader provider: String
318
+ attr_reader purpose: String
319
+ attr_reader runtime: String
320
+ attr_reader declared: bool
321
+ attr_reader verification: AiVerification
322
+
323
+ def self.from: (untyped) -> AiAgent?
324
+ def initialize: (Hash[String, untyped]) -> void
325
+ def declared?: () -> bool
326
+ end
327
+
268
328
  class EventTrigger
269
329
  SIGNAL_ATTESTATION_FAILED: String
270
330
  SIGNAL_ATTESTATION_MISSING: String
@@ -321,6 +381,7 @@ module Fingerly
321
381
  attr_reader occurred_at: Time?
322
382
  attr_reader suspect_score: Integer?
323
383
  attr_reader triggers: Array[EventTrigger]
384
+ attr_reader ai: AiAgent?
324
385
  attr_reader document: Hash[String, untyped]?
325
386
  attr_reader raw: Hash[String, untyped]
326
387
 
@@ -413,6 +474,7 @@ module Fingerly
413
474
 
414
475
  attr_reader events: Events
415
476
  attr_reader device_check: DeviceCheck
477
+ attr_reader agents: Agents
416
478
 
417
479
  def initialize: (
418
480
  secret_key: String,
@@ -426,8 +488,137 @@ module Fingerly
426
488
  def get: (String) -> Hash[String, untyped]
427
489
  def spend_device_check: (untyped, String, String?) -> Hash[String, untyped]
428
490
  def post_consume: (String) -> Hash[String, untyped]
491
+ def post_json: (String, String) -> Hash[String, untyped]
492
+
493
+ def inspect: () -> String
494
+ end
495
+
496
+ class Agents
497
+ MAX_SIGHTINGS: Integer
498
+
499
+ def initialize: (Client) -> void
500
+ def report_sightings: (Array[AgentSightingInput]) -> AgentSightingsReport
501
+ def self.wire: (AgentSightingInput) -> Hash[String, untyped]
502
+
503
+ def inspect: () -> String
504
+ end
505
+
506
+ class AgentSightingInput
507
+ attr_reader user_agent: String
508
+ attr_reader client_ip: String
509
+ attr_reader host: String?
510
+ attr_reader request_method: String?
511
+ attr_reader signature: String?
512
+ attr_reader signature_input: String?
513
+ attr_reader signature_agent: String?
514
+ attr_reader count: Integer?
515
+
516
+ def initialize: (
517
+ user_agent: String,
518
+ client_ip: String,
519
+ ?host: String?,
520
+ ?request_method: String?,
521
+ ?signature: String?,
522
+ ?signature_input: String?,
523
+ ?signature_agent: String?,
524
+ ?count: Integer?
525
+ ) -> void
526
+ def to_h: () -> Hash[String, untyped]
527
+ end
528
+
529
+ class AgentSighting
530
+ attr_reader ttl: Integer
531
+ attr_reader visitor_kind: String
532
+ attr_reader agent: String?
533
+ attr_reader ai: AiAgent?
534
+
535
+ def initialize: (Hash[String, untyped]) -> void
536
+ end
537
+
538
+ class AgentSightingsReport
539
+ attr_reader rules_version: String
540
+ attr_reader items: Array[AgentSighting]
541
+ attr_reader raw: Hash[String, untyped]
542
+
543
+ def initialize: (Hash[String, untyped]) -> void
544
+ end
545
+
546
+ class AgentSightings
547
+ UNKNOWN: AgentSighting
548
+ DEFAULT_CAPACITY: Integer
549
+ MAX_CAPACITY: Integer
550
+ MAX_PENDING: Integer
551
+ FLUSH_ITEMS: Integer
552
+ FLUSH_BYTES: Integer
553
+ FLUSH_AFTER: Integer
554
+ FLUSH_DEADLINE: Integer
555
+ IN_FLIGHT_WAIT: Integer
556
+ TRAP_WAIT: Integer
557
+ MAX_TTL: Integer
558
+ MAX_HEADER_BYTES: Integer
559
+ MAX_COUNT: Integer
560
+ TICK: Float
561
+ AUTH_PAUSE: Integer
562
+ RATE_LIMIT_PAUSE: Integer
563
+ RETRY_AFTER_MIN: Integer
564
+ RETRY_AFTER_MAX: Integer
565
+ BACKOFF_MIN: Integer
566
+ BACKOFF_MAX: Integer
567
+ LOG_INTERVAL: Integer
568
+ WELL_KNOWN_PATHS: Array[String]
569
+
570
+ type request = Hash[String, untyped] | _HasEnv
571
+
572
+ interface _HasEnv
573
+ def env: () -> Hash[String, untyped]
574
+ end
575
+
576
+ attr_reader capacity: Integer
577
+
578
+ def initialize: (
579
+ secret_key: String,
580
+ client_ip: ^(ProxyRequest) -> String?,
581
+ ?capacity: Integer,
582
+ ?endpoint: String?,
583
+ ?allow_insecure_loopback: bool,
584
+ ?configure_http: (^(untyped) -> void)?,
585
+ ?logger: untyped,
586
+ ?clock: (^() -> Numeric)?
587
+ ) -> void
588
+ def lookup: (request) -> AgentSighting
589
+ def report: (request) -> nil
590
+ def flush: () -> nil
591
+ def close: () -> nil
592
+ def dropped_count: () -> Integer
593
+ def self.key: (
594
+ user_agent: String,
595
+ client_ip: String,
596
+ ?signature_agent: String,
597
+ ?signed: bool
598
+ ) -> String?
599
+ def self.canonical_ip: (untyped) -> String?
600
+ def self.monotonic: () -> Float
601
+ def self.trap_context?: () -> bool
602
+ def self.rate_limit_pause: (APIError) -> Integer
603
+ def self.outside_trap: (Array[Thread::Mutex]) { () -> void } -> nil
429
604
 
430
605
  def inspect: () -> String
606
+
607
+ class Middleware
608
+ DEFAULT_CONTENT_TYPES: Array[String]
609
+
610
+ def initialize: (
611
+ untyped app,
612
+ AgentSightings sightings,
613
+ ?content_types: Array[String],
614
+ ?include_path_prefixes: Array[String],
615
+ ?exclude_path_prefixes: Array[String]
616
+ ) -> void
617
+ def call: (Hash[String, untyped]) -> untyped
618
+ def reported?: (String, untyped) -> bool
619
+
620
+ def inspect: () -> String
621
+ end
431
622
  end
432
623
 
433
624
  class DeviceCheck
@@ -447,7 +638,10 @@ module Fingerly
447
638
  ?page: Integer?,
448
639
  ?limit: Integer?,
449
640
  ?visitor: String?,
450
- ?level: String?
641
+ ?level: String?,
642
+ ?kind: String?,
643
+ ?ai_provider: String?,
644
+ ?ai_purpose: String?
451
645
  ) -> EventPage
452
646
  DEFAULT_MAX_AGE_SECONDS: Integer
453
647
 
@@ -507,6 +701,8 @@ module Fingerly
507
701
  MISSING_ORIGIN_JSON: String
508
702
  ALLOWED_PLATFORMS: Regexp
509
703
  FORWARDED_CLIENT_HEADERS: Array[String]
704
+ SIGNATURE_HEADERS: Array[String]
705
+ MAX_SIGNATURE_HEADER_BYTES: Integer
510
706
  RELAYABLE_STATUS: Range[Integer]
511
707
  BODILESS: Array[Integer]
512
708
  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.0
4
+ version: 0.5.0
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