firegex 5.1.3__tar.gz → 5.1.4__tar.gz

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 (35) hide show
  1. {firegex-5.1.3 → firegex-5.1.4}/PKG-INFO +1 -1
  2. {firegex-5.1.3 → firegex-5.1.4}/firegex/__init__.py +1 -1
  3. {firegex-5.1.3 → firegex-5.1.4}/firegex/pyfilters/models/http.py +190 -101
  4. {firegex-5.1.3 → firegex-5.1.4}/firegex.egg-info/PKG-INFO +1 -1
  5. {firegex-5.1.3 → firegex-5.1.4}/setup.py +1 -1
  6. {firegex-5.1.3 → firegex-5.1.4}/MANIFEST.in +0 -0
  7. {firegex-5.1.3 → firegex-5.1.4}/README.md +0 -0
  8. {firegex-5.1.3 → firegex-5.1.4}/firegex/__main__.py +0 -0
  9. {firegex-5.1.3 → firegex-5.1.4}/firegex/cli.py +0 -0
  10. {firegex-5.1.3 → firegex-5.1.4}/firegex/pyfilters/__init__.py +0 -0
  11. {firegex-5.1.3 → firegex-5.1.4}/firegex/pyfilters/internals/__init__.py +0 -0
  12. {firegex-5.1.3 → firegex-5.1.4}/firegex/pyfilters/internals/data.py +0 -0
  13. {firegex-5.1.3 → firegex-5.1.4}/firegex/pyfilters/internals/exceptions.py +0 -0
  14. {firegex-5.1.3 → firegex-5.1.4}/firegex/pyfilters/internals/models.py +0 -0
  15. {firegex-5.1.3 → firegex-5.1.4}/firegex/pyfilters/internals/zstd_compat.py +0 -0
  16. {firegex-5.1.3 → firegex-5.1.4}/firegex/pyfilters/models/__init__.py +0 -0
  17. {firegex-5.1.3 → firegex-5.1.4}/firegex/pyfilters/models/tcp.py +0 -0
  18. {firegex-5.1.3 → firegex-5.1.4}/firegex/pyfilters/proxysim/__init__.py +0 -0
  19. {firegex-5.1.3 → firegex-5.1.4}/firegex/regex/__init__.py +0 -0
  20. {firegex-5.1.3 → firegex-5.1.4}/firegex/regex/sim.py +0 -0
  21. {firegex-5.1.3 → firegex-5.1.4}/firegex.egg-info/SOURCES.txt +0 -0
  22. {firegex-5.1.3 → firegex-5.1.4}/firegex.egg-info/dependency_links.txt +0 -0
  23. {firegex-5.1.3 → firegex-5.1.4}/firegex.egg-info/entry_points.txt +0 -0
  24. {firegex-5.1.3 → firegex-5.1.4}/firegex.egg-info/requires.txt +0 -0
  25. {firegex-5.1.3 → firegex-5.1.4}/firegex.egg-info/top_level.txt +0 -0
  26. {firegex-5.1.3 → firegex-5.1.4}/llhttp/LICENSE.pyllhttp +0 -0
  27. {firegex-5.1.3 → firegex-5.1.4}/llhttp/PROVENANCE.md +0 -0
  28. {firegex-5.1.3 → firegex-5.1.4}/llhttp/lib/LICENSE.llhttp +0 -0
  29. {firegex-5.1.3 → firegex-5.1.4}/llhttp/lib/api.c +0 -0
  30. {firegex-5.1.3 → firegex-5.1.4}/llhttp/lib/http.c +0 -0
  31. {firegex-5.1.3 → firegex-5.1.4}/llhttp/lib/llhttp.c +0 -0
  32. {firegex-5.1.3 → firegex-5.1.4}/llhttp/lib/llhttp.h +0 -0
  33. {firegex-5.1.3 → firegex-5.1.4}/llhttp/llhttp_module.c +0 -0
  34. {firegex-5.1.3 → firegex-5.1.4}/pyproject.toml +0 -0
  35. {firegex-5.1.3 → firegex-5.1.4}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: firegex
