-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy path_reader.py
More file actions
2162 lines (1859 loc) · 84.7 KB
/
_reader.py
File metadata and controls
2162 lines (1859 loc) · 84.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""TIFF/COG reader: tile/strip assembly, windowed reads, HTTP range requests."""
from __future__ import annotations
import math
import mmap
import os as _os_module
import threading
import urllib.request
from collections import OrderedDict
from concurrent.futures import ThreadPoolExecutor
import numpy as np
from ._compression import (
COMPRESSION_LERC,
COMPRESSION_NONE,
decompress,
fp_predictor_decode,
lerc_decompress_with_mask,
predictor_decode,
unpack_bits,
)
from ._dtypes import SUB_BYTE_BPS, resolve_bits_per_sample, tiff_dtype_to_numpy
from ._geotags import (
GeoInfo,
GeoTransform,
RASTER_PIXEL_IS_POINT,
extract_geo_info,
extract_geo_info_with_overview_inheritance,
)
from ._header import (
IFD,
TIFFHeader,
parse_all_ifds,
parse_header,
select_overview_ifd,
validate_tile_layout,
)
# ---------------------------------------------------------------------------
# Allocation guard: reject TIFF dimensions that would exhaust memory
# ---------------------------------------------------------------------------
#: Default maximum total pixel count (width * height * samples).
#: ~1 billion pixels, which is ~4 GB for float32 single-band.
#: Override per-call via the ``max_pixels`` keyword argument.
MAX_PIXELS_DEFAULT = 1_000_000_000
def _check_dimensions(width, height, samples, max_pixels):
"""Raise ValueError if the requested allocation exceeds *max_pixels*."""
total = width * height * samples
if total > max_pixels:
raise ValueError(
f"TIFF image dimensions ({width} x {height} x {samples} = "
f"{total:,} pixels) exceed the safety limit of "
f"{max_pixels:,} pixels. Pass a larger max_pixels value to "
f"read_to_array() if this file is legitimate."
)
#: Default per-tile (or per-strip) compressed-byte cap. A crafted
#: ``TileByteCounts`` / ``StripByteCounts`` entry can declare arbitrarily
#: many bytes. On HTTP, the reader would issue a Range GET sized by the
#: attacker's value; on local files, mmap slicing is bounded by the file
#: size but a small compressed slice can still decompress (deflate/zstd/
#: lzw) into hundreds of MiB. 256 MiB tolerates legitimate large tiles
#: (RGB JPEG2000 at very high resolution can land in the tens of MB)
#: while keeping the fetch / decode bounded. Override via the
#: ``XRSPATIAL_COG_MAX_TILE_BYTES`` environment variable. Issues #1536
#: (HTTP) and #1664 (local).
MAX_TILE_BYTES_DEFAULT = 256 << 20 # 256 MiB
def _max_tile_bytes_from_env() -> int:
"""Read the per-tile byte cap from the environment, or fall back to the default.
Non-integer, empty, zero, or negative values all fall back to
``MAX_TILE_BYTES_DEFAULT``. Matches the policy used by the HTTP
timeout helpers so callers don't accidentally set an unreachable
1-byte cap with ``XRSPATIAL_COG_MAX_TILE_BYTES=-1``.
"""
raw = _os_module.environ.get('XRSPATIAL_COG_MAX_TILE_BYTES')
if raw is None:
return MAX_TILE_BYTES_DEFAULT
try:
val = int(raw)
except (TypeError, ValueError):
return MAX_TILE_BYTES_DEFAULT
return val if val > 0 else MAX_TILE_BYTES_DEFAULT
# ---------------------------------------------------------------------------
# Data source abstraction
# ---------------------------------------------------------------------------
#: Soft cap on the number of mmap entries the reader keeps open at once.
#: When the cache size exceeds this, the least-recently-used *idle* entry
#: (refcount 0) is closed. In-use entries are never evicted. Override via
#: the ``XRSPATIAL_GEOTIFF_MMAP_CACHE_SIZE`` environment variable.
_DEFAULT_MMAP_CACHE_SIZE = 32
def _mmap_cache_size_from_env() -> int:
"""Read the cache size cap from the environment, falling back to the default."""
raw = _os_module.environ.get('XRSPATIAL_GEOTIFF_MMAP_CACHE_SIZE')
if raw is None:
return _DEFAULT_MMAP_CACHE_SIZE
try:
val = int(raw)
except (TypeError, ValueError):
return _DEFAULT_MMAP_CACHE_SIZE
return max(1, val)
class _MmapCache:
"""Thread-safe, reference-counted, bounded LRU mmap cache.
Multiple threads reading the same file share a single read-only mmap.
The cache keeps idle (refcount 0) mmaps around so repeated opens of the
same file avoid the cost of re-mapping. When the number of entries
exceeds the cap (default 32, or ``XRSPATIAL_GEOTIFF_MMAP_CACHE_SIZE``),
the least-recently-used *idle* entry is evicted. Entries with active
references are never evicted.
mmap slicing on a read-only mapping is thread-safe (no seek involved).
"""
def __init__(self, max_size: int | None = None):
self._lock = threading.Lock()
# path -> entry list. Each entry is
# [fh, mm, size, refcount, ident, orphaned]
#
# ``ident`` is (st_ino, st_size, st_mtime_ns) used to spot files that
# were replaced (e.g. via ``os.replace`` on an atomic write) at the
# same path. ``orphaned`` is True once the entry has been removed
# from ``self._entries`` (typically because the underlying file was
# replaced). An orphaned entry is no longer the cache slot for the
# path, but live ``_FileSource`` instances still hold the entry list
# by reference and decrement *its* refcount on release. This keeps
# holders of the old mmap unaffected by any new acquires for the
# same path. ``OrderedDict`` gives LRU semantics via move_to_end.
self._entries: OrderedDict[str, list] = OrderedDict()
self._max_size = (max_size if max_size is not None
else _mmap_cache_size_from_env())
@staticmethod
def _file_ident(path: str):
"""Return a (st_ino, st_size, st_mtime_ns) tuple for *path* or None."""
try:
st = _os_module.stat(path)
except OSError:
return None
return (st.st_ino, st.st_size, st.st_mtime_ns)
@staticmethod
def _close_entry_locked(entry):
"""Close the file handle and mmap for *entry* (must be idle)."""
if entry[1] is not None:
entry[1].close()
entry[0].close()
def acquire(self, path: str):
"""Get or create a read-only mmap for *path*.
Returns ``(mm, size, entry)``. The opaque ``entry`` token must be
passed back to :meth:`release` so the matching reference count is
decremented even after the cache slot has been replaced (e.g. by an
atomic file overwrite at the same path).
"""
real = _os_module.path.realpath(path)
with self._lock:
entry = self._entries.get(real)
ident = self._file_ident(real)
if entry is not None:
# If the file at this path has been replaced (different inode,
# size, or mtime) the cached mmap is stale. Drop the entry so
# we re-open below. If the old entry is still in use by other
# callers, leave their mmap valid -- they still hold a
# reference -- but mark it orphaned so a later release of
# *that* entry closes its own resources rather than touching
# the new cache slot.
if ident is not None and entry[4] != ident:
self._entries.pop(real)
entry[5] = True # orphaned
if entry[3] <= 0:
self._close_entry_locked(entry)
entry = None
if entry is not None:
entry[3] += 1
self._entries.move_to_end(real)
return entry[1], entry[2], entry
fh = open(real, 'rb')
fh.seek(0, 2)
size = fh.tell()
fh.seek(0)
if size > 0:
mm = mmap.mmap(fh.fileno(), 0, access=mmap.ACCESS_READ)
else:
mm = None
# Re-stat after opening so size matches the mmap we built.
ident = self._file_ident(real) or (0, size, 0)
new_entry = [fh, mm, size, 1, ident, False]
self._entries[real] = new_entry
self._evict_locked()
return mm, size, new_entry
def release(self, entry):
"""Decrement the reference count for the supplied entry token.
When the count hits zero on a still-cached entry, it stays cached
(keyed by realpath) until LRU eviction or :meth:`clear`. When the
count hits zero on an orphaned entry, its file handle and mmap are
closed immediately because no further callers can reach it.
"""
with self._lock:
entry[3] -= 1
if entry[3] > 0:
return
if entry[5]:
# Orphaned: not in the dict; close now.
self._close_entry_locked(entry)
return
# Find the path so we can move it to the LRU tail. The entry
# identity is unique per realpath while non-orphaned, so a
# linear search over a small dict is fine.
for key, ent in self._entries.items():
if ent is entry:
self._entries.move_to_end(key)
break
self._evict_locked()
def _evict_locked(self):
"""Drop oldest *idle* entries until the cache is at or below the cap."""
if len(self._entries) <= self._max_size:
return
# Walk from the front (oldest); only close idle (refcount 0) entries.
# An in-use entry can still happen to be at the front if the same
# file was acquired long ago and held; skip it.
to_drop = []
for key, entry in list(self._entries.items()):
if len(self._entries) - len(to_drop) <= self._max_size:
break
if entry[3] <= 0:
to_drop.append(key)
for key in to_drop:
entry = self._entries.pop(key)
self._close_entry_locked(entry)
def clear(self):
"""Close and drop all idle entries (used by tests)."""
with self._lock:
for key in [k for k, v in self._entries.items() if v[3] <= 0]:
entry = self._entries.pop(key)
self._close_entry_locked(entry)
# Module-level cache shared across all reads
_mmap_cache = _MmapCache()
class _FileSource:
"""Local file data source using a shared, thread-safe mmap cache."""
def __init__(self, path: str):
self._path = path
self._mm, self._size, self._entry = _mmap_cache.acquire(path)
def read_range(self, start: int, length: int) -> bytes:
if self._mm is not None:
return self._mm[start:start + length]
return b''
def read_all(self):
"""Return mmap object (supports slicing, struct.unpack_from, len)."""
if self._mm is not None:
return self._mm
return b''
@property
def size(self) -> int:
return self._size
def close(self):
if self._entry is not None:
_mmap_cache.release(self._entry)
self._entry = None
def _get_http_pool():
"""Return a module-level urllib3 PoolManager, or None if unavailable."""
global _http_pool
if _http_pool is not None:
return _http_pool
try:
import urllib3
_http_pool = urllib3.PoolManager(
num_pools=10,
maxsize=10,
retries=urllib3.Retry(
total=2,
backoff_factor=0.1,
# Redirects are *not* delegated to urllib3 -- they're
# followed manually in ``_HTTPSource._request`` so each
# ``Location`` runs through ``_validate_http_url`` before
# the next GET. Issue #1664.
redirect=False,
),
)
return _http_pool
except ImportError:
return None
_http_pool = None
# ---------------------------------------------------------------------------
# SSRF defenses for _HTTPSource (issue #1664)
# ---------------------------------------------------------------------------
#: Maximum number of redirects to follow when fetching a TIFF over HTTP.
_HTTP_MAX_REDIRECTS = 5
#: Default connect / read timeouts (seconds) for HTTP TIFF fetches.
_HTTP_CONNECT_TIMEOUT_DEFAULT = 10.0
_HTTP_READ_TIMEOUT_DEFAULT = 30.0
#: URL schemes that ``_HTTPSource`` accepts. The HTTP source is a Range
#: GET implementation backed by urllib3 / urllib, both of which only speak
#: ``http`` and ``https`` -- widening here would just push the failure to
#: connect time. fsspec handles every other ``scheme://`` and is routed
#: separately by :func:`_open_source`.
_HTTP_ALLOWED_SCHEMES = ('http', 'https')
def _http_allow_private_hosts() -> bool:
"""Return True if loopback / link-local / private IPs are allowed."""
raw = _os_module.environ.get('XRSPATIAL_GEOTIFF_ALLOW_PRIVATE_HOSTS')
if raw is None:
return False
return raw.strip().lower() in ('1', 'true', 'yes', 'on')
def _http_timeout_from_env(var_name: str, default: float) -> float:
"""Parse a positive-float timeout from the named env var, or fall back."""
raw = _os_module.environ.get(var_name)
if raw is None:
return default
try:
val = float(raw)
except (TypeError, ValueError):
return default
return val if val > 0 else default
def _http_connect_timeout() -> float:
return _http_timeout_from_env(
'XRSPATIAL_GEOTIFF_HTTP_CONNECT_TIMEOUT',
_HTTP_CONNECT_TIMEOUT_DEFAULT,
)
def _http_read_timeout() -> float:
return _http_timeout_from_env(
'XRSPATIAL_GEOTIFF_HTTP_READ_TIMEOUT',
_HTTP_READ_TIMEOUT_DEFAULT,
)
class UnsafeURLError(ValueError):
"""Raised when an HTTP URL fails the SSRF allow-list check.
Subclasses ``ValueError`` so existing callers that catch ``ValueError``
on bad input keep working. Carries the offending URL on ``.url`` for
structured logging.
"""
def __init__(self, msg: str, url: str | None = None):
super().__init__(msg)
self.url = url
def _ip_is_private(ip_str: str) -> bool:
"""Return True if *ip_str* is a loopback, link-local, or private IP.
Covers both IPv4 and IPv6. Multicast and unspecified addresses are
treated as unsafe (no legitimate reason to GET a TIFF from them, and
cloud metadata sometimes lives behind link-local IPv6).
"""
import ipaddress
try:
ip = ipaddress.ip_address(ip_str)
except ValueError:
# Not a literal IP -- caller must resolve it first.
return False
# ``is_private`` is True for RFC1918 (10/8, 172.16/12, 192.168/16),
# the IPv6 ULAs (fc00::/7), and -- in stdlib >= 3.4 -- also for
# loopback / link-local. Stay explicit so we don't depend on subtle
# behaviour across Python versions.
return (
ip.is_loopback
or ip.is_link_local
or ip.is_private
or ip.is_multicast
or ip.is_unspecified
or ip.is_reserved
)
def _validate_http_url(url: str) -> None:
"""Reject URLs that would let ``_HTTPSource`` reach unsafe destinations.
Enforces:
* scheme in ``_HTTP_ALLOWED_SCHEMES`` (http / https)
* hostname resolves to at least one non-loopback, non-link-local,
non-private IP (override via ``XRSPATIAL_GEOTIFF_ALLOW_PRIVATE_HOSTS``)
* hostname is non-empty
Raises :class:`UnsafeURLError` (a ``ValueError`` subclass) on any of
the above. Issue #1664.
"""
import socket
from urllib.parse import urlparse
if not isinstance(url, str) or not url:
raise UnsafeURLError(
"HTTP source requires a non-empty URL string", url=url)
parsed = urlparse(url)
scheme = (parsed.scheme or '').lower()
if scheme not in _HTTP_ALLOWED_SCHEMES:
raise UnsafeURLError(
f"URL scheme {scheme!r} is not in the allow-list "
f"{_HTTP_ALLOWED_SCHEMES}. Only HTTP(S) is supported; other "
f"schemes are dispatched via fsspec. URL: {url!r}",
url=url,
)
host = parsed.hostname
if not host:
raise UnsafeURLError(
f"URL {url!r} has no hostname", url=url)
if _http_allow_private_hosts():
return
# Resolve and reject if any resolved IP is in a private/loopback/link-
# local/multicast range. Rejecting on *any* match (rather than all)
# prevents DNS-rebind tricks that return both a public and a private
# IP for the same name. socket.getaddrinfo handles IPv4, IPv6, and
# literal IP strings uniformly.
try:
infos = socket.getaddrinfo(host, parsed.port, type=socket.SOCK_STREAM)
except socket.gaierror as e:
raise UnsafeURLError(
f"could not resolve host {host!r}: {e}", url=url) from e
for info in infos:
sockaddr = info[4]
# sockaddr is (ip, port) for AF_INET and (ip, port, flow, scope)
# for AF_INET6 -- the IP is always index 0.
ip_str = sockaddr[0]
# IPv6 scoped addresses come back as 'fe80::1%eth0' -- strip the
# zone id before passing to ipaddress.
if '%' in ip_str:
ip_str = ip_str.split('%', 1)[0]
if _ip_is_private(ip_str):
raise UnsafeURLError(
f"host {host!r} resolves to {ip_str!r}, which is in a "
f"loopback / link-local / private range. Set "
f"XRSPATIAL_GEOTIFF_ALLOW_PRIVATE_HOSTS=1 to allow.",
url=url,
)
# ---------------------------------------------------------------------------
# HTTP range coalescing
# ---------------------------------------------------------------------------
#: Default gap threshold (bytes) for merging adjacent COG tile ranges into a
#: single GET. COG tiles are stored sequentially, so most adjacent ranges
#: differ by zero (back-to-back) or a few bytes; 1 MB tolerates small holes
#: caused by interleaved overview/mask data without ballooning over-fetch.
#: Most tiles are well under 1 MB compressed, so the coalesced GET stays
#: O(num_tiles) bytes plus at most one threshold of slack between tiles.
COALESCE_GAP_THRESHOLD_DEFAULT = 1 << 20 # 1 MB
def coalesce_ranges(
ranges: list[tuple[int, int]],
gap_threshold: int = COALESCE_GAP_THRESHOLD_DEFAULT,
) -> tuple[list[tuple[int, int]], list[tuple[int, int, int]]]:
"""Merge nearby ``(offset, length)`` ranges into fewer larger ones.
Parameters
----------
ranges : list of (offset, length)
Per-tile byte ranges to fetch. Order is preserved in the
``mapping`` output so callers can reassemble per-tile bytes.
gap_threshold : int
Maximum gap, in bytes, between two adjacent ranges before they
are merged. A gap of zero means perfectly back-to-back; larger
gaps trade some over-fetch for fewer round-trips.
Returns
-------
merged : list of (start, length)
Coalesced ranges, sorted by ``start``. Issue one GET per entry.
mapping : list of (merged_idx, rel_offset, length)
For each input range (in input order), the index of the merged
range its bytes live in, the offset within that merged range,
and the original length. Use with :func:`split_coalesced_bytes`.
Notes
-----
Empty input returns ``([], [])``. Negative gap thresholds disable
merging entirely (every input becomes its own merged range).
"""
if not ranges:
return [], []
# Tag each input with its original index so we can rebuild mapping.
indexed = sorted(
((off, length, i) for i, (off, length) in enumerate(ranges)),
key=lambda t: t[0],
)
merged: list[tuple[int, int]] = []
# mapping[input_idx] -> (merged_idx, rel_offset, length)
mapping: list[tuple[int, int, int]] = [(0, 0, 0)] * len(ranges)
cur_start, cur_length, first_idx = indexed[0]
cur_end = cur_start + cur_length
members = [(first_idx, cur_start, cur_length)]
for off, length, orig_idx in indexed[1:]:
gap = off - cur_end
if gap_threshold >= 0 and gap <= gap_threshold:
# Extend current merged range. Gaps may be negative if a
# later-listed range overlaps an earlier one; clamp so the
# merged length covers both.
new_end = max(cur_end, off + length)
cur_length = new_end - cur_start
cur_end = new_end
members.append((orig_idx, off, length))
else:
merged_idx = len(merged)
merged.append((cur_start, cur_length))
for orig, m_off, m_len in members:
mapping[orig] = (merged_idx, m_off - cur_start, m_len)
cur_start, cur_length, cur_end = off, length, off + length
members = [(orig_idx, off, length)]
merged_idx = len(merged)
merged.append((cur_start, cur_length))
for orig, m_off, m_len in members:
mapping[orig] = (merged_idx, m_off - cur_start, m_len)
return merged, mapping
def split_coalesced_bytes(
merged_bytes: list[bytes],
mapping: list[tuple[int, int, int]],
) -> list[bytes]:
"""Slice merged-GET payloads back into per-tile bytes using *mapping*.
Inverse of :func:`coalesce_ranges`. ``merged_bytes[i]`` must be the
bytes returned by the GET for the ``i``th merged range; the output
is one bytes object per original input range, in input order.
"""
out: list[bytes] = [b''] * len(mapping)
for orig_idx, (merged_idx, rel_off, length) in enumerate(mapping):
chunk = merged_bytes[merged_idx]
out[orig_idx] = chunk[rel_off:rel_off + length]
return out
class _ValidatingRedirectHandler(urllib.request.HTTPRedirectHandler):
"""Stdlib redirect handler that re-validates each ``Location``.
The default ``HTTPRedirectHandler`` follows 3xx responses with no
awareness of the SSRF allow-list, so a public URL could 302 into a
loopback or private IP. This subclass calls :func:`_validate_http_url`
on every redirect target before building the follow-up request, and
caps the chain at :data:`_HTTP_MAX_REDIRECTS`. Issue #1664.
"""
max_redirections = _HTTP_MAX_REDIRECTS
def redirect_request(self, req, fp, code, msg, headers, newurl):
_validate_http_url(newurl)
return super().redirect_request(req, fp, code, msg, headers, newurl)
_stdlib_opener = None
def _get_stdlib_opener():
"""Return a stdlib opener with the validating redirect handler installed."""
global _stdlib_opener
if _stdlib_opener is None:
_stdlib_opener = urllib.request.build_opener(
_ValidatingRedirectHandler())
return _stdlib_opener
class _HTTPSource:
"""HTTP data source using range requests with connection reuse.
Uses urllib3.PoolManager when available (reuses TCP connections and
TLS sessions across range requests to the same host). Falls back to
stdlib urllib.request if urllib3 is not installed.
"""
def __init__(self, url: str):
# SSRF defense (issue #1664): validate scheme / host *before*
# any network call. UnsafeURLError subclasses ValueError so
# callers that already catch ValueError keep working. The check
# is best-effort -- DNS results can change between validate
# time and connect time, but rejecting at construction blocks
# the vast majority of static SSRF payloads.
_validate_http_url(url)
self._url = url
self._size = None
self._pool = _get_http_pool()
self._connect_timeout = _http_connect_timeout()
self._read_timeout = _http_read_timeout()
def _urllib3_timeout(self):
"""Build a urllib3 Timeout object lazily.
Imported here so that the module-level import of urllib3 stays
optional (we fall back to stdlib if urllib3 is missing).
"""
import urllib3
return urllib3.Timeout(
connect=self._connect_timeout, read=self._read_timeout)
def _request(self, headers: dict | None = None):
"""Issue a GET with manual, validated redirect following.
urllib3's built-in redirect follower has no validation hook, so
we set ``redirect=False`` and walk the chain ourselves. Each
``Location`` runs through :func:`_validate_http_url` before the
next GET, defeating a public-to-private 3xx bounce. Cap at
:data:`_HTTP_MAX_REDIRECTS` hops. Issue #1664.
"""
from urllib.parse import urljoin
pool = self._pool
timeout = self._urllib3_timeout()
current_url = self._url
for _ in range(_HTTP_MAX_REDIRECTS + 1):
resp = pool.request(
'GET', current_url,
headers=headers,
timeout=timeout,
redirect=False,
)
if 300 <= resp.status < 400 and resp.status != 304:
location = resp.headers.get('Location')
if not location:
return resp
# Resolve relative ``Location`` against the URL we just
# requested, not against ``self._url``: chained
# redirects can land us on a different origin.
next_url = urljoin(current_url, location)
_validate_http_url(next_url)
current_url = next_url
continue
return resp
raise UnsafeURLError(
f"More than {_HTTP_MAX_REDIRECTS} HTTP redirects "
f"starting from {self._url!r}",
url=self._url,
)
def read_range(self, start: int, length: int) -> bytes:
end = start + length - 1
headers = {'Range': f'bytes={start}-{end}'}
if self._pool is not None:
return self._request(headers=headers).data
# Fallback: stdlib. urlopen's ``timeout`` is a single value, so
# use the more conservative read timeout; the connect timeout
# isn't separately controllable on stdlib urllib. The opener
# carries ``_ValidatingRedirectHandler`` so 3xx hops are re-
# validated and capped at ``_HTTP_MAX_REDIRECTS``.
req = urllib.request.Request(self._url, headers=headers)
with _get_stdlib_opener().open(req, timeout=self._read_timeout) as resp:
return resp.read()
def read_ranges(
self,
ranges: list[tuple[int, int]],
max_workers: int = 8,
) -> list[bytes]:
"""Fetch multiple ranges concurrently using a thread pool.
Each ``(start, length)`` pair is fetched with its own range request,
but requests run in parallel so total wall time is bounded by the
slowest worker rather than ``len(ranges) * RTT``.
Returns the bytes for each range in input order.
"""
if not ranges:
return []
if len(ranges) == 1:
start, length = ranges[0]
return [self.read_range(start, length)]
workers = min(max_workers, len(ranges))
results: list[bytes | None] = [None] * len(ranges)
with ThreadPoolExecutor(max_workers=workers) as ex:
future_to_idx = {
ex.submit(self.read_range, start, length): i
for i, (start, length) in enumerate(ranges)
}
for fut in future_to_idx:
idx = future_to_idx[fut]
results[idx] = fut.result()
return results # type: ignore[return-value]
def read_ranges_coalesced(
self,
ranges: list[tuple[int, int]],
max_workers: int = 8,
gap_threshold: int = COALESCE_GAP_THRESHOLD_DEFAULT,
) -> list[bytes]:
"""Fetch *ranges* using merged GETs where adjacent ranges allow it.
Wrapper around :meth:`read_ranges` that first calls
:func:`coalesce_ranges` to group nearby ranges into fewer larger
GETs, then splits the responses back per-input via
:func:`split_coalesced_bytes`. Returns bytes in input order, same
as :meth:`read_ranges`.
Setting *gap_threshold* to a negative number disables merging
and falls back to one GET per input range.
"""
if not ranges:
return []
merged, mapping = coalesce_ranges(ranges, gap_threshold=gap_threshold)
merged_bytes = self.read_ranges(merged, max_workers=max_workers)
return split_coalesced_bytes(merged_bytes, mapping)
def read_all(self) -> bytes:
if self._pool is not None:
return self._request().data
req = urllib.request.Request(self._url)
with _get_stdlib_opener().open(req, timeout=self._read_timeout) as resp:
return resp.read()
@property
def size(self) -> int | None:
return self._size
def close(self):
pass
_CLOUD_SCHEMES = ('s3://', 'gs://', 'az://', 'abfs://')
def _is_fsspec_uri(path: str) -> bool:
"""Check if a path is a fsspec-compatible URI (not http/https/local)."""
if not isinstance(path, str):
return False
if path.startswith(('http://', 'https://')):
return False
return '://' in path
def _is_file_like(obj) -> bool:
"""Return True if obj exposes a binary file-like interface (read+seek+tell).
``tell`` is required because :class:`_BytesIOSource` uses it to compute
the buffer length via seek-to-end. ``os.PathLike`` instances don't
expose ``read``/``seek``/``tell`` and are excluded here so that
:func:`_coerce_path` can convert them to ``str`` upstream.
"""
return (
not isinstance(obj, str)
and hasattr(obj, 'read')
and hasattr(obj, 'seek')
and hasattr(obj, 'tell')
)
def _coerce_path(source):
"""Normalize ``os.PathLike`` (e.g. ``pathlib.Path``) to ``str``.
Strings and binary file-likes pass through unchanged. Used at the top
of every public reader/writer entry so that ``Path('mosaic.vrt')``
dispatches to the VRT path, ``Path('x.tif')`` derives a ``name``, etc.
"""
if isinstance(source, _os_module.PathLike):
return _os_module.fspath(source)
return source
class _BytesIOSource:
"""Data source backed by an in-memory or any seekable binary file-like.
Wraps a `BytesIO` or any object exposing ``read``/``seek`` so the reader
can issue windowed byte reads without touching the filesystem. Concurrent
callers (e.g. parallel tile decode) are serialized through a lock around
the seek+read pair so they don't race on the underlying buffer's cursor.
"""
def __init__(self, fileobj):
# _is_file_like (the gate that lets us reach this constructor)
# already requires read/seek/tell, so we can call tell() directly
# rather than guarding it. We do still defend against tell raising
# on a closed/detached buffer with an informative error.
self._fh = fileobj
self._lock = threading.Lock()
try:
cur = fileobj.tell()
fileobj.seek(0, 2)
self._size = fileobj.tell()
fileobj.seek(cur)
except (OSError, ValueError) as e:
raise ValueError(
f"file-like source is not usable for size measurement: "
f"{type(e).__name__}: {e}"
) from e
def read_range(self, start: int, length: int) -> bytes:
if length <= 0:
return b''
with self._lock:
self._fh.seek(start)
return self._fh.read(length)
def read_all(self):
with self._lock:
self._fh.seek(0)
return self._fh.read()
@property
def size(self) -> int:
return self._size
def close(self):
# Don't close the caller's buffer -- they own it.
self._fh = None
class _CloudSource:
"""Cloud storage data source using fsspec.
Supports S3, GCS, Azure Blob Storage, and any other fsspec backend.
Requires the appropriate library (s3fs, gcsfs, adlfs) to be installed.
"""
def __init__(self, url: str, **storage_options):
try:
import fsspec
except ImportError:
raise ImportError(
"fsspec is required to read from cloud storage. "
"Install it with: pip install fsspec")
self._url = url
self._fs, self._path = fsspec.core.url_to_fs(url, **storage_options)
self._size = self._fs.size(self._path)
def read_range(self, start: int, length: int) -> bytes:
with self._fs.open(self._path, 'rb') as f:
f.seek(start)
return f.read(length)
def read_all(self) -> bytes:
with self._fs.open(self._path, 'rb') as f:
return f.read()
@property
def size(self) -> int:
return self._size
def close(self):
pass
def _open_source(source):
"""Open a data source (local file, URL, cloud path, or file-like)."""
source = _coerce_path(source)
if _is_file_like(source):
return _BytesIOSource(source)
if not isinstance(source, str):
raise TypeError(
f"source must be a str path/URL or a binary file-like object "
f"with read+seek methods, got {type(source).__name__}")
if source.startswith(('http://', 'https://')):
return _HTTPSource(source)
if _is_fsspec_uri(source):
return _CloudSource(source)
return _FileSource(source)
def _apply_predictor(chunk: np.ndarray, pred: int, width: int,
height: int, bytes_per_sample: int,
samples: int = 1,
byte_order: str = '<') -> np.ndarray:
"""Apply the appropriate predictor decode to decompressed data.
``width``, ``height``, ``bytes_per_sample``, and ``samples`` describe
the raw pixel layout before predictor inversion: ``width * samples``
samples per row, each ``bytes_per_sample`` bytes wide.
Predictor=2 (horizontal differencing) operates at the *sample* level
per TIFF Technical Note (libtiff/GDAL convention): the difference is
taken between adjacent same-component samples in the sample's
natural bit width, with stride equal to ``samples`` samples. A
byte-wise implementation drops the inter-byte carry for multi-byte
samples and produces wrong values.
Predictor=3 (floating-point) byte-swizzles each row into
``bytes_per_sample`` interleaved lanes of length ``width * samples``,
per TIFF Technical Note 3. The un-transpose stage has to put the
MSB lane at the file's high-order byte position, which differs for
big- vs little-endian files; ``byte_order`` carries that.
"""
if pred == 2:
return predictor_decode(chunk, width, height,
bytes_per_sample, samples=samples,
byte_order=byte_order)
elif pred == 3:
return fp_predictor_decode(chunk, width * samples, height,
bytes_per_sample,
big_endian=(byte_order == '>'))
return chunk
def _packed_byte_count(pixel_count: int, bps: int) -> int:
"""Compute the number of packed bytes for sub-byte bit depths."""
return (pixel_count * bps + 7) // 8
def _int_nodata_in_range(nodata_int: int, dtype: np.dtype) -> bool:
"""Return True iff *nodata_int* is representable as *dtype*.
Used to gate ``dtype.type(int(...))`` casts that would otherwise raise
``OverflowError`` on real-world files that pair an unsigned dtype with
a negative GDAL_NODATA sentinel (e.g. uint16 + ``-9999``). When the
sentinel cannot be represented, the file's pixels can never match it,
so the caller should treat the sentinel as a no-op for value matching
(still surfacing it via ``attrs['nodata']`` so write round-trips
preserve the original tag).
"""
if dtype.kind not in ('u', 'i'):
return False
info = np.iinfo(dtype)
return info.min <= nodata_int <= info.max
def _resolve_masked_fill(nodata_str: str | None, dtype: np.dtype):
"""Resolve the value to use when restoring LERC-masked pixels.
Mirrors :func:`_sparse_fill_value` but defaults to NaN for floating
dtypes when the file does not declare a nodata sentinel. Float
rasters with no GDAL_NODATA tag still benefit from NaN propagation
because LERC's zero fill would silently masquerade as a real
measurement at z == 0.
Note: integer dtypes with no GDAL_NODATA tag fall back to ``0``,
which is the same value LERC zero-fills masked pixels with -- in
that case the mask application is intentionally a no-op. We avoid
inventing an integer sentinel (e.g. iinfo.max) because doing so
would silently change pixel values for files that never declared
one, breaking downstream consumers that key off the original data.
Out-of-range integer sentinels (e.g. ``uint16`` paired with
``GDAL_NODATA="-9999"``, common on legacy GDAL files) cannot be
represented in the file dtype and so cannot match any decoded
pixel; we fall back to ``0`` rather than raising ``OverflowError``
on the dtype cast.
"""
if nodata_str is not None:
try:
v = float(nodata_str)
if dtype.kind == 'f':
return dtype.type(v)
if not math.isnan(v) and not math.isinf(v):
nodata_int = int(v)
if _int_nodata_in_range(nodata_int, dtype):
return dtype.type(nodata_int)
except (TypeError, ValueError):
pass
if dtype.kind == 'f':
return dtype.type(np.nan)
return dtype.type(0)
def _decode_strip_or_tile(data_slice, compression, width, height, samples,