|
23 | 23 | import pretend
|
24 | 24 | import pytest
|
25 | 25 |
|
| 26 | +from pypi_attestation_models import ( |
| 27 | + Attestation, |
| 28 | + InvalidAttestationError, |
| 29 | + VerificationError, |
| 30 | + VerificationMaterial, |
| 31 | +) |
26 | 32 | from pyramid.httpexceptions import HTTPBadRequest, HTTPForbidden, HTTPTooManyRequests
|
| 33 | +from sigstore.verify import Verifier |
27 | 34 | from sqlalchemy import and_, exists
|
28 | 35 | from sqlalchemy.exc import IntegrityError
|
29 | 36 | from sqlalchemy.orm import joinedload
|
@@ -2385,6 +2392,82 @@ def test_upload_fails_without_oidc_publisher_permission(
|
2385 | 2392 | "See /the/help/url/ for more information."
|
2386 | 2393 | ).format(project.name)
|
2387 | 2394 |
|
| 2395 | + def test_upload_attestation_fails_without_oidc_publisher( |
| 2396 | + self, |
| 2397 | + monkeypatch, |
| 2398 | + pyramid_config, |
| 2399 | + db_request, |
| 2400 | + metrics, |
| 2401 | + project_service, |
| 2402 | + macaroon_service, |
| 2403 | + ): |
| 2404 | + project = ProjectFactory.create() |
| 2405 | + owner = UserFactory.create() |
| 2406 | + maintainer = UserFactory.create() |
| 2407 | + RoleFactory.create(user=owner, project=project, role_name="Owner") |
| 2408 | + RoleFactory.create(user=maintainer, project=project, role_name="Maintainer") |
| 2409 | + |
| 2410 | + EmailFactory.create(user=maintainer) |
| 2411 | + db_request.user = maintainer |
| 2412 | + raw_macaroon, macaroon = macaroon_service.create_macaroon( |
| 2413 | + "fake location", |
| 2414 | + "fake description", |
| 2415 | + [caveats.RequestUser(user_id=str(maintainer.id))], |
| 2416 | + user_id=maintainer.id, |
| 2417 | + ) |
| 2418 | + identity = UserTokenContext(maintainer, macaroon) |
| 2419 | + |
| 2420 | + filename = "{}-{}.tar.gz".format(project.name, "1.0") |
| 2421 | + attestation = Attestation( |
| 2422 | + version=1, |
| 2423 | + verification_material=VerificationMaterial( |
| 2424 | + certificate="some_cert", transparency_entries=[dict()] |
| 2425 | + ), |
| 2426 | + message_signature="some_signature", |
| 2427 | + ) |
| 2428 | + |
| 2429 | + pyramid_config.testing_securitypolicy(identity=identity) |
| 2430 | + db_request.POST = MultiDict( |
| 2431 | + { |
| 2432 | + "metadata_version": "1.2", |
| 2433 | + "name": project.name, |
| 2434 | + "attestations": f"[{attestation.model_dump_json()}]", |
| 2435 | + "version": "1.0", |
| 2436 | + "filetype": "sdist", |
| 2437 | + "md5_digest": _TAR_GZ_PKG_MD5, |
| 2438 | + "content": pretend.stub( |
| 2439 | + filename=filename, |
| 2440 | + file=io.BytesIO(_TAR_GZ_PKG_TESTDATA), |
| 2441 | + type="application/tar", |
| 2442 | + ), |
| 2443 | + } |
| 2444 | + ) |
| 2445 | + |
| 2446 | + storage_service = pretend.stub(store=lambda path, filepath, meta: None) |
| 2447 | + extract_http_macaroon = pretend.call_recorder(lambda r, _: raw_macaroon) |
| 2448 | + monkeypatch.setattr( |
| 2449 | + security_policy, "_extract_http_macaroon", extract_http_macaroon |
| 2450 | + ) |
| 2451 | + |
| 2452 | + db_request.find_service = lambda svc, name=None, context=None: { |
| 2453 | + IFileStorage: storage_service, |
| 2454 | + IMacaroonService: macaroon_service, |
| 2455 | + IMetricsService: metrics, |
| 2456 | + IProjectService: project_service, |
| 2457 | + }.get(svc) |
| 2458 | + db_request.user_agent = "warehouse-tests/6.6.6" |
| 2459 | + |
| 2460 | + with pytest.raises(HTTPBadRequest) as excinfo: |
| 2461 | + legacy.file_upload(db_request) |
| 2462 | + |
| 2463 | + resp = excinfo.value |
| 2464 | + |
| 2465 | + assert resp.status_code == 400 |
| 2466 | + assert resp.status == ( |
| 2467 | + "400 Attestations are currently only supported when using Trusted " |
| 2468 | + "Publishing with GitHub Actions." |
| 2469 | + ) |
| 2470 | + |
2388 | 2471 | @pytest.mark.parametrize(
|
2389 | 2472 | "plat",
|
2390 | 2473 | [
|
@@ -3293,6 +3376,233 @@ def test_upload_succeeds_creates_release(
|
3293 | 3376 | ),
|
3294 | 3377 | ]
|
3295 | 3378 |
|
| 3379 | + def test_upload_with_valid_attestation_succeeds( |
| 3380 | + self, |
| 3381 | + monkeypatch, |
| 3382 | + pyramid_config, |
| 3383 | + db_request, |
| 3384 | + metrics, |
| 3385 | + ): |
| 3386 | + from warehouse.events.models import HasEvents |
| 3387 | + |
| 3388 | + project = ProjectFactory.create() |
| 3389 | + version = "1.0" |
| 3390 | + publisher = GitHubPublisherFactory.create(projects=[project]) |
| 3391 | + claims = { |
| 3392 | + "sha": "somesha", |
| 3393 | + "repository": f"{publisher.repository_owner}/{publisher.repository_name}", |
| 3394 | + "workflow": "workflow_name", |
| 3395 | + } |
| 3396 | + identity = PublisherTokenContext(publisher, SignedClaims(claims)) |
| 3397 | + db_request.oidc_publisher = identity.publisher |
| 3398 | + db_request.oidc_claims = identity.claims |
| 3399 | + |
| 3400 | + db_request.db.add(Classifier(classifier="Environment :: Other Environment")) |
| 3401 | + db_request.db.add(Classifier(classifier="Programming Language :: Python")) |
| 3402 | + |
| 3403 | + filename = "{}-{}.tar.gz".format(project.name, "1.0") |
| 3404 | + attestation = Attestation( |
| 3405 | + version=1, |
| 3406 | + verification_material=VerificationMaterial( |
| 3407 | + certificate="somebase64string", transparency_entries=[dict()] |
| 3408 | + ), |
| 3409 | + message_signature="somebase64string", |
| 3410 | + ) |
| 3411 | + |
| 3412 | + pyramid_config.testing_securitypolicy(identity=identity) |
| 3413 | + db_request.user = None |
| 3414 | + db_request.user_agent = "warehouse-tests/6.6.6" |
| 3415 | + db_request.POST = MultiDict( |
| 3416 | + { |
| 3417 | + "metadata_version": "1.2", |
| 3418 | + "name": project.name, |
| 3419 | + "attestations": f"[{attestation.model_dump_json()}]", |
| 3420 | + "version": version, |
| 3421 | + "summary": "This is my summary!", |
| 3422 | + "filetype": "sdist", |
| 3423 | + "md5_digest": _TAR_GZ_PKG_MD5, |
| 3424 | + "content": pretend.stub( |
| 3425 | + filename=filename, |
| 3426 | + file=io.BytesIO(_TAR_GZ_PKG_TESTDATA), |
| 3427 | + type="application/tar", |
| 3428 | + ), |
| 3429 | + } |
| 3430 | + ) |
| 3431 | + |
| 3432 | + storage_service = pretend.stub(store=lambda path, filepath, meta: None) |
| 3433 | + db_request.find_service = lambda svc, name=None, context=None: { |
| 3434 | + IFileStorage: storage_service, |
| 3435 | + IMetricsService: metrics, |
| 3436 | + }.get(svc) |
| 3437 | + |
| 3438 | + record_event = pretend.call_recorder( |
| 3439 | + lambda self, *, tag, request=None, additional: None |
| 3440 | + ) |
| 3441 | + monkeypatch.setattr(HasEvents, "record_event", record_event) |
| 3442 | + |
| 3443 | + verify = pretend.call_recorder(lambda _self, _verifier, _policy, _dist: None) |
| 3444 | + monkeypatch.setattr(Attestation, "verify", verify) |
| 3445 | + monkeypatch.setattr(Verifier, "production", lambda: pretend.stub()) |
| 3446 | + |
| 3447 | + resp = legacy.file_upload(db_request) |
| 3448 | + |
| 3449 | + assert resp.status_code == 200 |
| 3450 | + |
| 3451 | + assert len(verify.calls) == 1 |
| 3452 | + |
| 3453 | + def test_upload_with_malformed_attestation_fails( |
| 3454 | + self, |
| 3455 | + monkeypatch, |
| 3456 | + pyramid_config, |
| 3457 | + db_request, |
| 3458 | + metrics, |
| 3459 | + ): |
| 3460 | + from warehouse.events.models import HasEvents |
| 3461 | + |
| 3462 | + project = ProjectFactory.create() |
| 3463 | + version = "1.0" |
| 3464 | + publisher = GitHubPublisherFactory.create(projects=[project]) |
| 3465 | + claims = { |
| 3466 | + "sha": "somesha", |
| 3467 | + "repository": f"{publisher.repository_owner}/{publisher.repository_name}", |
| 3468 | + "workflow": "workflow_name", |
| 3469 | + } |
| 3470 | + identity = PublisherTokenContext(publisher, SignedClaims(claims)) |
| 3471 | + db_request.oidc_publisher = identity.publisher |
| 3472 | + db_request.oidc_claims = identity.claims |
| 3473 | + |
| 3474 | + db_request.db.add(Classifier(classifier="Environment :: Other Environment")) |
| 3475 | + db_request.db.add(Classifier(classifier="Programming Language :: Python")) |
| 3476 | + |
| 3477 | + filename = "{}-{}.tar.gz".format(project.name, "1.0") |
| 3478 | + |
| 3479 | + pyramid_config.testing_securitypolicy(identity=identity) |
| 3480 | + db_request.user = None |
| 3481 | + db_request.user_agent = "warehouse-tests/6.6.6" |
| 3482 | + db_request.POST = MultiDict( |
| 3483 | + { |
| 3484 | + "metadata_version": "1.2", |
| 3485 | + "name": project.name, |
| 3486 | + "attestations": "[{'a_malformed_attestation': 3}]", |
| 3487 | + "version": version, |
| 3488 | + "summary": "This is my summary!", |
| 3489 | + "filetype": "sdist", |
| 3490 | + "md5_digest": _TAR_GZ_PKG_MD5, |
| 3491 | + "content": pretend.stub( |
| 3492 | + filename=filename, |
| 3493 | + file=io.BytesIO(_TAR_GZ_PKG_TESTDATA), |
| 3494 | + type="application/tar", |
| 3495 | + ), |
| 3496 | + } |
| 3497 | + ) |
| 3498 | + |
| 3499 | + storage_service = pretend.stub(store=lambda path, filepath, meta: None) |
| 3500 | + db_request.find_service = lambda svc, name=None, context=None: { |
| 3501 | + IFileStorage: storage_service, |
| 3502 | + IMetricsService: metrics, |
| 3503 | + }.get(svc) |
| 3504 | + |
| 3505 | + record_event = pretend.call_recorder( |
| 3506 | + lambda self, *, tag, request=None, additional: None |
| 3507 | + ) |
| 3508 | + monkeypatch.setattr(HasEvents, "record_event", record_event) |
| 3509 | + |
| 3510 | + def failing_verify(_self, _verifier, _policy, _dist): |
| 3511 | + raise InvalidAttestationError |
| 3512 | + |
| 3513 | + monkeypatch.setattr(Attestation, "verify", failing_verify) |
| 3514 | + monkeypatch.setattr(Verifier, "production", lambda: pretend.stub()) |
| 3515 | + |
| 3516 | + with pytest.raises(HTTPBadRequest) as excinfo: |
| 3517 | + legacy.file_upload(db_request) |
| 3518 | + |
| 3519 | + resp = excinfo.value |
| 3520 | + |
| 3521 | + assert resp.status_code == 400 |
| 3522 | + assert resp.status.startswith( |
| 3523 | + "400 Error while decoding the included attestation:" |
| 3524 | + ) |
| 3525 | + |
| 3526 | + def test_upload_with_failing_attestation_fails( |
| 3527 | + self, |
| 3528 | + monkeypatch, |
| 3529 | + pyramid_config, |
| 3530 | + db_request, |
| 3531 | + metrics, |
| 3532 | + ): |
| 3533 | + from warehouse.events.models import HasEvents |
| 3534 | + |
| 3535 | + project = ProjectFactory.create() |
| 3536 | + version = "1.0" |
| 3537 | + publisher = GitHubPublisherFactory.create(projects=[project]) |
| 3538 | + claims = { |
| 3539 | + "sha": "somesha", |
| 3540 | + "repository": f"{publisher.repository_owner}/{publisher.repository_name}", |
| 3541 | + "workflow": "workflow_name", |
| 3542 | + } |
| 3543 | + identity = PublisherTokenContext(publisher, SignedClaims(claims)) |
| 3544 | + db_request.oidc_publisher = identity.publisher |
| 3545 | + db_request.oidc_claims = identity.claims |
| 3546 | + |
| 3547 | + db_request.db.add(Classifier(classifier="Environment :: Other Environment")) |
| 3548 | + db_request.db.add(Classifier(classifier="Programming Language :: Python")) |
| 3549 | + |
| 3550 | + filename = "{}-{}.tar.gz".format(project.name, "1.0") |
| 3551 | + attestation = Attestation( |
| 3552 | + version=1, |
| 3553 | + verification_material=VerificationMaterial( |
| 3554 | + certificate="somebase64string", transparency_entries=[dict()] |
| 3555 | + ), |
| 3556 | + message_signature="somebase64string", |
| 3557 | + ) |
| 3558 | + |
| 3559 | + pyramid_config.testing_securitypolicy(identity=identity) |
| 3560 | + db_request.user = None |
| 3561 | + db_request.user_agent = "warehouse-tests/6.6.6" |
| 3562 | + db_request.POST = MultiDict( |
| 3563 | + { |
| 3564 | + "metadata_version": "1.2", |
| 3565 | + "name": project.name, |
| 3566 | + "attestations": f"[{attestation.model_dump_json()}]", |
| 3567 | + "version": version, |
| 3568 | + "summary": "This is my summary!", |
| 3569 | + "filetype": "sdist", |
| 3570 | + "md5_digest": _TAR_GZ_PKG_MD5, |
| 3571 | + "content": pretend.stub( |
| 3572 | + filename=filename, |
| 3573 | + file=io.BytesIO(_TAR_GZ_PKG_TESTDATA), |
| 3574 | + type="application/tar", |
| 3575 | + ), |
| 3576 | + } |
| 3577 | + ) |
| 3578 | + |
| 3579 | + storage_service = pretend.stub(store=lambda path, filepath, meta: None) |
| 3580 | + db_request.find_service = lambda svc, name=None, context=None: { |
| 3581 | + IFileStorage: storage_service, |
| 3582 | + IMetricsService: metrics, |
| 3583 | + }.get(svc) |
| 3584 | + |
| 3585 | + record_event = pretend.call_recorder( |
| 3586 | + lambda self, *, tag, request=None, additional: None |
| 3587 | + ) |
| 3588 | + monkeypatch.setattr(HasEvents, "record_event", record_event) |
| 3589 | + |
| 3590 | + def failing_verify(_self, _verifier, _policy, _dist): |
| 3591 | + raise VerificationError("verification failed") |
| 3592 | + |
| 3593 | + monkeypatch.setattr(Attestation, "verify", failing_verify) |
| 3594 | + monkeypatch.setattr(Verifier, "production", lambda: pretend.stub()) |
| 3595 | + |
| 3596 | + with pytest.raises(HTTPBadRequest) as excinfo: |
| 3597 | + legacy.file_upload(db_request) |
| 3598 | + |
| 3599 | + resp = excinfo.value |
| 3600 | + |
| 3601 | + assert resp.status_code == 400 |
| 3602 | + assert resp.status.startswith( |
| 3603 | + "400 Could not verify the uploaded artifact using the included attestation" |
| 3604 | + ) |
| 3605 | + |
3296 | 3606 | @pytest.mark.parametrize(
|
3297 | 3607 | "version, expected_version",
|
3298 | 3608 | [
|
|
0 commit comments