3
- Version: 5.1.3
3
+ Version: 5.1.4
4
4
  Summary: Firegex client
5
5
  Author-email: Pwnzer0tt1 <pwnzer0tt1@poliba.it>
6
6
  Project-URL: Homepage, https://github.com/pwnzer0tt1/firegex
@@ -1,6 +1,6 @@
1
1
 
2
2
  def _get_version():
3
- v = "5.1.3" if "{" not in "5.1.3" else "0.0.0"
3
+ v = "5.1.4" if "{" not in "5.1.4" else "0.0.0"
4
4
  if v == "0.0.0":
5
5
  import os
6
6
  env_v = os.getenv("FIREGEX_VERSION")
@@ -75,6 +75,7 @@ class InternalHTTPMessage:
75
75
  upgrading_to_h2: bool = field(default=False)
76
76
  upgrading_to_ws: bool = field(default=False)
77
77
  added_to_history: bool = field(default=False)
78
+ headers_emitted: bool = field(default=False)
78
79
  #: How much of `body` has already been handed to a model that consumes it.
79
80
  #:
80
81
  #: On the message rather than beside it because that is what it belongs to: a
@@ -303,76 +304,77 @@ class InternalCallbackHandler:
303
304
  self.msg.body = self.buffers._body_buffer
304
305
  self.msg.should_upgrade = self.should_upgrade
305
306
  self.buffers._body_buffer = b""
306
- encodings = [ele.strip() for ele in self.content_encoding.lower().split(",")]
307
- decode_success = True
308
- decoding_body = self.msg.body
309
- # Every step under the same limit, so a stack of encodings cannot multiply past it.
310
- limit = self.max_decoded_size
311
- try:
312
- for enc in reversed(encodings):
313
- if not enc:
314
- continue
315
- if enc == "deflate":
316
- # Both spellings, because both are on the wire. RFC 7230 defines
317
- # `deflate` as the zlib format of RFC 1950, and plenty of servers
318
- # send the raw stream instead — browsers accept either, and a decoder
319
- # that takes only one lets half the compressed traffic reach a filter
320
- # still compressed, where a pattern finds nothing and the only trace
321
- # is a line on stdout saying it skipped.
322
- for wbits in (-zlib.MAX_WBITS, zlib.MAX_WBITS):
307
+ if self.save_body:
308
+ encodings = [ele.strip() for ele in self.content_encoding.lower().split(",")]
309
+ decode_success = True
310
+ decoding_body = self.msg.body
311
+ # Every step under the same limit, so a stack of encodings cannot multiply past it.
312
+ limit = self.max_decoded_size
313
+ try:
314
+ for enc in reversed(encodings):
315
+ if not enc:
316
+ continue
317
+ if enc == "deflate":
318
+ # Both spellings, because both are on the wire. RFC 7230 defines
319
+ # `deflate` as the zlib format of RFC 1950, and plenty of servers
320
+ # send the raw stream instead — browsers accept either, and a decoder
321
+ # that takes only one lets half the compressed traffic reach a filter
322
+ # still compressed, where a pattern finds nothing and the only trace
323
+ # is a line on stdout saying it skipped.
324
+ for wbits in (-zlib.MAX_WBITS, zlib.MAX_WBITS):
325
+ try:
326
+ decoding_body = _inflate(decoding_body, wbits, limit)
327
+ except zlib.error:
328
+ continue
329
+ break
330
+ else:
331
+ print("Error decompressing deflate: neither raw nor zlib-wrapped: "
332
+ "skipping", flush=True)
333
+ decode_success = False
334
+ break
335
+ elif enc == "br":
323
336
  try:
