@@ -40,9 +40,12 @@ Some facts and figures:
40
40
Archives are extracted using a :ref: `filter <tarfile-extraction-filter >`,
41
41
which makes it possible to either limit surprising/dangerous features,
42
42
or to acknowledge that they are expected and the archive is fully trusted.
43
- By default, archives are fully trusted, but this default is deprecated
44
- and slated to change in Python 3.14.
45
43
44
+ .. versionchanged :: 3.14
45
+ Set the default extraction filter to :func: `data <data_filter> `,
46
+ which disallows some dangerous features such as links to absolute paths
47
+ or paths outside of the destination. Previously, the filter strategy
48
+ was equivalent to :func: `fully_trusted <fully_trusted_filter> `.
46
49
47
50
.. function :: open(name=None, mode='r', fileobj=None, bufsize=10240, **kwargs)
48
51
@@ -495,18 +498,18 @@ be finalized; only the internally used file object will be closed. See the
495
498
The *filter * argument specifies how ``members `` are modified or rejected
496
499
before extraction.
497
500
See :ref: `tarfile-extraction-filter ` for details.
498
- It is recommended to set this explicitly depending on which *tar * features
499
- you need to support.
501
+ It is recommended to set this explicitly only if specific *tar * features
502
+ are required, or as ``filter='data' `` to support Python versions with a less
503
+ secure default (3.13 and lower).
500
504
501
505
.. warning ::
502
506
503
507
Never extract archives from untrusted sources without prior inspection.
504
- It is possible that files are created outside of *path *, e.g. members
505
- that have absolute filenames starting with ``"/" `` or filenames with two
506
- dots ``".." ``.
507
508
508
- Set ``filter='data' `` to prevent the most dangerous security issues,
509
- and read the :ref: `tarfile-extraction-filter ` section for details.
509
+ Since Python 3.14, the default (:func: `data <data_filter> `) will prevent
510
+ the most dangerous security issues.
511
+ However, it will not prevent *all * unintended or insecure behavior.
512
+ Read the :ref: `tarfile-extraction-filter ` section for details.
510
513
511
514
.. versionchanged :: 3.5
512
515
Added the *numeric_owner * parameter.
@@ -517,6 +520,9 @@ be finalized; only the internally used file object will be closed. See the
517
520
.. versionchanged :: 3.12
518
521
Added the *filter * parameter.
519
522
523
+ .. versionchanged :: 3.14
524
+ The *filter * parameter now defaults to ``'data' ``.
525
+
520
526
521
527
.. method :: TarFile.extract(member, path="", set_attrs=True, *, numeric_owner=False, filter=None)
522
528
@@ -536,10 +542,8 @@ be finalized; only the internally used file object will be closed. See the
536
542
537
543
.. warning ::
538
544
539
- See the warning for :meth: `extractall `.
540
-
541
- Set ``filter='data' `` to prevent the most dangerous security issues,
542
- and read the :ref: `tarfile-extraction-filter ` section for details.
545
+ Never extract archives from untrusted sources without prior inspection.
546
+ See the warning for :meth: `extractall ` for details.
543
547
544
548
.. versionchanged :: 3.2
545
549
Added the *set_attrs * parameter.
@@ -602,14 +606,8 @@ be finalized; only the internally used file object will be closed. See the
602
606
String names are not allowed for this attribute, unlike the *filter *
603
607
argument to :meth: `~TarFile.extract `.
604
608
605
- If ``extraction_filter `` is ``None `` (the default),
606
- calling an extraction method without a *filter * argument will raise a
607
- ``DeprecationWarning ``,
608
- and fall back to the :func: `fully_trusted <fully_trusted_filter> ` filter,
609
- whose dangerous behavior matches previous versions of Python.
610
-
611
- In Python 3.14+, leaving ``extraction_filter=None `` will cause
612
- extraction methods to use the :func: `data <data_filter> ` filter by default.
609
+ If ``extraction_filter `` is ``None `` (the default), extraction methods
610
+ will use the :func: `data <data_filter> ` filter by default.
613
611
614
612
The attribute may be set on instances or overridden in subclasses.
615
613
It also is possible to set it on the ``TarFile `` class itself to set a
@@ -619,6 +617,14 @@ be finalized; only the internally used file object will be closed. See the
619
617
To set a global default this way, a filter function needs to be wrapped in
620
618
:func: `staticmethod() ` to prevent injection of a ``self `` argument.
621
619
620
+ .. versionchanged :: 3.14
621
+
622
+ The default filter is set to :func: `data <data_filter> `,
623
+ which disallows some dangerous features such as links to absolute paths
624
+ or paths outside of the destination.
625
+ Previously, the default was equivalent to
626
+ :func: `fully_trusted <fully_trusted_filter> `.
627
+
622
628
.. method :: TarFile.add(name, arcname=None, recursive=True, *, filter=None)
623
629
624
630
Add the file *name * to the archive. *name * may be any type of file
@@ -969,6 +975,12 @@ In most cases, the full functionality is not needed.
969
975
Therefore, *tarfile * supports extraction filters: a mechanism to limit
970
976
functionality, and thus mitigate some of the security issues.
971
977
978
+ .. warning ::
979
+
980
+ None of the available filters blocks *all * dangerous archive features.
981
+ Never extract archives from untrusted sources without prior inspection.
982
+ See also :ref: `tarfile-further-verification `.
983
+
972
984
.. seealso ::
973
985
974
986
:pep: `706 `
@@ -992,12 +1004,13 @@ can be:
992
1004
993
1005
* ``None `` (default): Use :attr: `TarFile.extraction_filter `.
994
1006
995
- If that is also ``None `` (the default), raise a `` DeprecationWarning ``,
996
- and fall back to the `` 'fully_trusted' `` filter, whose dangerous behavior
997
- matches previous versions of Python.
1007
+ If that is also ``None `` (the default), the `` 'data' `` filter will be used.
1008
+
1009
+ .. versionchanged :: 3.14
998
1010
999
- In Python 3.14, the ``'data' `` filter will become the default instead.
1000
- It's possible to switch earlier; see :attr: `TarFile.extraction_filter `.
1011
+ The default filter is set to :func: `data <data_filter> `.
1012
+ Previously, the default was equivalent to
1013
+ :func: `fully_trusted <fully_trusted_filter> `.
1001
1014
1002
1015
* A callable which will be called for each extracted member with a
1003
1016
:ref: `TarInfo <tarinfo-objects >` describing the member and the destination
@@ -1080,6 +1093,9 @@ reused in custom filters:
1080
1093
1081
1094
Return the modified ``TarInfo `` member.
1082
1095
1096
+ Note that this filter does not block *all * dangerous archive features.
1097
+ See :ref: `tarfile-further-verification ` for details.
1098
+
1083
1099
1084
1100
.. _tarfile-extraction-refuse :
1085
1101
@@ -1093,6 +1109,8 @@ With ``errorlevel=0`` the error will be logged and the member will be skipped,
1093
1109
but extraction will continue.
1094
1110
1095
1111
1112
+ .. _tarfile-further-verification :
1113
+
1096
1114
Hints for further verification
1097
1115
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1098
1116
@@ -1110,9 +1128,10 @@ Here is an incomplete list of things to consider:
1110
1128
disk, memory and CPU usage.
1111
1129
* Check filenames against an allow-list of characters
1112
1130
(to filter out control characters, confusables, foreign path separators,
1113
- etc. ).
1131
+ and so on ).
1114
1132
* Check that filenames have expected extensions (discouraging files that
1115
- execute when you “click on them”, or extension-less files like Windows special device names).
1133
+ execute when you “click on them”, or extension-less files like Windows
1134
+ special device names).
1116
1135
* Limit the number of extracted files, total size of extracted data,
1117
1136
filename length (including symlink length), and size of individual files.
1118
1137
* Check for files that would be shadowed on case-insensitive filesystems.
0 commit comments