diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml new file mode 100644 index 0000000..bc72a49 --- /dev/null +++ b/.github/workflows/codeql.yml @@ -0,0 +1,41 @@ +name: CodeQL + +on: + push: + branches: [main] + pull_request: + branches: [main] + schedule: + - cron: '0 6 * * 1' + +permissions: + contents: read + +jobs: + analyze: + runs-on: ubuntu-latest + timeout-minutes: 15 + permissions: + security-events: write + packages: read + + strategy: + fail-fast: false + matrix: + language: [python] + + steps: + - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 + with: + persist-credentials: false + + - name: Initialize CodeQL + uses: github/codeql-action/init@c10b8064de6f491fea524254123dbe5e09572f13 # v4 + with: + languages: ${{ matrix.language }} + queries: security-and-quality + + - name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@c10b8064de6f491fea524254123dbe5e09572f13 # v4 + with: + category: "/language:${{ matrix.language }}" diff --git a/.github/workflows/scorecard.yml b/.github/workflows/scorecard.yml new file mode 100644 index 0000000..51539ac --- /dev/null +++ b/.github/workflows/scorecard.yml @@ -0,0 +1,41 @@ +name: OpenSSF Scorecard + +on: + branch_protection_rule: + workflow_dispatch: + schedule: + - cron: '0 5 * * 1' + push: + branches: [main] + +permissions: read-all + +jobs: + analysis: + runs-on: ubuntu-latest + permissions: + security-events: write + id-token: write + steps: + - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 + with: + persist-credentials: false + + - name: Run Scorecard + uses: ossf/scorecard-action@4eaacf0543bb3f2c246792bd56e8cdeffafb205a # v2.4.3 + with: + results_file: results.sarif + results_format: sarif + publish_results: true + + - name: Upload Scorecard results + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 + with: + name: scorecard-results + path: results.sarif + retention-days: 5 + + - name: Upload to code-scanning + uses: github/codeql-action/upload-sarif@c10b8064de6f491fea524254123dbe5e09572f13 # v4 + with: + sarif_file: results.sarif diff --git a/README.md b/README.md index 027ee65..eb07691 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,17 @@ # pipelock-verify -[![PyPI version](https://img.shields.io/pypi/v/pipelock-verify.svg)](https://pypi.org/project/pipelock-verify/) -[![Python versions](https://img.shields.io/pypi/pyversions/pipelock-verify.svg)](https://pypi.org/project/pipelock-verify/) +[![PyPI](https://img.shields.io/pypi/v/pipelock-verify.svg?label=PyPI&logo=pypi&logoColor=white)](https://pypi.org/project/pipelock-verify/) +[![Python](https://img.shields.io/pypi/pyversions/pipelock-verify.svg?label=Python&logo=python&logoColor=white)](https://pypi.org/project/pipelock-verify/) [![CI](https://github.com/luckyPipewrench/pipelock-verify-python/actions/workflows/ci.yml/badge.svg)](https://github.com/luckyPipewrench/pipelock-verify-python/actions/workflows/ci.yml) +[![CodeQL](https://github.com/luckyPipewrench/pipelock-verify-python/actions/workflows/codeql.yml/badge.svg)](https://github.com/luckyPipewrench/pipelock-verify-python/actions/workflows/codeql.yml) +[![OpenSSF Scorecard](https://api.scorecard.dev/projects/github.com/luckyPipewrench/pipelock-verify-python/badge)](https://scorecard.dev/viewer/?uri=github.com/luckyPipewrench/pipelock-verify-python) [![License: Apache-2.0](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](LICENSE) -Python verifier for [Pipelock](https://github.com/luckyPipewrench/pipelock) -action receipts. Verifies the Ed25519 signature, chain linkage, and -flight-recorder wrapping of receipts emitted by the Pipelock mediator. +**Python verifier for [Pipelock](https://github.com/luckyPipewrench/pipelock) action receipts.** Verifies the Ed25519 signature, chain linkage, and flight-recorder wrapping of receipts emitted by the Pipelock mediator. -The library mirrors the Go reference implementation byte-for-byte. The -conformance golden files in `tests/conformance/` are generated by Pipelock's -Go code and verified identically by both sides. +Mirrors the Go reference implementation byte-for-byte. The conformance golden files in `tests/conformance/` are generated by Pipelock's Go code and verified identically by both sides. + +[Install](#install) · [Usage](#usage) · [What gets verified](#what-gets-verified) · [Canonicalization](#canonicalization-rules) · [Spec](https://pipelab.org/learn/action-receipt-spec/) · [Go reference](https://github.com/luckyPipewrench/pipelock) ## Install @@ -19,8 +19,7 @@ Go code and verified identically by both sides. pip install pipelock-verify ``` -Only one runtime dependency: [`cryptography`](https://cryptography.io) for -the Ed25519 primitives. +Only one runtime dependency: [`cryptography`](https://cryptography.io) for the Ed25519 primitives. ## Usage diff --git a/pipelock_verify/_verify.py b/pipelock_verify/_verify.py index 9abc610..e44e504 100644 --- a/pipelock_verify/_verify.py +++ b/pipelock_verify/_verify.py @@ -64,17 +64,34 @@ def _is_valid_rfc3339(value: Any) -> bool: """Return True if value parses as an RFC 3339 timestamp Go would accept. - Python's datetime.fromisoformat accepts the ``Z`` suffix natively only - on 3.11+, so we substitute it with ``+00:00`` before parsing to support - 3.9 and 3.10 as well. + Go's ``time.RFC3339Nano`` allows up to 9 fractional digits (nanoseconds). + Python's ``datetime.fromisoformat`` tops out at microsecond precision + (6 fractional digits) and the ``Z`` suffix only parses natively on 3.11+. + We handle both differences here so valid Go timestamps verify on 3.9-3.13. """ if not isinstance(value, str): return False if not _RFC3339_RE.match(value): return False candidate = value[:-1] + "+00:00" if value.endswith("Z") else value + # Truncate fractional seconds to 6 digits so fromisoformat accepts + # Go's nanosecond timestamps on Python 3.9/3.10. The regex above has + # already validated the overall shape, so we know there is at most one + # ``.`` before the timezone offset. + match = re.match( + r"^(?P\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})" + r"(?:\.(?P\d{1,9}))?" + r"(?P[+-]\d{2}:\d{2})$", + candidate, + ) + if match is None: + return False + prefix = match.group("prefix") + frac = match.group("frac") or "" + offset = match.group("offset") + truncated = f"{prefix}.{frac[:6]}{offset}" if frac else f"{prefix}{offset}" try: - datetime.fromisoformat(candidate) + datetime.fromisoformat(truncated) except ValueError: return False return True