324
- decoding_body = _inflate(decoding_body, wbits, limit)
325
- except zlib.error:
326
- continue
327
- break
337
+ decoding_body = _unbrotli(decoding_body, limit)
338
+ except _TooLarge:
339
+ raise
340
+ except Exception as e:
341
+ print(f"Error decompressing brotli: {e}: skipping", flush=True)
342
+ decode_success = False
343
+ break
344
+ elif (
345
+ enc == "gzip" or enc == "x-gzip"
346
+ ): # https://datatracker.ietf.org/doc/html/rfc2616#section-3.5
347
+ try:
348
+ decoding_body = _gunzip(decoding_body, limit)
349
+ except _TooLarge:
350
+ raise
351
+ except Exception as e:
352
+ print(f"Error decompressing gzip: {e}: skipping", flush=True)
353
+ decode_success = False
354
+ break
355
+ elif enc == "zstd":
356
+ try:
357
+ decoding_body = _unzstd(decoding_body, limit)
358
+ except _TooLarge:
359
+ raise
360
+ except Exception as e:
361
+ print(f"Error decompressing zstd: {e}: skipping", flush=True)
362
+ decode_success = False
363
+ break
364
+ elif enc == "identity":
365
+ pass # No need to do anything https://datatracker.ietf.org/doc/html/rfc2616#section-3.5 (it's possible to be found also if it should't be used)
328
366
  else:
329
- print("Error decompressing deflate: neither raw nor zlib-wrapped: "
330
- "skipping", flush=True)
331
367
  decode_success = False
332
368
  break
333
- elif enc == "br":
334
- try:
335
- decoding_body = _unbrotli(decoding_body, limit)
336
- except _TooLarge:
337
- raise
338
- except Exception as e:
339
- print(f"Error decompressing brotli: {e}: skipping", flush=True)
340
- decode_success = False
341
- break
342
- elif (
343
- enc == "gzip" or enc == "x-gzip"
344
- ): # https://datatracker.ietf.org/doc/html/rfc2616#section-3.5
345
- try:
346
- decoding_body = _gunzip(decoding_body, limit)
347
- except _TooLarge:
348
- raise
349
- except Exception as e:
350
- print(f"Error decompressing gzip: {e}: skipping", flush=True)
351
- decode_success = False
352
- break
353
- elif enc == "zstd":
354
- try:
355
- decoding_body = _unzstd(decoding_body, limit)
356
- except _TooLarge:
357
- raise
358
- except Exception as e:
359
- print(f"Error decompressing zstd: {e}: skipping", flush=True)
360
- decode_success = False
361
- break
362
- elif enc == "identity":
363
- pass # No need to do anything https://datatracker.ietf.org/doc/html/rfc2616#section-3.5 (it's possible to be found also if it should't be used)
364
- else:
365
- decode_success = False
366
- break
367
- except _TooLarge:
368
- print(f"[WARNING] The body decodes to more than {limit} bytes "
369
- f"(FGEX_STREAM_MAX_SIZE): left as it arrived", flush=True)
370
- self.msg.decoded_too_large = True
371
- decode_success = False
369
+ except _TooLarge:
370
+ print(f"[WARNING] The body decodes to more than {limit} bytes "
371
+ f"(FGEX_STREAM_MAX_SIZE): left as it arrived", flush=True)
372
+ self.msg.decoded_too_large = True
373
+ decode_success = False
372
374
 
373
- if decode_success:
374
- self.msg.body = decoding_body
375
- self.msg.body_decoded = True
375
+ if decode_success:
376
+ self.msg.body = decoding_body
377
+ self.msg.body_decoded = True
376
378
 
377
379
  self.msg.message_complete = True
378
380
  self.has_begun = False
@@ -602,6 +604,18 @@ class HttpHistory:
602
604
  elif hasattr(obj, "history"):
603
605
  return obj.history
604
606
 
