-
Notifications
You must be signed in to change notification settings - Fork 985
/
Copy pathlegacy.py
1520 lines (1345 loc) · 55 KB
/
legacy.py
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
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import email
import hashlib
import hmac
import os.path
import re
import tarfile
import tempfile
import zipfile
from cgi import FieldStorage, parse_header
import packaging.requirements
import packaging.specifiers
import packaging.utils
import packaging.version
import pkg_resources
import requests
import wtforms
import wtforms.validators
from pyramid.httpexceptions import (
HTTPBadRequest,
HTTPException,
HTTPForbidden,
HTTPGone,
HTTPPermanentRedirect,
)
from pyramid.response import Response
from pyramid.view import view_config
from sqlalchemy import func, orm
from sqlalchemy.orm.exc import MultipleResultsFound, NoResultFound
from trove_classifiers import classifiers, deprecated_classifiers
from warehouse import forms
from warehouse.admin.flags import AdminFlagValue
from warehouse.classifiers.models import Classifier
from warehouse.email import send_basic_auth_with_two_factor_email
from warehouse.events.tags import EventTag
from warehouse.metrics import IMetricsService
from warehouse.packaging.interfaces import IFileStorage
from warehouse.packaging.models import (
Dependency,
DependencyKind,
Description,
File,
Filename,
JournalEntry,
Project,
Release,
Role,
)
from warehouse.packaging.tasks import update_bigquery_release_files
from warehouse.utils import http, readme
from warehouse.utils.project import add_project, validate_project_name
from warehouse.utils.security_policy import AuthenticationMethod
ONE_MB = 1 * 1024 * 1024
ONE_GB = 1 * 1024 * 1024 * 1024
MAX_FILESIZE = 100 * ONE_MB
MAX_SIGSIZE = 8 * 1024
MAX_PROJECT_SIZE = 10 * ONE_GB
PATH_HASHER = "blake2_256"
# Wheel platform checking
# Note: defining new platform ABI compatibility tags that don't
# have a python.org binary release to anchor them is a
# complex task that needs more than just OS+architecture info.
# For Linux specifically, the platform ABI is defined by each
# individual distro version, so wheels built on one version may
# not even work on older versions of the same distro, let alone
# a completely different distro.
#
# That means new entries should only be added given an
# accompanying ABI spec that explains how to build a
# compatible binary (see the manylinux specs as examples).
# These platforms can be handled by a simple static list:
_allowed_platforms = {
"any",
"win32",
"win_arm64",
"win_amd64",
"win_ia64",
"manylinux1_x86_64",
"manylinux1_i686",
"manylinux2010_x86_64",
"manylinux2010_i686",
"manylinux2014_x86_64",
"manylinux2014_i686",
"manylinux2014_aarch64",
"manylinux2014_armv7l",
"manylinux2014_ppc64",
"manylinux2014_ppc64le",
"manylinux2014_s390x",
"linux_armv6l",
"linux_armv7l",
}
# macosx is a little more complicated:
_macosx_platform_re = re.compile(r"macosx_(?P<major>\d+)_(\d+)_(?P<arch>.*)")
_macosx_arches = {
"ppc",
"ppc64",
"i386",
"x86_64",
"arm64",
"intel",
"fat",
"fat32",
"fat64",
"universal",
"universal2",
}
_macosx_major_versions = {
"10",
"11",
"12",
"13",
}
# manylinux pep600 and musllinux pep656 are a little more complicated:
_linux_platform_re = re.compile(r"(?P<libc>(many|musl))linux_(\d+)_(\d+)_(?P<arch>.*)")
_jointlinux_arches = {
"x86_64",
"i686",
"aarch64",
"armv7l",
"ppc64le",
"s390x",
}
_manylinux_arches = _jointlinux_arches | {"ppc64"}
_musllinux_arches = _jointlinux_arches
# Actual checking code;
def _valid_platform_tag(platform_tag):
if platform_tag in _allowed_platforms:
return True
m = _macosx_platform_re.match(platform_tag)
if (
m
and m.group("major") in _macosx_major_versions
and m.group("arch") in _macosx_arches
):
return True
m = _linux_platform_re.match(platform_tag)
if m and m.group("libc") == "musl":
return m.group("arch") in _musllinux_arches
if m and m.group("libc") == "many":
return m.group("arch") in _manylinux_arches
return False
_error_message_order = ["metadata_version", "name", "version"]
_dist_file_re = re.compile(r".+?\.(tar\.gz|zip|whl|egg)$", re.I)
_wheel_file_re = re.compile(
r"""
^
(?P<namever>(?P<name>.+?)(-(?P<ver>\d.+?))?)
(
(-(?P<build>\d.*?))?
-(?P<pyver>.+?)
-(?P<abi>.+?)
-(?P<plat>.+?)
(?:\.whl|\.dist-info)
)
$
""",
re.VERBOSE,
)
_project_name_re = re.compile(
r"^([A-Z0-9]|[A-Z0-9][A-Z0-9._-]*[A-Z0-9])$", re.IGNORECASE
)
_legacy_specifier_re = re.compile(r"^(?P<name>\S+)(?: \((?P<specifier>\S+)\))?$")
_valid_description_content_types = {"text/plain", "text/x-rst", "text/markdown"}
_valid_markdown_variants = {"CommonMark", "GFM"}
def _exc_with_message(exc, message, **kwargs):
# The crappy old API that PyPI offered uses the status to pass down
# messages to the client. So this function will make that easier to do.
resp = exc(detail=message, **kwargs)
# We need to guard against characters outside of iso-8859-1 per RFC.
# Specifically here, where user-supplied text may appear in the message,
# which our WSGI server may not appropriately handle (indeed gunicorn does not).
status_message = message.encode("iso-8859-1", "replace").decode("iso-8859-1")
resp.status = "{} {}".format(resp.status_code, status_message)
return resp
def _validate_pep440_version(form, field):
parsed = packaging.version.parse(field.data)
# Check that this version is a valid PEP 440 version at all.
if not isinstance(parsed, packaging.version.Version):
raise wtforms.validators.ValidationError(
"Start and end with a letter or numeral containing only "
"ASCII numeric and '.', '_' and '-'."
)
# Check that this version does not have a PEP 440 local segment attached
# to it.
if parsed.local is not None:
raise wtforms.validators.ValidationError("Can't use PEP 440 local versions.")
def _parse_legacy_requirement(requirement):
parsed = _legacy_specifier_re.search(requirement)
if parsed is None:
raise ValueError("Invalid requirement.")
return parsed.groupdict()["name"], parsed.groupdict()["specifier"]
def _validate_pep440_specifier(specifier):
try:
packaging.specifiers.SpecifierSet(specifier)
except packaging.specifiers.InvalidSpecifier:
raise wtforms.validators.ValidationError(
"Invalid specifier in requirement."
) from None
def _validate_pep440_specifier_field(form, field):
return _validate_pep440_specifier(field.data)
def _validate_legacy_non_dist_req(requirement):
try:
req = packaging.requirements.Requirement(requirement.replace("_", ""))
except packaging.requirements.InvalidRequirement:
raise wtforms.validators.ValidationError(
"Invalid requirement: {!r}".format(requirement)
) from None
if req.url is not None:
raise wtforms.validators.ValidationError(
"Can't direct dependency: {!r}".format(requirement)
)
if any(
not identifier.isalnum() or identifier[0].isdigit()
for identifier in req.name.split(".")
):
raise wtforms.validators.ValidationError("Use a valid Python identifier.")
def _validate_legacy_non_dist_req_list(form, field):
for datum in field.data:
_validate_legacy_non_dist_req(datum)
def _validate_legacy_dist_req(requirement):
try:
req = packaging.requirements.Requirement(requirement)
except packaging.requirements.InvalidRequirement:
raise wtforms.validators.ValidationError(
"Invalid requirement: {!r}.".format(requirement)
) from None
if req.url is not None:
raise wtforms.validators.ValidationError(
"Can't have direct dependency: {!r}".format(requirement)
)
def _validate_legacy_dist_req_list(form, field):
for datum in field.data:
_validate_legacy_dist_req(datum)
def _validate_requires_external(requirement):
name, specifier = _parse_legacy_requirement(requirement)
# TODO: Is it really reasonable to parse the specifier using PEP 440?
if specifier is not None:
_validate_pep440_specifier(specifier)
def _validate_requires_external_list(form, field):
for datum in field.data:
_validate_requires_external(datum)
def _validate_project_url(value):
try:
label, url = (x.strip() for x in value.split(",", maxsplit=1))
except ValueError:
raise wtforms.validators.ValidationError(
"Use both a label and an URL."
) from None
if not label:
raise wtforms.validators.ValidationError("Use a label.")
if len(label) > 32:
raise wtforms.validators.ValidationError("Use 32 characters or less.")
if not url:
raise wtforms.validators.ValidationError("Use an URL.")
if not http.is_valid_uri(url, require_authority=False):
raise wtforms.validators.ValidationError("Use valid URL.")
def _validate_project_url_list(form, field):
for datum in field.data:
_validate_project_url(datum)
def _validate_rfc822_email_field(form, field):
email_validator = wtforms.validators.Email(message="Use a valid email address")
addresses = email.utils.getaddresses([field.data])
for real_name, address in addresses:
email_validator(form, type("field", (), {"data": address}))
def _validate_description_content_type(form, field):
def _raise(message):
raise wtforms.validators.ValidationError(
f"Invalid description content type: {message}"
)
content_type, parameters = parse_header(field.data)
if content_type not in _valid_description_content_types:
_raise("type/subtype is not valid")
charset = parameters.get("charset")
if charset and charset != "UTF-8":
_raise("Use a valid charset")
variant = parameters.get("variant")
if (
content_type == "text/markdown"
and variant
and variant not in _valid_markdown_variants
):
_raise(
"Use a valid variant, expected one of {}".format(
", ".join(_valid_markdown_variants)
)
)
def _validate_no_deprecated_classifiers(form, field):
invalid_classifiers = set(field.data or []) & deprecated_classifiers.keys()
if invalid_classifiers:
first_invalid_classifier_name = sorted(invalid_classifiers)[0]
deprecated_by = deprecated_classifiers[first_invalid_classifier_name]
if deprecated_by:
raise wtforms.validators.ValidationError(
f"Classifier {first_invalid_classifier_name!r} has been "
"deprecated, use the following classifier(s) instead: "
f"{deprecated_by}"
)
else:
raise wtforms.validators.ValidationError(
f"Classifier {first_invalid_classifier_name!r} has been deprecated."
)
def _validate_classifiers(form, field):
invalid = sorted(set(field.data or []) - classifiers)
if invalid:
if len(invalid) == 1:
raise wtforms.validators.ValidationError(
f"Classifier {invalid[0]!r} is not a valid classifier."
)
else:
raise wtforms.validators.ValidationError(
f"Classifiers {invalid!r} are not valid classifiers."
)
def _construct_dependencies(form, types):
for name, kind in types.items():
for item in getattr(form, name).data:
yield Dependency(kind=kind.value, specifier=item)
class ListField(wtforms.Field):
def process_formdata(self, valuelist):
self.data = [v.strip() for v in valuelist if v.strip()]
# TODO: Eventually this whole validation thing should move to the packaging
# library and we should just call that. However until PEP 426 is done
# that library won't have an API for this.
class MetadataForm(forms.Form):
# Metadata version
metadata_version = wtforms.StringField(
description="Metadata-Version",
validators=[
wtforms.validators.DataRequired(),
wtforms.validators.AnyOf(
# Note: This isn't really Metadata 2.0, however bdist_wheel
# claims it is producing a Metadata 2.0 metadata when in
# reality it's more like 1.2 with some extensions.
["1.0", "1.1", "1.2", "2.0", "2.1"],
message="Use a known metadata version.",
),
],
)
# Identity Project and Release
name = wtforms.StringField(
description="Name",
validators=[
wtforms.validators.DataRequired(),
wtforms.validators.Regexp(
_project_name_re,
re.IGNORECASE,
message=(
"Start and end with a letter or numeral containing "
"only ASCII numeric and '.', '_' and '-'."
),
),
],
)
version = wtforms.StringField(
description="Version",
validators=[
wtforms.validators.DataRequired(),
wtforms.validators.Regexp(
r"^(?!\s).*(?<!\s)$",
message="Can't have leading or trailing whitespace.",
),
_validate_pep440_version,
],
)
# Additional Release metadata
summary = wtforms.StringField(
description="Summary",
validators=[
wtforms.validators.Optional(),
wtforms.validators.Length(max=512),
wtforms.validators.Regexp(
r"^.+$", # Rely on the fact that . doesn't match a newline.
message="Use a single line only.",
),
],
)
description = wtforms.StringField(
description="Description", validators=[wtforms.validators.Optional()]
)
author = wtforms.StringField(
description="Author", validators=[wtforms.validators.Optional()]
)
description_content_type = wtforms.StringField(
description="Description-Content-Type",
validators=[wtforms.validators.Optional(), _validate_description_content_type],
)
author_email = wtforms.StringField(
description="Author-email",
validators=[wtforms.validators.Optional(), _validate_rfc822_email_field],
)
maintainer = wtforms.StringField(
description="Maintainer", validators=[wtforms.validators.Optional()]
)
maintainer_email = wtforms.StringField(
description="Maintainer-email",
validators=[wtforms.validators.Optional(), _validate_rfc822_email_field],
)
license = wtforms.StringField(
description="License", validators=[wtforms.validators.Optional()]
)
keywords = wtforms.StringField(
description="Keywords", validators=[wtforms.validators.Optional()]
)
classifiers = ListField(
description="Classifier",
validators=[_validate_no_deprecated_classifiers, _validate_classifiers],
)
platform = wtforms.StringField(
description="Platform", validators=[wtforms.validators.Optional()]
)
# URLs
home_page = wtforms.StringField(
description="Home-Page",
validators=[wtforms.validators.Optional(), forms.URIValidator()],
)
download_url = wtforms.StringField(
description="Download-URL",
validators=[wtforms.validators.Optional(), forms.URIValidator()],
)
# Dependency Information
requires_python = wtforms.StringField(
description="Requires-Python",
validators=[wtforms.validators.Optional(), _validate_pep440_specifier_field],
)
# File information
pyversion = wtforms.StringField(validators=[wtforms.validators.Optional()])
filetype = wtforms.StringField(
validators=[
wtforms.validators.DataRequired(),
wtforms.validators.AnyOf(
["bdist_egg", "bdist_wheel", "sdist"], message="Use a known file type."
),
]
)
comment = wtforms.StringField(validators=[wtforms.validators.Optional()])
md5_digest = wtforms.StringField(validators=[wtforms.validators.Optional()])
sha256_digest = wtforms.StringField(
validators=[
wtforms.validators.Optional(),
wtforms.validators.Regexp(
r"^[A-F0-9]{64}$",
re.IGNORECASE,
message="Use a valid, hex-encoded, SHA256 message digest.",
),
]
)
blake2_256_digest = wtforms.StringField(
validators=[
wtforms.validators.Optional(),
wtforms.validators.Regexp(
r"^[A-F0-9]{64}$",
re.IGNORECASE,
message="Use a valid, hex-encoded, BLAKE2 message digest.",
),
]
)
# Legacy dependency information
requires = ListField(
validators=[wtforms.validators.Optional(), _validate_legacy_non_dist_req_list]
)
provides = ListField(
validators=[wtforms.validators.Optional(), _validate_legacy_non_dist_req_list]
)
obsoletes = ListField(
validators=[wtforms.validators.Optional(), _validate_legacy_non_dist_req_list]
)
# Newer dependency information
requires_dist = ListField(
description="Requires-Dist",
validators=[wtforms.validators.Optional(), _validate_legacy_dist_req_list],
)
provides_dist = ListField(
description="Provides-Dist",
validators=[wtforms.validators.Optional(), _validate_legacy_dist_req_list],
)
obsoletes_dist = ListField(
description="Obsoletes-Dist",
validators=[wtforms.validators.Optional(), _validate_legacy_dist_req_list],
)
requires_external = ListField(
description="Requires-External",
validators=[wtforms.validators.Optional(), _validate_requires_external_list],
)
# Newer metadata information
project_urls = ListField(
description="Project-URL",
validators=[wtforms.validators.Optional(), _validate_project_url_list],
)
def full_validate(self):
# All non source releases *must* have a pyversion
if (
self.filetype.data
and self.filetype.data != "sdist"
and not self.pyversion.data
):
raise wtforms.validators.ValidationError(
"Python version is required for binary distribution uploads."
)
# All source releases *must* have a pyversion of "source"
if self.filetype.data == "sdist":
if not self.pyversion.data:
self.pyversion.data = "source"
elif self.pyversion.data != "source":
raise wtforms.validators.ValidationError(
"Use 'source' as Python version for an sdist."
)
# We *must* have at least one digest to verify against.
if not self.md5_digest.data and not self.sha256_digest.data:
raise wtforms.validators.ValidationError(
"Include at least one message digest."
)
_safe_zipnames = re.compile(r"(purelib|platlib|headers|scripts|data).+", re.I)
# .tar uncompressed, .tar.gz .tgz, .tar.bz2 .tbz2
_tar_filenames_re = re.compile(r"\.(?:tar$|t(?:ar\.)?(?P<z_type>gz|bz2)$)")
def _is_valid_dist_file(filename, filetype):
"""
Perform some basic checks to see whether the indicated file could be
a valid distribution file.
"""
# If our file is a zipfile, then ensure that it's members are only
# compressed with supported compression methods.
if zipfile.is_zipfile(filename):
with zipfile.ZipFile(filename) as zfp:
for zinfo in zfp.infolist():
if zinfo.compress_type not in {
zipfile.ZIP_STORED,
zipfile.ZIP_DEFLATED,
}:
return False
tar_fn_match = _tar_filenames_re.search(filename)
if tar_fn_match:
# Ensure that this is a valid tar file, and that it contains PKG-INFO.
z_type = tar_fn_match.group("z_type") or ""
try:
with tarfile.open(filename, f"r:{z_type}") as tar:
# This decompresses the entire stream to validate it and the
# tar within. Easy CPU DoS attack. :/
bad_tar = True
member = tar.next()
while member:
parts = os.path.split(member.name)
if len(parts) == 2 and parts[1] == "PKG-INFO":
bad_tar = False
member = tar.next()
if bad_tar:
return False
except (tarfile.ReadError, EOFError):
return False
elif filename.endswith(".exe"):
# The only valid filetype for a .exe file is "bdist_wininst".
if filetype != "bdist_wininst":
return False
# Ensure that the .exe is a valid zip file, and that all of the files
# contained within it have safe filenames.
try:
with zipfile.ZipFile(filename, "r") as zfp:
# We need the no branch below to work around a bug in
# coverage.py where it's detecting a missed branch where there
# isn't one.
for zipname in zfp.namelist(): # pragma: no branch
if not _safe_zipnames.match(zipname):
return False
except zipfile.BadZipFile:
return False
elif filename.endswith(".msi"):
# The only valid filetype for a .msi is "bdist_msi"
if filetype != "bdist_msi":
return False
# Check the first 8 bytes of the MSI file. This was taken from the
# legacy implementation of PyPI which itself took it from the
# implementation of `file` I believe.
with open(filename, "rb") as fp:
if fp.read(8) != b"\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1":
return False
elif filename.endswith(".zip") or filename.endswith(".egg"):
# Ensure that the .zip/.egg is a valid zip file, and that it has a
# PKG-INFO file.
try:
with zipfile.ZipFile(filename, "r") as zfp:
for zipname in zfp.namelist():
parts = os.path.split(zipname)
if len(parts) == 2 and parts[1] == "PKG-INFO":
# We need the no branch below to work around a bug in
# coverage.py where it's detecting a missed branch
# where there isn't one.
break # pragma: no branch
else:
return False
except zipfile.BadZipFile:
return False
elif filename.endswith(".whl"):
# Ensure that the .whl is a valid zip file, and that it has a WHEEL
# file.
try:
with zipfile.ZipFile(filename, "r") as zfp:
for zipname in zfp.namelist():
parts = os.path.split(zipname)
if len(parts) == 2 and parts[1] == "WHEEL":
# We need the no branch below to work around a bug in
# coverage.py where it's detecting a missed branch
# where there isn't one.
break # pragma: no branch
else:
return False
except zipfile.BadZipFile:
return False
# If we haven't yet decided it's not valid, then we'll assume it is and
# allow it.
return True
def _is_duplicate_file(db_session, filename, hashes):
"""
Check to see if file already exists, and if it's content matches.
A file is considered to exist if its filename *or* blake2 digest are
present in a file row in the database.
Returns:
- True: This file is a duplicate and all further processing should halt.
- False: This file exists, but it is not a duplicate.
- None: This file does not exist.
"""
file_ = (
db_session.query(File)
.filter(
(File.filename == filename)
| (File.blake2_256_digest == hashes["blake2_256"])
)
.first()
)
if file_ is not None:
return (
file_.filename == filename
and file_.sha256_digest == hashes["sha256"]
and file_.md5_digest == hashes["md5"]
and file_.blake2_256_digest == hashes["blake2_256"]
)
return None
@view_config(
route_name="forklift.legacy.file_upload",
uses_session=True,
require_csrf=False,
require_methods=["POST"],
has_translations=True,
)
def file_upload(request):
# If we're in read-only mode, let upload clients know
if request.flags.enabled(AdminFlagValue.READ_ONLY):
raise _exc_with_message(
HTTPForbidden, "Read-only mode: Uploads are temporarily disabled."
)
if request.flags.enabled(AdminFlagValue.DISALLOW_NEW_UPLOAD):
raise _exc_with_message(
HTTPForbidden,
"New uploads are temporarily disabled. "
"See {projecthelp} for more information.".format(
projecthelp=request.help_url(_anchor="admin-intervention")
),
)
# Log an attempt to upload
metrics = request.find_service(IMetricsService, context=None)
metrics.increment("warehouse.upload.attempt")
# Before we do anything, if there isn't an authenticated identity with
# this request, then we'll go ahead and bomb out.
if request.identity is None:
raise _exc_with_message(
HTTPForbidden,
"Invalid or non-existent authentication information. "
"See {projecthelp} for more information.".format(
projecthelp=request.help_url(_anchor="invalid-auth")
),
)
# These checks only make sense when our authenticated identity is a user,
# not a project identity (like OIDC-minted tokens.)
if request.user:
# Ensure that user has a verified, primary email address. This should both
# reduce the ease of spam account creation and activity, as well as act as
# a forcing function for https://github.com/pypa/warehouse/issues/3632.
# TODO: Once https://github.com/pypa/warehouse/issues/3632 has been solved,
# we might consider a different condition, possibly looking at
# User.is_active instead.
if not (request.user.primary_email and request.user.primary_email.verified):
raise _exc_with_message(
HTTPBadRequest,
(
"User {!r} does not have a verified primary email address. "
"Please add a verified primary email before attempting to "
"upload to PyPI. See {project_help} for more information."
).format(
request.user.username,
project_help=request.help_url(_anchor="verified-email"),
),
) from None
# Do some cleanup of the various form fields
for key in list(request.POST):
value = request.POST.get(key)
if isinstance(value, str):
# distutils "helpfully" substitutes unknown, but "required" values
# with the string "UNKNOWN". This is basically never what anyone
# actually wants so we'll just go ahead and delete anything whose
# value is UNKNOWN.
if value.strip() == "UNKNOWN":
del request.POST[key]
# Escape NUL characters, which psycopg doesn't like
if "\x00" in value:
request.POST[key] = value.replace("\x00", "\\x00")
# We require protocol_version 1, it's the only supported version however
# passing a different version should raise an error.
if request.POST.get("protocol_version", "1") != "1":
raise _exc_with_message(HTTPBadRequest, "Unknown protocol version.")
# Check if any fields were supplied as a tuple and have become a
# FieldStorage. The 'content' and 'gpg_signature' fields _should_ be a
# FieldStorage, however.
# ref: https://github.com/pypi/warehouse/issues/2185
# ref: https://github.com/pypi/warehouse/issues/2491
for field in set(request.POST) - {"content", "gpg_signature"}:
values = request.POST.getall(field)
if any(isinstance(value, FieldStorage) for value in values):
raise _exc_with_message(HTTPBadRequest, f"{field}: Should not be a tuple.")
# Validate and process the incoming metadata.
form = MetadataForm(request.POST)
if not form.validate():
for field_name in _error_message_order:
if field_name in form.errors:
break
else:
field_name = sorted(form.errors.keys())[0]
if field_name in form:
field = form[field_name]
if field.description and isinstance(field, wtforms.StringField):
error_message = (
"{value!r} is an invalid value for {field}. ".format(
value=(
field.data[:30] + "..." + field.data[-30:]
if field.data and len(field.data) > 60
else field.data or ""
),
field=field.description,
)
+ "Error: {} ".format(form.errors[field_name][0])
+ "See "
"https://packaging.python.org/specifications/core-metadata"
+ " for more information."
)
else:
error_message = "Invalid value for {field}. Error: {msgs[0]}".format(
field=field_name, msgs=form.errors[field_name]
)
else:
error_message = "Error: {}".format(form.errors[field_name][0])
raise _exc_with_message(HTTPBadRequest, error_message)
# Ensure that we have file data in the request.
if "content" not in request.POST:
raise _exc_with_message(HTTPBadRequest, "Upload payload does not have a file.")
# Look up the project first before doing anything else, this is so we can
# automatically register it if we need to and can check permissions before
# going any further.
project = (
request.db.query(Project)
.filter(Project.normalized_name == func.normalize_pep426_name(form.name.data))
.first()
)
if project is None:
# Another sanity check: we should be preventing non-user identities
# from creating projects in the first place with scoped tokens,
# but double-check anyways.
if not request.user:
raise _exc_with_message(
HTTPBadRequest,
(
"Non-user identities cannot create new projects. "
"You must first create a project as a user, and then "
"configure the project to use OpenID Connect."
),
)
# We attempt to create the project.
try:
validate_project_name(form.name.data, request)
except HTTPException as exc:
raise _exc_with_message(exc.__class__, exc.detail) from None
project = add_project(form.name.data, request)
# Then we'll add a role setting the current user as the "Owner" of the
# project.
request.db.add(Role(user=request.user, project=project, role_name="Owner"))
# TODO: This should be handled by some sort of database trigger or a
# SQLAlchemy hook or the like instead of doing it inline in this
# view.
request.db.add(
JournalEntry(
name=project.name,
action="add Owner {}".format(request.user.username),
submitted_by=request.user,
submitted_from=request.remote_addr,
)
)
project.record_event(
tag=EventTag.Project.RoleAdd,
ip_address=request.remote_addr,
additional={
"submitted_by": request.user.username,
"role_name": "Owner",
"target_user": request.user.username,
},
)
# Check that the identity has permission to do things to this project, if this
# is a new project this will act as a sanity check for the role we just
# added above.
allowed = request.has_permission("upload", project)
if not allowed:
reason = getattr(allowed, "reason", None)
if request.user:
msg = (
(
"The user '{0}' isn't allowed to upload to project '{1}'. "
"See {2} for more information."
).format(
request.user.username,
project.name,
request.help_url(_anchor="project-name"),
)
if reason is None
else allowed.msg
)
else:
msg = (
(
"The given token isn't allowed to upload to project '{0}'. "
"See {1} for more information."
).format(
project.name,
request.help_url(_anchor="project-name"),
)
if reason is None
else allowed.msg
)
raise _exc_with_message(HTTPForbidden, msg)
# Check if the user has 2FA and used basic auth
# NOTE: We don't need to guard request.user here because basic auth
# can only be used with user identities.
if (
request.authentication_method == AuthenticationMethod.BASIC_AUTH
and request.user.has_two_factor
):
# Eventually, raise here to disable basic auth with 2FA enabled
send_basic_auth_with_two_factor_email(
request, request.user, project_name=project.name
)
# Update name if it differs but is still equivalent. We don't need to check if
# they are equivalent when normalized because that's already been done when we
# queried for the project.
if project.name != form.name.data:
project.name = form.name.data
# Render our description so we can save from having to render this data every time
# we load a project description page.
rendered = None
if form.description.data:
description_content_type = form.description_content_type.data
if not description_content_type:
description_content_type = "text/x-rst"