607
+ for key in ("http_module", "http_full", "http_header"):
608
+ if (
609
+ f"{key}_in" in internal_data.data_handler_context
610
+ or f"{key}_out" in internal_data.data_handler_context
611
+ ):
612
+ if not internal_data.call_mem.get(key, False):
613
+ _flush_pending_history(internal_data, key)
614
+ req_history_deque, resp_history_deque = _get_history_deques(
615
+ internal_data, key
616
+ )
617
+ return HttpHistory(list(req_history_deque), list(resp_history_deque))
618
+
605
619
  req_history_deque = internal_data.data_handler_context.get(
606
620
  "http_history_requests", deque()
607
621
  )
@@ -635,6 +649,75 @@ def _trim_history(history: deque, budget: int) -> None:
635
649
  total -= _held(history.popleft())
636
650
 
637
651
 
652
+ def _get_history_deques(
653
+ internal_data: DataStreamCtx, parser_class: str
654
+ ) -> tuple[deque, deque]:
655
+ raw_max = internal_data.filter_glob.get("FGEX_MAX_HISTORY_SIZE", 100)
656
+ try:
657
+ max_history = max(0, int(raw_max))
658
+ except (ValueError, TypeError):
659
+ max_history = 100
660
+
661
+ req_key = f"{parser_class}_history_requests"
662
+ req_history_deque: deque = internal_data.data_handler_context.setdefault(
663
+ req_key, deque(maxlen=max_history)
664
+ )
665
+ if req_history_deque.maxlen != max_history:
666
+ req_history_deque = deque(req_history_deque, maxlen=max_history)
667
+ internal_data.data_handler_context[req_key] = req_history_deque
668
+
669
+ resp_key = f"{parser_class}_history_responses"
670
+ resp_history_deque: deque = internal_data.data_handler_context.setdefault(
671
+ resp_key, deque(maxlen=max_history)
672
+ )
673
+ if resp_history_deque.maxlen != max_history:
674
+ resp_history_deque = deque(resp_history_deque, maxlen=max_history)
675
+ internal_data.data_handler_context[resp_key] = resp_history_deque
676
+
677
+ internal_data.data_handler_context["http_history_requests"] = req_history_deque
678
+ internal_data.data_handler_context["http_history_responses"] = resp_history_deque
679
+ return req_history_deque, resp_history_deque
680
+
681
+
682
+ def _record_in_history(
683
+ internal_data: DataStreamCtx,
684
+ parser_class: str,
685
+ parser: InternalHttpRequest | InternalHttpResponse,
686
+ msg: InternalHTTPMessage,
687
+ is_input: bool,
688
+ ) -> None:
689
+ if not msg.message_complete or msg.added_to_history:
690
+ return
691
+ req_history_deque, resp_history_deque = _get_history_deques(
692
+ internal_data, parser_class
693
+ )
694
+ msg.added_to_history = True
695
+ if is_input:
696
+ req_history_deque.append(HttpFullRequest(parser, msg))
697
+ _trim_history(req_history_deque, internal_data.stream_max_size)
698
+ else:
699
+ resp_history_deque.append(HttpFullResponse(parser, msg))
700
+ _trim_history(resp_history_deque, internal_data.stream_max_size)
701
+
702
+
703
+ def _flush_pending_history(
704
+ internal_data: DataStreamCtx, parser_class: str
705
+ ) -> tuple[deque, deque]:
706
+ """Move any completed messages still waiting in `parser_class`'s queues into its
707
+ history before a new packet clears them.
708
+ """
709
+ req_history_deque, resp_history_deque = _get_history_deques(
710
+ internal_data, parser_class
711
+ )
712
+ for suffix, is_req in (("in", True), ("out", False)):
713
+ p = internal_data.data_handler_context.get(f"{parser_class}_{suffix}")
714
+ if p is None:
715
+ continue
716
+ for msg in p.pop_all_messages():
717
+ _record_in_history(internal_data, parser_class, p, msg, is_req)
718
+ return req_history_deque, resp_history_deque
719
+
720
+
638
721
  class InternalBasicHttpMetaClass:
639
722
  """Internal class to handle HTTP requests and responses"""
640
723
 
@@ -751,6 +834,10 @@ class InternalBasicHttpMetaClass:
751
834
  def _should_release_body_chunks(cls) -> bool:
752
835
  return False
753
836
 
837
+ @classmethod
838
+ def _save_body(cls) -> bool:
839
+ return True
840
+
754
841
  @staticmethod
755
842
  def _before_fetch_callable_checks(internal_data: DataStreamCtx) -> bool:
756
843
  raise NotImplementedError()
@@ -789,6 +876,7 @@ class InternalBasicHttpMetaClass:
789
876
  # above is: parsing happens before any instance exists, and a flag applied
790
877
  # afterwards would take effect one packet late.
791
878
  parser.release_body_chunks = cls._should_release_body_chunks()
879
+ parser.save_body = cls._save_body()
792
880
  parser.max_decoded_size = internal_data.stream_max_size
793
881
 
794
882
 
@@ -796,7 +884,7 @@ class InternalBasicHttpMetaClass:
796
884
  cls._parser_class(), False
797
885
  ): # Need to parse HTTP
798
886
  internal_data.call_mem[cls._parser_class()] = True
799
- parser.pop_all_messages() # Delete content on message deque
887
+ _flush_pending_history(internal_data, cls._parser_class())
800
888
 
801
889
  # Setting websocket options if needed to the client parser
802
890
  if internal_data.current_pkt.is_input:
@@ -835,7 +923,7 @@ class InternalBasicHttpMetaClass:
835
923
  raise NotReadyToRun()
836
924
 
837
925
  internal_data.call_mem["headers_were_set"] = (
838
- parser.msg.headers_complete
926
+ parser.msg.headers_complete and not parser.msg.message_complete
839
927
  ) # This information is usefull for building the real object
840
928
 
841
929
  try:
@@ -922,10 +1010,15 @@ class InternalBasicHttpMetaClass:
922
1010
  raise NotReadyToRun()
923
1011
 
924
1012
  messages_tosend: list[InternalHTTPMessage] = []
1013
+ completed_for_history: list[InternalHTTPMessage] = []
925
1014
  for i in range(len(parser.messages)):
926
- messages_tosend.append(parser.pop_message())
1015
+ msg = parser.pop_message()
1016
+ if not cls._save_body() and msg.headers_emitted:
1017
+ completed_for_history.append(msg)
1018
+ else:
1019
+ messages_tosend.append(msg)
927
1020
 
928
- if len(messages_tosend) > 0:
1021
+ if len(messages_tosend) > 0 or len(completed_for_history) > 0:
929
1022
  internal_data.call_mem["headers_were_set"] = (
930
1023
  False # New messages completed so the current message headers were not set in this case
931
1024
  )
@@ -945,6 +1038,7 @@ class InternalBasicHttpMetaClass:
945
1038
  # the second one twice: once from the queue and once from here. Blocking is
946
1039
  # idempotent and survived that, which is why it went unnoticed — anything
947
1040
  # that counts did not.
1041
+ parser.msg.headers_emitted = True
948
1042
  messages_tosend.append(parser.msg)
949
1043
 
950
1044
  if (
@@ -965,35 +1059,23 @@ class InternalBasicHttpMetaClass:
965
1059
  parser.msg
966
1060
  ) # Also the current message needs to beacase a stream is going on
967
1061
 
1062
+ req_history_deque, resp_history_deque = _get_history_deques(
1063
+ internal_data, cls._parser_class()
1064
+ )
1065
+ for msg in completed_for_history:
1066
+ _record_in_history(
1067
+ internal_data,
1068
+ cls._parser_class(),
1069
+ parser,
1070
+ msg,
1071
+ internal_data.current_pkt.is_input,
1072
+ )
1073
+
968
1074
  messages_to_call = len(messages_tosend)
969
1075
 
970
1076
  if messages_to_call == 0:
971
1077
  raise NotReadyToRun()
972
1078
 
973
- raw_max = internal_data.filter_glob.get("FGEX_MAX_HISTORY_SIZE", 100)
974
- try:
975
- max_history = max(0, int(raw_max))
976
- except (ValueError, TypeError):
977
- max_history = 100
978
-
979
- req_history_deque: deque = (
980
- internal_data.data_handler_context.setdefault(
981
- "http_history_requests", deque(maxlen=max_history)
982
- )
983
- )
984
- if req_history_deque.maxlen != max_history:
985
- req_history_deque = deque(req_history_deque, maxlen=max_history)
986
- internal_data.data_handler_context["http_history_requests"] = req_history_deque
987
-
988
- resp_history_deque: deque = (
989
- internal_data.data_handler_context.setdefault(
990
- "http_history_responses", deque(maxlen=max_history)
991
- )
992
- )
993
- if resp_history_deque.maxlen != max_history:
994
- resp_history_deque = deque(resp_history_deque, maxlen=max_history)
995
- internal_data.data_handler_context["http_history_responses"] = resp_history_deque
996
-
997
1079
  built_instances = []
998
1080
  for msg in messages_tosend:
999
1081
  history_snapshot = HttpHistory(
@@ -1003,14 +1085,13 @@ class InternalBasicHttpMetaClass:
1003
1085
  instance._history = history_snapshot
1004
1086
  built_instances.append(instance)
1005
1087
 
1006
- if msg.message_complete and not msg.added_to_history:
1007
- msg.added_to_history = True
1008
- if internal_data.current_pkt.is_input:
1009
- req_history_deque.append(HttpFullRequest(parser, msg))
1010
- _trim_history(req_history_deque, internal_data.stream_max_size)
1011
- else:
1012
- resp_history_deque.append(HttpFullResponse(parser, msg))
1013
- _trim_history(resp_history_deque, internal_data.stream_max_size)
1088
+ _record_in_history(
1089
+ internal_data,
1090
+ cls._parser_class(),
1091
+ parser,
1092
+ msg,
1093
+ internal_data.current_pkt.is_input,
1094
+ )
1014
1095
 
1015
1096
  if len(built_instances) == 1:
1016
1097
  res = built_instances[0]
@@ -1133,6 +1214,10 @@ class HttpRequestHeader(HttpRequest):
1133
1214
  This data handler will be called only once, the headers are complete, the body will be empty and not buffered
1134
1215
  """
1135
1216
 
1217
+ @classmethod
1218
+ def _save_body(cls) -> bool:
1219
+ return False
1220
+
1136
1221
  def _contructor_hook(self):
1137
1222
  self._parser.save_body = False
1138
1223
 
@@ -1147,6 +1232,10 @@ class HttpResponseHeader(HttpResponse):
1147
1232
  This data handler will be called only once, the headers are complete, the body will be empty and not buffered
1148
1233
  """
1149
1234
 
1235
+ @classmethod
1236
+ def _save_body(cls) -> bool:
1237
+ return False
1238
+
1150
1239
  def _contructor_hook(self):
1151
1240
  self._parser.save_body = False
1152
1241
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: firegex
3
- Version: 5.1.3
3
+ Version: 5.1.4
4
4
  Summary: Firegex client
5
5
  Author-email: Pwnzer0tt1 <pwnzer0tt1@poliba.it>
6
6
  Project-URL: Homepage, https://github.com/pwnzer0tt1/firegex
@@ -26,7 +26,7 @@ LLHTTP = setuptools.Extension(
26
26
  # Everything else about the package is in pyproject.toml. The version stays here because
27
27
  # the release workflow writes the tag over the placeholder, which is not a valid version
28
28
  # until it has.
29
- VERSION = "5.1.3"
29
+ VERSION = "5.1.4"
30
30
 
31
31
  setuptools.setup(
32
32
  version=VERSION if "{" not in VERSION else "0.0.0", #uv pip install -U . --no-cache-dir for testing
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes