diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..fac1faa --- /dev/null +++ b/.dockerignore @@ -0,0 +1,17 @@ +.git +.github +.idea +.vscode +target +investigation +docs +scripts +*.log +.env +.env.* +secrets +backups + +.client-tools/ + +.client-venv/ diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..1a1a6f8 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,11 @@ +* text=auto eol=lf +mvnw.cmd text eol=crlf +# Calendar fixtures retain the accepted/client-emitted representation byte for byte. +src/test/resources/calendar/**/*.ics -text whitespace=cr-at-eol +src/test/resources/calendar/*.ics -text whitespace=cr-at-eol + +# Deliver upstream license texts verbatim, including their original trailing whitespace. +licenses/EPL-2.0.txt -text -whitespace +licenses/LGPL-2.1.txt -text +licenses/logback-LICENSE.txt -text -whitespace +licenses/asm-9.7.1-copyright-and-license.txt -text diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..a9bf102 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,178 @@ +name: verify + +on: + push: + pull_request: + workflow_dispatch: + +permissions: + contents: read + +defaults: + run: + shell: bash + +concurrency: + group: verify-${{ github.ref }} + cancel-in-progress: true + +jobs: + release-tools: + runs-on: ubuntu-24.04 + timeout-minutes: 10 + steps: + - uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0 + with: + persist-credentials: false + fetch-depth: 0 + - name: Validate release helpers and rejection paths before qualification + run: | + python3 -m unittest discover -s qualification/release -p 'test_*.py' + python3 -m unittest discover -s qualification/distribution -p 'test_*.py' + python3 -m py_compile scripts/*.py + for script in scripts/*.sh; do bash -n "$script"; done + bash scripts/validate-workflows.sh + + application: + needs: release-tools + runs-on: ubuntu-24.04 + timeout-minutes: 90 + steps: + - uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0 + with: + persist-credentials: false + fetch-depth: 0 + - name: Install the exact pinned Temurin JDK + run: bash scripts/install-ci-jdk.sh + - name: Install pinned independent CalDAV client test dependencies + run: | + python3 -m venv .client-venv + .client-venv/bin/python -m pip install --disable-pip-version-check --no-cache-dir -r qualification/clients/requirements.txt -r qualification/clients/desktop-requirements.txt + bash qualification/clients/install-thunderbird.sh .client-tools + - name: Require Docker and run unit and integration suites + env: + BUSYSTACK_CLIENT_PYTHON: ${{ github.workspace }}/.client-venv/bin/python + BUSYSTACK_THUNDERBIRD_BINARY: ${{ github.workspace }}/.client-tools/thunderbird/thunderbird + run: | + docker info > /dev/null + ./mvnw --batch-mode --no-transfer-progress clean verify + ./mvnw --batch-mode --no-transfer-progress -f investigation/protocol/pom.xml -s investigation/protocol/settings.xml test + ./mvnw --batch-mode --no-transfer-progress -f investigation/oauth/pom.xml test + - name: Resolve qualified Maven identity + id: identity + run: echo "jar=$(python3 scripts/release_version.py --source-sha "$GITHUB_SHA" --format jar)" >> "$GITHUB_OUTPUT" + - name: Build deployable container + run: | + jar=$(python3 scripts/release_version.py --source-sha "$GITHUB_SHA" --format jar) + python3 scripts/build-image.py --image busystack-caldav:ci --jar "target/$jar" + docker image inspect --format '{{.Id}}' busystack-caldav:ci | tee target/application-image-id.txt + sha256sum "target/$jar" | cut -d ' ' -f 1 > target/application-jar-sha256.txt + - name: Inspect actual source, JAR and container license delivery + run: | + python3 -m unittest discover -s qualification/distribution -p 'test_*.py' + python3 scripts/verify-distribution.py --image busystack-caldav:ci + - name: Scan final container OS packages with a pinned scanner and current database + run: python3 scripts/check-container-advisories.py --image busystack-caldav:ci + - name: Verify packaged startup, restart, and database recovery + run: python3 scripts/verify-operations.py --image busystack-caldav:ci | tee target/operations-qualification.txt + - name: Verify qualification refresh and private snapshot bookkeeping + run: python3 -m unittest discover -s qualification/passkeys -p 'test_*.py' + - name: Build isolated browser qualification image + run: docker build --tag busystack-passkey-browser:qualification qualification/passkeys + - name: Verify browser WebAuthn and MFA over trusted test TLS + run: python3 qualification/passkeys/run.py --application-image busystack-caldav:ci --groups | tee target/browser-qualification.txt + - name: Verify explicit strict synchronization override over real HTTPS + run: python3 qualification/passkeys/run.py --application-image busystack-caldav:ci --groups --strict-sync-depth | tee target/strict-browser-qualification.txt + - name: Verify HTTPS reverse proxy with preserved servlet path + run: python3 qualification/passkeys/run.py --application-image busystack-caldav:ci --proxy --groups | tee target/proxy-qualification.txt + - name: Build disposable real Keycloak browser fixture + run: docker build --tag busystack-keycloak-browser:26.8.1 qualification/keycloak + - name: Verify cross-site organization login and local passkey MFA + run: python3 qualification/passkeys/run.py --application-image busystack-caldav:ci --oidc --groups | tee target/oidc-browser-qualification.txt + - name: Verify HTTPS local account recovery after restoring an older database + run: python3 qualification/passkeys/run.py --application-image busystack-caldav:ci --proxy --groups --restore --clients | tee target/https-restore-qualification.txt + - name: Verify HTTPS federated recovery against the same real upstream provider + run: python3 qualification/passkeys/run.py --application-image busystack-caldav:ci --oidc --groups --restore | tee target/oidc-restore-qualification.txt + - name: Retain exact tested publication candidate only after successful qualification + run: python3 scripts/release-candidate.py --output target/release-candidate --image busystack-caldav:ci + - name: Upload successful candidate separately from failure evidence + uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4 # v5 + with: + name: verified-candidate-${{ github.run_id }}-${{ github.run_attempt }} + path: target/release-candidate/ + retention-days: 30 + compression-level: 0 + if-no-files-found: error + - name: Retain qualification evidence + if: always() + uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4 # v5 + with: + name: application-evidence-${{ github.run_attempt }} + retention-days: 14 + path: | + target/surefire-reports/ + target/failsafe-reports/ + target/classes/META-INF/sbom/application.cdx.json + target/${{ steps.identity.outputs.jar }} + target/*.cdx.json + target/*-qualification.txt + target/application-image-id.txt + target/application-jar-sha256.txt + target/container-os-advisories*.json + target/qualification/ + investigation/*/target/surefire-reports/ + if-no-files-found: error + + dependency-audit: + needs: release-tools + runs-on: ubuntu-24.04 + timeout-minutes: 40 + steps: + - uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0 + with: + persist-credentials: false + fetch-depth: 0 + - name: Install the exact pinned Temurin JDK + run: bash scripts/install-ci-jdk.sh + - name: Generate runtime inventory and verify advisory gate failure behavior + run: | + ./mvnw --batch-mode --no-transfer-progress -DskipTests package + python3 -m unittest discover -s qualification/security -p 'test_*.py' + - name: Verify known-vulnerable package is detected + run: | + if python3 scripts/check-advisories.py --sbom qualification/security/positive-control.cdx.json --output target/osv-positive-control.json; then + echo 'Advisory positive control incorrectly reported success.' >&2 + exit 1 + else + audit_status=$? + test "$audit_status" -eq 1 + fi + python3 - <<'PY' + import json + from pathlib import Path + result = json.loads(Path('target/osv-positive-control.json').read_text()) + assert result['status'] == 'advisories-returned' + assert result['affectedPackageCount'] == 1 + assert any(item['id'] == 'GHSA-jfh8-c2jp-5v3q' for package in result['packages'] for item in package['advisories']) + PY + - name: Check runtime dependencies against current OSV advisories + run: python3 scripts/check-advisories.py + - name: Additional NVD audit when feed credential is configured + env: + NVD_API_KEY: ${{ secrets.NVD_API_KEY }} + run: | + if [[ -n "${NVD_API_KEY:-}" ]]; then + ./mvnw --batch-mode --no-transfer-progress org.owasp:dependency-check-maven:13.0.0:check -DnvdApiKeyEnvironmentVariable=NVD_API_KEY -DfailBuildOnCVSS=7 -DfailOnError=true -DossIndexAnalyzerEnabled=false -Dformats=HTML,JSON + else + echo 'Additional NVD audit unexecuted: no NVD_API_KEY configured. The required OSV check ran above.' + fi + - name: Retain dependency audit + if: always() + uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4 # v5 + with: + name: dependency-audit-${{ github.run_attempt }} + retention-days: 14 + path: | + target/osv-*.json + target/dependency-check-report.* + if-no-files-found: error diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..dc9172d --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,135 @@ +name: release + +on: + workflow_dispatch: + inputs: + version: + description: Exact release version + type: string + required: true + verification_run: + description: Exact successful main verification run ID + type: string + required: true + source_sha: + description: Exact qualified main commit (40 hex characters) + type: string + required: true + phase: + description: Stage and qualify first; publish consumes the successful staging run + type: choice + options: [stage, publish] + required: true + candidate_version: + description: Maven version followed by -rc.N; required when staging + type: string + required: false + immutability_confirmed: + description: Admin checked immutable-releases enabled via gh API immediately before publish + type: boolean + default: false + required: true + stage_run: + description: Successful staging run ID (required for publish) + type: string + required: false + +permissions: + contents: read + +concurrency: + group: release-${{ inputs.version }} + cancel-in-progress: false + +defaults: + run: + shell: bash + +jobs: + promote: + if: github.repository == 'busystack/caldav-server' && github.ref == 'refs/heads/main' + runs-on: ubuntu-24.04 + timeout-minutes: 90 + permissions: + contents: write + actions: read + packages: write + steps: + - uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0 + with: + persist-credentials: false + fetch-depth: 0 + - name: Validate dispatch and release tooling + env: + RELEASE_VERSION: ${{ inputs.version }} + RELEASE_SHA: ${{ inputs.source_sha }} + RELEASE_RUN: ${{ inputs.verification_run }} + RELEASE_STAGE_RUN: ${{ inputs.stage_run }} + RELEASE_PHASE: ${{ inputs.phase }} + RELEASE_CANDIDATE_VERSION: ${{ inputs.candidate_version }} + run: | + [[ "$RELEASE_SHA" =~ ^[0-9a-f]{40}$ ]] + test "$(git rev-parse HEAD)" = "$GITHUB_SHA" + [[ "$RELEASE_RUN" =~ ^[0-9]+$ ]] + [[ -z "$RELEASE_STAGE_RUN" || "$RELEASE_STAGE_RUN" =~ ^[0-9]+$ ]] + if [[ "$RELEASE_PHASE" == stage ]]; then + test "$RELEASE_SHA" = "$GITHUB_SHA" + python3 scripts/release_version.py --source-sha "$RELEASE_SHA" --requested "$RELEASE_VERSION" --candidate "$RELEASE_CANDIDATE_VERSION" + else + git merge-base --is-ancestor "$RELEASE_SHA" HEAD + fi + python3 scripts/release_version.py --source-sha "$RELEASE_SHA" --requested "$RELEASE_VERSION" + python3 -m unittest discover -s qualification/release -p 'test_*.py' + - name: Install isolated official release-attestation CLI + run: bash scripts/install-release-gh.sh + - name: Install selected JDK for downloaded JAR qualification + run: bash scripts/install-ci-jdk.sh + - name: Build browser fixture (application is promoted without rebuilding) + run: docker build --tag busystack-passkey-browser:qualification qualification/passkeys + - name: Authenticate repository-associated GHCR publication with ephemeral workflow token + env: + GH_TOKEN: ${{ github.token }} + run: printf '%s' "$GH_TOKEN" | docker login ghcr.io --username "$GITHUB_ACTOR" --password-stdin + - name: Validate verification, promote and qualify exact artifacts + env: + GH_TOKEN: ${{ github.token }} + RELEASE_VERSION: ${{ inputs.version }} + RELEASE_RUN: ${{ inputs.verification_run }} + RELEASE_SHA: ${{ inputs.source_sha }} + RELEASE_PHASE: ${{ inputs.phase }} + IMMUTABILITY_CONFIRMED: ${{ inputs.immutability_confirmed }} + RELEASE_STAGE_RUN: ${{ inputs.stage_run }} + RELEASE_CANDIDATE_VERSION: ${{ inputs.candidate_version }} + run: | + arguments=(--version "$RELEASE_VERSION" --verification-run "$RELEASE_RUN" --source-sha "$RELEASE_SHA" --phase "$RELEASE_PHASE" --work "$RUNNER_TEMP/release-work") + if [[ "$RELEASE_PHASE" == stage ]]; then arguments+=(--candidate-version "$RELEASE_CANDIDATE_VERSION"); fi + if [[ -n "$RELEASE_STAGE_RUN" ]]; then arguments+=(--stage-run "$RELEASE_STAGE_RUN"); fi + if [[ "$IMMUTABILITY_CONFIRMED" == true ]]; then arguments+=(--immutability-confirmed); fi + python3 scripts/publish-release.py "${arguments[@]}" + - name: Retain successful staging assets + if: success() && inputs.phase == 'stage' + uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4 # v5 + with: + name: release-stage-${{ github.run_id }}-${{ github.run_attempt }} + path: | + ${{ runner.temp }}/release-work/assets/ + ${{ runner.temp }}/release-work/stage.json + ${{ runner.temp }}/release-work/installation.json + ${{ runner.temp }}/release-work/release-notes.md + compression-level: 0 + retention-days: 30 + if-no-files-found: error + - name: Retain operation journal and qualification results on every attempt + if: always() + uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4 # v5 + with: + name: release-operations-${{ github.run_id }}-${{ github.run_attempt }} + path: | + ${{ runner.temp }}/release-work/release-attestation.json + ${{ runner.temp }}/release-work/operations.jsonl + ${{ runner.temp }}/release-work/*installation.json + retention-days: 90 + if-no-files-found: warn + - name: Remove ephemeral registry login + if: always() + run: docker logout ghcr.io diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6eb69d0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,16 @@ +target/ +**/target/ +.idea/ +*.iml +.env +.env.* +!.env.example +backups/ +*.log +__pycache__/ +**/node_modules/ +.attach_pid* + +.client-venv/ + +.client-tools/ diff --git a/.mvn/jvm.config b/.mvn/jvm.config new file mode 100644 index 0000000..37543d7 --- /dev/null +++ b/.mvn/jvm.config @@ -0,0 +1 @@ +-Duser.timezone=UTC diff --git a/.mvn/wrapper/maven-wrapper.properties b/.mvn/wrapper/maven-wrapper.properties new file mode 100644 index 0000000..533e775 --- /dev/null +++ b/.mvn/wrapper/maven-wrapper.properties @@ -0,0 +1,4 @@ +wrapperVersion=3.3.4 +distributionType=only-script +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.12/apache-maven-3.9.12-bin.zip +distributionSha256Sum=305773a68d6ddfd413df58c82b3f8050e89778e777f3a745c8e5b8cbea4018ef diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..4025460 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +# Use python3 scripts/build-image.py to supply the verified Maven artifact. +FROM eclipse-temurin:25-jdk@sha256:3ab7fc8d0b4ade8fef96903fb36767c1dbb4fd1535dcfad85b59c17afbbb5857 AS runtime +ARG APPLICATION_VERSION +ARG SOURCE_REVISION +WORKDIR /opt/busystack +RUN ["/bin/bash", "-ec", "[[ ${APPLICATION_VERSION:-} =~ ^(0|[1-9][0-9]*)[.](0|[1-9][0-9]*)[.](0|[1-9][0-9]*)$ && ${SOURCE_REVISION:-} =~ ^[0-9a-f]{40}$ ]] || { echo 'Build with python3 scripts/build-image.py: valid Maven version and Git revision are required' >&2; exit 1; }"] +COPY .image-input/ /tmp/busystack-image-input/ +RUN ["/bin/bash", "-ec", "cd /tmp/busystack-image-input; [[ ${APPLICATION_VERSION:-} =~ ^(0|[1-9][0-9]*)[.](0|[1-9][0-9]*)[.](0|[1-9][0-9]*)$ && ${SOURCE_REVISION:-} =~ ^[0-9a-f]{40}$ ]] || { echo 'Build with python3 scripts/build-image.py: valid Maven version and Git revision are required' >&2; exit 1; }; test \"$(cat version)\" = \"$APPLICATION_VERSION\"; test \"$(cat revision)\" = \"$SOURCE_REVISION\"; sha256sum --check SHA256SUMS; mkdir extracted; cd extracted; jar xf ../application.jar META-INF/MANIFEST.MF META-INF/maven/org.busystack/caldav-server/pom.properties; tr -d '\\r' < META-INF/MANIFEST.MF | grep -Fx \"Implementation-Version: $APPLICATION_VERSION\"; grep -Fx \"version=$APPLICATION_VERSION\" META-INF/maven/org.busystack/caldav-server/pom.properties; grep -Fx 'groupId=org.busystack' META-INF/maven/org.busystack/caldav-server/pom.properties; grep -Fx 'artifactId=caldav-server' META-INF/maven/org.busystack/caldav-server/pom.properties; cd ..; mv application.jar /opt/busystack/application.jar; mkdir /opt/busystack/licenses; mv LICENSE NOTICE THIRD-PARTY-NOTICES runtime-dependency-inventory.json third-party /opt/busystack/licenses/; rm -rf /tmp/busystack-image-input"] +LABEL org.opencontainers.image.title="BusyStack CalDAV Server" \ + org.opencontainers.image.description="Independent calendar and task server" \ + org.opencontainers.image.version="${APPLICATION_VERSION}" \ + org.opencontainers.image.licenses="Apache-2.0" \ + org.opencontainers.image.source="https://github.com/busystack/caldav-server" \ + org.opencontainers.image.revision="${SOURCE_REVISION}" +ENV SERVER_ADDRESS=0.0.0.0 +RUN groupadd --gid 10001 busystack && useradd --uid 10001 --gid 10001 --no-create-home --home-dir /nonexistent --shell /usr/sbin/nologin busystack && chown -R 10001:10001 /opt/busystack +USER 10001:10001 +EXPOSE 8080 +ENTRYPOINT ["java", "-XX:MaxRAMPercentage=70.0", "-Djava.io.tmpdir=/tmp", "-jar", "/opt/busystack/application.jar"] diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + 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. diff --git a/NOTICE b/NOTICE new file mode 100644 index 0000000..006e678 --- /dev/null +++ b/NOTICE @@ -0,0 +1,18 @@ +BusyStack CalDAV Server +https://github.com/busystack/caldav-server + +The project is licensed under Apache License 2.0. Contributors retain the +copyrights applicable to their contributions; this notice does not assign them +to a new legal entity. + +This distribution includes the official unmodified iCal4j 4.4.0 artifact under +BSD-3-Clause, Copyright (c) Ben Fortuna. Its complete BSD notice is delivered in +META-INF/licenses/ical4j-parser-BSD-3-Clause.txt and its original nested JAR. + +The database migrations contain adapted Spring Security 7.1.2 and Spring +Session 4.1.1 JDBC schemas under Apache License 2.0. Their original artifacts, +modifications and attribution are recorded in META-INF/licenses/spring-derived-schemas.txt. + +Dependencies retain their separate licenses and upstream notices. Their license +and NOTICE files remain in the unmodified nested JARs under BOOT-INF/lib; this +project license does not replace them. See THIRD-PARTY-NOTICES. diff --git a/README.md b/README.md index e69de29..96f13b1 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,74 @@ +# BusyStack CalDAV Server + +A self-hosted CalDAV server for calendars and tasks, with PostgreSQL persistence +and built-in account administration. It supports local accounts and optional +OpenID Connect login through an existing identity provider. + +## Capabilities + +- Event and task collections, recurrence and exceptions, custom timezones, + calendar queries, free/busy reports and incremental synchronization. +- Conditional writes with strong ETags and owner-controlled individual or group + read/write sharing. +- Named, revocable DAV credentials and OAuth authorization with PKCE. +- Passkeys, password-plus-passkey MFA, recovery codes and browser-based account + and administrator tools. + +## Install + +Use the **Docker Compose deployment bundle** from the +[latest stable release](https://github.com/busystack/caldav-server/releases/latest). +Download its deployment archive, `SHA256SUMS` and `release-manifest.json`, verify +the checksums, extract the archive and follow its `INSTALL.md`. + +The bundle supports **Linux AMD64**, Docker Engine and Compose v2 (2.24.4 or newer). +It pulls the verified application image by version and immutable registry digest +and includes a private PostgreSQL service. No source checkout, Maven or host Java +is required. Supply your own domain, HTTPS termination and private secret files. +Keep the database private and the application HTTP listener on loopback. + +The executable JAR is an alternative for administrators managing Java 25, +PostgreSQL 18.6 and their own process supervision. See the +[installation guide](deployment/INSTALL.md) and [operations guide](docs/operations.md) +for configuration, administrator bootstrap, updates, backups and restoration. +Use the generated instructions included with the downloaded release. + +## Connect a client + +Sign in to the account interface, enroll a passkey, complete MFA, create event or +task collections and issue a named DAV credential. Configure your CalDAV client +with the displayed server address and username, using that credential as its +password. The account password is not the default DAV credential. Registered +OAuth clients can use the browser authorization flow. + +Qualified graphical clients are Thunderbird 159.0 and GNOME Calendar 50.0 / +Evolution Data Server 3.56.2 in the configurations listed in the +[compatibility documentation](docs/observance-until-compatibility.md). + +## Documentation and limitations + +Start with [account security](docs/account-security.md), +[passkeys and MFA](docs/passkeys-and-mfa.md), [OAuth](docs/oauth.md), +[optional OIDC](docs/keycloak.md), [sharing groups](docs/groups.md) and +[calendar semantics](docs/calendar-semantics.md). + +The account interface is not a web calendar editor. CardDAV, scheduling and +invitation delivery, alarm execution and automatic failover are outside the +supported profile. Stored attendees, organizers, attachments and alarms do not +cause outbound actions. The documented +[synchronization Depth and timezone-input compatibility behavior](docs/conformance.md) +remains configurable; see its scope and strict-mode settings before changing it. + +## Development + +Use the Maven wrapper with the [declared toolchain](docs/versions.md) and required +integration-test prerequisites in [client verification](qualification/clients/README.md). +`./mvnw -B -ntp clean verify` runs formatting, static analysis and application tests. +`python3 scripts/build-image.py` builds and verifies an image using Maven's version +automatically. See the [release procedure](docs/development/releasing.md) for the +supported development Compose command and distribution checks. + +Contributions can be proposed through repository issues and pull requests. Keep +changes focused and run the relevant verification before submitting them. +The project is licensed under [Apache License 2.0](LICENSE); dependencies retain +their [own licenses and notices](docs/third-party-licenses.md). diff --git a/THIRD-PARTY-NOTICES b/THIRD-PARTY-NOTICES new file mode 100644 index 0000000..b9b35b7 --- /dev/null +++ b/THIRD-PARTY-NOTICES @@ -0,0 +1,67 @@ +Third-party distribution material +================================= + +This project uses official, unmodified org.mnode.ical4j:ical4j:4.4.0 under +BSD-3-Clause. Its Ben Fortuna copyright and complete license remain in its +original nested JAR and META-INF/licenses/ical4j-parser-BSD-3-Clause.txt. +The former modified parser/timezone implementation copies and patch payloads +have been removed. Historical provenance remains in Git history. Current +integration is documented in docs/ical4j-parser-patch.md and +docs/ical4j-timezone-patch.md. Packaged runtime JARs are checked against the +SHA-256 of official Maven Central bytes in qualification/distribution/runtime-artifacts.json. + +Spring Security 7.1.2 / Spring Session 4.1.1 schemas are adapted under Apache-2.0. +See META-INF/licenses/spring-derived-schemas.txt for original paths and changes. +Do not edit already-applied migrations to adjust notices: additional notice +material accompanies the immutable migrations. + +Spring Session core and JDBC 4.1.1 are Apache-2.0. Their published POM's +"Broadcom Foundation License" is an unintended release-tool metadata error +confirmed and corrected by the maintainer in: +https://github.com/spring-projects/spring-session/issues/3910 +The already-published POM cannot change; 4.1.2 was the correction milestone. +The original POM declarations are retained in the SBOM. The Apache conclusion, +actual JAR/license/POM hashes and maintainer evidence are recorded separately in +docs/third-party-licenses.md; dependencies are neither altered nor suppressed. + +The executable archive retains every original nested dependency JAR, including +its original license, copyright, third-party and NOTICE files. The generated +META-INF/sbom/application.cdx.json identifies exact production versions and +POM-declared licenses. Runtime dependency archive and notice hashes are checked +by scripts/verify-distribution.py, whose inspection report is retained in CI. + +Some artifacts omit license text from their JARs. The applicable unmodified +Apache-2.0 license is delivered as META-INF/LICENSE for those Apache-licensed +components; ASM's BSD copyright/conditions and Logback's dual-license text are +also delivered separately in META-INF/licenses. This is not a replacement of +their separate copyright or license. Upstream notices that are present remain +in the nested artifacts without alteration. + +Logback 1.5.38 is dual licensed under EPL-2.0 OR LGPL-2.1-only. The distribution +uses the EPL-2.0 alternative, retains both alternatives and upstream license-declaration +material, and offers the exact unmodified sources via Maven Central: +https://repo.maven.apache.org/maven2/ch/qos/logback/logback-core/1.5.38/logback-core-1.5.38-sources.jar +https://repo.maven.apache.org/maven2/ch/qos/logback/logback-classic/1.5.38/logback-classic-1.5.38-sources.jar +Jakarta Annotation API 3.0.0 is distributed under the EPL-2.0 alternative; its +full original LICENSE.md and NOTICE.md remain in its nested JAR. Its source: +https://repo.maven.apache.org/maven2/jakarta/annotation/jakarta.annotation-api/3.0.0/jakarta.annotation-api-3.0.0-sources.jar +No source modifications are made to these separately linked dependencies. + +The final container additionally includes Eclipse Temurin OpenJDK 25.0.4.1+1, +GPL-2.0 with Classpath Exception, its assembly exception and component notices +under /opt/java/openjdk/legal. Exact upstream source distribution and license: +https://github.com/adoptium/temurin25-binaries/releases/tag/jdk-25.0.4.1%2B1 +https://openjdk.org/legal/gplv2+ce.html +The Classpath Exception applies to application linking; it does not relicense +the JDK itself. Ubuntu OS package copyrights/licenses remain available under +/usr/share/doc//copyright. The pinned parent image's original legal +material is retained. Corresponding Ubuntu package sources are available through +https://archive.ubuntu.com/ubuntu/pool/ and https://security.ubuntu.com/ubuntu/pool/; +use the exact package/version inventory, Source field and repository listed in +the image, not an unrelated current binary release. The container-package inventory and vulnerability report +are separate from the Maven dependency SBOM and are retained as CI evidence. + +Bedework investigation modules, Testcontainers, JUnit, Python/Thunderbird, +Chromium/Playwright, Nginx and Keycloak qualification fixtures are development +and test tooling, absent from the production application runtime. Their +separate upstream licenses are not covered by this project's license grant. diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 0000000..8e29102 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,87 @@ +name: busystack-caldav + +services: + database: + image: postgres:18.6@sha256:edb0c08dc33befd0262ab3b5cf1c4f3f8cfd8b09e88c05b45adee29d6761ddb6 + restart: unless-stopped + environment: + POSTGRES_DB: busystack + POSTGRES_USER: postgres + POSTGRES_PASSWORD_FILE: /run/secrets/postgres_admin_password + POSTGRES_INITDB_ARGS: --auth-host=scram-sha-256 --data-checksums + secrets: + - postgres_admin_password + - source: database_password + target: spring.datasource.password + volumes: + - database:/var/lib/postgresql + - ./scripts/init-db.sh:/docker-entrypoint-initdb.d/10-busystack.sh:ro + healthcheck: + test: [CMD-SHELL, 'PGPASSWORD="$$(cat /run/secrets/spring.datasource.password)" psql -h 127.0.0.1 -U busystack -d busystack --no-psqlrc --tuples-only --no-align --command "SELECT 1" > /dev/null'] + interval: 10s + timeout: 5s + retries: 10 + shm_size: 128mb + security_opt: + - no-new-privileges:true + + application: + build: + context: ${BUSYSTACK_BUILD_CONTEXT:?Run python3 scripts/build-image.py --compose ...} + args: + APPLICATION_VERSION: ${BUSYSTACK_BUILD_VERSION:?Run python3 scripts/build-image.py --compose ...} + SOURCE_REVISION: ${BUSYSTACK_SOURCE_REVISION:?Run python3 scripts/build-image.py --compose ...} + image: ${BUSYSTACK_APPLICATION_IMAGE:?Run python3 scripts/build-image.py --compose ...} + restart: unless-stopped + depends_on: + database: + condition: service_healthy + environment: + SERVER_ADDRESS: 0.0.0.0 + SPRING_CONFIG_IMPORT: configtree:/run/secrets/ + SPRING_DATASOURCE_URL: jdbc:postgresql://database:5432/busystack + SPRING_DATASOURCE_USERNAME: busystack + BUSYSTACK_PUBLIC_BASE_URL: ${BUSYSTACK_PUBLIC_BASE_URL:?Set the externally visible HTTPS base URL} + BUSYSTACK_ISSUER: ${BUSYSTACK_ISSUER:?Set the externally visible HTTPS OAuth issuer} + BUSYSTACK_ALLOW_INSECURE_LOCAL: ${BUSYSTACK_ALLOW_INSECURE_LOCAL:-false} + BUSYSTACK_DAV_ALLOW_SYNC_DEPTH_ONE: ${BUSYSTACK_DAV_ALLOW_SYNC_DEPTH_ONE:-true} + BUSYSTACK_DAV_REPAIR_LOCAL_OBSERVANCE_UNTIL: ${BUSYSTACK_DAV_REPAIR_LOCAL_OBSERVANCE_UNTIL:-true} + secrets: + - source: database_password + target: spring.datasource.password + ports: + - '127.0.0.1:${BUSYSTACK_HTTP_PORT:-8080}:8080' + read_only: true + tmpfs: + - /tmp:rw,nosuid,nodev,noexec,size=128m,uid=10001,gid=10001 + cap_drop: + - ALL + security_opt: + - no-new-privileges:true + mem_limit: 1g + healthcheck: + test: + - CMD + - bash + - -ec + - | + authority_and_path="$${BUSYSTACK_PUBLIC_BASE_URL#*://}" + prefix="" + if [[ "$$authority_and_path" == */* ]]; then prefix="/$${authority_and_path#*/}"; fi + exec 3<>/dev/tcp/127.0.0.1/8080 + printf 'GET %s/actuator/health/readiness HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n' "$$prefix" >&3 + IFS= read -r status <&3 + [[ "$$status" == *" 200 "* ]] + interval: 15s + timeout: 5s + start_period: 60s + retries: 5 + +volumes: + database: + +secrets: + database_password: + file: ${BUSYSTACK_DATABASE_PASSWORD_FILE:?Set a private absolute path outside the repository} + postgres_admin_password: + file: ${BUSYSTACK_POSTGRES_ADMIN_PASSWORD_FILE:?Set a private absolute path outside the repository} diff --git a/deployment/.env.example b/deployment/.env.example new file mode 100644 index 0000000..4f30574 --- /dev/null +++ b/deployment/.env.example @@ -0,0 +1,7 @@ +# Supply your real HTTPS hostname and operator-managed certificate. +BUSYSTACK_PUBLIC_BASE_URL=https://calendar.example.org +BUSYSTACK_ISSUER=https://calendar.example.org +BUSYSTACK_DATABASE_PASSWORD_FILE=/absolute/private/path/database-password +BUSYSTACK_POSTGRES_ADMIN_PASSWORD_FILE=/absolute/private/path/postgres-admin-password +BUSYSTACK_HTTP_PORT=8080 +# Set COMPOSE_PROJECT_NAME to an isolated name when exercising a trial/restore. diff --git a/deployment/INSTALL.md b/deployment/INSTALL.md new file mode 100644 index 0000000..c814476 --- /dev/null +++ b/deployment/INSTALL.md @@ -0,0 +1,86 @@ +# Install BusyStack CalDAV Server @VERSION@ + +Supported container platform: Linux AMD64 with Docker Engine and Compose v2 +(2.24.4 or newer). Supply a domain, DNS, a trusted HTTPS certificate/private key +and a reverse proxy on the same host. The download supplies no domain or +production certificate. PostgreSQL 18.6 is included as a private persistent +service. No host Java or Maven is required for this installation. + +Download `caldav-server-@VERSION@-deploy.tar.gz`, `SHA256SUMS` and +`release-manifest.json` from https://github.com/busystack/caldav-server/releases/tag/v@VERSION@. +Verify the downloaded archive against SHA256SUMS before extraction. The release +manifest is authoritative for the source, JAR checksum, tested Linux AMD64 image +registry digest and qualification evidence. Checksums detect corruption; the +immutable GitHub release and its release attestation bind the publisher's assets. + +```sh +sha256sum --check --ignore-missing SHA256SUMS +tar -xzf caldav-server-@VERSION@-deploy.tar.gz +cd caldav-server-@VERSION@-deploy +cp .env.example .env +``` + +Edit `.env` with the actual HTTPS URL/issuer and private absolute secret paths. +Create distinct database secrets as shown in `docs/operations.md` (private parent +directory mode 0700, database files readable by container UIDs). Do not put secrets +in `.env`, command lines or the archive. Keep the administrator bootstrap file +owner-only. Then, from this extracted directory: + +```sh +docker compose config --quiet +docker compose pull +docker compose up -d --no-build --wait +docker compose ps +``` + +Compose pulls the published version **and registry digest** and mounts only the +included initialization script and your explicit secret files. Configure your +proxy using `nginx-root.conf.example` or `nginx-subpath.conf.example`, replacing +the hostname and certificate paths. Validate the proxy configuration before +reload. Do not expose the database or application HTTP port to the network. +For a subpath, set both URLs to the same `/calendar` suffix and preserve that path. + +Follow the included `docs/operations.md` for the exact local administrator +bootstrap command, first HTTPS sign-in, passkey enrollment and MFA, first user, +event/task collections and named DAV credential. Connect the real client to the +account page's displayed HTTPS CalDAV address using its named credential. +The primary account password is not the DAV password. See the included account, +OAuth and optional external-identity guides for those workflows. + +## Updates, backup, restoration and removal + +Back up privately using `bash scripts/backup.sh /private/backup/new.dump`; retain +its `.sha256`, the deployment configuration, image digest and secrets separately. +For restoration, use a new empty database/project with the application stopped, +then `bash scripts/restore.sh /private/backup/new.dump`. The restore procedure +invalidates restored accounts/security credentials before controlled individual +recovery; follow the complete operations guide before starting normal service. +Never restore over a live database. Check the archive checksum first. + +For an update, read the target release/migration notes, back up, stop the +application, replace the bundle with the target release's separately verified +bundle, preserve the intended project/database volume and secret paths, pull and +start. Changing an image does not reverse PostgreSQL migrations. Use the target +release's version and digest rather than `latest`. + +`docker compose down` removes services while retaining database storage. +Only after verifying backups and explicitly deciding to discard this installation, +`docker compose down --volumes` deletes its database. Remove its proxy configuration +and private secrets when no longer needed. Keep backups under your retention policy. + +## JAR alternative + +Download `caldav-server-@VERSION@.jar` and verify its SHA-256. Use official Java 25 +(the selected qualified runtime is Temurin 25.0.4.1+1) and PostgreSQL 18.6. Configure +an application-owned PostgreSQL database/role, file-backed Spring config-tree +secrets, equal canonical HTTPS public URL and issuer, and loopback listener. +Use the operations guide's bootstrap/recovery commands with the downloaded JAR +path in place of `target/caldav-server-@VERSION@.jar`. Run `java -jar +/path/caldav-server-@VERSION@.jar`. A JAR needs a host JDK/runtime and an operator-managed +process; it does not install a service manager or database. Container deployment +above needs neither host Java nor a compiler. + +License: Apache-2.0; retain the included LICENSE, NOTICE, THIRD-PARTY-NOTICES and +third-party license material. Client support is version/configuration scoped; +read `docs/releases/@VERSION@.md` for tested profiles and exclusions. The account UI +is not a calendar editor; no invitations, scheduling, reminders or CardDAV. diff --git a/deployment/nginx-root.conf.example b/deployment/nginx-root.conf.example new file mode 100644 index 0000000..5b50ecd --- /dev/null +++ b/deployment/nginx-root.conf.example @@ -0,0 +1,19 @@ +server { + listen 443 ssl; + server_name calendar.example.org; # OPERATOR: replace domain and supply DNS + ssl_certificate /etc/ssl/caldav/fullchain.pem; # OPERATOR: trusted chain + ssl_certificate_key /etc/ssl/caldav/privkey.pem; # OPERATOR: protect private key + ssl_protocols TLSv1.2 TLSv1.3; + ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305; + gzip off; + client_max_body_size 1m; + proxy_connect_timeout 5s; + proxy_read_timeout 120s; + proxy_set_header Host calendar.example.org; + proxy_set_header Forwarded ""; + proxy_set_header X-Forwarded-Host ""; + proxy_set_header X-Forwarded-Proto ""; + location = /actuator { return 404; } + location ^~ /actuator/ { return 404; } + location / { proxy_pass http://127.0.0.1:8080; } +} diff --git a/deployment/nginx-subpath.conf.example b/deployment/nginx-subpath.conf.example new file mode 100644 index 0000000..62d4de8 --- /dev/null +++ b/deployment/nginx-subpath.conf.example @@ -0,0 +1,27 @@ +server { + listen 443 ssl; + server_name calendar.example.org; # OPERATOR: replace domain and supply DNS + ssl_certificate /etc/ssl/caldav/fullchain.pem; # OPERATOR: trusted chain + ssl_certificate_key /etc/ssl/caldav/privkey.pem; # OPERATOR: protect private key + ssl_protocols TLSv1.2 TLSv1.3; + ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305; + gzip off; + client_max_body_size 1m; + proxy_connect_timeout 5s; + proxy_read_timeout 120s; + proxy_set_header Host calendar.example.org; + proxy_set_header Forwarded ""; + proxy_set_header X-Forwarded-Host ""; + proxy_set_header X-Forwarded-Proto ""; + # Set both public URL and issuer to https://calendar.example.org/calendar + location = /.well-known/caldav { return 308 https://calendar.example.org/calendar/dav/; } + location = /.well-known/oauth-authorization-server/calendar { + proxy_pass http://127.0.0.1:8080/calendar/.well-known/oauth-authorization-server; + } + location = /calendar { return 308 https://calendar.example.org/calendar/; } + location = /actuator { return 404; } + location ^~ /actuator/ { return 404; } + location = /calendar/actuator { return 404; } + location ^~ /calendar/actuator/ { return 404; } + location /calendar/ { proxy_pass http://127.0.0.1:8080; } +} diff --git a/docs/account-security.md b/docs/account-security.md new file mode 100644 index 0000000..f9ab274 --- /dev/null +++ b/docs/account-security.md @@ -0,0 +1,163 @@ +# Account security workflows + +Account management provides **Sessions**, **Recovery**, **Password**, and passkey +settings. These are account controls; calendar and task content remains in CalDAV +clients. Local accounts work without an identity provider or email service. + +## Browser sessions + +Spring Session JDBC **4.1.1**, selected by the Boot **4.1.2** BOM, persists browser +sessions in PostgreSQL. Migration V6 installs its standard PostgreSQL tables; +`spring.session.jdbc.initialize-schema=never` prevents automatic schema creation. +The [module artifact](https://repo.maven.apache.org/maven2/org/springframework/session/spring-session-jdbc/4.1.1/) +contains an Apache-2.0 license; the maintainer resolved the published POM mismatch as an unintended publishing-tool error. See the [specific artifact license review](third-party-licenses.md). The application uses the documented +[JDBC customization interface](https://docs.spring.io/spring-session/reference/configuration/jdbc.html) +for concurrent attribute creation. + +The `JSESSIONID` cookie is Secure and HttpOnly, with SameSite=Lax. Lax permits the +safe top-level redirect back from an upstream OIDC provider. Cookie-authenticated +mutations still require CSRF tokens; relaxing SameSite does not remove that +protection. Sessions expire after thirty minutes of inactivity. Recent primary and +passkey factors expire for sensitive-operation authorization after ten minutes, +even when a browser session remains active. + +`/security/sessions` lists creation, last-access and expiration times, and marks +the current browser. It does not reveal session bearer values. Ending a session +requires recent MFA and only affects an owned session. Disabling an account, +changing its password, or using recovery ends every session and changes the +account's durable security generation. Password and passkey user lookups capture +that generation alongside credential state. A login already checking an older +credential cannot create a valid session after a completed reset. + +The application uses Spring's new-session fixation strategy, creating a new +database session row after authentication. A delayed request holding an earlier +anonymous session must not restore the old cookie identifier. Session validity is +checked against durable account state on requests and sensitive service mutations; +there is no process-local revocation cache. + +## Recovery codes + +After recent primary and passkey verification, `/security/recovery` generates ten +independent high-entropy recovery codes. They are shown once on a response marked +`Cache-Control: no-store`. Store them securely offline. Generating another set +revokes every unused code in the earlier set. Each code expires after one year. +The database stores a SHA-256 verifier of the random credential, not the credential +itself; human passwords use adaptive password hashing instead. + +If a passkey is lost, first complete a recent primary sign-in, then submit one +code at `/security/recovery`. Recovery is an alternative second-factor operation; +it does not replace a password or a trusted upstream sign-in. The code is consumed +atomically with removing passkeys, recording their revocation tombstones, revoking +DAV and OAuth access, and ending all browser sessions. Sign in again and enroll a +new passkey. Unused codes remain available until used, regenerated, expired, or +revoked by a password change or administrative recovery. + +Concurrent use of one code permits one successful reset. A persistence failure +rolls the entire reset back, including code consumption. Expired, wrong-account, +incorrect, used, revoked, and restored-old-epoch codes are rejected. No code is +accepted without current primary authentication and the correct internal account. + +An externally managed account retains its verified issuer/subject mapping. Recovery +does not create a local password or bypass upstream account restrictions. If the +primary credential is also lost, use the controlled administrator-assisted local +operation documented in [operations](operations.md); the public server does not +provide a first-run or password-reset takeover endpoint. + +## Password changes and upgrades + +Local users change their password at `/security/password` after recent MFA. +Passwords need at least fourteen characters and at most seventy-two UTF-8 bytes. +The configured verifier is BCrypt with cost 12. Password changes atomically rotate +the account security generation, invalidate sessions, revoke DAV/OAuth access and +recovery codes, and retain registered passkeys. An externally managed account's +password must be changed at its identity provider. + +Spring's `UserDetailsPasswordService` hook upgrades weaker BCrypt work factors +after successful verification. An upgrade of the same password keeps the account +generation. The update compares the loaded hash and captured generation against +current database state, so a delayed upgrade cannot overwrite a concurrent +password change or revive an older credential. + +## Passkey challenge lifecycle + +Spring Security and WebAuthn4J still generate and verify the actual challenges, +origins, relying-party IDs, signatures and authenticator data. The application adds +a PostgreSQL single-use guard around that verification. When the framework saves +typed request or creation options, the application records a verifier of the exact +challenge, session, purpose, recovery epoch, and available account generation before +writing the HTTP response. A matching assertion consumes the durable guard before +framework verification. Failed attempts also consume it; request fresh options +before retrying. + +Challenges expire after five minutes. A different session, purpose, account +generation, or recovery epoch cannot reuse them. Single-use enforcement does not +depend on synchronizing a Java session object, which would not coordinate separate +JDBC session wrappers or processes. Authentication bodies remain limited to 64 KiB; +encoded bodies are rejected and chunked JSON is bounded while reading. + +## Login protection and audit metadata + +The default policy tracks five account attempts within a five-minute observation +window and thirty attempts per connection peer within one minute. The next attempt +after an allowance is exhausted starts a thirty-second account cooldown or a +sixty-second peer cooldown. A new window begins after cooldown. Attempts during +cooldown do not extend it, successful authentication clears the account counter, +and throttling never disables the account. Counter +updates are transactional and durable across restarts. The counter store is capped +at ten thousand buckets and expires inactive buckets after one hour. + +The peer is the actual servlet connection address. Arbitrary forwarded headers +cannot choose it. Behind a reverse proxy, its connections share that peer allowance; the current +application does not derive peers from forwarded headers. Account throttling still distinguishes normalized account identifiers. +These limits are an operational policy, not a scalability claim. + +Security audit rows contain fixed action/outcome values, an optional internal +account ID, a timestamp, and a verifier of the connection-peer identifier. They do +not contain passwords, recovery codes, tokens, assertion bodies, or calendar +content. Hashing the peer is pseudonymization rather than guaranteed anonymity. +History is pruned to about ten thousand rows with writer-concurrency headroom and +a thirty-day age limit. Scheduled housekeeping also expires throttle buckets and +challenge guards. No telemetry or remote audit service is required. + +## Restart, upgrade and restore + +Normal restarts retain session, credential and revocation state. Restoring older +database state uses the explicit recovery policy in the operations procedure: +clear browser sessions and recovery codes, change the global recovery epoch and +account generations, and revoke other independently issued credentials. Old +backups must never silently revive a previously revoked session or recovery code. + +V6 introduces captured account generations and revokes OAuth families created +before that boundary. Those applications must obtain fresh browser authorization; +calendar ownership, collections, resource bytes and synchronization history are +preserved. Review migration effects before upgrades. Session attribute serialization +is framework-version dependent; a future framework upgrade may require deliberately +ending sessions rather than assuming arbitrary serialization compatibility. + +## Verification status + +`AccountSecurityWorkflowsIT` exercises real PostgreSQL and HTTPS with a disposable +test CA. It covers sessions, ownership, CSRF, fixation races, reset generations, +recovery consumption/concurrency/rollback, password changes and hash upgrades, +challenge guards, login throttling and audit retention. Seeded factor contexts +test application workflows and are explicitly distinct from a real WebAuthn +ceremony. `PasskeyCeremonyGuardTest` verifies boundary and body handling; the +separate browser qualification must verify actual authenticator operations. + +The focused run on 2026-10-27 executed all **19 workflow tests** and **17 unit +tests**, with zero failures, errors, or skips, on Temurin 25.0.4.1, Maven 3.9.12, +Boot 4.1.2, Security 7.1.2, and PostgreSQL 18.6. The command was: + +```sh +./mvnw -B -ntp spotless:apply verify -Dit.test=AccountSecurityWorkflowsIT +``` + +Consult the executed qualification report for final combined results and remaining +checks. These controls have not received an independent security assessment or +certification. + +## DAV credential verifier profile + +Each issued DAV credential contains a cryptographically random 256-bit secret and its credential UUID. The current verifier is versioned as `{dav-sha256-v1}` and hashes a fixed purpose label, UUID and secret using SHA-256. Verification uses a constant-time comparison. A fast verifier is appropriate for this generated entropy; human passwords retain BCrypt cost12. Plaintext secrets are displayed only at issuance and are absent from the database. + +Existing BCrypt DAV verifiers are verified with the established encoder and upgraded by a conditional database update. A concurrent revocation or replacement prevents that upgrade from restoring access. The focused PostgreSQL credential suite covers generated-secret verification, legacy upgrades and the revocation race. No process-local credential cache delays disabling or revocation; already authorized in-flight work follows the documented transaction boundary. diff --git a/docs/adr/0001-protocol-engine.md b/docs/adr/0001-protocol-engine.md new file mode 100644 index 0000000..61417f3 --- /dev/null +++ b/docs/adr/0001-protocol-engine.md @@ -0,0 +1,86 @@ +# ADR 0001: CalDAV engine integration gate + +Status: accepted application-owned DAV adapter; qualification in progress. Checked 2026-10-27. + +## Selected implementation + +Use Spring MVC for the embedded HTTP boundary, the JDK's namespace-aware hardened XML parser/serializer for protocol documents, and iCal4j 4.4.0 for calendaring models, validation, parsing and recurrence generation. Spring Security remains the authentication and authorization foundation. This is an application-owned CalDAV implementation, not a claim that Spring or iCal4j supplies a complete protocol engine. + +The released Bedework servlet runs on the approved Java/Jakarta/Spring runtime. It was not rejected as incompatible. The integration exercise below reproduced incorrect atomic PROPPATCH behavior, URI form decoding and ETag encoding, as well as unconditional unsupported capability advertising. Correcting those handlers would require maintaining a protocol fork alongside a substantial PostgreSQL/backend implementation. Its 3.x iCal4j fork also shares package names with current upstream 4.4.0; the two cannot simply coexist on the application's classpath. The application-owned adapter avoids that combined fork/model boundary while retaining the established calendaring library. Its cost is explicit ownership of the RFC responsibilities below and a larger independent conformance-testing obligation. + +| Application responsibility | Implementation and evidence | +| --- | --- | +| HTTP methods, canonical URIs, conditions and bounded XML | `protocol/DavController`, `DavPaths`, `HttpConditions`, `DavIf`, `DavXml`; `DavHttpIT`, `DavPathsTest`, `DavXmlTest`, `HttpConditionsTest` | +| Discovery, live/dead properties, atomic updates and ACL/principal reports | `DavProperties`, `DavPrincipalReports`, `DavResources`; raw HTTPS tests against PostgreSQL | +| Query filters, recurrence, task ranges, calendar-data projection and free/busy | `calendar/query`, `calendar/recurrence`; semantic/projection tests and independent clients | +| Authoritative resource bytes, UID constraints, conditional mutations and namespace operations | `CalendarStore`, `DavStore`, versioned migrations; PostgreSQL transaction and concurrency tests | +| Snapshot synchronization, deletion history and permission/restore invalidation | Per-collection row-locked revisions plus generation/recovery epochs; `CalendarStoreIT`, `DavStoreIT`, raw REPORT tests | +| Browser/DAV/OAuth boundaries | Spring Security filter chains and persistent application repositories; security HTTP and real browser qualification | + +The initial integrated run exposed Spring MVC's disabled OPTIONS dispatch; enabling dispatch corrected it and the later raw HTTPS suite passed. The selected implementation now includes bounded query/recurrence semantics, standard group ACLs and required principal reports. Actual python-caldav3.6.0 and Thunderbird157.0.1 native calendar/task operations have passed through the application with the explicit sync Depth1 compatibility profile. The [conformance matrix](../conformance.md) and final artifact evidence record the exact qualification boundaries; these results do not retrospectively qualify the rejected Bedework backend. + +## Published candidate + +Bedework's generic server is a real reusable candidate, rather than iCalendar parsing alone. The latest release in [Maven Central metadata](https://repo.maven.apache.org/maven2/org/bedework/bw-caldav-server/maven-metadata.xml) is `org.bedework:bw-caldav-server:6.0.1`, published 2025-07-17. Its [module POM](https://repo.maven.apache.org/maven2/org/bedework/bw-caldav-server/6.0.1/bw-caldav-server-6.0.1.pom) explicitly declares Apache-2.0 and provided `jakarta.servlet-api:6.0.0`. The associated `bw-webdav:6.0.0` module also explicitly declares Apache-2.0. These statements do not establish the licenses of every transitive artifact. + +The upstream [source repository](https://github.com/Bedework/bw-caldav) was at `ae0071112e9107c7d034227f610e4282b629821c` (2026-08-08). Its development POM is `6.1.0-SNAPSHOT`, with `bedework-parent:260-SNAPSHOT`. Development-head sources must not be mistaken for the released artifact. The probe uses release 6.0.1 source and binaries from Central. + +The released module depends on Bedework's fork `org.bedework.ical4j:ical4j:3.0.24`, not current upstream iCal4j 4.x. The fork's [specific published POM](https://repo.maven.apache.org/maven2/org/bedework/ical4j/ical4j/3.0.24/ical4j-3.0.24.pom) declares BSD-3-Clause. Other direct dependencies include Bedework CalDAV utility/XML modules, WebDAV, access controls, utility/calendar/JMX/XML modules, SOAP and synchronization XML bindings, vCard, and JDKIM. The generic server POM does not require the full Bedework calendar database implementation, Hibernate, a broker, or a separate identity service. The complete resolved dependency inventory must nevertheless be reviewed before adoption. + +## Backend boundary and gaps + +`org.bedework.caldav.server.sysinterface.SysIntf` is the backend integration contract. It receives a `jakarta.servlet.http.HttpServletRequest` and account name during initialization. Its calendar objects and methods cover collections, events/tasks, access checks, queries, free/busy, resource conversion, and synchronization. Implementing the contract with application PostgreSQL repositories is structurally possible; this is not evidence that it has been implemented or tested. + +The servlet constructs `SysIntf` by a configured class name and public no-argument constructor, rather than Spring constructor injection. A narrow lifecycle adapter would be needed. Spring Security's request principal is available through the servlet request, so authentication need not be replaced by Bedework authentication. + +Released source has handlers for calendar-query, calendar-multiget, free-busy-query, WebDAV Sync and principal/ACL reports, plus component/property/parameter filtering. `VTODO` appears in component capability and report processing. These observations are source evidence only: they do not qualify task time-range matching, recurrence, lossless storage, conditional requests, or report conformance against a PostgreSQL backend. + +Two concrete integration risks are already visible in release sources: + +* `CaldavBWIntf#getDavHeader` unconditionally adds `calendar-schedule`, `calendar-auto-schedule`, `calendar-default-alarms`, and `calendarserver-sharing` for authenticated users. `WebdavNsIntf#getDavHeader` adds `extended-mkcol`. The BusyStack profile cannot use those defaults without suppressing unimplemented capabilities. +* `WebdavServlet#service` supports `X-HTTP-Method-Override`, and invalidates an existing HTTP session by default unless `preserve-session` is configured. Those behaviors need explicit security-boundary decisions. The servlet also contains optional request/content dumping; private calendar content must never be enabled in production logging. + +`UrlHandler` offers a constructor with a configured prefix/context, but namespace initialization also derives prefixes from the request. Correct public URLs behind a proxy therefore require validation and real tests, not simply passing Host/Forwarded headers through. + +## Alternatives + +Upstream [iCal4j](https://www.ical4j.org/) is a calendaring parser/model/recurrence library, not a complete protocol engine. Apache [Jackrabbit WebDAV](https://jackrabbit.apache.org/jcr/components/jackrabbit-webdav-library.html) is DAV infrastructure, not a turnkey CalDAV server. Either route leaves the complete CalDAV responsibility with this application. + +[Milton pricing](https://milton.io/pricing/) specifically includes CalDAV in its licensed offering. No redistribution decision has been made based on an unrelated Milton community module's license. The exact required CalDAV module and redistribution grant must be established before it can be adopted. + +## Investigation gate and historical findings + +The separate `investigation/protocol` Maven project records narrow runtime compatibility evidence only. It does not qualify a Bedework PostgreSQL backend. The selected adapter is exercised separately through the real application, PostgreSQL and Spring Security. A successful compilation or OPTIONS request cannot satisfy this gate. + +The selected implementation above records the concrete released-handler/model problems and the responsibilities assumed by this project. The remaining investigation notes preserve the evidence behind that choice. + +## Concrete PostgreSQL adapter implications + +At the investigation checkpoint, the initial `CalendarStore` supplied principal-authorized collection discovery, resource reads, serialized writes/deletes, sharing and sync snapshots. The table below records the concrete work a Bedework adoption would have required at that boundary. The selected application-owned adapter has since implemented those protocol responsibilities directly; this table is historical candidate analysis, not a list of current application stubs. + +| Integration responsibility | Work required before adoption | +| --- | --- | +| Principal/collection/event models | Concrete adapters for `AccessPrincipal`, `CalPrincipalInfo`, `CalDAVCollection`, `CalDAVEvent`, `SysiIcalendar`, system/auth properties, and property handlers; stable UUID href mapping | +| Calendar content | Preserve authoritative accepted bytes while supplying parsed models for report projection; `fromIcal`, `toCalendar`, and `writeCalendar` cannot become lossy reconstruction paths | +| Filtering/recurrence/free-busy | `FilterHandler.query` delegates the filter tree and retrieval mode to `SysIntf.getEvents`; the application must implement and test the complete query, task time-range and recurrence semantics | +| Conditional mutations | Retain request conditions in the request-specific adapter and pass them to the serialized storage operation; checking an ETag before starting the mutation transaction is insufficient | +| PROPPATCH | Replace/fix the released handler's atomicity/status behavior; a normal backend `updateCollection` cannot infer the handler's failed property results | +| COPY/MOVE/collection deletion | Add authorized transactional storage operations, destination preconditions, UID collision checks, revision updates, and deletion history; no unqualified rejection of required methods | +| ACLs and reports | Map collection sharing policy into Bedework `Acl`/`CurrentAccess`, enforce reads/writes at storage boundaries, persist supported ACL changes, and implement principal reports without enumeration leaks | +| Synchronization | Adapt each returned resource/deletion from one storage snapshot; avoid fetching a newer live object when serializing an earlier page/token; implement RFC 6578 truncation behavior | +| Runtime/library model | Choose the released Bedework iCal4j 3 fork or prove a compatible replacement; upstream iCal4j 4 has overlapping `net.fortuna.ical4j` packages and cannot simply be added alongside the fork | +| Transaction completion | Ensure errors roll back before committing; ensure committed status/headers cannot be sent before a database commit failure is known | + +This is substantial backend and integration work, rather than a servlet registration exercise. Unsupported scheduling, notifications, generic files, and proprietary sharing extensions must be removed from advertised/registered behavior. That exclusion does not permit stubs for required calendar-access methods. + +## Executed embedded HTTP evidence + +The [probe source](../../investigation/protocol/src/test/java/org/busystack/caldav/investigation/BedeworkHttpTest.java) executed six tests with no failures, errors, or skips. [Recorded command and environment](https://github.com/busystack/caldav-server/blob/f3ba60d360117c44b3dfeb6dc9848778a98ef06a/investigation/protocol/evidence/runtime-result.txt) distinguish compatibility checks from expected-defect reproductions. These fixtures do not qualify PostgreSQL or real calendar semantics. + +* Spring-authenticated OPTIONS and component PROPFIND work through the real released servlet in Tomcat 11.0.27 / Servlet 6.1 on Boot 4.1.2, Security 7.1.2 and Java 25. +* Empty `VTODO` calendar-query and mandatory `DAV:expand-property` return 207. Calendar-query initially failed 500 because a JAXB implementation was absent; adding Boot-managed `org.glassfish.jaxb:jaxb-runtime:4.0.9` resolved it. +* Mixed PROPPATCH of writable displayname and protected getetag invokes the successful setter and then backend `updateCollection`, despite the protected-property failure. No backend rollback occurs, and the successful property's required 424 dependency status is absent. This is a concrete handler defect to correct before adoption. +* A PUT fixture returning a quoted backend ETag `"probe-etag"` produces the HTTP header `%22probe-etag%22`. `WebdavNsIntf.putContent` applies form URL encoding to the header. A production integration must fix validator serialization as well as ensure byte-preserving semantics. +* A calendar-multiget href with literal `+` reaches backend collection lookup with a space. `CaldavReportMethod` applies form decoding to the href. URI handling must preserve literal plus and validate percent encoding before the engine can satisfy BusyStack's path contract. + +These reproductions establish released-handler corrections that a Bedework integration would require. They do not establish a Java/Jakarta/Spring incompatibility or a complete-engine qualification. The application-owned adapter decision above supersedes the earlier investigation-only status. diff --git a/docs/adr/0002-native-oauth-refresh.md b/docs/adr/0002-native-oauth-refresh.md new file mode 100644 index 0000000..0719903 --- /dev/null +++ b/docs/adr/0002-native-oauth-refresh.md @@ -0,0 +1,153 @@ +# ADR 0002: Public native clients need an application-owned refresh lifecycle + +Date: 2026-10-27. Status: framework risk confirmed; persistent application implementation added; qualification in progress. + +## Verified baseline + +Maven Central publishes Spring Boot **4.1.2**, the newest stable 4.x release in the +metadata checked on this date; 4.2.0-RC1 is a prerelease. Its dependency BOM selects +Spring Security **7.1.2**, Spring Framework **7.0.10**, Tomcat **11.0.27**, PostgreSQL +JDBC **42.7.14**, Flyway **12.4.0**, and Testcontainers **2.0.5**. No independent +Spring Security version override is needed. Boot's documented Java range includes +Java 25, and its embedded Tomcat implements Jakarta Servlet 6.1. + +Primary sources: + +- [Published Boot versions](https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-dependencies/maven-metadata.xml) +- [Exact Boot 4.1.2 BOM](https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-dependencies/4.1.2/spring-boot-dependencies-4.1.2.pom) +- [Boot system requirements](https://docs.spring.io/spring-boot/system-requirements.html) +- [Security 7.1.2 source and Apache-2.0 license](https://github.com/spring-projects/spring-security/tree/7.1.2) + +## Findings from the pinned implementation + +Registering `authorization_code`, `refresh_token`, public client authentication +`none`, mandatory PKCE, and `reuseRefreshTokens(false)` does **not** establish a +working public-client refresh lifecycle: + +1. `OAuth2RefreshTokenGenerator.generate` returns null for a public client's + authorization-code exchange. `OAuth2AuthorizationCodeAuthenticationProvider` + permits a custom generator to change that behavior. +2. `PublicClientAuthenticationConverter` only handles a PKCE code-exchange + request. `PublicClientAuthenticationProvider` calls the required code-verifier + authenticator. The stock pair cannot authenticate a subsequent public refresh + request without a code verifier; a fake embedded client secret is inappropriate. +3. With rotation enabled, `OAuth2RefreshTokenAuthenticationProvider` replaces the + authorization's single current refresh token and calls `save`. It does not + retain a spent-token family ledger. +4. `JdbcOAuth2AuthorizationService` finds tokens using ordinary `SELECT` and + updates the authorization by ID. It does not supply a row lock spanning + validation and rotation, an optimistic version check, or spent-token history. + A transaction around `save` alone cannot serialize the preceding check. + +These are source findings, not a claim that Spring Security is unsuitable. +The relevant extension points are supported framework APIs: + +- `OAuth2TokenGenerator` for issuance. +- `OAuth2ClientAuthenticationConfigurer.authenticationConverter` and + `authenticationProvider` for public refresh requests. +- `OAuth2TokenEndpointConfigurer.authenticationProviders` for wrapping/replacing + grant processing at the transaction boundary. +- `OAuth2AuthorizationService` for persistent grant state. + +References to the exact inspected version: + +- [Refresh generator](https://github.com/spring-projects/spring-security/blob/7.1.2/oauth2/oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/token/OAuth2RefreshTokenGenerator.java) +- [Public-client request converter](https://github.com/spring-projects/spring-security/blob/7.1.2/oauth2/oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/web/authentication/PublicClientAuthenticationConverter.java) +- [Public-client provider](https://github.com/spring-projects/spring-security/blob/7.1.2/oauth2/oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/authentication/PublicClientAuthenticationProvider.java) +- [Code exchange provider](https://github.com/spring-projects/spring-security/blob/7.1.2/oauth2/oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/authentication/OAuth2AuthorizationCodeAuthenticationProvider.java) +- [Refresh provider](https://github.com/spring-projects/spring-security/blob/7.1.2/oauth2/oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/authentication/OAuth2RefreshTokenAuthenticationProvider.java) +- [JDBC authorization service](https://github.com/spring-projects/spring-security/blob/7.1.2/oauth2/oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/JdbcOAuth2AuthorizationService.java) +- [Client authentication extension points](https://github.com/spring-projects/spring-security/blob/7.1.2/config/src/main/java/org/springframework/security/config/annotation/web/configurers/oauth2/server/authorization/OAuth2ClientAuthenticationConfigurer.java) +- [Protocol endpoint customization](https://docs.spring.io/spring-security/reference/servlet/oauth2/authorization-server/protocol-endpoints.html) +- [Maintainer confirmation, issue 18793](https://github.com/spring-projects/spring-security/issues/18793) +- [Earlier customization discussion, issue 1430](https://github.com/spring-attic/spring-authorization-server/issues/1430) + +## Decision + +Keep public native clients public. Use Spring Security's authorization server, +PKCE validation, endpoint filters, and token generation contracts. A production +refresh implementation must add persistent token-family state and serialize +refresh validation, token consumption, successor creation, and grant update in a +single PostgreSQL transaction. Reuse of a spent token must durably revoke its +family, including issued access credentials according to the selected token model. +That revocation must commit even though the HTTP response is `invalid_grant`. +The account's enabled state and grant revocation must be checked inside that same +mutation boundary. Concurrent use of one refresh token must never yield two valid +successor chains. + +Select opaque reference access tokens for the application. JDBC authorization +state and the refresh-family ledger permit revocation to apply on the next DAV +authentication without relying on access-token signing keys. Boot's default +startup-generated JWT/JWK auto-configuration is excluded. The isolated probe also +uses opaque tokens, but its test-only extensions lack replay protection and must +not be copied into production. + +## Application implementation + +`OAuthConfiguration` uses Spring's JDBC registration, authorization, and consent +stores, with PostgreSQL-specific schema types from migrations V2 and V3. The +application registers public native clients through a protected administration +page and requires S256, consent, and recent primary plus WebAuthn authentication. Primary +authentication is a local password or the configured, freshly verified upstream +OIDC login; arbitrary upstream tokens are not accepted for DAV. +Unused grants, response modes other than query, introspection metadata, signing-key, mTLS, and DPoP advertisements +are removed from the selected profile. + +`FrameworkGrantBoundary` wraps the framework code, refresh, and revocation +providers. `RefreshFamilyStore` retains hashed spent tokens and acquires a +PostgreSQL per-client transaction lock with registration recheck, then a +per-authorization advisory transaction lock before account/family/grant mutation. +V7 enforces public-client-ID uniqueness and registered-client foreign keys. A code +exchange, refresh, revocation, reset and registration removal follow compatible +lock ordering; removing a client cannot leave a newly committed descendant grant. Family registration joins the code-exchange transaction; refresh executes +its validation, consumption, framework save, and successor registration together. +Replay revocation commits before `invalid_grant` is returned, even if a caller +subsequently rolls back an outer transaction. + +`OAuthDavIntrospector` checks the current opaque token, issuer, DAV audience, +scope, account enabled state, recovery epoch, and family validity against durable +state on every DAV authentication. `OAuthClientService` and `OAuthGrantService` +enforce current account privileges and recent MFA for management mutations; +server-rendered pages retain browser CSRF protection. + +## Evidence and outstanding acceptance + +The executable investigation is [`investigation/oauth`](../../investigation/oauth/README.md). +It sends actual HTTP requests through embedded Tomcat and Spring Security, including +form login with CSRF and a session cookie. It exercises public S256 code exchange, +incorrect verifier, code replay, stock refresh rejection, supported extension +issuance/rotation, and the missing family-revocation control. + +The isolated investigation passed four tests; its recorded result is +[`runtime-result.txt`](https://github.com/busystack/caldav-server/blob/f3ba60d360117c44b3dfeb6dc9848778a98ef06a/investigation/oauth/evidence/runtime-result.txt). +`RefreshFamilyStoreIT` and `OAuthHttpIT` separately exercise the application against +real PostgreSQL. The latter seeds completed interactive grants for most token +flows, explicitly distinguishing token/DAV qualification from browser/passkey +qualification. Its real password-only browser test must deny authorization. +The full application run on 2026-10-27 executed `OAuthHttpIT` **16/16** and +`RefreshFamilyStoreIT` **9/9**, as part of **84 unit +114 integration tests**, with +zero failures, errors, or skips and no SpotBugs findings. It exercised real +PostgreSQL 18.6 and embedded Tomcat. The full build command was: + +```sh +./mvnw -B -ntp spotless:apply clean verify +``` + +Additional cases prove client-removal races, a consistent authorization/client +read snapshot, unsupported authentication rejection, account-reset/revocation +lock order and exact WebAuthn authentication serialization through a restricted +Spring-provided Jackson module. Follow-up protocol corrections have separate +focused reports; these counts describe the full-run baseline. + +That run used Temurin Java 25.0.4.1 and Maven 3.9.12. The PostgreSQL fixture is +pinned to `postgres:18.6@sha256:edb0c08dc33befd0262ab3b5cf1c4f3f8cfd8b09e88c05b45adee29d6761ddb6`. +The per-class Failsafe reports, rather than the presence of test source, are the +execution evidence. Consult the overall final report for other suites in the +same command and any later changes. + +Separate trusted-CA Chromium/CTAP2 browser profiles exercise actual passkey/MFA, +local consent, S256 exchange, refresh and revocation. Real HTTPS Keycloak tests +cover the optional upstream mode. Neither the framework probe nor seeded grants +qualify a physical authenticator or an independent native OAuth client; the +independent python-caldav and Thunderbird profiles use DAV credentials. Final +artifact, browser/proxy, recovery and restore evidence is recorded separately. diff --git a/docs/adr/0003-reproducible-packaging.md b/docs/adr/0003-reproducible-packaging.md new file mode 100644 index 0000000..cf148de --- /dev/null +++ b/docs/adr/0003-reproducible-packaging.md @@ -0,0 +1,38 @@ +# ADR 0003: Fix the build JVM timezone for reproducible executable archives + +Date: 2026-10-27. Status: accepted; isolated configuration and final host/container artifact equality verified. + +## Evidence + +The same application source produced host and Docker executable JARs with identical entry names, order, content bytes, compression methods, permissions and ordinary ZIP timestamps. Their SHA-256 values differed because 283 `BOOT-INF/classes` entries contained different NTFS timestamp extra fields. Decoding the first field gave `2026-10-27T07:00:00Z` on the Vancouver host and `2026-10-27T00:00:00Z` in the UTC container. Both ordinary ZIP timestamps were `2026-10-27T00:00:00`. The plain, pre-repackage JAR had no extra fields. + +This is a build-time timezone leak, rather than a difference in calendar data or Java compilation. The pinned Spring Boot 4.1.2 repackager transforms application entries and copies their file-time metadata. Its archive writer normalizes the ordinary timestamp using the configured output timestamp; the NTFS metadata still reflects the build JVM timezone. The relevant implementation is in `Packager.RepackagingEntryTransformer` and `JarWriter` in the published [Boot loader-tools 4.1.2 source archive](https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-loader-tools/4.1.2/spring-boot-loader-tools-4.1.2-sources.jar). + +## Decision + +Retain the pinned Spring framework and packaging plugin versions. Keep the fixed `project.build.outputTimestamp`, and configure the Maven JVM through the supported project file `.mvn/jvm.config`: + +```text +-Duser.timezone=UTC +``` + +[Maven documents this file as project JVM configuration](https://maven.apache.org/configure.html#a.mvn.2Fjvm.config_file.3A). [Boot documents `outputTimestamp` for executable packaging](https://docs.spring.io/spring-boot/maven-plugin/packaging.html#packaging.repackage-goal.parameter-details.outputTimestamp). These settings define the build environment. They do not convert stored iCalendar values, floating dates, application runtime configuration or user time zones. + +No custom ZIP rewriting, timestamp-stripping code or framework dependency override is introduced. + +## Verification + +An isolated minimal application used Java 25.0.4.1, Maven 3.9.12 and Boot 4.1.2, with the same fixed output timestamp. Maven JAR Plugin 3.4.2 and the current stable 3.5.1 both exhibited the same Vancouver/UTC difference. Upgrading the JAR plugin alone therefore did not fix the Boot repackage output. Apache's [plugin listing](https://maven.apache.org/plugins/) and the [published 3.5.1 POM](https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-jar-plugin/3.5.1/maven-jar-plugin-3.5.1.pom) establish that the latter was a stable release; no beta was adopted. + +The isolated executable JAR results were: + +| JAR plugin | JVM timezone | SHA-256 | +| --- | --- | --- | +|3.4.2|America/Vancouver|`b8e5711424355b62fc760bd4063d41c47acbbb82a3e087441cb84b120d63b1ce`| +|3.4.2|UTC|`1f08affbeae435d0190bf973ff75a281a11fdea12425780218a64464f54ea899`| +|3.5.1|America/Vancouver|`04f22edb900d44b8e6ce6f15726ce42bdc99113ede19d34743bb46cbe59fa037`| +|3.5.1|UTC|`402a3eb72cee56065da0137882a9537109477f8a5e7c38941285bbf54b1e6801`| + +A clean Plugin 3.4.2 rebuild under host `TZ=America/Vancouver`, with no timezone in `MAVEN_OPTS` and only the project JVM file above, exactly reproduced the UTC row's bytes and SHA-256. Plugin versions are compared within their own rows: their manifest metadata naturally differs across versions. + +The final application executable JAR built on the Vancouver host with the project JVM configuration is byte-for-byte identical to `/opt/busystack/application.jar` extracted from independently built image `sha256:461ca83649a45d8ca37ba1570584f567c0bbc95279b14a6662edfc9ce6987e89`. Both artifacts have SHA-256 `8c043227a66100baddafbcf8cf3b0beac748cc69d74a1811359bac07d05db4a6`; all 513 ZIP entries and the complete archive bytes match. This verifies the pinned build in those two Linux environments, following Maven's [independent-environment verification guidance](https://maven.apache.org/guides/mini/guide-reproducible-builds.html). diff --git a/docs/adr/0004-resumable-synchronization.md b/docs/adr/0004-resumable-synchronization.md new file mode 100644 index 0000000..939aa67 --- /dev/null +++ b/docs/adr/0004-resumable-synchronization.md @@ -0,0 +1,23 @@ +# ADR 0004: bounded synchronization with immutable revision history + +Status: accepted for the completion pass. The algorithm is specified before its implementation. + +RFC 6578 sections 3.6 and 3.7 require a truncated 207 multistatus to include a 507 response for the request collection and a token representing exactly the partial state delivered. A final revision alone cannot describe an incomplete initial listing. Nor is keyset paging through mutable current resource rows sufficient: a resource can disappear or move past a traversal boundary while requests are separated in time. + +The collection row continues to serialize mutation revision allocation through transaction commit. V10 adds immutable resource versions and tombstones indexed by collection, name, and revision. The migration backfills current resources; existing change history remains usable as the set of names changed since an old stable token. Database triggers capture every resource insert/revision update and every deletion change, including the existing COPY/MOVE/property paths. Historical content is never reconstructed from live resource rows. Historical dead properties are separate indexed rows keyed to the immutable version, not one aggregate XML/JSON value: names-only queries never fetch XML, and one selected property never decompresses unrelated values. The deferred COPY fill only populates an empty final current-version property snapshot; later mutations cannot rewrite an older snapshot. + +A traversal freezes an upper revision watermark W in its first repeatable-read request. An initial traversal selects the latest nondeleted version of every name at or before W. An incremental traversal selects the latest version of each changed name in (after,W], including tombstones. Both traverse names in PostgreSQL's explicit C collation with a strict keyset boundary. Mutations above W cannot change the frozen traversal; the final stable token is W, so those mutations are returned by the next incremental traversal. Delete/recreate is represented by the latest version at W, and each member URL occurs once per traversal. + +A truncated page returns an opaque durable random UUID continuation referencing its original lower revision, W, initial/incremental mode, and last accepted name. Continuations are immutable: retrying an earlier token cannot advance or consume it. Each lookup checks the current enabled account, collection authorization, generation and recovery epoch, actor binding, expiry, and history-retention boundary. Continuing after a restart uses the same PostgreSQL state. Continuations are not credentials and do not confer access. + +Persisting the final cursor uses a short Spring `REQUIRES_NEW`, read-committed transaction. PostgreSQL foreign-key parent checks can otherwise reject an insert in the report's older repeatable-read snapshot after an ordinary concurrent collection revision update, even though the referenced key is unchanged. This was reproduced by the writer-during-page-construction regression; no uncertain transaction is blindly retried. The helper rechecks current enabled state, effective access, generation, recovery epoch, revision and retention floor, then inserts under short parent key-share locks. Calendar data remains read from the original watermark. An outer serialization/response failure can leave an unused cursor, which confers no access and expires normally. The existing four DAV-work slots and ten-connection pool leave room for four report connections plus four nested cursor writes; pool acquisition/transaction failures fail the request without a success token. Cursor persistence has its own ten-second transaction/statement deadline: waiting behind a locked parent cannot indefinitely consume the servlet and DAV-work slot. Expected query/transaction deadline failures return HTTP 503 with `Retry-After: 1`, without a sync token; no automatic transaction retry occurs. A real HTTP regression first reproduced the unbounded wait and now requires bounded failure, no persisted cursor, slot release, and a successful subsequent report after releasing the writer. + +Metadata selection does not retrieve body columns and keyset candidates are fetched in batches of at most 128 rows. Content-bearing selection retrieves at most one individually bounded resource at a time. A separate 4 MiB retained-body work budget and 4 MiB metadata estimate prevent a small calendar-data projection from retaining arbitrarily many large unprojected resources. The protocol supplies a function measuring the serialized selected response for each item, including escaped XML, requested property overhead and member URL. A row is accepted only if it fits the page byte budget; reserved document/collection-truncation/token overhead is charged outside that budget. A first item that cannot fit fails explicitly without a successful token. A page either advances at least one member or is complete; no no-progress continuation is issued. Both the client-requested result count and server-selected response byte budget can truncate. A client `DAV:nresults` is a maximum, so a valid larger positive integer is capped at the server's 10,000-member page limit without integer overflow; it is not rejected merely for exceeding that ceiling. Zero still cannot produce a usable page and fails explicitly without a token. A changed member always carries a successful `DAV:propstat`, including an empty `DAV:prop` when no properties were selected. + +Continuation state expires after 24 hours. Independent scheduled read-committed maintenance prunes at most 1,024 expired rows per minute with `FOR UPDATE SKIP LOCKED`; expiration checks do not depend on cleanup. Maintenance never deletes inside a repeatable-read report, avoiding a concurrent-cleanup serialization race. Historical versions follow the existing explicit history-pruning policy: keep the newest version at or before the retention floor for initial listing, plus all versions above it. Pruning invalidates any older continuation. Restoration clears continuation state and rotates the global recovery and account security epochs according to the existing fail-closed recovery policy. Historical calendar bodies are part of PostgreSQL backup and require the same privacy protection as current bodies. All application instances must stop during the V10 history backfill and upgrade; this is not a rolling migration contract. + +The cost is retained historical body storage, rather than whole-collection heap allocation or a database snapshot held open across HTTP requests. There is no total collection quota introduced to avoid paging. Expired or pruned traversals receive DAV:valid-sync-token and must restart; excessive individual responses receive the existing explicit limit error. The standard depth-zero contract and the separately documented narrow default depth-one accommodation are unchanged by the cursor algorithm. + +Primary reference: [RFC 6578](https://www.rfc-editor.org/rfc/rfc6578.html#section-3.6). + +Regression establishment: the original `SyncPagingIT.initialSmallPageReturnsContinuationRatherThanRejectingAcceptedCollection` and `initialSyncExceedsFormerRowAndBodyThresholdsWithoutMissingSubsequentChanges` both failed with `StoreException.Kind.LIMIT` against an isolated exact `495989d` source archive before correction (2026-10-27). The expanded current suite additionally covers frozen initial/incremental views, retries, delete/recreate, Unicode, metadata and response-work limits, expiry/retention/access/recovery/disable invalidation, durable state, property history, COPY, and forward migration from V9. diff --git a/docs/calendar-semantics.md b/docs/calendar-semantics.md new file mode 100644 index 0000000..ef424a2 --- /dev/null +++ b/docs/calendar-semantics.md @@ -0,0 +1,23 @@ +# Calendar semantics and bounded work + +The [independent-audit correction record](standards-corrections.md) supersedes the earlier blanket validation claims. The later IV-CAL-01 follow-up checks original BYDAY ordinals, RFC duration alternatives and the exact ASCII initial calendar structure before typed normalization. Shared duration checks include relative triggers and duration-ended periods; valid ASCII structural case and the supported leading BOM affect only the derived reader, never authoritative bytes. `CalendarConstraints` enforces component and cross-property requirements at the write boundary, and `RecurrenceRuleSyntax` checks original RRULE parts before library normalization can discard duplicates. No event series is expanded for write validation. The parsing input adapter unfolds octets before strict UTF-8 decoding, including folds inside a multibyte character, and canonicalizes only structural identifier spelling in a derived reader. Already-valid input octets, case-sensitive identifiers and values remain unchanged. The default [local observance UNTIL input exception](observance-until-compatibility.md) runs before strict validation and makes corrected bytes authoritative for that narrow case; disabling it restores rejection. These corrections do not modify the upstream-derived parser or timezone-builder sources. + +`ObservanceZoneRules` constructs chronological rules from the submitted observances. Finite eras and explicit dates determine the final regime; an unbounded annual tail is admitted only after checking its ordering and offset continuity across the Gregorian cycle. Today's active STANDARD/DAYLIGHT pair is not used to select future rules. Unproven complex shapes retain explicit coverage limits and fail beyond coverage instead of returning guessed instants. List-TEXT filters use decoded values once, while URI and other non-TEXT properties retain their own representation. + +Previously accepted invalid objects are not migrated, deleted, normalized or hidden. Owners can enumerate metadata with PROPFIND or metadata-only sync, retrieve exact bytes with GET, then conditionally replace the same UID using its ETag or delete it. New writes and transfers are checked before mutation. Reports encountering invalid legacy data fail explicitly rather than silently omit members; correct the reported objects through ordinary authorized operations. Keep a backup of the original bytes during remediation. + +Non-Gregorian RSCALE execution remains unadvertised. An unsupported RSCALE is rejected with `CALDAV:supported-rscale`, following RFC 7529 §10's recommendation rather than accidentally executing a Gregorian interpretation. Legal leap-second `60` values remain a documented narrow limitation: the current temporal model rejects them instead of providing RFC 5545 §3.3.5's recommended `59` fallback. This is a SHOULD-level exception, not a claim of full leap-second support or a reason to rewrite stored bytes. The original leap-second/ordinary-second controls remain in the corpus and correction matrix. + +Complete accepted UTF-8 resource bytes remain authoritative. Every TZID parameter must have a matching embedded VTIMEZONE, including built-in IANA identifiers, as required by RFC4791 section4.1; the server does not silently supply missing definitions. Ordinary GET serves those bytes. Calendar-data without selectors retains ordinary source spelling and folding; if folding splits a UTF-8 code point, the XML representation unfolds that sequence before decoding so semantic characters are preserved without replacement characters. It does not rewrite persistence or change the GET validator. Derived iCal4j models are used for validation and queries. Report copies retain original lexical DURATION, RDATE PERIOD and TRIGGER values: P1D and PT24H have different meanings across a daylight-saving change. Partial projections preserve those distinctions and property parameters. Expanded reports are derived representations. Explicitly typed extension DATE-TIME/PERIOD values with TZID are converted to UTC while retaining extension parameters; an untyped timezone-qualified extension that cannot be interpreted fails explicitly. Expanded UTC date properties preserve extension parameters, and alarm trigger instances become equivalent absolute UTC values so nominal-day offsets and repeated alarms retain their original instants after timezone conversion. + +Calendar-query uses typed component, property, parameter, text and time-range filters. The required `i;ascii-casemap` and `i;octet` collations are supported; unsupported collations fail explicitly. DATE, floating, UTC and TZID-qualified values remain distinct. Embedded VTIMEZONE definitions and report/collection floating-timezone settings are used without fetching external TZURL data. Component and enum names are case insensitive; TEXT matching follows its declared collation. + +Recurrence generation uses iCal4j 4.4.0 RRULE operations, with application handling for RDATE/EXDATE, detached replacements and RANGE=THISANDFUTURE. Verified [RFC5545 erratum4271](https://www.rfc-editor.org/errata/eid4271) applies section3.3.5 to nonexistent local recurrence times: the pre-gap UTC offset determines their instant, and they consume COUNT. Invalid calendar dates remain omitted without consuming COUNT. Calendar durations apply nominal days before elapsed clock units; explicit DTEND/DUE retains exact elapsed duration. RFC 4791 section 9.9 task time-range rows, including undated, due-only, completed-only and created/completed tasks, have separate semantics from event overlap. Relative and repeated stored alarms are considered in query matching, including triggers outside the parent event interval; no alarm is executed. Free/busy excludes cancelled and transparent events, preserves tentative types and merges only equal busy types. + +One query/projection/free-busy instance carries the cumulative budget for a REPORT across its resources. Defaults bound output to 10,000 instances, estimated candidate work to 500,000, RRULE increments to 900 and recurrence search spans to 3,660 days. RRULE candidate work is checked before library expansion; the increment bound is below the pinned library's internal 1,000-increment stopping threshold, so that threshold cannot silently produce a successful partial answer. COUNT rules are evaluated from their original anchor through bounded library windows: small expired series do not scan every intervening year, while genuinely large histories can exceed the work bound. Open-ended negative answers beyond the bounded horizon fail rather than omit possible future matches. Limits produce explicit protocol errors through the HTTP adapter. Expanded component and repeated-alarm copies reserve a conservative cumulative 16MiB report-size estimate before cloning; separate projection input/output limits also run before copying, preventing a large recurring property from multiplying into an unbounded in-memory report. + +Input is limited to 1 MiB, with separate component/property/parameter/nesting bounds. Calendar text rejects disallowed C0 controls, DEL, isolated UTF-16 surrogates and U+FFFE/U+FFFF, so accepted resources can be represented in XML 1.0 calendar-data. TAB, folded lines, CRLF and valid Unicode remain accepted. This is a documented text security profile; it does not claim every possible UTF-8 noncharacter is accepted. Timezone observance recurrences are bounded before application rule construction. The active `BoundedTimeZoneRegistry` implements the supported registry interface and uses its registry-local ZoneId pool; the former unused upstream-derived builder has been removed. `ObservanceZoneRules` preserves chronological explicit RDATE transitions and finite UNTIL/COUNT histories. Annual future coverage requires a proof of the eventual transition cycle. Unproven unbounded shapes receive a finite materialization horizon and explicit coverage metadata; temporal queries and expansion beyond proven coverage fail. Ordinary reads continue to preserve those resources. Historical provenance and the current supported-interface integration are recorded in `ical4j-timezone-patch.md`. + +Evidence: `CalendarAuditCorrectionsTest`, `CalendarAuditAtomicityIT`, `CalendarSemanticsTest`, `CalendarFreeBusySourcesTest`, `CalendarParameterSemanticsTest`, `CalendarEdgeSemanticsTest`, `CalendarTextProfileTest`, `CalendarExpansionSemanticsTest`, `CalendarTimezoneRdateTest`, `CalendarTimezoneCoverageTest`, `CalendarDuplicateRecurrenceTest`, `CalendarTimezoneRequirementTest`, `CalendarDataProjectionTest`, and `ThunderbirdCalendarFixturesTest`. The Thunderbird fixtures preserve real native-provider output from qualification; their provenance is in `src/test/resources/calendar/clients/README.md`. HTTP/client evidence is maintained separately in the conformance and interoperability matrices. + +Normative references: [RFC 4791](https://www.rfc-editor.org/rfc/rfc4791.html), particularly sections 7.3, 7.5, 7.10 and 9.9; [RFC 5545](https://www.rfc-editor.org/rfc/rfc5545.html), particularly sections 3.3.6, 3.8.4.4 and 3.8.5.3; [RFC 6868](https://www.rfc-editor.org/rfc/rfc6868.html). The official unmodified parser and narrowly scoped application-owned parameter adapter are documented in `ical4j-parser-patch.md`; former copied implementations remain only in Git history. diff --git a/docs/conformance.md b/docs/conformance.md new file mode 100644 index 0000000..c74523c --- /dev/null +++ b/docs/conformance.md @@ -0,0 +1,89 @@ +# Standards and acceptance evidence + +The independent audit of `923ecacc6b35a77be15b6cdcde5e1b5f2b9f76f0` found 17 implementation defect groups despite the historical qualification below. The maintained [correction submission](standards-corrections.md) records each finding, its preserved baseline, implementation, verification and remaining limits. Its [derived 309-row disposition matrix](https://github.com/busystack/caldav-server/blob/f3ba60d360117c44b3dfeb6dc9848778a98ef06a/qualification/evidence/standards-corrections/matrix-dispositions.csv) retains the original assessments separately from developer evidence. Historical green runs do not verify these later corrections. This submission is for independent re-verification, not independent auditor approval. + +The concise table below records the intended capability profile and historical self-assessment references. Current clause-level dispositions and fresh evidence are maintained in the independent-audit correction submission and its derived309-row matrix; historical test references do not establish the corrected revision's conformance. It is an engineering self-assessment, not an independent conformance certification. Source `47b3543` passed a clean build with 169 unit and 174 PostgreSQL/HTTPS integration tests, zero failures/errors/skips and zero SpotBugs findings. Its [actual CI evidence](https://github.com/busystack/caldav-server/blob/f3ba60d360117c44b3dfeb6dc9848778a98ef06a/qualification/evidence/correction-ci-final.json) also records source/JAR/container license delivery, both independent clients under the default configuration, explicit strict mode, all six packaged browser profiles, same-version/V9 recovery and separate Java/OS advisory checks. Additional local measurements and restoration records are in the maintained [qualification index](https://github.com/busystack/caldav-server/blob/f3ba60d360117c44b3dfeb6dc9848778a98ef06a/qualification/evidence/README.md). Historical checkpoint records remain unchanged and do not qualify later source. + +The current project license is Apache License 2.0. [Distribution review and actual artifact inspection](third-party-licenses.md) record the separate upstream licenses, resolved Spring Session metadata error and source/JAR/container notice obligations. License selection and an unrequested external certification are not completion blockers. + +The primary RFCs, their verified errata and relevant update boundaries are recorded in [standards review](standards-review.md). The application owns the protocol responsibilities described in [ADR 0001](adr/0001-protocol-engine.md); Spring Security and iCal4j are not advertised as turnkey CalDAV engines. + +## HTTP, DAV and calendar access + +| Obligation and reference | Classification | Implementation and executable evidence | +| --- | --- | --- | +| Stable resource identity, collection membership and canonical URI handling — RFC4918 §§5,8; RFC4791 §4 | Mandatory | `DavPaths`, `DavResources`, durable account/collection UUIDs and V8 bindings. `DavPathsTest`, `DavStoreIT.namespacesHandleUnicodePercentSignsAndMoveWithoutChangingCollectionIdentity`, and `DavHttpIT.acceptedOctetsStrongValidatorsUnicodeAndLiteralPlusRoundTrip` cover Unicode, literal plus, percent escapes, membership and accepted bytes. Username changes retain ownership (`CalendarStoreIT`). | +| OPTIONS, GET, HEAD, PUT, DELETE — RFC9110; RFC4918 §9; RFC4791 §5 | Mandatory by resource type | `DavController`, exact servlet/security method allowlist, `HttpConditions`. Real HTTPS `DavHttpIT` exercises supported methods, calendar/task CRUD, collection GET/HEAD, component and UID restrictions, rejected-write rollback and per-resource errors. OPTIONS dispatch is enabled and tested through the real container. | +| MKCOL, MKCALENDAR, COPY and MOVE — RFC4918 §§9.3,9.8–9.9; RFC4791 §5.3 | Mandatory where applicable | Calendar-home folders and calendar collections use `DavStore` transactional namespace operations; destination/overwrite/source conditions and UID/component collisions are enforced. `DavHttpIT.copyMoveResourcesAndCollectionsRespectCollisionsAndSyncHistory`, collection creation preconditions, and `DavStoreIT` cases cover deep/depth-zero copies, recursive deletion, sharing exclusion on copies and rollback. The shared `DavNamespace` invariant counts at most 20 segments after `/dav/`, including `calendars`, the stable home UUID and the resource leaf, and 4,096 canonical percent-encoded path bytes including separators. Calendar admission reserves both a leaf segment and enough encoded space for every allowed 255-unit resource name; deployment prefixes have a separate boundary. Transfers preflight every resulting descendant and overwrite target under mutation locks, with V12 database guards. Boundary, Unicode/percent encoding and failed-overwrite atomicity regressions are in `DavStoreIT`/`DavHttpIT`. The same admission checks exclude XML 1.0-invalid U+FFFE/U+FFFF before COPY/MOVE/PUT mutations; `copyAndMoveRejectXmlInvalidDestinationSegmentsBeforeMutation` also preserves valid U+FDD0 and supplementary U+1FFFE resources. No general file service is offered. | +| PROPFIND and property discovery/statuses — RFC4918 §§9.1,14; RFC4791 §§5–6 | Mandatory | `DavProperties` computes resource-type-specific live properties and stores namespaced dead XML separately. Requests distinguish allprop/propname/explicit properties, absent/unsupported properties and inaccessible resources. `DavPropertyBoundsIT` and `DavHttpIT` cover selected/name-only/all-property retrieval, inherited/overridden/empty XML language and mixed content. Requested properties use bounded projections; propname reads names without XML values or calendar content. A target admits at most 1 MiB aggregate dead-property XML, 64 KiB per value and 1,000 names, enforced transactionally and by V11; oversized legacy targets remain readable selectively and can be reduced. `DavXml` bounds construction work at 16 MiB and 50,000 nodes before imported DOM copies as well as final serialization. Multi-resource oversized operations fail before success is committed. | +| Atomic PROPPATCH — RFC4918 §9.2 | Mandatory | `DavStore` validates protected/live/dead updates before committing metadata/property state in one transaction. Raw HTTPS mixed-success requests return the required dependency failures; PostgreSQL rollback tests inject a database failure. Effective in-scope `xml:lang` is materialized when detaching accepted properties for MKCALENDAR and PROPPATCH; nearer overrides and explicit empty language remain distinct. Resource metadata never rewrites authoritative iCalendar bytes. Previously discarded language context cannot be reconstructed. | +| Strong validators and conditional requests — RFC9110 §§8.8,13; RFC4918 §10.4 | Mandatory | Already-valid request bytes are served unchanged; strong ETags identify the actual stored bytes. The [observance UNTIL admission exception](observance-until-compatibility.md) stores corrected validated bytes and omits ETag/Last-Modified on transformed PUT responses, including repeat submissions (RFC9110 §9.3.4). `HttpConditions`, `DavIf` and storage mutation locks evaluate HTTP and tagged/untagged DAV conditions before mutation within the lock boundary. Tests cover stale ETags, create-only collisions, negation, cross-resource conditions, sync state tokens, in-flight writes and failed-precondition immutability. The final date increment adds Last-Modified/getlastmodified, lock-safe If-Unmodified-Since, If-Modified-Since and ETag precedence, legacy HTTP date formats/century rules, whole-second comparison and future-date clamping; its focused PostgreSQL/HTTPS run passed 58 cases. Exact-byte no-op PUT and metadata-only PROPPATCH preserve content modification time. Unsupported locking is not advertised. | +| Modification dates and date conditions — [RFC9110 §§8.8.2,13.1.3–13.1.4,13.2.2](https://www.rfc-editor.org/rfc/rfc9110.html#section-13.1.3); [RFC4918 §§8.6,15.7](https://www.rfc-editor.org/rfc/rfc4918.html#section-15.7) | Mandatory If-Unmodified-Since; implemented recommendations for Last-Modified/If-Modified-Since | Stored calendar-resource dates are assigned with PostgreSQL `clock_timestamp()` after mutation locks. GET/HEAD, 304 and protected `getlastmodified` expose consistent GMT whole-second dates, bounded by response Date even for future stored timestamps. Exact-byte writes at the same URI and metadata-only updates retain the content date. ETag conditions take precedence over their date alternatives; malformed/list/duplicate dates are ignored. Dateless dynamic collections omit modification dates and ignore date conditions. `HttpDateValuesTest`, `HttpConditionsTest`, `WriteConditionTest` and raw HTTPS/storage date regressions cover all three HTTP date formats, stale-write rollback, lock waits, long transactions and COPY/MOVE. Dates have HTTP's one-second resolution; strong ETags remain the preferred concurrent-write validator. | +| Principal/current-user/calendar-home discovery — RFC3744 §4; RFC5397; RFC6764 | Mandatory profile | `DiscoveryController`, `DavProperties` and stable principal registry expose current-user-principal, principal-URL, calendar-home-set, collection enumeration, reports/component capabilities and effective privileges. Well-known redirects and configured-root/subpath operation are tested over actual HTTPS; DNS SRV/TXT records are operator controlled. | +| ACL properties, ACL changes and reports — RFC3744 §§3–9; RFC4791 §6 | Mandatory | `DavPrincipalReports`, `DavProperties`, `DavStore` and `GroupService` implement supported-privilege-set, current-user-privilege-set, ACL restrictions/inheritance semantics and supported coarse READ/WRITE grants. `principal-property-search`, `principal-search-property-set`, `principal-match`, `acl-principal-prop-set` and mandatory `expand-property` have raw HTTP tests. Unsupported partial/escalating ACEs fail with protocol preconditions rather than silently widening permissions. Required read-acl checks, report scope, property conjunction, group owner and restricted membership metadata have regressions. | +| Calendar-query — RFC4791 §§7.4,7.8,9.7 | Mandatory | `CalendarQuery` handles component/property/parameter filters, is-not-defined, text-match collations/negation, time ranges and alarm/component nesting. `CalendarRecurrence` and task-specific matching preserve floating/date/zoned semantics and avoid event-overlap shortcuts for VTODO. Metadata candidates are traversed in bounded batches, and only one individually bounded calendar body is parsed at a time; a narrow result is not rejected for unrelated aggregate stored-body size. Collection time zones are fetched directly once per report. Semantic suites, `CalendarCandidateBudgetTest`, and `DavHttpIT.queryMultigetAndFreeBusyUseProtocolBodiesAndBoundedRanges` plus large-body and 10,037-original selective-query/free-busy regressions exercise model and wire behavior. Existing originals consume cumulative candidate work; actual query matches have a separate 10,000-resource bound while generated recurrence and retained response bounds remain enforced. Unsupported collations/filter shapes return defined errors. | +| Calendar-multiget — RFC4791 §7.9 | Mandatory | `DavController` resolves requested hrefs within the correct collection and ACL boundary, preserving per-resource statuses, duplicates/cardinality and calendar-data selectors. Expected missing-ancestor, missing-leaf and denied-member lookup failures become per-resource statuses without losing successful members or leaving a rollback-only transaction. Invalid report targets, href origins/membership, malformed requests and database/computation failures retain their distinct contracts. Nested expand-property has equivalent lookup boundaries. Metadata-only repeats are charged for selected response data rather than unrelated calendar bodies; genuine oversized calendar-data responses still fail deliberately. Raw tests cover mixed results, repeated-read bounds and component/property selection. It is not an arbitrary URL fetcher. | +| Calendar-data selection/expand/limit-recurrence/limit-freebusy — RFC4791 §§7.6,9.6 | Mandatory report behavior | `CalendarDataProjection`, `CalendarReports` and `CalendarRecurrence` produce report copies; no-selector reads use authoritative bytes. Ten projection tests cover nested/unknown/duplicate selectors, no-value parameters, transform-before-selection, whole intersecting FREEBUSY periods/FBTYPE, lexical duration preservation and cumulative report budgets. Semantic tests cover recurring exceptions and expansion. | +| Free-busy-query — RFC4791 §7.10 | Mandatory | `CalendarFreeBusy` computes bounded busy periods from readable opaque VEVENT instances, including recurrence/overrides and cancellation/transparency semantics. Candidate metadata/body processing is batched with bounded output/recurrence work. Raw HTTP tests enforce collection-only targets, conceal unreadable targets and check actual VFREEBUSY results. This is calendar access, not scheduling delivery. | +| Bounded input/output and failure contracts — RFC4918/4791 preconditions; RFC9110 errors | Security profile | `DavController` bounded body reader, `DavXml`, parser callbacks, `QueryLimits`, `CalendarReportSize` and report preflights bound streamed bodies, XML depth/nodes, iCalendar complexity, recurrence work, resource counts and aggregate bytes before expensive copies. Malformed XML/DTD/entities, hostile destinations, chunked oversized bodies and bounded query/report failures are tested. Errors distinguish unsupported methods/properties, bad syntax, ACL denial and calendar preconditions. | + +## iCalendar representation and semantics + +| Obligation — RFC5545 / RFC6868 | Evidence and boundaries | +| --- | --- | +| Complete resources, properties and parameters | `CalendarContent` validates with iCal4j while PostgreSQL keeps accepted UTF-8 bytes. GET/HEAD validators and ordinary round trips do not reconstruct objects from derived indexes. Independent-client fixtures cover Unicode, unknown X-properties and alarms. Synthetic parser/parameter tests additionally cover unknown parameters, organizer/attendee and ATTACH preservation; these are distinct evidence sources. No external ATTACH/TZURL retrieval is performed; a local socket test verifies the non-fetch boundary. | +| Events and recurrence | `CalendarRecurrence`, `RecurrenceExpander` and semantic tests cover RRULE/RDATE/EXDATE, unbounded rules with bounded queries, detached/cancelled overrides, RECURRENCE-ID/RANGE, duplicate-instance validation, all-day exclusive end dates and UID consistency. Work limits fail explicitly rather than silently dropping matches. | +| Floating/date/UTC/zoned values and custom VTIMEZONE | `TemporalSupport`, `FloatingTimezone`, bounded timezone registry and coverage validation distinguish all value kinds, DST gaps/folds and leap dates. Required embedded timezone references are validated. Verified RFC5545 erratum4271 uses the pre-gap offset for nonexistent recurrence times. [Official parser integration](ical4j-parser-patch.md) and [timezone correction](ical4j-timezone-patch.md) document exact upstream source, license and changes. | +| Tasks | VTODO collections support undated tasks, supported DTSTART/DUE/DURATION combinations, STATUS/COMPLETED/PERCENT-COMPLETE/PRIORITY/RELATED-TO, recurrence and VALARM preservation. Task range predicates follow RFC4791's task cases. Both independent clients exercise actual task creation/update/completion/deletion. The server does not synthesize a next task or execute alarms. | +| Report copy fidelity | `CalendarReports` preserves lexical nominal/accurate duration distinctions (P1D versus PT24H), RDATE and trigger spellings during transformed report copies. Semantic tests include DST and native Thunderbird fixtures. A transformed REPORT result is distinct from authoritative GET bytes. | +| Verified errata | `CalendarErrataTest` and the semantic suites exercise applicable corrected REQUEST-STATUS cardinality, quoted ALTREP commas, absolute alarms and DST behavior; see [review decisions](standards-review.md). No blanket claim is inferred for every possible client-generated calendar object. | + +## Synchronization, durability and permission changes + +| RFC6578 obligation | Implementation/evidence | +| --- | --- | +| Initial state, incremental resources and deletions | `CalendarStore` allocates revisions under a per-collection row lock inside the mutation transaction, atomically storing content/indexes/revision/history. V10 immutable resource versions, normalized historical property rows and durable continuations freeze an upper revision across separate HTTP requests. Initial and incremental reports use consistent version/history snapshots without a database transaction held open between pages; see [ADR 0004](adr/0004-resumable-synchronization.md). `CalendarStoreIT.serializesCommitOrderWhileReportsUseCommittedSnapshots` explicitly exercises the late-commit sequence-number hazard. | +| Scoped tokens and invalid state | Stable tokens include collection identity, state generation and global recovery epoch. Opaque continuation state is additionally bound to the requesting account, fixed lower/upper revisions, cursor, retention floor and 24-hour expiry; every page rechecks account state and current collection authorization; sharing/group changes and restoration invalidate affected state. Invalid/wrong-collection/old-generation/retention-expired tokens fail with valid-sync-token. Revoked permissions cannot use old tokens to access content. | +| Limited results and interrupted responses | Initial and incremental pages honor the RFC 6578 truncated 207/collection-507/continuation-token contract. Valid positive client result ceilings above the server page cap are accepted and capped, including integers larger than a Java int. An explicit empty property selection still produces a successful empty propstat for each changed member. The cursor records the last delivered name at a frozen watermark; incomplete pages never append the latest live revision. Retry does not consume a token. Writes above the watermark, deletions, delete/recreate and names across cursor boundaries are delivered by the subsequent stable-token traversal. Metadata requests do not fetch bodies. Calendar-data pages additionally bound retained raw bodies and charge selected serialized XML, escaping, properties and construction work. A first item that cannot fit fails explicitly without a successful token; a truncated page must make progress. `SyncPagingIT` covers more than 10,000 resources and 32 MiB, small pages, metadata/content, concurrent mutations, retries, retention, revocation and restart. No separate pagination API or PROPFIND pagination is invented. | +| History retention | Change records, immutable resource bodies and property snapshots are retained indefinitely by default; protect historical private data in PostgreSQL backups. Transactional `CalendarStore.pruneHistory` minimum-token invalidation is tested; there is no automatic history-pruning job or exposed operator API. Expired continuation rows are independently reclaimed in bounded batches; expiry enforcement does not depend on that cleanup. Monitor growth as described in [operations](operations.md). | +| Restart/restore | Durable PostgreSQL state survives process restarts. Offline restore rotates recovery/account epochs, disables every account and clears restored factors/sessions/DAV/OAuth credentials before individually verified recovery; restoration also clears durable continuation rows while retaining authoritative version/property history, so old tokens and credentials cannot silently revive. Forward migrations V10–V13 preserve already-applied V1–V9 checksums. `SyncPagingIT` exercises V9 upgrade; the packaged operation and HTTPS restoration entry points cover the new schema and inherited property metadata. Prior local and real-provider HTTPS restoration passed old-token rejection, fresh synchronization, authoritative task bytes and separately recovered READ-recipient access/revocation; these are separate from database-only checks. | + +## Authentication and application scope + +| Requirement | Implementation and actual evidence | +| --- | --- | +| Local accounts, recent-factor MFA and management | Spring Security password authentication plus persistent WebAuthn, BCrypt12 with framework upgrade strategy and bounded Unicode/72-byte password validation; controlled offline bootstrap; current enabled account/security-epoch checks; durable throttling, sessions, audit and single-use recovery. `AccountSecurityWorkflowsIT` has nineteen HTTPS/PostgreSQL lifecycle tests. Server-rendered forms retain CSRF. Prior direct/proxy/real-provider Chromium profiles exercised actual CTAP2 enrollment/assertion, primary-only denial, sensitive mutations, ordinary recovery-code issue/use/replay/reset, fresh MFA, durable session/passkey restart and account disabling. Local and federated HTTPS restored-user workflows also passed. | +| Revocable ordinary DAV access — RFC7617 | Named device credentials use 256-bit random secrets with domain/UUID-bound constant-time SHA256 verifiers; legacy BCrypt verifiers migrate by a guarded CAS. Human passwords remain adaptive BCrypt and cannot authenticate DAV. Three credential database tests cover migration and revocation races; browser issue/display-once/revoke plus independent clients exercise Basic over trusted HTTPS. DAV credentials cannot authenticate administration. | +| Native OAuth — RFC8252/7636/9700/8414 | Embedded Spring authorization server, public native clients with S256, exact registered redirects/loopback-port rules, opaque audience/scoped access tokens and durable rotated refresh-family replay detection. No implicit/password grant, fabricated native secret or unrestricted registration. `OAuthHttpIT` and `RefreshFamilyStoreIT` test code/refresh replay, concurrent exchange, family revocation, disable/reset/expiry, client deletion/FKs and strict WebAuthn-principal persistence. Prior browser profiles passed actual S256 consent/code/token/refresh/revoke and a cookie-authenticated missing-CSRF 403 before successful consent. Fresh authorization after restoring the older database also passed while old access/refresh credentials failed. | +| Optional upstream OIDC | Spring OIDC client maps verified issuer+subject, closed administrator allowlist, explicit MFA-protected linking, no email autolink or automatic administrator role, no local-password fallback, and 24-hour bounded upstream freshness. Five real HTTPS Keycloak26.8.1 tests plus four external-identity tests passed; prior distinct-site Chromium/Keycloak callback/MFA/restart and restored-provider profiles passed with normal CA trust and Lax cookies. Local mode runs without Keycloak. [OIDC policy](keycloak.md) documents the local credential revocation boundary and outages. | +| Sharing and administration | One authoritative collection has direct/group READ/WRITE grants. Current actor/ownership checks run in services/storage for discovery, GET, reports, freebusy, multiget and sync. `GroupServiceIT`, `DavStoreIT` and `DavHttpIT` cover direct/group overlap, membership changes ordered against writes, revocation and metadata isolation. Grant creation has no smaller hidden count ceiling than maintenance. Owned-grant listing uses 50-row dashboard pages; membership update/deletion streams UUID-ordered affected locks and uses V13 set-based binding reconciliation, effective-permission changes and generation invalidation in one transaction. Large-grant and many-collection regressions are in `GroupServiceIT`; the required packaged workload additionally exercises 12,500 grants, first/last pages, removal and membership/deletion through actual forms. Administrators manage group membership; joining an explicitly granted group gives its access, while administrator status alone does not bypass ownership. Prior browser profiles passed real group lifecycle and owner grant/revoke forms; both local/federated restores separately recovered a READ recipient, proved GET 200/PUT 403 and later owner revocation, while preserving a disabled WRITE grant. | +| HTTPS, browser/DAV boundaries and deployment URLs | Separate chains, strict DAV allowlist, cookie/browser CSRF, DAV Origin/Fetch Metadata checks, configured canonical URLs and response redirects, management concealment, no arbitrary forwarded trust. `SecurityBoundaryIT` uses a disposable CA and actual embedded TLS. Prior pinned Nginx /team and distinct-site browser profiles verified normal certificate validation, metadata rewrites, hostile Host handling, privacy and streamed limits. Public/issuer prefixes are canonical ASCII paths; Unicode DAV names are supported separately. | + +[ASVS5 applicability and evidence](security-verification.md) records implemented controls and unverified recommendations. It is not external certification. Physical authenticators, additional client platforms and an independent security assessment remain unexecuted. + +## Declared profiles and exclusions + +* **Digest deviation:** RFC4918 §20's historical Digest requirement is deliberately not implemented. The HTTPS Basic device-credential/Bearer profile avoids the weak password-storage requirements identified by [Spring's Digest guidance](https://docs.spring.io/spring-security/reference/servlet/authentication/passwords/digest.html). Literal satisfaction of every historical DAV security requirement is not claimed. +* **Local observance UNTIL input compatibility:** the default [narrow repair](observance-until-compatibility.md) accepts and corrects local DATE-TIME boundaries in embedded timezone-observance RRULEs only. Stored data must still pass complete strict validation. This intentionally qualifies RFC4791 §5.3.2.1 input validity; `BUSYSTACK_DAV_REPAIR_LOCAL_OBSERVANCE_UNTIL=false` restores strict rejection. Historical audit and rejected-upload evidence remain unchanged. +* **Sync Depth compatibility:** RFC6578 §3.2 requires HTTP Depth 0. The executable JAR and Compose both default to the narrow Depth 1 accommodation only for DAV:sync-collection with XML sync-level 1. Missing Depth means zero and explicit Depth 0 remains supported. `BUSYSTACK_DAV_ALLOW_SYNC_DEPTH_ONE=false` is the strict override. Invalid/duplicate/infinite Depth still fails. Scope, ACLs, snapshots, tokens and limits are unchanged. This deliberately deviates from the nonzero-header rejection requirement. Current independent qualification leaves the setting absent and records actual effective protocol behavior separately from configuration input; a separate packaged HTTPS profile verifies explicit false. +* **Stored scheduling data:** RFC6638 scheduling, invitation/email delivery, push and server-executed reminders are outside scope and not advertised. Organizer/attendee/alarm preservation does not imply execution. +* **Other extensions:** DAV locking, extended MKCOL, proprietary client sharing, CardDAV and a general WebDAV file service are not advertised. Required calendar namespace operations remain implemented. + +## Independent clients and qualification boundaries + +| Client/environment | Exact version | Executed result | +| --- | --- | --- | +| python-caldav | 3.6.0 with locked icalendar7.3.0 and twenty-one pinned test dependencies | Actual HTTPS discovery, Unicode calendar/event/task CRUD, preservation, recurrence/query/expansion, sync/deletions and same-object cross-client updates passed in `IndependentClientIT`; Historical run used explicit Depth 1 compatibility; the correction-pass runner omits the setting and verifies actual missing/0/1 behavior. Artifact `target/qualification/python-caldav.json`. | +| Mozilla Thunderbird desktop binary/native CalDAV provider | 157.0.1, SHA512-pinned Mozilla installer; isolated headless Marionette profile | Actual native event/VTODO creation, updates/completion, recurrence, alarm/extension round trips and deletion/sync passed. Four-resource same-object Thunderbird→Python→Thunderbird→Python checks cover all-day exclusive ends, floating values, detached exceptions/EXDATE and task completion timestamps. Both clients passed against source `47b3543` with the compatibility setting omitted and actual depth behavior recorded. [Native result](https://github.com/busystack/caldav-server/blob/f3ba60d360117c44b3dfeb6dc9848778a98ef06a/qualification/evidence/correction-final47-thunderbird.json) and dummy ICS fixtures qualify the real desktop provider; that provider profile does not itself qualify graphical setup. | +| Chromium virtual authenticator | Playwright1.61.1 / Chromium149.0.7827.55 | Current root/default, explicit strict, Nginx /team and distinct-site Keycloak account/groups/recovery profiles passed, together with local/federated HTTPS restore. See the source/artifact/configuration records in the qualification index; no physical-authenticator claim. | +| Real disposable Keycloak | 26.8.1, pinned official distribution | HTTPS provider/code/nonce/PKCE, linking, disable/outage/freshness and local-mode independence passed; final combined suite and cross-site browser/restore runs are separately recorded. | +| DAVx⁵, Apple Calendar, Evolution | No qualified version | Unexecuted; no compatibility or task claim. | + +The [qualification index](../qualification/evidence/README.md) identifies current +version-specific client checks and archived historical results. Thunderbird 159.0 +(system/explicit Vancouver and explicit Berlin) and GNOME Calendar 50.0 / EDS +3.56.2 (Vancouver) passed the recorded real-editor workflows. The final release +manifest records source, CI attempt, image/JAR identities and deployment checks. +These results are not certification or claims about untested clients/platforms. +Third-party licenses remain independent; [operating limits](performance.md) do not +constitute capacity or high-availability guarantees. \ No newline at end of file diff --git a/docs/development/releasing.md b/docs/development/releasing.md new file mode 100644 index 0000000..8782043 --- /dev/null +++ b/docs/development/releasing.md @@ -0,0 +1,153 @@ +# Release procedure + +The literal `project.version` of the application `pom.xml` is the only maintained +application version. `scripts/release_version.py` reads that direct Maven element, +checks the project coordinates, and accepts only `X.Y.Z` without leading zeros. +Do not maintain an application version in another configuration file. Dependency, +probe, and private browser-harness package versions identify separate software. + +Work on the version's release branch. Preserve historical evidence and published +versions. Updating the application version means changing the application POM, +writing that version's release notes and obtaining fresh qualification; no release +script, workflow or Dockerfile needs a version-number edit. + +## Developer builds + +Use the pinned JDK described in `docs/versions.md`, the Maven wrapper and Python 3. +Commit source changes before producing an image with an exact Git revision: + +```sh +python3 scripts/build-image.py +``` + +This runs Maven clean package, selects exactly the Maven-named executable JAR, +validates Maven coordinates, manifest and embedded SBOM, and builds a Linux AMD64 +image named `busystack-caldav:`. It supplies the version and Git +revision to Docker and verifies the resulting OCI labels and exact JAR checksum. +`target/image-build.json` records the identities. No version argument is accepted. +CI uses the same entry point with `--jar` to package the already tested JAR without +rebuilding it. Stale extra JARs and dirty source checkouts are rejected. + +For development Compose, configure the ordinary database secrets and HTTPS URLs +from the operations guide, then use the same entry point: + +```sh +python3 scripts/build-image.py --compose up -d --no-build --wait +``` + +The wrapper supplies Compose's image, build context, version and source revision. +Raw source-directory Docker and Compose builds fail without these inputs. The +Dockerfile also validates the supplied metadata against the exact packaged JAR. +Development Compose can build only the prepared, checked context. Published +Compose has no application build and pulls the version plus registry digest. + +## Qualification and source identities + +Run the complete application, investigation, security, distribution and packaged +HTTPS suites from `.github/workflows/ci.yml`. Pull requests verify without +publishing. Candidate retention does not publish a release or registry tag. + +Perform the established real-client editor qualification on the actual version's +executable: Thunderbird system Vancouver, explicit Vancouver and explicit Berlin, +and GNOME Calendar/EDS Vancouver. Retain raw results and failed attempts in CI artifacts or the external audit archive, with a concise index; do not commit routine logs, screenshots, credentials or workstation diagnostics. Record +only executed results at `qualification/evidence/releases//verification.json`. +The record identifies `version`, `jarSha256`, the actual client source/image, +`runtimeInputs` SHA-256 values for `Dockerfile` and `compose.yaml`, client versions, +passed profile results and limitations. Historical internal-baseline replacement +work remains historical evidence; it is not a permanent future-version gate. +The reusable four client profiles and executable/runtime binding remain required. +Do not copy old pass claims to a new version. + +The successful CI candidate retains the exact JAR, embedded SBOM, saved container +and complete source snapshot. Its version is resolved at its exact source commit, +not at a later controller checkout. The snapshot is checked against every Git +blob. Candidate identity distinguishes source SHA/tree, workflow/run/attempt, +JAR SHA-256, local Docker image ID, canonical saved-image configuration digest +and saved-image checksum. Loading on another Docker engine may produce a different +local ID; the loaded runtime configuration, filesystem layers and JAR must match. + +## Approved integration + +Create a PR from the release branch into `main`. After owner release approval, +squash it with commit message `Release v` and preserve the release branch. +For the first implementation release, verify `main` contains exactly the original +`first commit` and that release commit, with the approved complete tree. Qualify +that final squash commit in CI. Official artifacts must identify that commit; +a branch candidate's different source SHA is not an official final revision. +Do not add commits to `main` merely to retain generated release metadata. + +While `main` contains only the original README, manual Actions dispatch is +unavailable because workflows are absent from the default branch. Do not add a +workflow to work around this. Development and push verification use the release +branch until the approved squash merge. + +## Stage, then publish + +Use authenticated `gh`. Both operations are explicit manual dispatches on `main`; +concurrency is version-specific without cancellation. Minimum workflow permissions +are contents write, Actions read and packages write for the promotion job. + +```sh +SOURCE_SHA=the_exact_successfully_qualified_main_commit +VERIFY_RUN=the_successful_verification_run_id +VERSION=$(python3 scripts/release_version.py --source-sha "$SOURCE_SHA") +gh workflow run release.yml --ref main \ + -f version="$VERSION" -f source_sha="$SOURCE_SHA" \ + -f verification_run="$VERIFY_RUN" -f phase=stage \ + -f candidate_version="$VERSION-rc.1" +``` + +Staging must execute at the qualified commit. It verifies the repository, CI +workflow, event, SHA, run attempt and all required jobs (`release-tools`, +`application`, `dependency-audit`). It rejects version mismatches, missing or +ambiguous artifacts, conflicting image tags and invalid qualification records. +It loads the saved tested image without rebuilding, validates OCI metadata and +JAR bytes, and pushes only the selected readable candidate tag. + +The registry manifest digest is distinct from the local Docker image ID. Staging +builds the deployment bundle only from its checked qualified-source snapshot: +Compose, initialization/backup/restoration scripts, documentation, proxy examples, +release notes, licenses and evidence. `deployment/INSTALL.md` and operator guides +are maintained templates; `@VERSION@` is rendered from that source's POM. +Generated image digests and checksums come from verified artifacts. The complete +bundle includes the SBOM and version-specific client record. + +Staging tests the exact digest-pinned archive from an arbitrary extraction path, +including Compose recovery, HTTPS browser restoration and the independent JAR +alternative. It retains all six final assets, release notes, installation receipt +and a stage record hashing them. Changed assets require a new verified candidate. + +An administrator must verify that immutable releases are enabled using GitHub's +repository immutable-releases API immediately before publication. Confirm the +repository-associated GHCR package is public; do not change unrelated packages. +Then dispatch against the exact successful staging run: + +```sh +STAGE_RUN=the_successful_staging_run_id +gh workflow run release.yml --ref main \ + -f version="$VERSION" -f source_sha="$SOURCE_SHA" \ + -f verification_run="$VERIFY_RUN" -f phase=publish \ + -f stage_run="$STAGE_RUN" -f immutability_confirmed=true +``` + +A newer trusted publication controller may resume the qualified ancestor source. +The requested version must still match the old source POM, candidate JAR, SBOM and +OCI labels. The controller may orchestrate validation; its files cannot become +release payloads. Publication downloads and validates the exact staged assets and +notes. It does not regenerate them from the controller. Installation and stage +receipts must match the selected version, source, CI attempt and artifact hashes. +The final image tag is attached to the exact staged registry manifest bytes using +the registry API. Publication does not export the image again through a different +Docker engine, which could change the registry manifest digest. + +Publication creates or matches the exact `v` tag, versioned image and +draft release. It rejects conflicting content, uploads only missing exact assets, +checks all remote digests, then publishes immutably. Interrupted operations can +resume with the same source/run/stage. Never overwrite a conflicting image, move a +tag or replace an immutable release. No `latest` image tag is created. + +Verify the immutable release attestation, each asset, anonymous GHCR access and +anonymous downloaded-asset installation. Retain the operation journal and actual +identities. A failed step is not a successful release. Existing published versions +remain untouched unless their separately approved retirement conditions are met. +For releases with discontinuous ancestry, compare explicit tag-to-tag source trees. diff --git a/docs/groups.md b/docs/groups.md new file mode 100644 index 0000000..a1be5ee --- /dev/null +++ b/docs/groups.md @@ -0,0 +1,17 @@ +# Groups and calendar sharing + +An administrator with recent primary authentication and passkey verification manages local groups under **Administration → Sharing groups**. Groups have stable internal identifiers, a display name and up to 1,000 account members. The interface creates, renames, replaces membership and deletes groups. Deleting a group removes its collection grants. Account disabling still denies access even when its membership remains recorded. + +A collection owner uses **Group sharing** on the account page to grant read-only or read/write access, or remove a grant. Sharing references the same authoritative collection. Administrators manage membership: granting a group therefore delegates control of its current and future membership. An administrator explicitly adding their own account receives that group's granted access; administrative status by itself does not bypass calendar permissions. Individual account grants and group grants combine to provide the effective access. + +The sharing dropdown exposes group names and identifiers. DAV membership metadata is limited to members, owners of collections granted to the group, and administrators. Group administration, owned grants and effective calendar permissions are checked in the services as well as the HTTP layer. Mutations require current account state and recent MFA; browser forms retain CSRF protection. + +Membership/grant changes reconcile shared collection bindings and change affected collection synchronization generations. A client with an invalidated stable or continuation token must perform an initial synchronization again; continuation state cannot retain access after account disabling or permission revocation. Removing group access preserves any access separately granted to the same account. Backup/restoration retains groups, grants, memberships and retired principal identifiers; operators must review these restored permissions before re-enabling accounts. + +The directory supports at most 1,000 groups, at most 1,000 accounts per group and 10,000 recorded group memberships in total. Group creation/replacement checks these existing admission bounds before accepting changes. No separate grant-count ceiling makes accepted sharing states impossible to maintain: a group may be granted many collections and an owner may have more than 10,000 group grants. + +The account dashboard lists owned group grants in pages of 50, with Previous/Next controls and removal actions on every page. Membership maintenance streams affected collection locks in stable UUID order; V13 reconciles bindings and generations with set-based SQL under the exclusive mutation lock and one transaction. Readers therefore observe the complete permission change after commit, rather than partially revoked batches. Group deletion removes its memberships and grants atomically. Individual READ/WRITE grants continue to contribute independently to effective access. Group/principal retirement identifiers remain durable and are never reused. + +`GroupServiceIT` exercises many grants across a modest number of calendars/groups and a single group spanning more than 10,000 calendars, including listing, removal, membership replacement and deletion. The required packaged workload additionally seeds 12,500 disposable grants across 25 calendars and 500 groups, then uses actual CSRF-protected browser forms to inspect the first/last pages, revoke a grant, replace membership and delete a group while recording Docker/DB observations. SQL fixture population is not a measurement of HTTP grant-creation throughput. + +The reviewed `495989d` browser/group results are historical. Correction-pass tests and final packaged results must be linked to their exact source/image in the maintained [conformance matrix](conformance.md) and [qualification index](https://github.com/busystack/caldav-server/blob/f3ba60d360117c44b3dfeb6dc9848778a98ef06a/qualification/evidence/README.md) before claiming they passed. This workflow description is not an independent security assessment. diff --git a/docs/ical4j-parser-patch.md b/docs/ical4j-parser-patch.md new file mode 100644 index 0000000..1b39441 --- /dev/null +++ b/docs/ical4j-parser-patch.md @@ -0,0 +1,35 @@ +# Official iCal4j parser integration + +The current distribution uses **official, unmodified iCal4j 4.4.0**. The former +`Ical4jCalendarParser` implementation and parser patch payload were removed during +v0.0.1 preparation. Their original provenance and corrections remain in Git at +`e094e9bf4ce34b5a8559ab25fc29f06971163752`; they are historical evidence, not a +supported patch application path. + +The selected [official source archive](https://repo.maven.apache.org/maven2/org/mnode/ical4j/ical4j/4.4.0/ical4j-4.4.0-sources.jar) +has SHA-256 `0300f9b2946cd70a879585c7ba91978899373c813929f030700dfcf78175574e`. +Its actual public API provides `CalendarParser.parse(Reader, ContentHandler)`; +`CalendarParserImpl` implements it. `ContentHandler.parameter(String, String)` +is scalar. The official parser does not consume a comma-separated parameter list. +`DefaultContentHandler(Consumer, TimeZoneRegistry, ContentHandlerContext)` +and parameter/property factories remain the model construction extension points. + +Independently written BusyStack `ParameterInput` scans only unfolded content-line +parameter headers. It retains each raw parameter list in a per-parse queue and +passes a safe scalar placeholder to the official parser. The corresponding +callback must match its queued name and placeholder before dispatching original +values to the existing handlers. It does not parse components, property values, +recurrence or timezone rules. iCal4j still performs calendar parsing and model +construction. The existing structural-case/UTF-8 adapter, limits, lexical-value +handler, sequential RFC 6868 decoding and strict factories remain in use. +No reflection, shadowing, binary patch or copied upstream parser is involved. + +The adapter rejects malformed quotes/delimiters and bounds each list at 1000 +values. Existing aggregate limits still apply. Stored accepted bytes and ETags +remain authoritative; this derived reader is never written to persistence. +`CalendarParameterSemanticsTest` retains MEMBER lists, embedded commas, `CN=^^n`, +`CN=^'Babe^'`, delegation, extensions and projection regressions. The calendar +and protocol suites retain malformed-boundary, UTF-8, atomic-write and query tests. +`verify-dependency-integrity.py` checks absence of removed classes/shadow namespaces +and exact official nested JAR bytes plus all 99 Spring Boot executable-loader classes. The BSD notice remains because iCal4j is +still distributed, not because modified implementation source remains. diff --git a/docs/ical4j-timezone-patch.md b/docs/ical4j-timezone-patch.md new file mode 100644 index 0000000..86e3b53 --- /dev/null +++ b/docs/ical4j-timezone-patch.md @@ -0,0 +1,19 @@ +# Official iCal4j timezone integration + +The current distribution uses **official, unmodified iCal4j 4.4.0**. The former +`Ical4jZoneRulesBuilder` copy and timezone patch payload were removed during +v0.0.1 preparation. It had no active production references; the clean build and +packaged integrity gate prevent stale classes from being delivered. Original +provenance and defect evidence remain in Git at `e094e9b`, without history rewrite. + +The active application-owned `BoundedTimeZoneRegistry` implements the official +`TimeZoneRegistry` interface. `ObservanceZoneRules` integrates official iCal4j +recurrence results into chronological rules, retaining explicit RDATE transitions, +finite COUNT/UNTIL, bounded unproven cycles and coverage checks. No implementation +was moved or copied during removal. See [calendar semantics](calendar-semantics.md), +`CalendarTimezoneRdateTest` and `CalendarTimezoneCoverageTest`. + +This is separate from the approved [local observance UNTIL input repair](observance-until-compatibility.md). +That repair, disable setting, strict corrected-data validation and transformed PUT +validator omission remain unchanged. Official dependency bytes and notices are +checked as described in [parser integration](ical4j-parser-patch.md). diff --git a/docs/keycloak.md b/docs/keycloak.md new file mode 100644 index 0000000..a5f70bc --- /dev/null +++ b/docs/keycloak.md @@ -0,0 +1,79 @@ +# Optional organization login through OIDC + +The default deployment uses local accounts and works with no Keycloak container, LDAP, or external identity service. To enable organization login, configure an existing OpenID Connect provider. The application uses Spring Security OAuth2 Login; it does not embed Keycloak or accept arbitrary provider tokens as DAV credentials. + +The identity boundary is: + +```text +local password or verified upstream OIDC → internal account UUID → local permissions + → local WebAuthn factor + → this server's OAuth issuer / DAV credentials +``` + +## Configuration and provider registration + +Create a confidential OIDC client at the provider, with authorization code flow enabled and the exact redirect URI `https://calendar.example/team/login/oauth2/code/upstream` for an application deployed at `https://calendar.example/team`. Require PKCE `S256`. Disable implicit, direct password grants, service accounts, and unused flows for that client. BusyStack requests `openid profile`, `prompt=login`, and `max_age=0`; registration uses the validated deployment URL, rather than request Host or Forwarded headers. + +Add operator configuration, keeping the secret outside the image and repository: + +```yaml +busystack: + public-base-url: https://calendar.example/team + issuer: https://calendar.example/team + oidc: + enabled: true + issuer: https://identity.example/realms/organization + client-id: busystack-calendar + client-secret: ${BUSYSTACK_OIDC_CLIENT_SECRET} + maximum-authentication-age: 24h + allowed-users: + - subject: "the-verified-immutable-provider-user-id" + username: alice + display-name: Alice +``` + +Obtain subjects from the identity provider's administrative records. They are not email addresses, display names, or necessarily usernames. In Keycloak the subject normally identifies the realm user by its immutable user ID. Each subject and internal username in the allowlist must be unique. Default provisioning is closed: an authenticated provider user outside the list is denied, unless an existing account owner has explicitly linked that identity through the protected linking flow. + +The issuer must use HTTPS in production. The only HTTP exception is loopback with the explicitly enabled development configuration; there is no option to trust all certificates. Discovery uses Spring's standard issuer metadata implementation, deferred until an upstream login is attempted. Local startup and local login do not need an available provider. Invalid operator settings fail at startup; failed discovery or login fails closed without a local-password fallback for managed accounts. + +Browser session cookies use `SameSite=Lax` to permit the top-level OIDC GET callback. CSRF protection remains enabled for browser mutations, including identity linking. The framework verifies authorization response state, ID-token signature, issuer, audience, and nonce. The application additionally requires a recent verified `auth_time` and the configured issuer/subject mapping. + +## Accounts, linking, and MFA + +New allowlisted users receive a stable internal UUID and no local password. They are ordinary users. Upstream realm roles, group claims, email, and preferred username do not grant BusyStack administration or select an existing account. Calendar permissions and administrative status are stored locally. + +Linking an existing account requires recent MFA and an explicit POST from the account interface. The server stores a five-minute linking intent bound to the session and exact OAuth authorization request state, then requires fresh provider authentication. The verified callback binds the exact issuer and subject to the existing internal UUID. If the external identity already belongs to another account, or the account already has a different mapping, the operation fails. Matching names or email never trigger linking. + +**Linking replaces local password sign-in for that account.** Its previous password is removed; existing calendar ownership and passkeys remain attached to the same UUID. Linking rotates the account security generation and revokes existing sessions, DAV credentials, and local OAuth grants. A locally bootstrapped administrator may deliberately link their own account after MFA, preserving their existing local administrative assignment. Realm administrators are never automatically application administrators. + +Local users complete password plus WebAuthn MFA. Federated users complete a fresh upstream authorization-code login plus local WebAuthn MFA. Both factors must be recent for sensitive operations and local client authorization. An application-only trusted-upstream authority accompanies the typed upstream factor; ordinary DAV Bearer authentication does not receive those browser factors. WebAuthn account lookup supports enabled externally managed accounts, while the password provider rejects accounts without a local password. There is no fabricated password used to make federation work. + +Upstream access and refresh tokens are discarded after login. This server's embedded OAuth authorization server remains the client-facing issuer, with its own registered clients, opaque tokens, scopes, audience, and revocation policy. Neither the upstream access token nor its ID token is a BusyStack DAV credential. + +## Outages, disabling, and recovery + +Local disabling immediately prevents subsequent browser, DAV, and local OAuth use according to the same account checks as local users. Independently issued credentials are also checked against durable external-authentication freshness. The maximum is 24 hours; operators may shorten it to at least one minute. A verified organization login refreshes that account's bound for all of its independently issued credentials. Once it expires, the user must sign in through the provider again. A passkey alone does not extend the upstream verification timestamp. + +Upstream disabling or logout is **not an immediate push revocation** of BusyStack DAV credentials. This implementation does not consume Keycloak back-channel logout, SCIM, or provider-admin events. An upstream-disabled account may retain previously issued local access until the documented freshness limit, credential expiry, or local revocation, whichever occurs first. Removing an automatically provisioned subject from the configured allowlist denies further access when that updated configuration is loaded. Explicitly linked identities remain governed by their local account and freshness policy. + +During an upstream outage, already issued access may continue inside its existing bound; the outage does not extend it. Local users can still authenticate. Federated accounts cannot fall back to a local password. Application logout clears the local session; it does not promise to terminate every upstream or device session. Fresh organization login always requests provider reauthentication. + +Use provider-side recovery for a managed identity. The local offline password-reset command rejects externally managed accounts. An operator can use the separate offline external-account recovery command with the exact existing issuer and subject: + +```sh +java -jar caldav-server.jar --spring.main.web-application-type=none \ + --busystack.recover-external-account=true \ + --busystack.recovery-username=alice \ + --busystack.recovery-issuer=https://identity.example/realms/organization \ + --busystack.recovery-subject=the-existing-immutable-subject +``` + +That command enables the selected existing account, removes its passkeys and independent credentials/grants, invalidates its sessions, and expires its upstream verification timestamp. It does not create a local password, change the mapping, or authenticate the user; fresh provider login remains required before client access resumes. A wrong issuer/subject pair fails without changing the account. Keep an independently recoverable local administrator for provider outages. + +Changing the configured issuer does not relink old identities. Restoring or migrating a Keycloak realm must preserve the issuer/subject relationship or use a deliberate administrative migration. A matching email address is not a migration key. + +## Verification + +`KeycloakOidcIT` uses a disposable **Keycloak 26.8.1** container pinned to manifest digest `sha256:a78d6b79572168cb25886b80a8b5b8d4ed4390743e3eecb810538683e209dd6b`, actual HTTPS browser-form/code exchange, real signed ID tokens, application PostgreSQL, and application/provider HTTPS certificates signed by an isolated test CA. Both services bind only to loopback. The test fork trusts that CA with normal hostname verification, and restores its previous JVM trust settings afterward. The disposable [test Dockerfile](../qualification/keycloak/Dockerfile) copies the unchanged distribution from that pinned image onto the selected Temurin 25 base. Both inputs are fixed, and the fixture is separate from the application deployment. [Keycloak supports OpenJDK 25](https://www.keycloak.org/server/supported-configurations). This is a provider test fixture, not an additional default deployment service. `ExternalIdentityIT` covers persistent mapping/linking rules separately. The explicit-link HTTP case seeds the initial MFA context and old OAuth grant rows, then exercises real CSRF enforcement, interleaved authorization callbacks, provider authentication, linking, grant revocation, and a replacement JDBC session. Those seeded preconditions do not qualify WebAuthn cryptography. `LinkingAuthorizationRequestResolverTest` checks that parallel authorization requests cannot consume another request's linking intent. That focused HTTP harness does not emulate browser SameSite enforcement, and its localhost ports are the same site. Subsequent packaged qualification separately exercises a cross-site browser callback, upstream login plus local WebAuthn, and federated restoration against the real provider; see the [release verification record](https://github.com/busystack/caldav-server/blob/f3ba60d360117c44b3dfeb6dc9848778a98ef06a/qualification/evidence/releases/0.0.1/verification.json) for the final artifact and the [correction submission](standards-corrections.md) for earlier source-scoped results. A virtual authenticator does not qualify physical devices. Executed on 2026-10-27 with Temurin 25.0.4.1 and Maven 3.9.12: `./mvnw -B -ntp spotless:apply verify -Dit.test=KeycloakOidcIT,ExternalIdentityIT` passed 17 unit tests and all nine selected integration tests, with zero failures, errors, or skips; SpotBugs reported zero high-priority findings. These results cover the listed HTTP/service cases, rather than those unexecuted browser cases. + +Upstream references: [Spring Security OAuth2 Login](https://docs.spring.io/spring-security/reference/servlet/oauth2/login/core.html), [Keycloak OIDC endpoints](https://www.keycloak.org/securing-apps/oidc-layers), [Keycloak published version](https://www.keycloak.org/downloads), and [OIDC subject semantics](https://openid.net/specs/openid-connect-core-1_0.html#SubjectIDTypes). diff --git a/docs/oauth.md b/docs/oauth.md new file mode 100644 index 0000000..9c19452 --- /dev/null +++ b/docs/oauth.md @@ -0,0 +1,153 @@ +# OAuth client access + +The embedded authorization server is this application's client-facing issuer. +Upstream OIDC login, when configured and qualified, is a separate authentication +relationship. An upstream ID token or arbitrary external access token is not a DAV +credential. + +## Register an application + +An administrator signs in, completes primary authentication and passkey verification, then opens +`/admin/oauth-clients`. Register the application's client ID, display name, and +redirect URIs. Changes require both factors within the last ten minutes, enforced +by the service as well as the browser boundary. Registration is administrator +controlled; there is no unrestricted dynamic registration endpoint. + +Native applications are public clients: authentication method `none`, no client +secret, authorization code plus S256 PKCE, and scope `dav`. The scope permits the +user's existing DAV permissions; it does not grant access to another user's +private collections or administration. The server requires consent and recent MFA +before authorizing an application, including when earlier consent is reusable. + +Accepted redirects are HTTPS URLs, HTTP IP-loopback URLs with a port, and +application-owned reverse-domain schemes such as `org.example.calendar:/callback`. +Administrators must register the real application's redirects. Hosted and custom +scheme redirects use exact matching. The framework permits a different port for +an otherwise matching IP-loopback redirect, as required for native applications. +Redirect fragments and embedded credentials are rejected. + +## Connect a capable client + +For a root issuer `https://calendar.example`, metadata is at +`https://calendar.example/.well-known/oauth-authorization-server`. The issuer and +all endpoint URLs come from validated deployment configuration, never request +Host or forwarded headers. Issuer subpath deployments require the explicit RFC +8414 metadata proxy route described in the deployment documentation; do not assume +that appending a well-known path after an issuer subpath is equivalent. + +Use an established OAuth client implementation. Open the authorization endpoint +in the system browser with `response_type=code`, the registered `client_id` and +`redirect_uri`, scope `dav`, unpredictable `state`, and a S256 PKCE challenge. The +user signs in with a local password or the configured upstream OIDC provider and +completes passkey verification before approving access. Exchange the returned code at `/oauth2/token` using form parameters +`grant_type=authorization_code`, `client_id`, `redirect_uri`, `code`, and +`code_verifier`. Do not send a fabricated native-client secret or the user's +account password. + +Send the resulting opaque access token as `Authorization: Bearer …` to DAV +resources. Refresh using a form POST to `/oauth2/token` with +`grant_type=refresh_token`, `client_id`, and `refresh_token`. Access tokens last ten +minutes; the refresh family has an absolute thirty-day lifetime. The issuer does +not enable implicit, password, client-credentials, device, or token-exchange grants +for registered native applications. + +Each refresh rotates the refresh token and replaces the current access token. +Clients must serialize refresh operations and atomically retain the latest token +pair. Replaying a spent refresh token revokes its entire family, including the +current access token. Concurrent use therefore yields at most one successful +rotation, and the losing replay invalidates its descendant. If the successful +response is lost, obtain a new browser authorization instead of blindly retrying +the consumed token. + +Ordinary CalDAV clients that do not support this OAuth onboarding use a separately +issued DAV credential with HTTP Basic over HTTPS. CalDAV service discovery does +not register an OAuth client or make every client OAuth capable. + +The optional RFC 9728 protected-resource metadata extension is not advertised or +implemented. DAV Bearer challenges retain their RFC 6750 error and status +information without a `resource_metadata` parameter. Authorization-server +metadata described above remains available; it is a separate protocol endpoint. + +## Manage and revoke access + +Users view grant metadata at `/oauth-grants`; tokens are never displayed there. +Revoking a grant requires recent MFA. It invalidates that family's access and +refresh credentials and removes associated consent. A client can also submit its +current access or refresh token to `/oauth2/revoke` with its public `client_id`. +Removing an administrator-registered client revokes its grants and removes its +stored authorization state. Disabling a local account revokes all its families. + +DAV token validation checks expiry, the configured issuer and DAV audience, scope, +account state, family revocation, and the recovery epoch against PostgreSQL on +every authentication. There is no process-local validity cache. Revocation applies +to authentications after its transaction commits; a request already being handled +may finish. A revoked or disabled account never gains browser administration from +a DAV token. + +## Persistence and recovery + +Migrations V2 and V3 create the token-family ledger and Spring Security's JDBC +client, authorization, and consent stores. V7 adds registered-client integrity +constraints and cascading grant cleanup; duplicate public client IDs are rejected. Spent refresh tokens are retained as +SHA-256 verifiers of high-entropy framework-generated tokens. Spring's JDBC +authorization store contains current bearer credential values and serialized +security state; protect database access and backups as secret material. The +selected opaque model does not need signing keys. Boot's demonstration ephemeral +JWT/JWK auto-configuration is excluded. + +Code exchange, refresh consumption, token replacement, and framework grant updates +use PostgreSQL transactions. A per-client transaction lock and registration recheck +precede authorization, account and grant/family row locks. Removing a registration +therefore cannot race a token exchange, refresh or late authorization save into +reviving its grants. Native refresh takes its client lock inside its independent +transaction; an outer transaction never holds the same lock across it. Spent-token +replay revocation commits before the adapter returns `invalid_grant`. A failure to +persist a successful rotation rolls back both grant and token changes. No blind +automatic mutation retry is used. + +Authorization state retains the stable account UUID and the security generation +captured with the original credential snapshot. Resetting an account invalidates +older grants and sessions. JDBC authorization lookups use one repeatable-read +snapshot for the authorization and its registered client, and the supported +Jackson mapper includes Spring's exact WebAuthn module and restrictive type +validator. These controls also apply when the interactive principal is a real +WebAuthn authentication rather than a password-token fixture. + +Do not delete spent-token history for an unexpired family: that removes replay +evidence. Expired families may be removed after the operational retention period; +their token rows cascade. The current implementation does not schedule cleanup. +The documented restore operation clears authorization and consent records, +revokes families, and changes the recovery epoch, requiring fresh browser +authorization instead of reviving credentials from an older backup. + +## Evidence limits + +[ADR 0002](adr/0002-native-oauth-refresh.md) records the exact framework behavior +and isolated four-test HTTP investigation. `RefreshFamilyStoreIT` exercises real +PostgreSQL transaction, replay, concurrent refresh, rollback, disable, expiry, and +recovery behavior. `OAuthHttpIT` exercises the actual HTTP code/token/DAV boundaries +using seeded completed-interactive-grant fixtures, and checks that a real +password-only browser session cannot authorize a client. Seeded grants do not +qualify a WebAuthn ceremony, browser consent flow, an independent native client, or +upstream Keycloak. Consult the executed test report and conformance matrix before +claiming those capabilities. + +The full application run on 2026-10-27 passed all 16 `OAuthHttpIT` and nine +`RefreshFamilyStoreIT` tests as part of 84 unit and 114 integration tests, with zero +failures, errors, or skips and no SpotBugs findings. It used Temurin 25.0.4.1, +Maven 3.9.12, Boot 4.1.2, Security 7.1.2 and PostgreSQL 18.6. Coverage includes client +removal during exchange/refresh/read, duplicate registration, revocation/reset +lock ordering, actual WebAuthn-principal JDBC round trips, unsupported client +credentials, metadata capability restrictions, code/refresh replay and concurrent +refresh. These counts record the full-run baseline; subsequent focused protocol +corrections must be read with their own reports. + +The token HTTP suite seeds completed grants for most exchange cases. The separate +trusted-CA browser qualification uses Chromium's CTAP2 virtual authenticator for +actual MFA, local consent, S256 exchange, refresh and grant revocation; that is not +physical-authenticator or third-party native OAuth-client qualification. The +python-caldav and Thunderbird CalDAV client profiles use DAV credentials. See the +maintained conformance and operational reports for the final artifact, browser, +proxy, restore and upstream-provider results. + +The IV-AUTH-01 [follow-up evidence](standards-corrections.md#corrections-and-regression-coverage) verifies RFC 6749 §3.2 empty-as-omitted behavior at the public token/revocation converter. Exactly empty optional `client_secret` follows omission after duplicate checks; nonempty/whitespace secrets remain unsupported. Valid refreshes retain normal rotation, invalid grants and client/parameter errors remain OAuth JSON 400/401 responses, and rejected requests do not redirect to browser login or consume valid refresh grants. diff --git a/docs/observance-until-compatibility.md b/docs/observance-until-compatibility.md new file mode 100644 index 0000000..24f7fdd --- /dev/null +++ b/docs/observance-until-compatibility.md @@ -0,0 +1,96 @@ +# Local observance UNTIL input compatibility + +BusyStack defaults to one narrowly defined calendar admission repair. In an RRULE directly in +`VCALENDAR → VTIMEZONE → STANDARD` or `DAYLIGHT`, a local DATE-TIME UNTIL is converted to UTC +by subtracting that observance's own TZOFFSETFROM. For example, `20260308T020000` with +`TZOFFSETFROM:-0800` becomes `20260308T100000Z`. No host timezone, current offset, User-Agent, +PRODID, account type or hardcoded zone name selects this behavior. + +This accommodates the client output described in [Mozilla bug 2004421](https://bugzilla.mozilla.org/show_bug.cgi?id=2004421). +It is an **input compatibility exception** to [RFC 4791 §5.3.2.1](https://www.rfc-editor.org/rfc/rfc4791.html#section-5.3.2.1), +which requires valid submitted calendar data. The original input is invalid under +[RFC 5545 §§3.3.10, 3.6.5](https://www.rfc-editor.org/rfc/rfc5545.html#section-3.6.5). +Validating the corrected stored representation does not make this an unconditional standards +conformance claim. The independent standards audit and historical rejection evidence are retained. + +Disable only this repair to restore the preceding rejection policy: + +```sh +export BUSYSTACK_DAV_REPAIR_LOCAL_OBSERVANCE_UNTIL=false +docker compose up -d application +``` + +The equivalent JAR property is `busystack.dav.repair-local-observance-until=false`. +The executable and Compose both default to true. The separate approved sync Depth compatibility +setting and the HTTPS DAV credential/Bearer authentication profile are unchanged. + +The application-owned adapter retains component boundaries and original octets. It unfolds only +for inspection and refolds changed RRULE lines as necessary; all unrelated bytes remain intact. +Offsets with seconds and UTC date rollover are accounted for. A valid UTC UNTIL is untouched. +Mandatory DTSTART/TZOFFSETFROM/TZOFFSETTO must be present and unambiguous; unsafe dates, offsets, +duplicate rule parts and COUNT/UNTIL conflicts reject. Leap-second candidates or UTC conversions +outside years 0001–9999 are explicitly unsupported rather than guessed. Existing byte, +component, property, timezone and recurrence work bounds still apply. No recurrence is expanded +by the adapter. The complete corrected calendar must pass the unchanged strict parser and +validator, so another invalid component/property still rejects the whole request before mutation. +Event/task rules, text and opaque extension values are never repaired. Collection timezone +properties and report timezone inputs retain their existing strict validation. + +Existing database rows are untouched. Legacy invalid objects remain available for authorized GET, +conditional replacement and deletion; a new authorized PUT can apply this repair. Semantic reports +still fail explicitly on invalid legacy members instead of silently omitting them. + +Repaired bytes are the single authoritative stored representation used by GET, ETags, metadata, +history, queries, expansion, free/busy, multiget and synchronization. Valid requests retain their +original bytes. There is no read-time repair, bulk rewrite, schema change or third-party patch. +The existing transaction locks, permissions, UID conflicts and write preconditions still apply. +The minimal structured `calendar_input_repaired` diagnostic records the named repair and rule +count, without calendar contents, identities or credentials. + +A successful transformed PUT omits **both ETag and Last-Modified**, including a repeated request +whose corrected bytes already match storage. Transformation is relative to that request, not to +the previous object. GET and DAV:getetag subsequently expose the strong ETag for the stored +corrected bytes. An unchanged valid PUT retains the existing ETag response behavior. This follows +[RFC 9110 §9.3.4](https://www.rfc-editor.org/rfc/rfc9110.html#section-9.3.4); +clients must retrieve the stored representation/validator before subsequent conditional edits. + +Permanent regressions use unchanged captured Thunderbird 159.0 Vancouver/Berlin uploads and +compare every finite cutoff's final included and first excluded recurrence using the submitted +offset, independently of host timezone data. Focused HTTPS tests additionally check validator +omission, corrected retrieval/reports/history, rejected-write atomicity, UID conflicts, stale +preconditions, concurrent replacements and the explicit disabled configuration. Client and final +artifact qualification results are recorded separately; a unit test is not a graphical client pass. + +## Qualified client configurations + +The [v0.1.0 qualification receipt](../qualification/evidence/releases/0.1.0/verification.json) +binds these actual editor checks to the executable checksum and runtime inputs. + +| Client | Configuration | Real-editor result | +| --- | --- | --- | +| Official Thunderbird 159.0 | System-selected America/Vancouver | Passed | +| Official Thunderbird 159.0 | Explicit America/Vancouver | Passed | +| Official Thunderbird 159.0 | Explicit Europe/Berlin | Passed | +| GNOME Calendar 50.0 / EDS 3.56.2 | America/Vancouver | Passed | + +Thunderbird completed creation, synchronization, corrected retrieval, conditional +time editing/reopening, a finite daily series with an edited occurrence, client +restart, task completion and deletion. Subsequent valid uploads retained corrected +UTC observance boundaries and used the corrected strong ETag without duplicate +creation or conflict loops. + +GNOME completed timed creation, native-selector editing, reopening after separate +client/server restarts, a three-occurrence daily series and All Events deletion +(DELETE 204 and independent GET 404). Normal certificate verification was retained +in isolated profiles; no installed client or system trust configuration changed. + +These were automated real-editor operations, not manual-human or physical-device +qualification. Other versions, platforms and zones are outside this scope. Earlier +strict rejection, explicit-UTC checks and failed automation attempts remain in the +[historical evidence archive](../qualification/evidence/README.md#historical-records). + +The maintained restoration corpus includes unchanged captured uploads and fixed +expected corrected hashes. It checks corrected bytes, ETags, history and expanded +UTC times after actual database restoration. The separate provider tests and +packaged browser/recovery profiles run in CI; their results are not inferred from +these editor checks. diff --git a/docs/operations.md b/docs/operations.md new file mode 100644 index 0000000..dcf272e --- /dev/null +++ b/docs/operations.md @@ -0,0 +1,339 @@ +# Operations + +This maintained installation source uses `@VERSION@`; release bundles render it +from the qualified Maven project. Use the generated guide for installation. + +These instructions describe BusyStack CalDAV Server @VERSION@. The [release manifest](https://github.com/busystack/caldav-server/releases/download/v@VERSION@/release-manifest.json) identifies the source, artifacts, checksums, Linux AMD64 image digest and verification evidence. + +The default deployment runs one application and PostgreSQL. External identity integration is optional. The project is Apache-2.0, with [separate third-party licenses and notices](third-party-licenses.md). + +## Administrator installation + +Start with the [v@VERSION@ release assets](https://github.com/busystack/caldav-server/releases/tag/v@VERSION@): `caldav-server-@VERSION@-deploy.tar.gz`, `SHA256SUMS` and `release-manifest.json`. Verify the archive checksum, extract it into any installation directory and follow its `INSTALL.md`. All referenced deployment files, initialization/backup/restoration scripts, notices, safe configuration and HTTPS proxy examples are included. Its production Compose file pulls the image by version and registry manifest digest without an application build. Container deployment requires Linux AMD64 and Docker/Compose, with no checkout, Maven or host JDK. Supply and renew your own domain/DNS and trusted HTTPS certificate. + +```sh +sha256sum --check --ignore-missing SHA256SUMS +tar -xzf caldav-server-@VERSION@-deploy.tar.gz +cd caldav-server-@VERSION@-deploy +cp .env.example .env +# Edit .env and create private secrets using the instructions below. +docker compose config --quiet +docker compose pull +docker compose up -d --no-build --wait +``` + +For the JAR alternative, download and checksum `caldav-server-@VERSION@.jar`, use Java 25 (selected Temurin 25.0.4.1+1) and PostgreSQL 18.6 with a dedicated non-superuser owner role, file-backed Spring config-tree credentials, loopback HTTP and an HTTPS reverse proxy. Substitute its absolute downloaded path for `target/caldav-server-@VERSION@.jar` in command examples below. No native service manager or database installer is supplied. + +## Developer build and verification + +Install Java 25 and `unzip`, then use the checked-in wrapper. Keep `.mvn/jvm.config` in the checkout: it pins the build JVM to UTC so executable JAR timestamp metadata is reproducible across host timezones. It does not change application timezone semantics. + +```sh +python3 -m venv .client-venv +.client-venv/bin/python -m pip install -r qualification/clients/requirements.txt -r qualification/clients/desktop-requirements.txt +bash qualification/clients/install-thunderbird.sh .client-tools +BUSYSTACK_THUNDERBIRD_BINARY="$PWD/.client-tools/thunderbird/thunderbird" \ +BUSYSTACK_CLIENT_PYTHON="$PWD/.client-venv/bin/python" ./mvnw --batch-mode --no-transfer-progress verify +python3 scripts/build-image.py +python3 scripts/verify-operations.py --image "busystack-caldav:$(python3 scripts/release_version.py)" +``` + +`verify` includes the required PostgreSQL integration suite; Docker must be available. Integration prerequisites must fail visibly when absent. The shell-only Maven wrapper requires `unzip` for its SHA-256-pinned ZIP distribution; its upstream tar fallback cannot use the ZIP checksum. The supported image-build entry point packages through the pinned Maven wrapper and validates the exact JAR before Docker runs. CI supplies the already tested JAR to that same entry point without rebuilding it. An image build alone is not qualification. + +The executable JAR is `target/caldav-server-@VERSION@.jar`. The container name above is a developer-local build tag; administrator installations use the published digest recorded in the release manifest. Record the checkout revision with `git rev-parse HEAD`, and calculate the JAR checksum with `sha256sum target/caldav-server-@VERSION@.jar`. Preserve the matching source, image identity and configuration when installing or backing up the server. + +The Python 3 operations harness requires Compose 2.24.4 or newer for the port override. It creates two randomly named disposable Compose projects, bootstraps an account locally, checks the packaged application's probes/discovery/issuer and DAV challenge, restarts it, seeds database-recovery fixtures, then backs up and restores to the second database. It verifies data/ownership/sharing, restored security-state invalidation and controlled local/external offline recovery, and removes only its own projects and volumes. It never prints generated secrets. Its SQL-seeded fixtures are database procedure evidence, not independent CalDAV-client or passkey-authentication evidence. HTTPS/proxy/browser workflows require separate end-to-end tests. + +CI checks formatting, compilation/static analysis, unit/integration tests, packaging, container construction, SBOM generation, and a required public OSV advisory check with a known-vulnerable positive control. Feed failures, incomplete pagination and detected advisories fail the gate. An additional NVD audit runs only when its credential is configured; absence is recorded as unexecuted. The attempted anonymous NVD run failed before analysis. See [advisory-check scope and evidence](../qualification/security/README.md). Neither a successful advisory lookup nor this self-assessment is an independent security review. + +## Two-service Compose arrangement + +Requirements: Docker Engine with Compose v2, enough disk for PostgreSQL and backups, and an HTTPS reverse proxy operated on the same host. No Redis, Keycloak, LDAP, PHP, or frontend service is part of the default arrangement. + +Run the commands below from the extracted release deployment directory. Its production Compose pulls the published digest; use `docker compose pull` followed by `up --no-build`. Developers may separately use the repository's source-build Compose after building the matching local image; do not substitute that workflow for administrator installation. + +For a trial installation or restore exercise, choose a new Compose project and an unused loopback port before running any Compose command, for example `export COMPOSE_PROJECT_NAME=busystack-caldav-trial` and `export BUSYSTACK_HTTP_PORT=18080`. Use the same environment for startup, bootstrap, backup and restore. The proxy upstream must use the chosen port instead of 8080. Never reuse an existing deployment's project or database volume for an installation check. The ordinary installation uses the checked-in project name and loopback port 8080. + +Create two independent secrets outside the checkout. For example: + +```sh +install -d -m 700 "$HOME/.config/busystack-caldav" +(umask 077; openssl rand -base64 48 > "$HOME/.config/busystack-caldav/database-password") +(umask 077; openssl rand -base64 48 > "$HOME/.config/busystack-caldav/postgres-admin-password") +chmod 444 "$HOME/.config/busystack-caldav/database-password" "$HOME/.config/busystack-caldav/postgres-admin-password" +export BUSYSTACK_DATABASE_PASSWORD_FILE="$HOME/.config/busystack-caldav/database-password" +export BUSYSTACK_POSTGRES_ADMIN_PASSWORD_FILE="$HOME/.config/busystack-caldav/postgres-admin-password" +export BUSYSTACK_PUBLIC_BASE_URL=https://calendar.example.org +export BUSYSTACK_ISSUER=https://calendar.example.org +docker compose up -d --no-build +docker compose ps +``` + +The base URL and issuer must be equal, canonical absolute URLs with no trailing slash, query, credentials, fragment, or percent-encoded path. Configuration is validated at startup. These are external addresses, not `http://application:8080` or `http://database:5432`. Loopback HTTP is accepted only with the explicit development switch `BUSYSTACK_ALLOW_INSECURE_LOCAL=true`; that setting does not make HTTP suitable for network credential use. + +The application runs as UID/GID 10001, with a read-only root filesystem, a bounded temporary directory, dropped Linux capabilities, and a 1 GiB container memory limit. That memory limit is a conservative engineering default, not a measured production capacity claim. PostgreSQL data is persisted at `/var/lib/postgresql`, the PostgreSQL 18 image's volume root. No database port is published. The application listener is published only on `127.@VERSION@`, port 8080 by default (`BUSYSTACK_HTTP_PORT` can change the host port). + +PostgreSQL administration uses a separate secret. Initialization creates a non-superuser `busystack` database owner for application migrations and runtime. Initialization scripts run only on an empty database volume. Replacing a password file does not rotate an already initialized PostgreSQL role; rotate the database role and secret together during controlled downtime. + +Compose secret mounts supply `spring.datasource.password` through Spring's config-tree support. Local Compose implements file-backed secrets as bind mounts and preserves host file ownership/mode. Therefore the secret files need read permissions for the container users, while the enclosing host directory must remain mode 0700 to deny other host users access. Do not make that directory public. The separate one-time administrator bootstrap password file must remain owner-only. Do not commit a `.env` containing secrets, copy secrets into an image, or place credentials in an application's command line. + +## HTTPS and proxy paths + +Terminate TLS at a configured reverse proxy and allow its local connection to `127.@VERSION@:8080`. Deny public access to `/actuator/`; health probes are for local/container monitoring. Set request-size and timeout limits at the proxy as well as the application. Use valid certificates and clients' normal certificate validation. + +Put either location arrangement below inside your proxy's HTTPS virtual host, using its real certificate chain and private key. For example, the surrounding Nginx configuration is: + +```nginx +server { + listen 443 ssl; + server_name calendar.example.org; + ssl_certificate /etc/ssl/caldav/fullchain.pem; + ssl_certificate_key /etc/ssl/caldav/privkey.pem; + ssl_protocols TLSv1.2 TLSv1.3; + ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305; + gzip off; + # Insert the matching root or /calendar locations below. +} +``` + +Obtain and renew that certificate through your operator's CA/ACME procedure, protect its private key and test renewal before deployment. The application image contains no production certificate or trust bypass. Run the proxy's configuration validation before reloading it. + +The TLS 1.2 list permits ephemeral elliptic-curve key exchange with AEAD suites. It excludes static RSA and finite-field DH suites deprecated by RFC 10015; TLS 1.3 uses the runtime's modern TLS 1.3 suites. The embedded listener has the corresponding protocol/cipher allow-list when explicitly enabled with `server.ssl.enabled=true` and configured key material. Its default remains plain loopback HTTP behind the configured TLS terminator. The disposable qualification negotiates TLS 1.2/1.3, rejects obsolete protocol/key-exchange offers, and checks trusted certificate identity, wrong hostname and untrusted CA. These local tests do not qualify an operator's public certificate, DNS or network boundaries. + +Configured public and issuer URLs must use canonical ASCII deployment prefixes such as `/calendar`; literal or percent-encoded Unicode prefixes, semicolons and invalid ports are rejected at startup. Unicode names in calendar/resource segments remain supported. The configured path becomes the servlet context and must be preserved by the proxy. + +Disable compression at the application and reverse proxy so strong GET validators identify the representation served. For an Nginx hostname-root arrangement, the relevant locations are: + +```nginx +gzip off; +location ^~ /actuator/ { return 404; } +location / { + client_max_body_size 1m; + proxy_connect_timeout 5s; + proxy_read_timeout 120s; + proxy_pass http://127.@VERSION@:8080; + proxy_set_header Host calendar.example.org; + proxy_set_header Forwarded ""; + proxy_set_header X-Forwarded-Host ""; + proxy_set_header X-Forwarded-Proto ""; +} +``` + +This fragment belongs inside a TLS-enabled server configuration; it is not a complete TLS configuration. The application ignores forwarding headers and constructs discovery URLs from validated configuration. Prevent direct remote access to the internal HTTP listener. Do not enable blanket forwarded-header trust to change redirects or issuer values. + +A configured public subpath automatically sets the matching servlet context path. For `BUSYSTACK_PUBLIC_BASE_URL=https://calendar.example.org/calendar` and the same issuer, the proxy must **preserve** `/calendar` when forwarding; rendered browser links, cookie paths, callbacks and DAV endpoints then share that prefix. An explicitly configured `server.servlet.context-path` must match the public URL path or startup fails. Do not strip the prefix. + +Host-root discovery routes need explicit mappings. For the example above: + +```nginx +location = /.well-known/caldav { + return 308 https://calendar.example.org/calendar/dav/; +} +location = /.well-known/oauth-authorization-server/calendar { + proxy_pass http://127.@VERSION@:8080/calendar/.well-known/oauth-authorization-server; + proxy_set_header Host calendar.example.org; + proxy_set_header Forwarded ""; + proxy_set_header X-Forwarded-Host ""; + proxy_set_header X-Forwarded-Proto ""; +} +location ^~ /calendar/actuator/ { return 404; } +location /calendar/ { + client_max_body_size 1m; + proxy_pass http://127.@VERSION@:8080; + proxy_set_header Host calendar.example.org; + proxy_set_header Forwarded ""; + proxy_set_header X-Forwarded-Host ""; + proxy_set_header X-Forwarded-Proto ""; +} +``` + +The metadata URL follows [RFC 8414 §3](https://www.rfc-editor.org/rfc/rfc8414.html#section-3): the well-known segment is inserted before the issuer's path. The Spring application's local metadata endpoint remains inside its servlet context. Use literal configured authority/prefix values in these mappings. The embedded TLS security suite exercises a servlet subpath. The final packaged image passed actual pinned Nginx `/team`, root TLS, cross-site Keycloak and local/federated HTTPS restoration profiles with normal test-CA validation; see [exact image/evidence scope](https://github.com/busystack/caldav-server/blob/f3ba60d360117c44b3dfeb6dc9848778a98ef06a/qualification/evidence/README.md). Later protocol/schema changes require corresponding artifact reruns. + +## Local administrator bootstrap + +There is no default administrator password or unauthenticated setup endpoint. The local bootstrap command requires an empty account database, a private owner-only password file, and `--spring.main.web-application-type=none`. Use the executable JAR with database configuration supplied securely through environment/configuration: + +```sh +java -jar target/caldav-server-@VERSION@.jar \ + --spring.main.web-application-type=none \ + --busystack.bootstrap=true \ + --busystack.bootstrap-username=administrator \ + --busystack.bootstrap-password-file=/private/path/administrator-password +``` + +Passwords need at least fourteen characters and at most seventy-two UTF-8 bytes. Create the one-time file in a private directory, without putting the password in a process argument or shell command. For example, the following generates a 48-character ASCII password with owner-only permissions; record its contents securely in your password manager before use: + +```sh +install -d -m 700 "$HOME/.config/busystack-caldav" +(umask 077; openssl rand -hex 24 > "$HOME/.config/busystack-caldav/administrator-password") +``` + +Use that file's absolute path for `/private/path/administrator-password` in these examples. Remove the one-time file after successful bootstrap and securely recording the credential. Do not apply the database secrets' mode 0444 to it: bootstrap requires an owner-only file. After bootstrap, use the account interface to enroll a passkey and complete password plus WebAuthn MFA before issuing DAV credentials or changing collections, sharing, recovery codes and security settings. The interface also provides session and OAuth grant management; see [account security](account-security.md). + +For the Compose deployment, run the same local operation inside its private network. Mount an existing owner-only password file and run the one-time process with the host operator's UID so its permission check remains meaningful: + +```sh +docker compose stop application +docker compose run --rm --no-deps --user "$(id -u):$(id -g)" \ + --volume /private/path/administrator-password:/bootstrap-password:ro \ + application --spring.main.web-application-type=none \ + --busystack.bootstrap=true \ + --busystack.bootstrap-username=administrator \ + --busystack.bootstrap-password-file=/bootstrap-password +docker compose up -d application +``` + +The image must already be pulled (or source-built for development) and the database healthy. The command refuses to replace any existing account. Do not set bootstrap flags on the ordinary long-running application service. + +## First browser sign-in and user creation + +1. Open the configured public HTTPS address in a browser that trusts its certificate. Use the final hostname: passkeys are bound to its relying-party ID and origin. At **Sign in to BusyStack**, enter the bootstrap username/password and choose **Verify password**. +2. Follow **enroll your first passkey** or open **Passkeys and authentication**. Give it a name, choose **Register passkey**, and complete the authenticator's user-verification prompt. Choose **Verify passkey to complete MFA** for the same account. Registration alone is not the completed two-factor sign-in. +3. Return to **Account management**, then open **Administration**. Under **Provision an account**, enter the new user's username, display name and initial password and choose **Create account**. Usernames start with an ASCII letter or digit and contain only ASCII letters, digits, `.`, `_` or `-`, up to 64 characters. Passwords use the limits above. The new account is an ordinary user; administrator status does not grant access to that user's private calendars. +4. Give the initial credential to the intended user through a secure channel. Sign out of the administrator session. The new user signs in and enrolls/verifies their own passkey using the same steps. Account changes require both factors verified within ten minutes. When they become stale, return to `/login` and use **Verify password** and **Verify passkey** for the same account. For an already enrolled account, the passkey settings page requires that recent MFA and redirects to login until it is complete. +5. On the user's account page, use **Create a collection** to create an **Events** collection and a separate **Tasks** collection. Their displayed component types are `VEVENT` and `VTODO`. The account interface manages collections and access; create and edit the actual events/tasks in a CalDAV client. +6. Under **DAV credentials**, enter a device/application label and a lifetime from 1 to 365 days, then choose **Issue credential**. The **New DAV credential** page shows the complete secret once. Copy it directly into that client's password store; do not use the account password. Losing the secret requires issuing a replacement, not retrieving the old one. Return to the account page to revoke a device's credential when needed. + +Keep a second enrolled authenticator or securely stored recovery codes where possible. A virtual-authenticator test does not establish that a particular physical device or synchronized passkey provider works; check that device before relying on it. The documented offline recovery procedure remains available to the server operator. + +## Client access and sync compatibility + +After browser primary authentication and passkey verification, issue a named DAV credential for each application/device. Configure the client with the public HTTPS CalDAV endpoint, `/dav/`, the account username and that credential. The account password does not authenticate DAV requests. Revocation, expiry, local disabling and collection permissions are enforced for each client access; OAuth onboarding is a separate registered-client workflow described in [OAuth configuration](oauth.md). + +The account page's **Connect a CalDAV client** section supplies the full server address and username, including any configured deployment prefix. Copy those values rather than inventing a collection UUID or removing the prefix. In a client that supports CalDAV discovery, select a network CalDAV account/calendar, supply those values and the issued DAV secret, then select the discovered event and task collections. Clients may expose `VTODO` collections in a separate task view. Trust the server's normal certificate chain; do not disable certificate or hostname checking to connect. + +The release exercise inspected Thunderbird 157.0.1's graphical setup screens. For that version: + +1. Open the **Calendar** view and create a new calendar, choosing **On the Network**. +2. Copy **Username** from the BusyStack account page and paste its exact displayed DAV server address into **Location**. Leave **This location doesn't require credentials** unchecked, then choose **Find Calendars**. +3. In the password prompt, enter the named DAV credential, not the BusyStack account password. Optionally choose **Use Password Manager** to retain it for later synchronization. +4. Choose **CalDAV**, select the discovered event and task collections, and choose **Subscribe**. Use the calendar view for events and the task view for the `VTODO` collection. + +These labels and discovery steps are version-scoped; see the release record for completed content, synchronization and restart checks. Other client versions may present different controls. + +BusyStack defaults to the [local observance UNTIL input repair](observance-until-compatibility.md). It addresses captured Thunderbird 157.0.1/159.0 Vancouver and 159.0 Berlin uploads: local DATE-TIME UNTIL values only in VTIMEZONE STANDARD/DAYLIGHT RRULEs are converted using their own TZOFFSETFROM, then the entire corrected representation is strictly validated and stored. Already-valid data remains byte-identical. No Thunderbird-specific account setup or forced UTC timezone is required by this policy. Other malformed data still rejects atomically. + +This is an explicit input compatibility exception to RFC 4791 §5.3.2.1, not unconditional standards compliance. To restore the preceding rejection behavior set `BUSYSTACK_DAV_REPAIR_LOCAL_OBSERVANCE_UNTIL=false` and recreate the application service with `docker compose up -d application`. The equivalent JAR property is `busystack.dav.repair-local-observance-until=false`. A repaired PUT omits ETag and Last-Modified; clients retrieve the corrected representation and its strong ETag before conditional edits. The earlier strict-rejection and explicit-UTC results remain historical evidence, not qualification of the corrected candidate. Current client outcomes must be read from the release qualification record. + +Verify first use with a small event and task: create, synchronize, edit from another connected client or profile, synchronize again, complete a task, and delete test data. Confirm persistent data remains after `docker compose restart application`. This restart retains the database volume; `docker compose down --volumes` destroys that project's data and is not an update or restart command. + +The independently tested python-caldav 3.6.0 and Thunderbird 157.0.1 providers send HTTP `Depth: 1` for synchronization, while RFC 6578 requires `Depth: 0`. The narrowly constrained accommodation is enabled by default for both the executable JAR and Compose. Ordinary deployments need no override. To select strict RFC depth validation, explicitly disable it and restart: + +```sh +export BUSYSTACK_DAV_ALLOW_SYNC_DEPTH_ONE=false +docker compose up -d application +``` + +The default accepts `Depth: 1` only with XML sync-level 1. Missing Depth and `Depth: 0` retain standard behavior; explicit false rejects nonzero depth. It preserves collection scope, permissions, snapshot/token semantics and limits; invalid, duplicate and infinite Depth remain rejected. It is not literal compliance with that RFC requirement. The exact client/provider operations and native desktop limitations are in [independent-client qualification](../qualification/clients/README.md). Standard discovery and OAuth registration remain distinct. + +## Health, diagnostics and limits + +`/actuator/health/liveness` and `/actuator/health/readiness` return probe status without details. The Compose application health check calls readiness locally. Readiness includes the database check. Only health endpoints are exposed by Actuator; there is no public diagnostic dump. + +The database connection pool is bounded to 10 connections; connection acquisition times out after 10 seconds. Servlet form bodies are limited to 64 KiB, accepted calendar objects to 1 MiB in the persistence schema, and the container temporary filesystem to 128 MiB. DAV request bodies are streamed with a 1 MiB cap; compressed Content-Encoding is rejected with 415 rather than decompressed. The principal protocol/query limits are below; exceeding a budget returns an explicit failure rather than incomplete matching data. These are work bounds, not measured capacity guarantees. + +| Boundary | Current limit | +| --- | --- | +| DAV XML depth / elements / attributes per element | 32 / 20,000 / 100 | +| Accepted iCalendar component depth / components | 8 / 2,000 | +| iCalendar properties / parameter values | 20,000 / 40,000 | +| Query filter nodes / range days | 256 / 3,660 | +| Generated recurrence instances / rule increments / candidate work | 10,000 / 900 / 500,000 | +| Matched calendar-query resources | 10,000 | +| Complete DAV collection grant listing / ACL replacement | 1,000 direct/group entries; browser sharing uses separate paged management | +| Serialized XML response and cumulative report-copy/expansion reservations | 16 MiB (conservative traversal reservations may fail earlier) | +| Response DOM construction / nodes / concurrent DAV work | 16 MiB conservative work / 50,000 nodes / 4 requests | +| Dead properties per target / individual XML / aggregate XML | 1,000 / 64 KiB / 1 MiB | +| DAV namespace after `/dav/` | 20 segments including `calendars`, principal UUID and resource leaf; 4,096 encoded bytes, excluding the separately validated deployment prefix | +| Complete PROPFIND traversal / query candidate traversal | 10,000 nodes / 100,000 nodes; explicit limit errors, without fabricated PROPFIND pagination | +| Synchronization page | At most 10,000 returned members; larger valid client ceilings are accepted and capped, further bounded by actual selected XML and DOM work; 4 MiB retained bodies and 4 MiB metadata work | + +The fixed query budgets are maintained in `QueryLimits.defaults()`, with additional timezone and aggregate report guards. Raising a limit requires renewed hostile-input and memory qualification. + +Existing non-recurring components, consumed one resource at a time, spend cumulative candidate work rather than generated-instance capacity. Calendar-query counts actual matching resources separately. Recurrence generation and retained free/busy periods retain their existing instance guards; calendar-data projection separately bounds returned components. Unrelated originals therefore cannot exhaust a narrow query's result capacity merely by exceeding 10,000 stored resources. Exceeding computation or actual response limits still fails explicitly. + +Property writes enforce the aggregate limit under storage locks and V11 database guards. Existing oversized property sets remain stored: explicit small-value and name-only requests remain usable, while oversized all-property requests fail before retrieving all XML. Removal and nonincreasing repairs remain possible. Property names and selected values use separate SQL projections, including historical synchronization properties. Calendar bodies are fetched only when needed. Queries and free/busy scan at most 16 bodies / 4 MiB per batch; collection timezones are read directly and reused in a bounded cache. + +XML construction reserves node, attribute, UTF-16 and escaped UTF-8 work before allocating output subtrees. Responses are completed before HTTP success is committed. When four DAV operations are already active, further DAV work receives `503` with `Retry-After: 1` before dispatch or mutation. The default ten-connection pool also leaves room for four report snapshots plus their short independent cursor writes. The integration suite runs with a 256 MiB heap; this is verification evidence, not a production capacity guarantee. + +Each namespace segment is limited to 255 UTF-16 units. Semicolons and Unicode line/paragraph separators are rejected consistently with the existing HTTP firewall, including in Destination headers; controls, slash/backslash and dot segments remain rejected. U+FFFE and U+FFFF are also rejected because resource names appear in XML 1.0 property values. This narrow check preserves valid character data such as U+FDD0 and supplementary U+1FFFE; it does not reject Unicode noncharacters as a category. Calendar admission reserves one segment and the worst permitted encoded resource leaf, so a successfully created calendar can accept ordinary child resources. Storage, HTTP paths and database guards share these bounds. COPY/MOVE preflight all resulting descendant URLs and copied property budgets under transaction locks before replacing a destination. Collection depth is measured separately from the configured reverse-proxy prefix. + +Account, configured OIDC account, collection and group display labels must also be representable as XML 1.0 character data. Existing naming and control-character policies still apply. A legacy stored label containing an invalid XML character causes an explicit server error before response text is attached; it is not silently rewritten or included in a malformed successful XML response. Metadata requests that do not select the invalid label remain usable. + +Application logs must remain free of authorization headers, credential secrets, recovery data, and calendar content. Use container exit status, migration validation failures, health status and database diagnostics when investigating startup. No external telemetry service is required to serve the application. + +## Synchronization history and database growth + +Resource change and deletion history is retained indefinitely by default. There is no automatic time-based pruning job or operator pruning API. Collection permission changes rotate the collection generation; restoration rotates the global recovery epoch and invalidates all tokens while retaining collection identity. `CalendarStore.pruneHistory` has transactional tests for the collection lock and minimum retained revision, but its existence does not enable scheduled expiry. + +V10 retains immutable calendar representations and indexed historical property values. Initial and incremental synchronization freeze a revision watermark and page through names with a durable scoped cursor. Truncated reports return `207`, a collection-level `507` entry and a continuation token; the final token advances only to the original watermark. Concurrent updates, deletions and renamed/recreated names are returned in the following traversal. [ADR 0004](adr/0004-resumable-synchronization.md) specifies the algorithm, selected-response accounting and retry invariants. No aggregate calendar-body quota prevents an accepted collection from being synchronized. + +Continuation state expires after 24 hours and is checked against current access, collection generation, actor, recovery epoch and retention state on every request. A separate read-committed cleanup removes at most 1,024 expired states per minute; expiry does not depend on cleanup. A change to a collection's default timezone invalidates existing tokens. Revoked access cannot use old continuation state. Interrupted requests may leave an unused cursor, which confers no access and expires normally. + +Monitor database/volume growth, `pg_total_relation_size` for `calendar_resources`, `collection_changes`, `calendar_resource_versions`, `calendar_resource_version_properties` and `sync_continuations`, row counts, dead tuples and ordinary PostgreSQL vacuum statistics. Repeated edits/deletions add historical bodies and properties even when current resource count is stable. Recurrence rules are stored as submitted resources rather than materialized into an unbounded occurrence table. Retention needs storage planning and verified backups. + +Keep history intact under the supported default. Any future operator pruning interface must perform deletion and the minimum-token update atomically under the same per-collection lock; removing history rows alone cannot provide complete deltas to old clients. No unsupported manual pruning automation is prescribed here. + +## Backup and isolated restoration + +The database contains account/principal mappings, ownership, memberships, calendar representations, revisions, history, DAV credential verifiers, and the implemented OAuth persistence tables. Back up it and the deployment configuration/secret files. Store encrypted backups outside the server, control access, and periodically test restoration. The selected OAuth access-token model uses opaque reference tokens, so it does not require a JWT signing-key file. Any future cryptographic material must be added to the recovery contract before functionality relying on it is released. + +From the checkout with the same Compose configuration environment: + +```sh +install -d -m 700 /private/backups +./scripts/backup.sh /private/backups/caldav-2026-10-27.dump +``` + +The script creates a consistent PostgreSQL custom-format archive and a SHA-256 checksum with private file permissions. It refuses to overwrite an existing archive. Retain the exact application version, Compose configuration, and secret material separately. Never publish backups as CI artifacts. + +Restore only to a newly initialized, isolated database. Stop every application instance first. The script accepts a complete, unique, successful migration history from V1 through at most V13; failed, missing, duplicate, gapped and future histories fail closed. It refuses a running application or any nonempty target database and never removes an existing volume or database: + +```sh +docker compose stop application +./scripts/restore.sh /private/backups/caldav-2026-10-27.dump +``` + +After loading an archive at schema versions 1 through 13, restoration disables all accounts, changes the durable `system_state.recovery_epoch`, deletes all restored DAV credentials, passkeys and OAuth authorizations/consents, and revokes refresh-token families. It also deletes JDBC browser sessions, single-use recovery codes, WebAuthn ceremony records and login-throttle state, rotates every account security epoch and records the restore in the security audit. It preserves external issuer/subject mappings but expires their upstream-authentication timestamps. Sync tokens include the changed epoch; pre-restore tokens must be rejected. Disabling restored accounts and removing restored credentials/grants prevents passwords or credentials changed/revoked after the backup from silently becoming valid again. DAV bindings, folders, dead properties, sharing groups, group memberships/grants and the retired-principal identity registry remain authoritative data and are retained. Registered OAuth clients remain registered, but users must authorize fresh grants. If loading or invalidation fails, leave the application stopped and investigate. This script refuses later migrations until the recovery policy is updated for new persisted security state. + +Keep **all** application instances stopped while loading and invalidating restored state. For a supported older archive, including the reviewed V9 schema, the current image can then start with every account disabled to apply forward migrations; stop it again before controlled account recovery. This is an offline upgrade arrangement, not a rolling upgrade. Do not start another older application instance against the upgraded database. The required operations harness exercises both same-version restoration and V9 restoration followed by migration to V13. + +After independently verifying a user's identity, use the controlled local recovery operation with an owner-only file containing a fresh password: + +```sh +docker compose run --rm --no-deps --user "$(id -u):$(id -g)" \ + --volume /private/path/new-password:/recovery-password:ro \ + application --spring.main.web-application-type=none \ + --busystack.recover-account=true \ + --busystack.recovery-username=administrator \ + --busystack.recovery-password-file=/recovery-password +``` + +The operation enables the selected **local** account with its new password and removes its prior client credentials, OAuth grants and passkeys. It rejects externally managed identities; do not convert a federated account to local-password login during recovery. For a restored federated account, verify the trusted issuer and immutable subject independently, then run the matching operation without a password: + +```sh +docker compose run --rm --no-deps application \ + --spring.main.web-application-type=none \ + --busystack.recover-external-account=true \ + --busystack.recovery-username=organization-user \ + --busystack.recovery-issuer=https://identity.example.org/realms/organization \ + --busystack.recovery-subject=verified-existing-subject +``` + +This operation requires the existing issuer/subject pair to match exactly, expires prior upstream verification, clears client access/factors and enables only that account. The next interactive authentication must use the trusted upstream provider and fresh passkey enrollment. It does not create a local password or invent an identity mapping. The operations smoke test checks the offline decision and rejected subject mismatch. A separate final HTTPS profile passed fresh organization login/passkey enrollment against the same still-running real provider after restoration; old credentials/sync state and unauthorized shared writes remained rejected. + +Recover each account explicitly after identity verification; do not run a blanket SQL enable operation. Review restored administrative roles, sharing permissions and OAuth client registrations before allowing access: an older dump cannot know about later permission or registration changes. Users must enroll fresh passkeys and reauthorize clients. Securely remove the temporary password files. Recovery is an operator-controlled local process, without an email-service dependency or a public bypass endpoint. The executed authentication/restore evidence does not constitute independent security review. Any future persisted security state must be included in this policy before its feature is released. + +A full restore qualification must verify users, ownership, resource bytes, sharing, authentication, credential revocation, and invalidated synchronization tokens through the running application. A `pg_restore` exit code alone does not pass that acceptance requirement. See the maintained test evidence for what has actually executed. + +## Upgrades + +Take and verify a backup, retain the previous application artifact/configuration, stop the application, select the intended source revision, and build the new image before starting it against the existing database volume. Do not change dependency or base-image pins as an incidental part of an application update. Run migration validation and the qualification suite against an isolated restored copy before deployment. For the source-based Compose arrangement, after that rehearsal the application replacement is: + +```sh +python3 scripts/build-image.py --compose up -d --no-build --no-deps --wait application +``` + +Keep the same Compose project, secret paths and public URL. Inspect startup/migration status and verify discovery, an existing resource and synchronization before reopening access. Flyway applies versioned migrations and validates their checksums at startup; production schema auto-mutation is not enabled. Migrations are forward-only unless a particular migration has a separately tested reversal. Rolling back a JAR does not safely roll back a database schema. Version @VERSION@ uses migrations V1–V13 and introduces no migration during final release preparation. + +The native Thunderbird and Python client results are scoped to their recorded versions and interoperability profile; see [client evidence](../qualification/clients/README.md). This Compose arrangement provides no high availability, automatic failover or measured user-capacity guarantee. The [executed workload record](performance.md) reports the actual host/image/dataset, end-to-end latency, sampled memory/CPU and PostgreSQL behavior; raw evidence is retained separately. It is one sequential client, not a user-capacity or concurrency guarantee. diff --git a/docs/passkeys-and-mfa.md b/docs/passkeys-and-mfa.md new file mode 100644 index 0000000..4824524 --- /dev/null +++ b/docs/passkeys-and-mfa.md @@ -0,0 +1,21 @@ +# Passkeys and the MFA policy + +BusyStack connects Spring Security 7.1.2's WebAuthn mechanisms and WebAuthn4j verification to PostgreSQL. Account configuration is available at `/security/passkeys`. The interface uses local JavaScript and has no CDN or analytics dependency. Event and task content is managed by CalDAV clients. + +Sign in at `/login` using your account password. The first passkey enrollment requires a primary factor verified in the last ten minutes: a password for local users, or verified organization OIDC login for externally managed users. Enroll a resident passkey using an authenticator that supports user verification; BusyStack requires verification for registration and authentication. Then use **Verify passkey** to complete password plus WebAuthn MFA for the same account. Additional passkey enrollment and credential removal require the appropriate primary factor and WebAuthn within the same ten-minute period. See [optional Keycloak configuration](keycloak.md) for the externally managed policy. + +Account mutations and client authorization require recent MFA. The account overview can be read with a single authenticated factor, so an account without a passkey can reach enrollment. These explicit rules do not claim that a password-only overview is MFA. Losing the password or all authenticators does not grant an HTTP recovery bypass. + +The relying-party ID is the hostname of the validated public base URL; the allowed origin is its exact scheme, hostname and port. A reverse-proxy path is not part of an origin. Changing the public hostname can make existing authenticator registrations unusable. Deployment URLs must therefore be selected deliberately. Production uses HTTPS; explicitly enabled HTTP loopback development is separate configuration. + +Both the user handle and credential records are persistent. A user handle maps to the internal account UUID, and lookup uses the account's current username. Renaming a login therefore does not change calendar ownership or replace the registered passkey handle. Spring's JDBC credential repository stores public keys, authenticator metadata, signature counters, labels, and usage timestamps. Passwords and private authenticator keys are never stored in passkey records. + +The application binds an assertion to the account and credential selected by its server-side ceremony before invoking cryptographic verification. Anonymous discoverable assertions require a matching user handle; identified-account assertions must match the saved allow-list and current authenticated account, and any supplied handle must match the stored owner. A submitted username does not select a new account after assertion arrival. The application invokes the configured Spring verifier once, holds its pending credential update until the entire decision succeeds, and then saves verified backup state, UV initialization, counter and last-used information through the persistent repository. Rejected assertions cannot save those fields. The implementation uses the supported relying-party and repository interfaces without modifying Spring or WebAuthn4J. + +The server serializes each session's registration/authentication ceremonies and consumes their challenges once. Challenges expire after five minutes on the server. Registration challenges remain associated with the authenticated session. Failed assertions consume their challenge and require requesting fresh options. PostgreSQL transactions serialize writes for a credential; credential ownership and public key are immutable, counters cannot regress, and deletion records prevent a previously read assertion from reinserting a removed credential. Session IDs rotate after successful password or passkey authentication. Account disabling is checked for existing browser sessions and during passkey authentication. + +The local operator recovery command is documented in [operations](operations.md). It resets the selected account's password and revokes independently issued access, including registered passkeys. Use the documented offline procedure with a private password file. Recovery requires control of the server and database; there is no public first-run or recovery endpoint. Stop all application instances before this offline operation and restart afterward, as required by the supported procedure. Persistent account security epochs also reject old sessions and ceremonies; assertions check the captured epoch before updating credential state. This does not create an online recovery endpoint. Keep another administrator and more than one authenticator where appropriate. + +Implementation evidence is in `PasskeyCeremonyGuardTest`, `PasskeyPersistenceIT`, and the separate Chromium virtual-authenticator qualification harness. Check the qualification record for actual executed results. Database round-trip tests do not establish cryptographic verification, and a virtual authenticator does not qualify every hardware or synced passkey provider. + +The framework APIs and exact source inspected are [Spring Security passkeys](https://docs.spring.io/spring-security/reference/servlet/authentication/passkeys.html), [Spring Security MFA](https://docs.spring.io/spring-security/reference/servlet/authentication/mfa.html), and the [published WebAuthn 7.1.2 source artifact](https://repo.maven.apache.org/maven2/org/springframework/security/spring-security-webauthn/7.1.2/spring-security-webauthn-7.1.2-sources.jar). Notable application integration requirements are the enrollment filter's position before ordinary authorization rules, explicit enabled-account checks in the user lookup, persistent repositories, challenge lifetime enforcement, and session fixation protection. These controls still need independent security review; they are not a certification or equivalence claim. diff --git a/docs/performance.md b/docs/performance.md new file mode 100644 index 0000000..ea2fefc --- /dev/null +++ b/docs/performance.md @@ -0,0 +1,28 @@ +# Operating limits and workload scope + +## Processing bounds + +These results do not remove admission/work limits: a calendar representation is at most 1 MiB; one dead property is at most 64 KiB, with at most 1 MiB aggregate XML and 1,000 names per target. XML construction retains a conservative 16 MiB work and 50,000-node bound in addition to serialized limits. Synchronization pages have independent row, selected-response and retained-body limits rather than a total collection-size quota. The 97-row request used here deliberately exercises continuation often. + +Group directory admission remains 1,000 groups, 1,000 accounts per group and 10,000 recorded memberships in total. Grant maintenance has no smaller hidden 10,000-grant listing ceiling; the dashboard uses 50-row pages, and membership/permission changes remain one atomic transaction with bounded lock retrieval and set-based reconciliation. The exact protocol/computation profile and supported namespace limits remain in [operations](operations.md), [groups](groups.md) and [conformance](conformance.md). + + +## Historical measurements + +A synthetic packaged workload at source `47b3543674644d075ece97045088fc1e309e7d1e` +completed traversal of 10,037 resources (46,158,025 calendar bytes) in 104 content +pages and 104 metadata pages. It exercised continuation replay, concurrent +mutations, deletions, a JVM restart and a later incremental delta without fallback. +Separate property and sharing workloads exercised bounded reads and administrative +updates. These are historical results, not a v0.1.0 throughput or capacity claim. + +The application container had a 1 GiB memory limit and the normal 70% maximum-RAM +heap policy on a shared Linux AMD64 host. End-to-end measurements included client +parsing, TLS, application and database work; SQL fixture population was not HTTP +ingestion. Full datasets, measurements and unsuccessful attempts remain in the +[evidence archive](../qualification/evidence/README.md#historical-records). + +No service-level, multi-user scalability or high-availability guarantee follows. +Size the deployment using representative workloads, monitor database/storage +capacity and retain tested backups. The current CI correctness profile does not +rerun that historical performance workload. diff --git a/docs/releases/0.0.1.md b/docs/releases/0.0.1.md new file mode 100644 index 0000000..d986800 --- /dev/null +++ b/docs/releases/0.0.1.md @@ -0,0 +1,37 @@ +# BusyStack CalDAV Server 0.0.1 + +Initial release of BusyStack CalDAV Server, a self-hosted calendar and task server with PostgreSQL persistence, account administration and optional OpenID Connect login. The [release manifest](https://github.com/busystack/caldav-server/releases/download/v0.0.1/release-manifest.json) identifies the source, artifacts, checksums, image digest and verification evidence. + +## Installation and artifacts + +Use `caldav-server-0.0.1-deploy.tar.gz` from the [GitHub release](https://github.com/busystack/caldav-server/releases/tag/v0.0.1), verify `SHA256SUMS` and follow its `INSTALL.md`. This complete Compose bundle pulls `ghcr.io/busystack/caldav-server:0.0.1` pinned to the registry digest, on Linux AMD64, without source builds. The JAR alternative is `caldav-server-0.0.1.jar` with Java 25 and PostgreSQL 18.6. Other assets are `sbom.cdx.json`, `notices.tar.gz`, `release-manifest.json` and `SHA256SUMS`. See [operations](../operations.md) and [release procedure](../development/releasing.md). + +The default arrangement is one application and PostgreSQL, with HTTPS terminated by an operator-configured reverse proxy. It has no default administrator password or public first-run setup endpoint. Bootstrap the first administrator offline with an owner-only password file, then use browser administration to provision ordinary users. Each user enrolls and verifies a passkey before issuing named DAV credentials or changing collections and access. The operations guide covers the complete first-use sequence, client setup, controlled updates and restoration. + +The project is licensed under Apache-2.0. The executable JAR and image include the project license, required third-party notices and runtime CycloneDX SBOM. See the [license inventory](../third-party-licenses.md) and [dependency versions](../versions.md). The database schema is V1–V13. + +## Supported functionality + +* Independent CalDAV event (`VEVENT`) and task (`VTODO`) collections, discovery, queries, multiget and free/busy. +* Authoritative accepted iCalendar bytes, recurrence and exceptions, embedded custom time zones, conditional writes and ETags. +* Persistent incremental synchronization, bounded resumable pages, deletion history and restart recovery. +* Owner-controlled individual/group READ or WRITE sharing and DAV-visible access control. +* Local accounts, browser administration, persistent passkeys/MFA, recovery codes, revocable expiring DAV credentials and registered native OAuth clients using Authorization Code/S256 PKCE. +* Optional external OpenID Connect login through an existing Keycloak provider, with local account permissions and a local passkey factor. + +## Compatibility and limitations + +* The default [local observance UNTIL repair](../observance-until-compatibility.md) handles the Thunderbird local-UNTIL defect only in embedded timezone observances. It converts with TZOFFSETFROM, validates and stores corrected bytes, and omits validators on transformed PUT responses. Already-valid input remains byte-identical. `BUSYSTACK_DAV_REPAIR_LOCAL_OBSERVANCE_UNTIL=false` restores rejection. This is an RFC 4791 §5.3.2.1 input compatibility exception. It does not alter event/task recurrence rules or other validation. +* Tested client configurations include Thunderbird 159.0 with system and explicit America/Vancouver and explicit Europe/Berlin, and GNOME Calendar 50.0/EDS 3.56.2 with America/Vancouver. Automated real-editor workflows covered creation, editing, synchronization, recurrence, restart persistence and deletion, plus Thunderbird tasks and occurrence editing. The [client verification record](https://github.com/busystack/caldav-server/blob/f3ba60d360117c44b3dfeb6dc9848778a98ef06a/qualification/evidence/releases/0.0.1/official-dependencies/verification.json) documents the exact artifact, results and retained failed attempts. These results are scoped to the listed versions and configurations. +* Use HTTPS and a separate named DAV credential as the client's password; the primary account password does not authenticate DAV. Registered OAuth-capable clients can instead use the documented browser authorization flow. +* The default intentionally accepts `Depth: 1` only for sync-collection with XML sync-level 1 for the tested clients. Standard missing/`Depth: 0` behavior remains available. `BUSYSTACK_DAV_ALLOW_SYNC_DEPTH_ONE=false` selects strict depth handling. This is a documented RFC 6578 compatibility deviation, not a universal interoperability claim. +* The account interface manages collections, access and security; it is not a web calendar/task editor. Scheduling, invitation or notification delivery, alarm execution, CardDAV, general-purpose file hosting, DAV locking and non-Gregorian recurrence execution are outside the supported profile. +* Valid but computationally unsupported timezone/recurrence evaluations fail explicitly at their documented bounds. Leap-second values retain the documented rejection rather than the recommended ordinary-second fallback. Existing invalid legacy objects are preserved for authorized retrieval and repair; reports do not silently skip them. See [calendar semantics](../calendar-semantics.md). +* Synchronization/resource history is retained indefinitely by default and needs storage planning. Query/response limits are correctness controls, not a measured production capacity guarantee. The deployment does not provide automatic failover or high availability. +* Restoring an older database deliberately disables accounts and invalidates old sessions, factors, DAV/OAuth credentials and synchronization tokens before individual verified recovery. Backups and deployment secrets require private storage. Follow the supplied restore procedure; rolling back an application artifact does not roll back a database schema. +* Optional upstream account disabling/logout is not immediate push revocation of independently issued BusyStack access. The documented external-authentication freshness bound and local revocation policy apply; see [OIDC operations](../keycloak.md). +* Qualification is scoped to its recorded browser/client/runtime versions. Physical authenticators, other clients/platforms and operator-specific public DNS/certificates/network deployment are not established by virtual-authenticator or isolated test-CA results. The release record explicitly distinguishes automated browser/native-client tests from manual checks that could not be performed. + +## Verification + +The release manifest links the exact source, CI run, client results and installation checks. Use its checksums and image digest to identify the tested artifacts. GitHub release assets and the associated tag are immutable after publication. diff --git a/docs/releases/0.1.0-website-handoff.md b/docs/releases/0.1.0-website-handoff.md new file mode 100644 index 0000000..f7f5183 --- /dev/null +++ b/docs/releases/0.1.0-website-handoff.md @@ -0,0 +1,28 @@ +# Website handoff: BusyStack CalDAV Server v0.1.0 + +This handoff describes the repository's v0.1.0 distribution. Website edits and +deployment are a separate task; this release does not modify `busystack.org`. + +Use the [GitHub release](https://github.com/busystack/caldav-server/releases/tag/v0.1.0) +and its [release manifest](https://github.com/busystack/caldav-server/releases/download/v0.1.0/release-manifest.json) +for the published source revision, checksums, registry digest and verification +identities. Verify those public assets before updating website installation links. + +The primary installation is `caldav-server-0.1.0-deploy.tar.gz`, with Docker Engine +and Compose v2 on Linux AMD64. Its Compose configuration pulls +`ghcr.io/busystack/caldav-server:0.1.0` pinned to the manifest's immutable digest and +includes a private PostgreSQL service. Users supply their domain, HTTPS termination +and private secrets. No source checkout, Maven or host Java is required. +`caldav-server-0.1.0.jar` is the alternative for Java 25 / PostgreSQL 18.6 operators. +The release also supplies `SHA256SUMS`, `sbom.cdx.json` and `notices.tar.gz`. + +Describe calendar/task collections, recurrence, synchronization, sharing and +account administration within the [release notes](0.1.0.md) and +[documented limitations](../calendar-semantics.md). The account UI is not a calendar +editor. Do not claim CardDAV, scheduling/invitation delivery, alarm execution, +additional platforms or high availability. Client support is limited to the +recorded Thunderbird and GNOME Calendar configurations. + +Link installation, administrator bootstrap, passkeys/MFA, named DAV credentials, +backup and restoration guidance. Preserve the documented compatibility controls, +Apache-2.0 license and third-party notices. No additional package format is offered. diff --git a/docs/releases/0.1.0.md b/docs/releases/0.1.0.md new file mode 100644 index 0000000..e26144f --- /dev/null +++ b/docs/releases/0.1.0.md @@ -0,0 +1,42 @@ +# BusyStack CalDAV Server v0.1.0 + +## Overview + +BusyStack CalDAV Server is a self-hosted server for calendars and tasks, with +PostgreSQL persistence and account administration. v0.1.0 provides the correctly +versioned release with verified installation and publication infrastructure. +Existing CalDAV functionality and compatibility behavior are preserved. + +## Installation + +The recommended distribution is `caldav-server-0.1.0-deploy.tar.gz`. Download it, +`SHA256SUMS` and `release-manifest.json` from the +[release assets](https://github.com/busystack/caldav-server/releases/tag/v0.1.0), +verify checksums, extract it and follow its `INSTALL.md`. + +The bundle requires Linux AMD64, Docker Engine and Compose v2 (2.24.4 or newer). +It pulls the application by immutable registry digest and includes PostgreSQL. +Supply your own domain, HTTPS termination and private secrets. No source checkout, +Maven or host Java is needed. The alternative `caldav-server-0.1.0.jar` requires +Java 25 and PostgreSQL 18.6 managed by the administrator. + +## Compatibility + +Qualified real-editor profiles cover Thunderbird 159.0 with system and explicit +America/Vancouver and explicit Europe/Berlin, and GNOME Calendar 50.0 / Evolution +Data Server 3.56.2 with America/Vancouver. See +[compatibility details](../observance-until-compatibility.md), including the +existing local-observance `UNTIL` repair and synchronization Depth behavior. + +## Important limitations + +The account interface is not a calendar editor. CardDAV, scheduling/invitation +delivery, alarm execution and automatic failover are unsupported. See +[calendar semantics](../calendar-semantics.md) for the supported protocol profile. + +## Documentation + +- [Installation](../../deployment/INSTALL.md) +- [Administration, security, backup and restoration](../operations.md) +- [Client qualification](../../qualification/evidence/releases/0.1.0/verification.json) +- [Release manifest](https://github.com/busystack/caldav-server/releases/download/v0.1.0/release-manifest.json) diff --git a/docs/security-verification.md b/docs/security-verification.md new file mode 100644 index 0000000..4b9f615 --- /dev/null +++ b/docs/security-verification.md @@ -0,0 +1,44 @@ +# Security verification checklist + +This application-specific checklist uses [OWASP ASVS 5.0.0](https://asvs.owasp.org/) +and its [versioned requirements](https://github.com/OWASP/ASVS/blob/v5.0.0/5.0/docs_en/OWASP_Application_Security_Verification_Standard_5.0.0_en.csv). +It is not a complete per-requirement assessment, an ASVS-level claim or external +certification. Focused evidence covers the named checks, not an entire chapter. + +[Protocol correction traceability](standards-corrections.md) records the specific +passkey owner/backup-state, metadata and input-validation regressions. The +[qualification index](../qualification/evidence/README.md) identifies actual source, +artifact and client scope; the release manifest identifies final CI and distribution +provenance. Physical authenticators and broader independent security assessment +remain separate work. + +| ASVS 5.0.0 reference | Application scope and current evidence | Assessment or remaining work | +| --- | --- | --- | +| 1.2.4 | Production JDBC queries use bound parameters for account, resource, membership and credential values. Store/workflow tests exercise cross-account authorization and rollback. | Source/focused evidence; independent query/injection review remains. | +| 1.5.1 | DAV XML is untrusted protocol input. iCalendar parsing is a separate parser boundary; URL-bearing data must not trigger retrieval. `CalendarContentTest` checks a custom VTIMEZONE/TZURL against a local non-fetch sentinel. | `DavXmlTest`, `DavController` bounded body reader and actual HTTPS `DavHttpIT` cover DTD/entity rejection, nesting, malformed and streamed oversized XML independently of iCalendar tests. External review remains. | +| 3.3.1–3.3.4 | Session cookies are Secure, HttpOnly and SameSite=Lax; cross-site OIDC needs a safe top-level callback. Session values remain out of pages. | Partial: current `JSESSIONID` name lacks the ASVS cookie prefix. A `__Host-` cookie also conflicts with non-root cookie paths; a documented secure-prefix decision is required. Distinct-site browser callback qualification is recorded separately. | +| 3.5.1 | Browser mutations keep Spring CSRF protection. DAV uses a separate non-cookie chain with a strict browser Origin boundary; consent and explicit identity linking are protected browser operations. `SecurityBoundaryIT` and workflow tests exercise these boundaries. | Focused evidence; complete browser cross-origin matrix and independent review remain. | +| 6.2.1–6.2.12 | Local passwords use a 14-character minimum, no composition rule, exact verification and a 72-byte UTF-8 maximum; forms support password managers. Users change passwords after recent MFA and current-password verification. | Partial: no common/breached-password blocklist or contextual-word list; 72-byte BCrypt input limit cannot accept 64 arbitrary Unicode characters. Do not claim all password requirements. | +| 6.3.3, 6.4.3–6.4.4 | Sensitive account/client/collection changes require recent primary and actual WebAuthn factors. Initial factor enrollment requires primary authentication; operator recovery is local, identity-verified and clears old access. | `PasskeyPersistenceIT`, workflow tests and final local/upstream/restore CTAP2 browser evidence; physical devices and operational identity-proofing review remain. | +| 6.5.1–6.5.6 | Recovery codes are generated from secure randomness, stored as high-entropy verifiers, expire and are consumed transactionally; concurrent use has one winner. Factor removal/recovery revokes client access. | Focused `AccountSecurityWorkflowsIT` evidence; final browser issuance/anonymous denial/use/replay/reset and fresh MFA passed, followed by actual local/federated operator HTTPS restoration; operational identity-proofing review remains. No TOTP or push mechanism is advertised. | +| 7.2.1–7.2.4, 7.4.1–7.4.3 | Spring Session JDBC stores sessions; authentication rotates the identifier. Account generation checks prevent delayed logins/requests reviving reset access. Logout, disable, password change and recovery invalidate durable sessions. | The recorded HTTPS/PostgreSQL workflow and browser same-cookie/session/passkey restart/disable profiles passed; restored old sessions/factors were denied before fresh enrollment and durable restart. | +| 7.3.1–7.3.2 | Browser inactivity expires after 30 minutes; sensitive factors must be within ten minutes. Upstream identity verification has a configurable maximum of 24 hours. | Partial: a distinct absolute browser-session lifetime is not implemented. Recent-factor checks and upstream bounds do not substitute for that requirement. | +| 8.2.1–8.2.3 | Stable principal UUIDs, ownership and READ/WRITE memberships are checked in services/storage; application administration does not imply calendar ownership. | Actual HTTPS `DavHttpIT` and PostgreSQL `GroupServiceIT`/`DavStoreIT` cover discovery, direct resources, queries, multiget, freebusy, sync, ACL reports, group membership and revocation. The correction regressions additionally cover mixed-success multiget/expansion, paged sync with permission changes, large group/grant maintenance and atomic namespace transfers. See the current evidence index for executed counts; independent review remains. | +| 10.1.2, 10.2.1 | Spring OIDC client uses session-bound state/nonce/PKCE; explicit identity linking binds recent MFA intent to the matching callback state. Trusted issuer and subject determine identity; email and upstream roles do not grant ownership/admin. | The recorded real HTTPS Keycloak and identity persistence tests passed; final distinct-site Chromium organization+passkey callback/restart/disable and same-provider restoration profiles passed. | +| 10.4.1–10.4.6, 10.4.8–10.4.9 | Embedded Spring issuer permits approved public native code+S256 clients. JDBC grants and opaque reference tokens have durable rotating refresh families, replay revocation, absolute expiry and account-state enforcement. Users revoke grants in the browser. | Final `OAuthHttpIT` and ledger tests passed replay/concurrency/removal boundaries. Final browser S256 consent/code/refresh/grant-revocation and missing-CSRF 403 passed, followed by fresh grants after restoration with old credentials denied. | +| 10.4.7, 10.4.10, 10.4.13–10.4.16 | Open dynamic registration, confidential downstream clients, PAR, sender-constrained tokens and authorization-details are outside the selected public-native opaque bearer profile. | These mechanisms are not advertised. ASVS L3 requirements are not claimed; optional upstream OIDC is a separate confidential-client role. | +| 11.4.2 | Spring BCrypt cost 12 verifies human passwords and upgrades lower costs through its supported hook. Generated 256-bit DAV secrets use versioned, UUID/purpose-bound SHA-256 verifiers with constant-time comparison and a conditional legacy BCrypt upgrade; random OAuth/recovery values use framework generators/secure randomness and purpose-specific persisted state. | Focused lifecycle tests; independent cryptographic/configuration review and workload tuning remain. No custom authentication cryptography is implemented. | +| 12.2.2, 12.3.2–12.3.4 | Validated HTTPS public base/issuer; deployment terminates TLS at a restricted proxy or embedded TLS. Tests use a disposable CA with normal hostname and chain validation; provider trust is scoped to that fixture. | Final embedded HTTPS, Nginx `/team` browser, real cross-site provider and restored-provider profiles passed; operator certificate lifecycle remains an operational responsibility. No trust-all production switch exists. | +| 13.2.1, 13.3.1–13.3.4 | Compose mounts secrets from a host-private directory; application PostgreSQL role is a separate non-superuser. Bootstrap/recovery password files are owner-only. Images/repository contain no generated credentials. | Partial: static database password and file-backed secret lifecycle differ from the stronger ASVS backend-credential/secret-service model. Rotation and host access remain operator responsibilities. | +| 13.4.1–13.4.5 | Runtime image excludes source-control metadata/test code. Exact DAV firewall allowlist excludes TRACE/locking; only local health is exposed and the documented proxy blocks management paths. | Final non-root/read-only container, actual proxy management concealment/method/body-limit checks and restart/restore profiles passed. | +| 15.1.2, 15.2.1–15.2.4 | Pinned build/plugins/images, dependency management and runtime CycloneDX inventory; required OSV gate has pagination/feed-failure tests and a vulnerable positive control. | Apache-2.0 project licensing, retained dependency notices and the resolved Spring Session publishing-metadata error are documented in [third-party licenses](third-party-licenses.md). Source/JAR/container delivery is inspected. Java advisories and final-container OS packages have separate required scan records; feed failures are failures, not clean scans. See the [current advisory results](../qualification/security/README.md). | +| 15.2.2 | Resource/form/body, parser-callback, database-pool, ceremony and throttle bounds exist. Query/recurrence work must fail explicitly when its budget is exceeded. | Metadata projections, 128-row sync candidate batches and query body batches of at most 16 rows/4 MiB avoid whole-collection calendar-body retrieval. Existing originals spend shared candidate work; generated recurrence instances and actual query results have separate limits. Dead properties have atomic count/byte admission, selective SQL reads and name-only retrieval. XML construction reserves work before attaching/adopting nodes. `DavPropertyBoundsIT` passed at a 256 MiB heap, including concurrent stress and selective SQL/allocation checks. Current [measurements](performance.md) state the executed configuration; no unmeasured availability or capacity is inferred. | +| 16.2.5, 16.3.1–16.3.4, 16.4.1–16.4.3 | Durable bounded account security audit records actions/outcomes and hashed peers. HTTP fixture logs omit query strings, authorization values and contents. Errors suppress stack traces and parser payloads. | Partial: complete layer log inventory, coverage review, tamper-resistant off-host handling and operator retention policy are not qualified. No remote telemetry is required. | +| 16.5.1–16.5.3 | Explicit transaction rollback, protocol error mapping and local account disable; upstream outage does not invent a local-password fallback. Advisory/feed failures are visible failures. | Focused storage/workflow/Keycloak evidence; raw HTTPS protocol failures also exercise typed calendar/DAV preconditions, property dependency errors, ACL denial and bounded-work errors; independent review remains. | +| V5, V9, V17 | Calendar uploads are bounded iCalendar objects, with ATTACH data retained without retrieval. No general file service or WebRTC exists. CalDAV uses opaque tokens; upstream signed OIDC tokens are verified by Spring. | Applicable calendar upload/response and OIDC-token controls remain in scope; unrelated general file-service/WebRTC features are absent. | + +The project is licensed under Apache-2.0. Project and applicable third-party license/notice delivery, normalized Spring Session conclusions and exact artifact provenance are tracked in [the distribution inventory](third-party-licenses.md); historical evidence that predates the owner's license decision is retained unchanged. + +Default DAV synchronization accepts the narrowly bounded `Depth: 1` accommodation only with XML `sync-level=1`; ordinary omitted/zero depth remains supported and explicit `false` enables strict mode. This deliberate RFC 6578 header exception changes neither collection scope nor authorization. Continuations are scoped to actor, collection generation, recovery epoch and retained history, and every page checks current access. Immutable version snapshots and a fixed high watermark prevent a page token advancing beyond changes absent from the response. Restore clears continuation state with the existing account/credential invalidation policy. + +The current qualified source repeats Thunderbird 157.0.1/native-provider and python-caldav 3.6.0 cross-client calendar/task round trips without an enabling depth override, including recurring exceptions, all-day/floating data, alarms, extensions and completed tasks. Current root/proxy/Keycloak and local/federated restoration records identify actual configuration, fresh post-workload public refresh/MFA and denial of restored old credentials/sessions/sync state. Java advisories cover 112 runtime Maven versions with no returned advisories; the distinct final-image OS scan reports 46 MEDIUM and 12 LOW findings, zero HIGH/CRITICAL/UNKNOWN and no suppressions. Manual graphical setup, physical authenticators and additional platforms remain unexecuted. Independent review is not an added certification prerequisite. diff --git a/docs/standards-corrections.md b/docs/standards-corrections.md new file mode 100644 index 0000000..8e5381d --- /dev/null +++ b/docs/standards-corrections.md @@ -0,0 +1,68 @@ +# Protocol correction traceability + +This record maps previously reported protocol and security findings to their +application corrections and maintained regression tests. It is engineering +traceability, not certification or universal interoperability. + +The independent review covered 17 finding groups. Subsequent verification closed +sixteen original groups and identified two residual input-validation obligations: +original BYDAY/duration grammar and exactly empty public-client secrets. Their +regressions are retained in the calendar and OAuth test suites. Historical result +classifications, failed attempts, artifact identities and detailed audit reports +remain in the [evidence archive](../qualification/evidence/README.md#historical-records). +Current executable/client scope is recorded in the version-specific qualification +receipt; final distribution identities come from the release manifest. + +## Corrections and regression coverage + + + +The first submission corrected each original counterexample and regression case. Independent re-verification then closed sixteen groups and specified the residual CAL-01 input classes addressed above. This table retains the earlier corrections and named-version evidence. Exact file/method/line references, standards sections, baseline records and fix commits remain in the [earlier verification manifest](https://github.com/busystack/caldav-server/blob/f3ba60d360117c44b3dfeb6dc9848778a98ef06a/qualification/evidence/standards-corrections/verification.json). + +| ID / applicable normative sections | Original counterexample and root cause | Application correction | Regression evidence / fix commit / independent outcome | +| --- | --- | --- | --- | +| CAL-04 — RFC 5545 §§3.3.5,3.6.5; RFC 4791 §§7.4,7.10,9.6.5,9.9 | Valid future observance replacement resolved local noon in 2040 to 11Z, omitting its correct 10Z range; current-era last rules outlived finite materialization. | `ObservanceZoneRules` builds chronological eras and proves a final annual tail; unproven coverage fails explicitly. Submitted VTIMEZONE remains authoritative. | `CalendarAuditCorrectionsTest`, `DavHttpIT`; original isolated timezone probe; packaged audit-corpus restoration. Fix: `00bedd0`. Fresh original counterexample: corrected (details in manifest). | +| CAL-01 — RFC 4791 §5.3.2.1; RFC 5545 §§3.3.10,3.6.1,3.6.2,3.8.2 | Nine invalid event/task/RRULE fixtures accepted 201 and persisted; generic library validation missed cross-property and lexical rule constraints. A follow-up fresh probe at 5e15eb6 also found that timezone-only properties bypassed the shared constraints. | `CalendarConstraints`, `RecurrenceRuleSyntax` and handler integration check original syntax before normalization and before persistence; transfers preflight content, and `FloatingTimezone` shares observance constraints for properties and request timezones. | Original nine fixtures, legal neighbors, broader normative corpus, `CalendarAuditAtomicityIT`, legacy transfer regressions and independent PUT/GET records. Fix: `00bedd0`, `46d8687`, `279ae33`, `028bcd5`. Fresh original counterexample: corrected (details in manifest). | +| CAL-02 — RFC 5545 §§2,3.1,3.4; RFC 5234 §2.3 | Legal lowercase BEGIN/END rejected 403 before later identifier handling. | `CalendarParsingInput` canonicalizes only structural identifiers in a derived reader; raw input and case-sensitive values are preserved. | Original lowercase fixture, mixed hierarchy and mismatched-component controls. Fix: `00bedd0`. Fresh original counterexample: corrected (details in manifest). | +| CAL-03 — RFC 5545 §§3.3.11,3.8.1.2; RFC 4791 §§7.8,9.7.2,9.7.5 | Encoded list-TEXT was searched directly; accepted escaped category returned no matching href. | `CalendarTextValues` and typed query extraction decode TEXT once before required collation; URI data is not indiscriminately unescaped. | Both collations, escaped semicolon/comma/backslash/newline, repeated properties, positive/negated matches and SUMMARY controls. Fix: `00bedd0`. Fresh original counterexample: corrected (details in manifest). | +| CAL-05 — RFC 5545 §3.6.4; RFC 4791 §7.10 | Every generated freebusy had two DTSTAMPs: constructor and application each supplied one. | `DavController.freeBusy` uses the constructor timestamp once. | Complete generated calendar parsing/validation for empty/nonempty freebusy and corrected future timezone. Fix: `46d8687`. Fresh original counterexample: corrected (details in manifest). | +| AUTH-01 — WebAuthn L3 §7.2 step 6 | Real anonymous signed assertions accepted missing/unrelated handles; signature still selected the stored key owner. | `AccountRelyingPartyOperations` checks persistent owner, required handle, saved allow-list and identified account before one delegated verification. | Original signed HTTP probe; `PasskeyHttpIT` binding, allow-list, account and cryptographic controls. Fix: `4161cdd`. Fresh original counterexample: corrected (details in manifest). | +| AUTH-02 — WebAuthn L3 §7.2 step 24 | Accepted BS changes remained unstored; initial audit JDBC conclusion was static. | Supported relying-party/repository composition defers the framework update and saves verified BS/UV/counter/last-used once after the full decision, under existing account/revocation checks. | Real HTTPS/JDBC false→true→false, rejected-state immutability and separate JVM persistence in `PasskeyHttpIT`/`PasskeyRestartIT`. Fix: `4161cdd`. Corrected state transitions verified through fresh signed HTTPS/JDBC and restart tests; original method baseline preserved. | +| AUTH-03 — RFC 9728 §§3,5.1; RFC 6750 §3 | Invalid Bearer challenge advertised metadata returning anonymous 302/authenticated 404. | Supported application RFC 6750 entry point omits the unwanted optional metadata promise; authorization-server metadata is retained. | Actual root/prefix 401 challenges, 403 response and exact-framework challenge controls, revocation and AS metadata. Fix: `4161cdd`. Fresh original counterexample: corrected (details in manifest). | +| DAV-01 — RFC 4918 §17; RFC 4791 §1.2; RFC 3744 §10 | Harmless command/filter extensions returned 400/403; unfiltered child counts rejected unknown subtrees. | Contextual immediate-child selection and scalar text handling ignore whole extension subtrees without promoting descendant commands; stored XML remains untouched. Defined query timezones are validated before enumeration, including empty targets. | Paired PROPPATCH/ACL/filter/projection/principal-report controls, smuggling negatives, XML language/mixed content. Fix: `46d8687`, `028bcd5`. Fresh original counterexample: corrected (details in manifest). | +| DAV-02 — RFC 3744 §§5.3,5.4,8.1.1 | Explicit abstract grant accepted 200, abstract effective entries returned and owner all omitted. | `DavPrivileges` shares supported tree, nonabstract current privileges and coarse grant validation; no widened permission system. | Every resource kind; owner/READ/WRITE/group actors; atomic no-abstract rejection and unchanged effective access. Fix: `46d8687`. Fresh original counterexample: corrected (details in manifest). | +| DAV-03 — RFC 3744 §§5,5.2; RFC 4918 §§9.1,14.18 | Required group 404; owner propname omitted the defined ACL. | Protected empty group; distinct explicit/name/allprop inventories; owner-only ACL visibility without loading values for names. | Owner/reader property statuses and name-only budget controls. Fix: `46d8687`. Fresh original counterexample: corrected (details in manifest). | +| DAV-04 — RFC 4791 §§7.5,7.5.1 | Home/resource advertised matching reports but returned collation 404. | Shared actual report capability drives protected collation values/names for calendars/homes/folders/objects. | Advertisement, explicit property and write protection on all four target kinds; actual collation queries. Fix: `46d8687`. Fresh original counterexample: corrected (details in manifest). | +| DAV-05 — RFC 3744 §§3,3.1 | Unshared event GET 403 but OPTIONS 200 disclosed generic capabilities. | Target resolution/current read authorization precedes kind-specific OPTIONS response. | Owner/READ/WRITE/unrelated/revoked/nonexistent and discovery controls. Fix: `46d8687`. Fresh original counterexample: corrected (details in manifest). | +| DAV-06 — RFC 4791 §§1.3,5.2.10 | Invalid timezone 409 used generic DAV:valid-property-value. | Typed property errors retain CALDAV:valid-calendar-data and atomic dependent failures. | Invalid/multiple/inappropriate timezone values, valid control, sibling 424 and unchanged properties. Fix: `46d8687`, `279ae33`. Fresh original counterexample: corrected (details in manifest). | +| DAV-07 — RFC 4791 §§5.3.1,9.2 | Nonempty bare mkcalendar returned 201 despite missing mandatory set/prop. | Recognized structure required for a supplied XML body after contextual extension handling. | Missing/duplicate children reject without creation; empty HTTP body and legal empty/populated prop controls. Fix: `46d8687`. Fresh original counterexample: corrected (details in manifest). | +| DAV-08 — RFC 9110 §15.5.6; RFC 4791 §§1.3,5.3.1; RFC 3744 §§7.1.1,8.1.1 | Missing 405 Allow; missing existing-resource/privilege conditions; ACE count 507 and duplicate 400 lost specific contracts. | Operation-aware status/QName/header paths; report/sync limits keep their own behavior. | Exact 405 headers, resource-must-be-null, privilege children, limited-number-of-aces and no-ace-conflict with no mutation. Fix: `46d8687`. Fresh original counterexample: corrected (details in manifest). | +| DAV-09 — RFC 4791 §9.6 | PROPFIND returned report-only calendar-data as a successful property. | Explicit rendering context separates DAV properties from report selectors/data. | Explicit 404 and absent allprop/propname data; authorized query/multiget/sync retain calendar-data. Fix: `46d8687`. Fresh original counterexample: corrected (details in manifest). | + + + +## Verification gaps and dispositions + +The following paragraphs distinguish completed local verification from remaining scope limits. The manifest records exact commands/counts and preserves failed and superseded attempts. + +H-01 uses the original fixtures plus a standards-derived positive/negative calendar corpus. It checks RRULE structure and BY restrictions, conditional date/duration relationships, parameters, alarm anchors/repeats, attachment representation, numeric/period/date boundaries, CALSCALE, structural case, folded UTF-8 and TEXT interpretation. Split UTF-8 folding is unfolded before strict decoding and encoded faithfully in report XML; GET remains byte exact. A general `validate()` call is not treated as proof of all optional extension grammars. The matrix identifies the clauses exercised and remaining broad per-property grammar and specific interpretation limits. + +H-02 uses disposable embedded root and prefixed Nginx HTTPS arrangements, configured canonical discovery/issuer routes and hostile forwarded headers. Actual TLS negotiation checks supported 1.2/1.3, obsolete versions/key exchanges, trust and hostname identity. Production DNS, operator certificates and network isolation have not been provided and are not falsely reported as verified or defective. + +H-03 extends the actual packaged `backup.sh`/`restore.sh` workflow with nine original accepted fixtures, nine rejected controls, immutable history/properties, stable and continuation tokens, representative shares and the corrected future timezone. It verifies old sessions/DAV/OAuth/factors/state cannot revive, fresh recovered access preserves bytes, and revoked READ aliases are denied. No schema change is required by these corrections; existing forward migrations and same-version/V9 upgrade checks remain enabled. + +H-04 adds a real password→passkey→consent→code/S256→token/refresh/revoke journey, token-cache and request-boundary controls, signed registration/assertion negatives, persistent verified backup-state changes/restart, and a disposable HTTPS OIDC issuer serving genuine signed ID tokens, discovery, JWKS and UserInfo. Wrong issuer/state/nonce/audience/azp/signature/expiry/algorithm/key and subject mismatch are tested, with legal controls and key rotation. These are not provider mocks. The existing real Keycloak suite and packaged browser profiles remain separate checks. Fresh H-04 negatives also exposed registration exception translation and malformed token-parameter handling; application integration now returns the intended failure without login redirects, persisted credential changes or lost MFA continuity. These are developer verification discoveries, not newly assigned independent finding IDs. Physical authenticators and optional authenticator extensions remain unverified. The additional-audience trust-policy interpretation in AUTH-R078/R085 is retained separately from the executed issuer/audience/azp negatives. + +H-05 records URI alias authorization, property/snapshot interleavings and size boundaries. For freebusy, RFC 4791 §7.3 specifically requires collection timezone and §9.11 does not define a request-timezone child; RFC 4918 §17 therefore ignores that unexpected subtree. Calendar-query retains its defined request timezone. A clean floating-event HTTP control verifies the distinction. For mixed multi-valued parameter negation, RFC 4791§9.7.3 permits text-match in its grammar but omits that case from its prose; §9.7.5 does not place the list quantifier. The current per-value reading has support in RFC 5545§3.2.11’s individual MEMBER values. CAL-R086 retains this precise interpretation gap; no mandatory defect is inferred from the ambiguity. PUT body limits retain transport 413 and identify CALDAV:max-resource-size where applicable; generic report/sync limits are not globally remapped. + +H-06 retains the large-group test enabled and preserves all failed executions. Its original transaction timeout was not an assertion proving a protocol defect. Fresh isolated/full-suite/CI outcomes are recorded with actual counts; production transaction limits and assertions are not weakened to manufacture success. No new performance qualification is inferred. The current packaged CI correctness profile uses `--clients`, without the historical capacity/latency workload. + +## Preserved decisions and nonblocking recommendations + +Java 25, Spring Boot 4/Security 7, PostgreSQL, embedded authentication and optional Keycloak remain. The original correction pass retained dependency and base-image pins, Apache-2.0 delivery and third-party notices. The residual qualification refreshes only the official Temurin base digest for the confirmed OS advisory recorded in the archived packaging evidence; the Java release and Maven dependencies remain pinned. Official iCal4j artifacts remain unmodified; application-owned adapters use supported library APIs. No Digest, primary-password DAV, scheduling, CardDAV, locking, file hosting, non-Gregorian execution or notification feature is added. + +The approved default sync Depth: 1 accommodation applies only to sync-collection with XML sync-level=1; missing/Depth: 0 and explicit `allow-sync-depth-one=false` remain separately verified. HTTPS DAV credentials/Bearer remain the selected authentication profile, with the documented historical Digest deviation. Those decisions are not among the 17 implementation defects. + +Leap-second fallback remains the explicit narrow SHOULD exception described in [calendar semantics](calendar-semantics.md). Unsupported RSCALE now fails explicitly without adding execution. allprop omits ACL section 5 live properties and sync-token per their recommendations, while explicit/include/propname remain available. No-op direct grants and identical ACL replacements preserve sync state; real authorization changes still invalidate principal-dependent continuation snapshots. Authorized conflict-href diagnostics are deferred as a nonblocking SHOULD recommendation; the precise conflict condition remains available, and an inaccessible conflicting URL is never disclosed. + +Previously accepted invalid calendar objects remain retrievable through authorized GET and repairable with ETag/conditional PUT or DELETE. Invalid collection calendar-timezone values are repaired through authorized PROPPATCH. Do not batch-delete or rewrite them. No new migration or database recreation is prescribed. diff --git a/docs/standards-review.md b/docs/standards-review.md new file mode 100644 index 0000000..fe0addb --- /dev/null +++ b/docs/standards-review.md @@ -0,0 +1,60 @@ +# Standards updates and errata review + +Reviewed 2026-10-27 against the RFC Editor’s [machine-readable errata inventory](https://www.rfc-editor.org/errata.json), individual linked records and RFC information pages. Verified corrections inform implementation; Reported and Held records are considered separately and do not silently amend the contract. Rejected proposals are not adopted. This engineering review is not independent certification. See [conformance](conformance.md) for implementation/test traceability and the qualification record for executed results. + +## Inventory checked + +| RFC | Verified | Held | Reported | Rejected | +| --- | --- | --- | --- | --- | +| [RFC 9110](https://www.rfc-editor.org/errata/rfc9110) | 6 | 0 | 2 | 6 | +| [RFC 4918](https://www.rfc-editor.org/errata/rfc4918) | 3 | 3 | 3 | 0 | +| [RFC 4791](https://www.rfc-editor.org/errata/rfc4791) | 17 | 0 | 6 | 1 | +| [RFC 3744](https://www.rfc-editor.org/errata/rfc3744) | 1 | 0 | 0 | 0 | +| [RFC 5397](https://www.rfc-editor.org/errata/rfc5397) | 0 | 0 | 0 | 0 | +| [RFC 6764](https://www.rfc-editor.org/errata/rfc6764) | 0 | 0 | 0 | 0 | +| [RFC 6578](https://www.rfc-editor.org/errata/rfc6578) | 0 | 0 | 0 | 0 | +| [RFC 5545](https://www.rfc-editor.org/errata/rfc5545) | 22 | 4 | 2 | 11 | +| [RFC 6868](https://www.rfc-editor.org/errata/rfc6868) | 1 | 0 | 1 | 0 | + +## Relevant verified corrections + +| Records | Application decision and evidence | +| --- | --- | +| RFC4918 [1068](https://www.rfc-editor.org/errata/eid1068), [1519](https://www.rfc-editor.org/errata/eid1519), [1430](https://www.rfc-editor.org/errata/eid1430) | DAV If has its own bounded Boolean-list parser; XML hrefs use canonical configured URLs; dot segments are rejected; Destination is independently validated. MKCOL is implemented for ordinary container collections. `DavIf`, `DavPaths`, `DavHttpIT`, `DavStoreIT`. Locking remains outside the advertised profile. | +| RFC4791 [1002](https://www.rfc-editor.org/errata/eid1002), [1476](https://www.rfc-editor.org/errata/eid1476) | Collection creation checks parent authority; privilege failures use DAV’s namespace and structured resource/privilege details. Namespace-aware DOM serialization supplies explicit correct namespaces. `DavController`, `DavErrors`, `DavHttpIT`. | +| RFC4791 [4161](https://www.rfc-editor.org/errata/eid4161) and verified example corrections 4153–4160, 4162–4167 | Task alarms with a DUE-only parent use END; accepted object bytes retain embedded timezones. Report projection retains required timezone content or converts derived expanded date values to UTC. Task ranges and alarms have semantic tests; protocol responses follow requested property sets instead of copying example responses. | +| RFC5545 [1911](https://www.rfc-editor.org/errata/eid1911), [7691](https://www.rfc-editor.org/errata/eid7691), [7945](https://www.rfc-editor.org/errata/eid7945) | Established content-line parser handles quoted parameters and CRLF properties; authoritative bytes are retained. Parameter semantics and errata fixtures verify comma-bearing quoted ALTREP and multiple REQUEST-STATUS lines. | +| RFC5545 [1913](https://www.rfc-editor.org/errata/eid1913), [3779](https://www.rfc-editor.org/errata/eid3779), [4271](https://www.rfc-editor.org/errata/eid4271) | iCal4j generates RRULE instances. Numeric BYDAY restrictions remain library validated. Invalid dates are omitted; nonexistent local times use the pre-gap offset under §3.3.5 and count toward COUNT. The review removed an application override that incorrectly skipped gap instances. Regression cases distinguish gaps from invalid dates. | +| RFC5545 [2677](https://www.rfc-editor.org/errata/eid2677), [2039](https://www.rfc-editor.org/errata/eid2039), [6109](https://www.rfc-editor.org/errata/eid6109) | Multiple REQUEST-STATUS properties remain valid. Absolute alarm triggers carry VALUE=DATE-TIME. DATE event end is exclusive, including multi-day events. Calendar semantic/errata tests cover these cases. | +| RFC5545 [1916](https://www.rfc-editor.org/errata/eid1916), [3740](https://www.rfc-editor.org/errata/eid3740), [3883](https://www.rfc-editor.org/errata/eid3883), [4149](https://www.rfc-editor.org/errata/eid4149) | Corrected examples inform fixtures: URI values remain URI data, finite zoned UNTIL uses UTC, VTIMEZONE onset covers relevant dates, generated free/busy contains UID and DTSTAMP. Library validation and semantic/free-busy tests apply; examples are not used as protocol grammar. | +| RFC6868 [4383](https://www.rfc-editor.org/errata/eid4383) | Correction concerns a vCard GEO example; CardDAV/vCard are outside scope. iCalendar parameter caret encoding is tested separately in `CalendarParameterSemanticsTest`. | +| RFC9110 [7138](https://www.rfc-editor.org/errata/eid7138), [7306](https://www.rfc-editor.org/errata/eid7306), [7419](https://www.rfc-editor.org/errata/eid7419) and editorial 7105,7109,8268 | No custom Accept quality/range parser is introduced. Range support is not advertised; full representation responses are permitted. Explicit UTF-8 response types and strong stored-byte validators are tested. Servlet/Spring owns general HTTP message handling. | +| RFC3744 [235](https://www.rfc-editor.org/errata/eid235) | Editorial pointer to issues, not a replacement normative algorithm. Principal/ACL reports and effective privilege checks are traced in the conformance matrix. | + +Other verified RFC5545 editorial corrections (2497,4414,2038,2516,2527,3747,5602,5794,7332) affect typography, grammar references, examples or the contents; they introduce no separate application mechanism. The review does not elevate their original uncorrected examples over the grammar. + +## Reported and held records + +* RFC4791 4840: response compression is disabled for DAV; GET ETags identify the served authoritative bytes. 5481 reinforces retaining VTIMEZONE for task projections. 5846’s proposed writable component set is not adopted: component capability remains protected and is chosen at collection creation. 8761–8763 concern example property-set inconsistencies; responses use the actual requested properties. +* RFC4918 1207/1635 concern locking, outside the advertised profile. 1371 concerns failed-property status prose: atomic PROPPATCH has per-property errors and 424 dependencies. 4431 requests clarification of live-property definitions; the application maintains an explicit live/dead registry. 4435/4440 do not replace normative COPY semantics; source absence, explicit destinations and overwrite behavior have protocol/database tests. +* RFC5545 3038’s UTC DTSTAMP clarification is consistent with library validation. 5505’s quoted TZID concern is handled by the established parameter parser. 6316’s proposed EXDATE type clarification does not license lossy data rewriting. 8398 points out VTIMEZONE EXDATE inconsistency; accepted data is preserved and bounded observance generation remains library driven. 2495/4626 are editorial. Rejected alternative proposals are not treated as corrections. +* RFC6868 4374 is Reported. The implementation decodes left to right, once: literal ^^n remains ^n rather than becoming a newline through repeated replacement; this has a regression test. +* RFC9110 9162/9164 are Reported clarifications to header-list prose and normalized ABNF. They do not change the parser language. Duplicate ETag and DAV If fields are combined in order; duplicate conditional-date fields are invalid and ignored; duplicate singleton DAV fields fail explicitly. + +## Correction review of protocol boundaries + +The correction pass rechecks [RFC 6578 §§3.3, 3.6–3.7](https://www.rfc-editor.org/rfc/rfc6578.html#section-3.3): standard synchronization uses header depth zero, and truncation is represented within the multistatus result with continuation state. The default `Depth: 1` acceptance is a deliberate header exception restricted to XML `sync-level=1`; it does not claim strict compliance with §3.3. Explicit strict configuration rejects nonzero depth. No user-agent detection or broader traversal is introduced. + +[ADR 0004](adr/0004-resumable-synchronization.md) defines the fixed high-watermark traversal, immutable resource/property versions, replayable continuation boundaries and commit-safe collection revisions. Row and serialized/construction-work budgets bound each report. The returned continuation covers only emitted changes; later writes are obtained after that traversal completes. Current authorization, account status, collection generation, retention and recovery state remain mandatory on every page. Complete bounded PROPFIND responses retain explicit limit errors; synchronization supplies the resumable path. + +[RFC 4918 §4.3](https://www.rfc-editor.org/rfc/rfc4918.html#section-4.3) requires property language context to survive storage. MKCALENDAR and PROPPATCH now detach properties with their effective in-scope `xml:lang`, including nearer overrides and explicit empty values, while retaining namespace bindings, mixed content and whitespace. `DavPropertyBoundsIT` exercises real HTTP persistence and COPY/MOVE. Previously discarded context cannot be reconstructed from old stored values. + +Property retrieval selects only requested values; `propname` retrieves names directly. Atomic aggregate storage limits and XML construction budgets supplement parser and final serialized-output limits. One namespace invariant covers fixed principal/calendar components, encoded length, segment size and the reserved resource leaf. COPY/MOVE preflight resulting descendants before replacing destination state. Namespace values must remain representable as [XML 1.0 character data](https://www.w3.org/TR/xml/#NT-Char): the additional rejection is limited to BMP U+FFFE/U+FFFF, with U+FDD0 and supplementary U+1FFFE retained and tested. This does not classify every Unicode noncharacter as XML-invalid. Expected unavailable members of valid multiget/property-expansion reports retain their own statuses; invalid targets, malformed requests, database faults and computation limits keep their distinct request-level semantics. These corrections have named regressions in the conformance matrix; executed results are linked from the current evidence index rather than inferred from source inspection. + +## Updates and selected extension boundary + +RFC4791’s [information page](https://www.rfc-editor.org/info/rfc4791/) lists RFC5689,6638,6764,7809,7953,8996. RFC6764 discovery is implemented. TLS1.2/1.3 at the documented proxy arrangement follows RFC8996’s removal of TLS1.0/1.1. Extended MKCOL, automatic scheduling, timezone-by-reference and availability components are not advertised. Explicit embedded VTIMEZONE remains required for TZID-bearing accepted resources. Adding a dependency class does not enable an extension. + +RFC5545’s [information page](https://www.rfc-editor.org/info/rfc5545/) lists RFC5546,6868,7529,7953,7986,9073,9074,9253. RFC6868 parameter encoding is implemented. iTIP/scheduling execution, availability and specialized new-component services remain outside this calendar-access profile; valid supported-resource extension properties are retained without promising to execute them. Non-Gregorian RSCALE processing is not advertised. RFC4918’s RFC5689 update does not require advertising extended collection creation. RFC5397,6578,6764 had no errata records in the checked inventory. + +Recheck the linked primary inventories when updating this contract or its libraries. Current test results, deliberate Digest/security and optional sync-Depth compatibility deviations remain explicit in the conformance matrix; no claim of literal compliance with every historical HTTP/WebDAV provision is made. diff --git a/docs/third-party-licenses.md b/docs/third-party-licenses.md new file mode 100644 index 0000000..c6714dd --- /dev/null +++ b/docs/third-party-licenses.md @@ -0,0 +1,34 @@ +# Third-party license inventory and review + +The exact runtime module inventory is the generated CycloneDX file, `target/classes/META-INF/sbom/application.cdx.json`, embedded at `META-INF/sbom/application.cdx.json` in the executable JAR. It records each Maven package URL, resolved version and POM-declared license. CI retains this file for the artifact it built. Use [the build instructions](operations.md) to regenerate it; the dependency-management baseline and [version pins](versions.md) are maintained separately. + +The 2026-10-27 final source build contains 112 runtime Maven component entries, all with license metadata. The project root component records Apache-2.0, selected by the owner. Third-party component license declarations remain their original upstream metadata. Counts are observations of that generated artifact, not a substitute for regenerating the inventory after dependency changes. POM metadata is evidence to inspect, not a substitute for the license files and notices in the actual distribution. + +| Specific module/artifact | Observed license evidence and review decision | +| --- | --- | +| `org.springframework.session:spring-session-core:4.1.1` and `spring-session-jdbc:4.1.1` | Published POMs name “Broadcom Foundation License” without a URL; both published JARs include Apache-2.0 in `META-INF/LICENSE.txt`. The [tagged source license](https://github.com/spring-projects/spring-session/blob/4.1.1/LICENSE.txt) is also Apache-2.0. Resolved by the maintainer in [Spring Session issue #3910](https://github.com/spring-projects/spring-session/issues/3910): an unintended internal publishing-tool error changed only POM metadata. The source/header/license files remain Apache-2.0. The tooling was corrected and the issue assigned to 4.1.2 because immutable Central POMs cannot be changed. BusyStack retains BOM-selected 4.1.1, its original POM declarations in the SBOM and the hashes below; its documented reviewed conclusion is Apache-2.0. No dependency file is changed or scanner result suppressed. The issue also identified data-redis 4.1.1, which is not a production dependency here. | +| `org.mnode.ical4j:ical4j:4.4.0` | POM calls the license “iCal4j - License”; the selected source/license is BSD-3-Clause. Its parsing/recurrence use does not make it a CalDAV engine. Official unmodified dependency bytes are locked and verified; historical removal and current integration: [parser patch](ical4j-parser-patch.md) and [timezone patch](ical4j-timezone-patch.md). | +| `ch.qos.logback:logback-core:1.5.38`, `logback-classic:1.5.38` | Metadata includes both EPL-2.0 and LGPL-2.1-only alternatives. Distributed under the EPL-2.0 alternative; both original alternatives and the upstream license declaration are delivered in `META-INF/licenses`. The separately linked JARs are unchanged. Exact upstream sources are linked in `THIRD-PARTY-NOTICES`. | +| `jakarta.annotation:jakarta.annotation-api:3.0.0` | Metadata includes EPL-2.0 and GPL-2.0-with-classpath-exception alternatives; this distribution selects EPL-2.0 and retains the original `META-INF/LICENSE.md` and `NOTICE.md` unchanged. | +| Bedework protocol probe | Only `investigation/protocol` depends on Bedework. Its exact module/transitive inventory and license findings belong to that probe and [ADR 0001](adr/0001-protocol-engine.md); it is absent from the production runtime SBOM. | +| Browser, proxy and Keycloak fixtures | Test-only pinned images carry their own package licenses; neither those images nor their operating-system dependencies are implied to be covered by the Java runtime inventory. See [versions](versions.md). | + +Hashes for the Spring Session discrepancy, captured from the resolved Maven artifacts: + +| Artifact | SHA-256 | +| --- | --- | +| `spring-session-core-4.1.1.jar` | `01b836bfcc5b64f7039c3430980878112a10db5066eda4274a1d3eb874e49733` | +| `spring-session-jdbc-4.1.1.jar` | `669e4388684949edb0c5aef0a7f4e15af86d6c87ffb90df3145f073746870529` | +| Either JAR's `META-INF/LICENSE.txt` | `aac73b3148f6d1d7111dbca32099f68d26c644c6813ae1e4f05f6579aa2663fe` | +| `spring-session-core-4.1.1.pom` | `0dde28a7f8536093e8191cf2c7432318db69f53c3fba8e903a23cf1663e271d0` | +| `spring-session-jdbc-4.1.1.pom` | `bf661ce61020ae171e5495b0b9286ef725a04ebc8ee4e912a0453305bf585526` | + +## Distribution obligations and actual delivery + +The owner selected **Apache License 2.0**. The root `LICENSE` is the unmodified official ASF text, SHA-256 `cfc7749b96f63bd31c3c42b5c471bf756814053e847c10f3eb003417bc523d30`; `NOTICE` identifies incorporated source without inventing a legal entity or transferring third-party copyright. `THIRD-PARTY-NOTICES` describes the independent license alternatives and source availability. Source archives include these files; the executable archive packages them in `META-INF` and retains the original nested dependency JARs, their original notices and licensing text. Dependencies with missing JAR license text are covered by the delivered applicable license material, with additional ASM copyright/conditions and Logback license alternatives in `META-INF/licenses`. + +The two modified iCal4j implementations and their patch payloads have been removed. The official unmodified dependency remains distributed with its BSD notice. The parser/timezone documents retain historical references and explain the independent supported-interface integration. Immutable migrations V3, V4 and V6 retain their original checksum; additional `META-INF/licenses/spring-derived-schemas.txt` identifies the Apache-licensed upstream schema contracts and application changes without modifying an already-applied migration. + +The final image exposes project notices under `/opt/busystack/licenses`, retains the executable archive's dependency notices, records OCI project license/source/revision labels, and supplies the runtime inventory as `runtime-dependency-inventory.json`. Eclipse Temurin's original `/opt/java/openjdk/legal` contains GPL-2.0, Classpath Exception, assembly exception and component notices. Its exact [source distribution](https://github.com/adoptium/temurin25-binaries/releases/tag/jdk-25.0.4.1%2B1) remains identified. Ubuntu package copyright/license files remain under `/usr/share/doc`; their inventory and advisories are checked separately from Maven packages. Linking the application does not relicense the JDK or OS. Corresponding source links for the selected unmodified EPL components are delivered in `THIRD-PARTY-NOTICES`. + +Run `python3 scripts/verify-distribution.py --image busystack-caldav:ci` after committing and building through `python3 scripts/build-image.py --image busystack-caldav:ci`, which derives the version and source revision automatically. It inspects an actual `git archive` source distribution, executable JAR and final image, records nested original notice hashes and compares host/container JAR bytes. The [executed source/JAR/container report](https://github.com/busystack/caldav-server/blob/f3ba60d360117c44b3dfeb6dc9848778a98ef06a/qualification/evidence/correction-final47-distribution.json) passed for source `47b3543`, with Apache-2.0 on the project component and all 112 original runtime component declarations retained. This is reproducible distribution evidence, not a replacement of third-party licenses or an external legal certification. Investigation and qualification dependencies are excluded from production scope; their inventories and image licenses remain separate. diff --git a/docs/versions.md b/docs/versions.md new file mode 100644 index 0000000..fbe16b7 --- /dev/null +++ b/docs/versions.md @@ -0,0 +1,59 @@ +# Dependency and distribution baseline + +The versions below describe the maintained build and runtime inputs. Maven's application `project.version` is the single application-version authority. [Qualification records](../qualification/evidence/README.md) identify actual verified source and artifacts; third-party licenses remain independent of the project license. + +| Component | Selected version | Source and license | +| --- | --- | --- | +| Eclipse Temurin OpenJDK | 25.0.4.1+1, Java 25 LTS | [Adoptium releases](https://adoptium.net/temurin/releases/?version=25), [OpenJDK license](https://openjdk.org/legal/gplv2+ce.html): GPL-2.0 with Classpath Exception | +| Maven | 3.9.12 | [Maven release history](https://maven.apache.org/docs/history.html): Apache-2.0 | +| Maven Wrapper | 3.3.4 | [Wrapper](https://maven.apache.org/wrapper/): Apache-2.0; distribution and SHA-256 pinned in `.mvn/wrapper/maven-wrapper.properties` | +| Spring Boot | 4.1.2 | [Published parent](https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-starter-parent/4.1.2/), [requirements](https://docs.spring.io/spring-boot/system-requirements.html): Apache-2.0 | +| Spring Security | 7.1.2 | Managed by [Boot BOM](https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-dependencies/4.1.2/spring-boot-dependencies-4.1.2.pom): Apache-2.0 | +| Spring Session JDBC/core | 4.1.1 | Boot BOM; Apache-2.0; upstream confirmed erroneous 4.1.1 POM metadata (resolved in publishing tooling for 4.1.2); [specific artifact evidence](third-party-licenses.md) | +| Spring Framework | 7.0.10 | Boot BOM: Apache-2.0 | +| Embedded Tomcat | 11.0.27; Jakarta Servlet 6.1 | Boot BOM: Apache-2.0 | +| PostgreSQL server | 18.6 | [Supported releases](https://www.postgresql.org/support/versioning/), [license](https://www.postgresql.org/about/licence/): PostgreSQL License | +| PostgreSQL JDBC | 42.7.14 | Boot BOM; [license](https://jdbc.postgresql.org/about/license/): BSD-2-Clause | +| Flyway | 12.4.0 | Boot BOM; [Community source](https://github.com/flyway/flyway): Apache-2.0 for the required core and PostgreSQL modules | +| iCal4j | 4.4.0, official unmodified artifact | [Published artifact](https://repo.maven.apache.org/maven2/org/mnode/ical4j/ical4j/4.4.0/), [source](https://github.com/ical4j/ical4j): BSD-3-Clause | +| Testcontainers | 2.0.5 | Boot BOM; [source](https://github.com/testcontainers/testcontainers-java): MIT; test scope only | +| JUnit Jupiter | 6.0.3 | Boot BOM; EPL-2.0; test scope only | +| Playwright / bundled Chromium | 1.61.1 / 149.0.7827.55 | [Playwright](https://playwright.dev/): Apache-2.0; Chromium contains separately licensed components; test-only browser fixture | +| Nginx | 1.30.6 stable | [Official download](https://nginx.org/en/download.html), [license](https://nginx.org/LICENSE): BSD-2-Clause; optional test proxy, not an additional default service | +| Keycloak | 26.8.1 | [Official download](https://www.keycloak.org/downloads): Apache-2.0; optional test fixture only | +| OWASP Dependency-Check Maven | 13.0.0 | [Published artifact](https://repo.maven.apache.org/maven2/org/owasp/dependency-check-maven/13.0.0/), [configuration](https://dependency-check.github.io/DependencyCheck/dependency-check-maven/configuration.html): Apache-2.0; CI tool only | + +The Boot dependency-management baseline selects framework versions together. No Spring Framework or Spring Security version is independently overridden. Boot 4.1.2 supports Java 17 through 27 and Servlet 6.1; Java 25 is the application toolchain requirement. Java's LTS classification follows the [Java support roadmap](https://www.oracle.com/java/technologies/java-se-support-roadmap.html). + +The former modified iCal4j implementation copies and patch payloads have been removed. Official parser interfaces and application-owned adapters are described in [parser integration](ical4j-parser-patch.md), [timezone integration](ical4j-timezone-patch.md) and [calendar semantics](calendar-semantics.md). Exact official runtime JAR hashes are locked in `qualification/distribution/runtime-artifacts.json`. + +The Maven JVM timezone is pinned to UTC in `.mvn/jvm.config`, using the [supported Maven JVM options file](https://maven.apache.org/configure.html#jvm-options). Spring Boot 4.1.2 repackage otherwise emitted timezone-dependent NTFS extra timestamp fields despite a fixed `project.build.outputTimestamp`: the host UTC−07 and container UTC JARs had identical entry bytes/order but different ZIP metadata. UTC removes that environment difference without modifying archive bytes with a custom tool. Exact final host/container SHA256 comparison belongs to the packaged qualification record. + +Build plugins and formatting tools are explicitly versioned in [pom.xml](../pom.xml). `./mvnw package` generates `target/classes/META-INF/sbom/application.cdx.json`, a CycloneDX inventory of runtime dependencies and their declared licenses, which is included in the application artifact. This is evidence for review, an inventory of upstream declarations, with reviewed conclusions and actual notice delivery recorded in the third-party license document. The embedded SBOM identifies each exact runtime Maven module with its group/name/version, package URL and declared license. Inspect it directly with `unzip -p target/caldav-server-@VERSION@.jar META-INF/sbom/application.cdx.json`; this avoids a second stale dependency inventory. Bedework and its probes stay under `investigation/protocol` and are absent from the application runtime dependency graph. The final image OS packages have a separate pinned Trivy scan; their inventory/feed/image evidence is distinct from Maven advisories. + +## Container pins + +The Dockerfiles and Compose file use immutable registry digests, with descriptive version tags: + +| Image | Digest | +| --- | --- | +| `eclipse-temurin:25-jdk` | `sha256:3ab7fc8d0b4ade8fef96903fb36767c1dbb4fd1535dcfad85b59c17afbbb5857` | +| `mcr.microsoft.com/playwright:v1.61.1-noble` | `sha256:5b8f294aff9041b7191c34a4bab3ac270157a28774d4b0660e9743297b697e48` | +| `nginx:1.30.6-alpine` | `sha256:47928bf67eb62a1863cb08d9ecb52d3e704304ba614eaee17884204677702951` | +| `quay.io/keycloak/keycloak:26.8.1` | `sha256:a78d6b79572168cb25886b80a8b5b8d4ed4390743e3eecb810538683e209dd6b` | +| `testcontainers/ryuk:0.14.0` | `sha256:7c1a8a9a47c780ed0f983770a662f80deb115d95cce3e2daa3d12115b8cd28f0` | +| `postgres:18.6` | `sha256:edb0c08dc33befd0262ab3b5cf1c4f3f8cfd8b09e88c05b45adee29d6761ddb6` | + +Residual qualification on 2026-10-29 found HIGH [CVE-2026-111497](https://ubuntu.com/security/notices/USN-8956-1) in the previous official Temurin image (`libbrotli1` 1.2.0-3build1). The refreshed official digest above supplies Ubuntu’s fixed 1.2.0-3ubuntu0.1 package while retaining **Temurin 25.0.4.1+1** and the identical Java executable SHA-256 `7380ce48ed5013735d2c8414db54adb8f981e7933ff594bd36f3baccddaafba3`. The application, CI JDK installer and optional Keycloak fixture use the same refreshed pin. No advisory suppression or third-party patch is applied; the failed scan and subsequent qualification are retained separately. + +The supported developer build runs the Maven wrapper with the selected JDK and passes the verified JAR to Docker. The runtime image uses the same pinned Temurin release; no Maven builder image is needed. + +The Keycloak fixture copies the pinned official distribution onto the pinned Temurin JDK for a consistent test runtime. No Keycloak application code is changed. The production deployment does not contain Keycloak. Browser certificate tooling is pinned to Ubuntu `libnss3-tools=2:3.98-1ubuntu0.3`; generated certificates and trust databases exist only in disposable private fixture directories. + +CI installs the exact JDK from the pinned Temurin image through `scripts/install-ci-jdk.sh`, validates its release/runtime version and exports it for Maven. This avoids setup-java's SemVer rejection of the four-part 25.0.4.1 version without selecting another patch. + +CI actions use immutable Git commit references in [ci.yml](../.github/workflows/ci.yml). Updating a digest or dependency is a reviewed repository change followed by the required suites; pins do not remove the need to apply security updates. + +## Project license and distribution + +The owner selected Apache License 2.0. The source, executable JAR and image deliver the project license and applicable notices independently of third-party licenses. See [reviewed inventory and artifact checks](third-party-licenses.md). diff --git a/investigation/oauth/README.md b/investigation/oauth/README.md new file mode 100644 index 0000000..b0d18e3 --- /dev/null +++ b/investigation/oauth/README.md @@ -0,0 +1,32 @@ +# Native OAuth refresh framework probe + +This isolated Maven project tests Spring Boot 4.1.2 / Spring Security 7.1.2 through +a real embedded HTTP server. All accounts, registrations, grants, passwords, and +keys in this project are disposable **test fixtures**. None is production account +management or a production refresh implementation. + +Run with Java 25 and Maven 3.9.12: + +```sh +mvn -B -ntp -f investigation/oauth/pom.xml test +``` + +`NativeOAuthRiskTest` signs in through Spring's generated form login, preserving +CSRF protection and session cookies. A registered public client completes S256 +PKCE and obtains an opaque access token, but receives no refresh token from the +stock generator. The test also verifies incorrect-verifier rejection, code replay +rejection, and rejection of a stock public refresh request. + +`CustomizedRefreshRiskTest` enables test-only supported extension points for +refresh issuance and public client identification. It proves issuance and rotation +can work without a fake native client secret. It then replays a spent token and +shows that the current descendant remains usable: **this passing test demonstrates +a missing application security control, not a passing production security test**. + +The extensions in `PublicRefreshExtensions` deliberately lack durable token-family +replay/concurrency handling. Never use them in the production application. See +[ADR 0002](../../docs/adr/0002-native-oauth-refresh.md) for required production work. + +Results must be read from `target/surefire-reports` after execution. No Docker or +database is involved in this narrow framework investigation. It does not qualify +production persistence, HTTPS, refresh concurrency, expiration, or account disabling. diff --git a/investigation/oauth/pom.xml b/investigation/oauth/pom.xml new file mode 100644 index 0000000..123b857 --- /dev/null +++ b/investigation/oauth/pom.xml @@ -0,0 +1,23 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 4.1.2 + + + org.busystack.caldav.investigation + native-oauth-risk-probe + 0.0.1-SNAPSHOT + 25 + + org.springframework.bootspring-boot-starter-webmvc + org.springframework.bootspring-boot-starter-security-oauth2-authorization-server + org.springframework.bootspring-boot-starter-testtest + + + org.apache.maven.pluginsmaven-compiler-plugin3.15.0 + org.apache.maven.pluginsmaven-surefire-plugin3.5.4 + + diff --git a/investigation/oauth/src/test/java/org/busystack/caldav/investigation/CustomizedRefreshRiskTest.java b/investigation/oauth/src/test/java/org/busystack/caldav/investigation/CustomizedRefreshRiskTest.java new file mode 100644 index 0000000..74201d2 --- /dev/null +++ b/investigation/oauth/src/test/java/org/busystack/caldav/investigation/CustomizedRefreshRiskTest.java @@ -0,0 +1,45 @@ +package org.busystack.caldav.investigation; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Map; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.server.LocalServerPort; +import tools.jackson.databind.json.JsonMapper; + +@SpringBootTest( + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, + classes = NativeOAuthRiskTest.ProbeApplication.class, + properties = {"probe.custom-refresh=true", "spring.main.banner-mode=off", "logging.level.root=WARN"}) +class CustomizedRefreshRiskTest { + @LocalServerPort int port; + + @Test + void supportedExtensionsIssueAndRotateButStockProviderDoesNotRevokeFamilyOnReplay() throws Exception { + NativeOAuthRiskTest probe = new NativeOAuthRiskTest(); + probe.port = port; + var initial = probe.post(Map.of("grant_type", "authorization_code", "client_id", "native-probe", "redirect_uri", NativeOAuthRiskTest.CALLBACK, "code", probe.authorizationCode(), "code_verifier", NativeOAuthRiskTest.VERIFIER)); + assertThat(initial.statusCode()).isEqualTo(200); + String first = token(initial.body()); + var rotation = probe.post(Map.of("grant_type", "refresh_token", "client_id", "native-probe", "refresh_token", first)); + assertThat(rotation.statusCode()).isEqualTo(200); + String second = token(rotation.body()); + assertThat(second).isNotEqualTo(first); + + var replay = probe.post(Map.of("grant_type", "refresh_token", "client_id", "native-probe", "refresh_token", first)); + assertThat(replay.statusCode()).isEqualTo(400); + assertThat(replay.body()).contains("invalid_grant"); + + // This PASS proves the missing security control: the descendant remains usable after replay. + var descendant = probe.post(Map.of("grant_type", "refresh_token", "client_id", "native-probe", "refresh_token", second)); + assertThat(descendant.statusCode()).isEqualTo(200); + assertThat(token(descendant.body())).isNotEqualTo(second); + } + + private static String token(String body) { + String value = JsonMapper.builder().build().readTree(body).path("refresh_token").asString(); + assertThat(value).isNotBlank(); + return value; + } +} diff --git a/investigation/oauth/src/test/java/org/busystack/caldav/investigation/NativeOAuthRiskTest.java b/investigation/oauth/src/test/java/org/busystack/caldav/investigation/NativeOAuthRiskTest.java new file mode 100644 index 0000000..799a208 --- /dev/null +++ b/investigation/oauth/src/test/java/org/busystack/caldav/investigation/NativeOAuthRiskTest.java @@ -0,0 +1,156 @@ +package org.busystack.caldav.investigation; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.net.URI; +import java.net.URLDecoder; +import java.net.URLEncoder; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.util.Base64; +import java.util.Map; +import java.util.UUID; +import java.util.stream.Collectors; +import org.junit.jupiter.api.Test; +import org.springframework.boot.SpringBootConfiguration; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.server.LocalServerPort; +import org.springframework.context.annotation.Bean; +import org.springframework.core.annotation.Order; +import org.springframework.security.config.Customizer; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.core.userdetails.User; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.oauth2.core.AuthorizationGrantType; +import org.springframework.security.oauth2.core.ClientAuthenticationMethod; +import org.springframework.security.oauth2.server.authorization.InMemoryOAuth2AuthorizationService; +import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService; +import org.springframework.security.oauth2.server.authorization.client.InMemoryRegisteredClientRepository; +import org.springframework.security.oauth2.server.authorization.client.RegisteredClient; +import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository; +import org.springframework.security.oauth2.server.authorization.settings.AuthorizationServerSettings; +import org.springframework.security.oauth2.server.authorization.settings.ClientSettings; +import org.springframework.security.oauth2.server.authorization.settings.OAuth2TokenFormat; +import org.springframework.security.oauth2.server.authorization.settings.TokenSettings; +import org.springframework.security.oauth2.server.authorization.token.DelegatingOAuth2TokenGenerator; +import org.springframework.security.oauth2.server.authorization.token.OAuth2AccessTokenGenerator; +import org.springframework.security.oauth2.server.authorization.token.OAuth2RefreshTokenGenerator; +import org.springframework.security.oauth2.server.authorization.token.OAuth2TokenGenerator; +import org.springframework.security.provisioning.InMemoryUserDetailsManager; +import org.springframework.security.web.SecurityFilterChain; + +/** Test-only in-memory stock framework probe; this is not the application account system. */ +@SpringBootTest( + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, + classes = NativeOAuthRiskTest.ProbeApplication.class, + properties = {"spring.main.banner-mode=off", "logging.level.root=WARN"}) +class NativeOAuthRiskTest { + static final String VERIFIER = "a-valid-pkce-code-verifier-for-native-client-0123456789"; + static final String CALLBACK = "http://127.0.0.1:34567/callback"; + private final HttpClient client = HttpClient.newBuilder().cookieHandler(new java.net.CookieManager(null, java.net.CookiePolicy.ACCEPT_ALL)).followRedirects(HttpClient.Redirect.NEVER).build(); + @LocalServerPort int port; + + @Test + void publicS256CodeExchangeWorksButStockFrameworkDoesNotIssueRefreshToken() throws Exception { + String code = authorizationCode(); + HttpResponse tokens = post(Map.of("grant_type", "authorization_code", "client_id", "native-probe", "redirect_uri", CALLBACK, "code", code, "code_verifier", VERIFIER)); + assertThat(tokens.statusCode()).isEqualTo(200); + assertThat(tokens.body()).contains("\"access_token\"", "\"token_type\":\"Bearer\"").doesNotContain("\"refresh_token\""); + + HttpResponse replay = post(Map.of("grant_type", "authorization_code", "client_id", "native-probe", "redirect_uri", CALLBACK, "code", code, "code_verifier", VERIFIER)); + assertThat(replay.statusCode()).isEqualTo(400); + assertThat(replay.body()).contains("invalid_grant"); + } + + @Test + void wrongPkceVerifierIsRejected() throws Exception { + HttpResponse response = post(Map.of("grant_type", "authorization_code", "client_id", "native-probe", "redirect_uri", CALLBACK, "code", authorizationCode(), "code_verifier", "wrong-pkce-code-verifier-012345678901234567890123456789")); + assertThat(response.statusCode()).isEqualTo(400); + assertThat(response.body()).contains("invalid_grant"); + } + + @Test + void stockClientAuthenticationDoesNotRecognizePublicRefreshRequest() throws Exception { + HttpResponse response = post(Map.of("grant_type", "refresh_token", "client_id", "native-probe", "refresh_token", "not-a-real-token")); + assertThat(response.statusCode()).isEqualTo(401); + } + + String authorizationCode() throws Exception { + browserLogin(); + String challenge = Base64.getUrlEncoder().withoutPadding().encodeToString(MessageDigest.getInstance("SHA-256").digest(VERIFIER.getBytes(StandardCharsets.US_ASCII))); + String state = UUID.randomUUID().toString(); + String query = form(Map.of("response_type", "code", "client_id", "native-probe", "redirect_uri", CALLBACK, "scope", "dav", "state", state, "code_challenge", challenge, "code_challenge_method", "S256")); + HttpRequest request = HttpRequest.newBuilder(URI.create(base() + "/oauth2/authorize?" + query)).GET().build(); + HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); + assertThat(response.statusCode()).isEqualTo(302); + String location = response.headers().firstValue("Location").orElseThrow(); + assertThat(location).startsWith(CALLBACK).contains("state=" + state); + return java.util.Arrays.stream(URI.create(location).getRawQuery().split("&")).filter(p -> p.startsWith("code=")).map(p -> URLDecoder.decode(p.substring(5), StandardCharsets.UTF_8)).findFirst().orElseThrow(() -> new AssertionError(location)); + } + + HttpResponse post(Map parameters) throws Exception { + return client.send(HttpRequest.newBuilder(URI.create(base() + "/oauth2/token")).header("Content-Type", "application/x-www-form-urlencoded").POST(HttpRequest.BodyPublishers.ofString(form(parameters))).build(), HttpResponse.BodyHandlers.ofString()); + } + + private void browserLogin() throws Exception { + var page = client.send(HttpRequest.newBuilder(URI.create(base() + "/login")).GET().build(), HttpResponse.BodyHandlers.ofString()); + assertThat(page.statusCode()).isEqualTo(200); + var csrf = new java.util.concurrent.atomic.AtomicReference(); + new javax.swing.text.html.parser.ParserDelegator().parse(new java.io.StringReader(page.body()), new javax.swing.text.html.HTMLEditorKit.ParserCallback() { + @Override public void handleSimpleTag(javax.swing.text.html.HTML.Tag tag, javax.swing.text.MutableAttributeSet attributes, int position) { + if (tag == javax.swing.text.html.HTML.Tag.INPUT && "_csrf".equals(attributes.getAttribute(javax.swing.text.html.HTML.Attribute.NAME))) csrf.set((String) attributes.getAttribute(javax.swing.text.html.HTML.Attribute.VALUE)); + } + }, true); + assertThat(csrf.get()).isNotBlank(); + var result = client.send(HttpRequest.newBuilder(URI.create(base() + "/login")).header("Content-Type", "application/x-www-form-urlencoded").POST(HttpRequest.BodyPublishers.ofString(form(Map.of("username", "probe", "password", "probe-test-password", "_csrf", csrf.get())))).build(), HttpResponse.BodyHandlers.ofString()); + assertThat(result.statusCode()).isEqualTo(302); + assertThat(result.headers().firstValue("Location").orElseThrow()).doesNotContain("error"); + } + + private String base() { return "http://127.0.0.1:" + port; } + private static String form(Map parameters) { + return parameters.entrySet().stream().map(e -> URLEncoder.encode(e.getKey(), StandardCharsets.UTF_8) + "=" + URLEncoder.encode(e.getValue(), StandardCharsets.UTF_8)).collect(Collectors.joining("&")); + } + + @SpringBootConfiguration + @EnableAutoConfiguration + static class ProbeApplication { + @Bean @Order(1) + SecurityFilterChain authorizationServer(HttpSecurity http, @org.springframework.beans.factory.annotation.Value("${probe.custom-refresh:false}") boolean customRefresh, RegisteredClientRepository clients) throws Exception { + http.oauth2AuthorizationServer(server -> { + http.securityMatcher(server.getEndpointsMatcher()); + server.tokenGenerator(tokenGenerator(customRefresh)); + if (customRefresh) { + server.clientAuthentication(auth -> auth + .authenticationConverter(new PublicRefreshExtensions.Converter()) + .authenticationProvider(new PublicRefreshExtensions.Provider(clients))); + } + }).authorizeHttpRequests(requests -> requests.anyRequest().authenticated()).httpBasic(Customizer.withDefaults()); + return http.build(); + } + + @Bean @Order(2) + SecurityFilterChain other(HttpSecurity http) throws Exception { + return http.authorizeHttpRequests(requests -> requests.anyRequest().authenticated()).formLogin(Customizer.withDefaults()).build(); + } + + @Bean UserDetailsService users() { + return new InMemoryUserDetailsManager(User.withUsername("probe").password("{bcrypt}" + new BCryptPasswordEncoder().encode("probe-test-password")).roles("USER").build()); + } + + @Bean RegisteredClientRepository clients() { + return new InMemoryRegisteredClientRepository(RegisteredClient.withId("native-probe-id").clientId("native-probe").clientAuthenticationMethod(ClientAuthenticationMethod.NONE).authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE).authorizationGrantType(AuthorizationGrantType.REFRESH_TOKEN).redirectUri(CALLBACK).scope("dav").clientSettings(ClientSettings.builder().requireProofKey(true).requireAuthorizationConsent(false).build()).tokenSettings(TokenSettings.builder().reuseRefreshTokens(false).accessTokenFormat(OAuth2TokenFormat.REFERENCE).build()).build()); + } + + @Bean OAuth2AuthorizationService authorizations() { return new InMemoryOAuth2AuthorizationService(); } + @Bean AuthorizationServerSettings settings() { return AuthorizationServerSettings.builder().issuer("https://probe.invalid").build(); } + @Bean OAuth2TokenGenerator tokenGenerator(@org.springframework.beans.factory.annotation.Value("${probe.custom-refresh:false}") boolean customRefresh) { + return new DelegatingOAuth2TokenGenerator(new OAuth2AccessTokenGenerator(), customRefresh ? new PublicRefreshExtensions.Generator() : new OAuth2RefreshTokenGenerator()); + } + } +} diff --git a/investigation/oauth/src/test/java/org/busystack/caldav/investigation/PublicRefreshExtensions.java b/investigation/oauth/src/test/java/org/busystack/caldav/investigation/PublicRefreshExtensions.java new file mode 100644 index 0000000..d076aa2 --- /dev/null +++ b/investigation/oauth/src/test/java/org/busystack/caldav/investigation/PublicRefreshExtensions.java @@ -0,0 +1,65 @@ +package org.busystack.caldav.investigation; + +import jakarta.servlet.http.HttpServletRequest; +import java.time.Instant; +import java.util.Base64; +import org.springframework.security.authentication.AuthenticationProvider; +import org.springframework.security.core.Authentication; +import org.springframework.security.crypto.keygen.Base64StringKeyGenerator; +import org.springframework.security.oauth2.core.AuthorizationGrantType; +import org.springframework.security.oauth2.core.ClientAuthenticationMethod; +import org.springframework.security.oauth2.core.OAuth2AuthenticationException; +import org.springframework.security.oauth2.core.OAuth2ErrorCodes; +import org.springframework.security.oauth2.core.OAuth2RefreshToken; +import org.springframework.security.oauth2.server.authorization.OAuth2TokenType; +import org.springframework.security.oauth2.server.authorization.authentication.OAuth2ClientAuthenticationToken; +import org.springframework.security.oauth2.server.authorization.client.RegisteredClient; +import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository; +import org.springframework.security.oauth2.server.authorization.token.OAuth2TokenContext; +import org.springframework.security.oauth2.server.authorization.token.OAuth2TokenGenerator; +import org.springframework.security.web.authentication.AuthenticationConverter; + +/** + * Deliberately incomplete TEST fixture: demonstrates supported framework extension points only. + * It lacks persistent replay-family enforcement and MUST NOT be used by the production server. + */ +final class PublicRefreshExtensions { + private PublicRefreshExtensions() {} + + static final class Generator implements OAuth2TokenGenerator { + private final Base64StringKeyGenerator keys = new Base64StringKeyGenerator(Base64.getUrlEncoder().withoutPadding(), 96); + + @Override public OAuth2RefreshToken generate(OAuth2TokenContext context) { + if (!OAuth2TokenType.REFRESH_TOKEN.equals(context.getTokenType())) return null; + Instant now = Instant.now(); + return new OAuth2RefreshToken(keys.generateKey(), now, now.plus(context.getRegisteredClient().getTokenSettings().getRefreshTokenTimeToLive())); + } + } + + static final class ClientToken extends OAuth2ClientAuthenticationToken { + ClientToken(String clientId) { super(clientId, ClientAuthenticationMethod.NONE, null, null); } + } + + static final class Converter implements AuthenticationConverter { + @Override public Authentication convert(HttpServletRequest request) { + if (!"POST".equals(request.getMethod()) || !"refresh_token".equals(request.getParameter("grant_type")) || request.getHeader("Authorization") != null) return null; + String[] clientIds = request.getParameterValues("client_id"); + String[] grants = request.getParameterValues("grant_type"); + if (clientIds == null || clientIds.length != 1 || clientIds[0].isBlank() || grants == null || grants.length != 1) throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_REQUEST); + if (request.getParameter("client_secret") != null) return null; + return new ClientToken(clientIds[0]); + } + } + + static final class Provider implements AuthenticationProvider { + private final RegisteredClientRepository clients; + Provider(RegisteredClientRepository clients) { this.clients = clients; } + @Override public Authentication authenticate(Authentication authentication) { + RegisteredClient client = clients.findByClientId(authentication.getName()); + if (client == null || !client.getClientAuthenticationMethods().contains(ClientAuthenticationMethod.NONE) || !client.getAuthorizationGrantTypes().contains(AuthorizationGrantType.REFRESH_TOKEN)) throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_CLIENT); + // NONE identifies a public client; the framework grant provider validates token binding. + return new OAuth2ClientAuthenticationToken(client, ClientAuthenticationMethod.NONE, null); + } + @Override public boolean supports(Class type) { return ClientToken.class.isAssignableFrom(type); } + } +} diff --git a/investigation/oauth/src/test/resources/application.properties b/investigation/oauth/src/test/resources/application.properties new file mode 100644 index 0000000..f53ad61 --- /dev/null +++ b/investigation/oauth/src/test/resources/application.properties @@ -0,0 +1,2 @@ +# The native OAuth risk probes contain disposable demonstration credentials. +server.address=127.0.0.1 diff --git a/investigation/protocol/README.md b/investigation/protocol/README.md new file mode 100644 index 0000000..5b6d94f --- /dev/null +++ b/investigation/protocol/README.md @@ -0,0 +1,17 @@ +# Bedework runtime investigation + +This isolated project is **not the BusyStack production protocol implementation**. It deliberately uses test-only account and backend fixtures to distinguish servlet/framework compatibility from full backend implementation. It is not included as a runtime dependency in the main application. + +Run with Java 25 and the root Maven Wrapper: + +```sh +./mvnw -B -ntp -s investigation/protocol/settings.xml -f investigation/protocol/pom.xml test +``` + +The settings file mirrors dependency repositories to Maven Central, including repositories declared transitively by Bedework. It never enables snapshots. If the user's global Maven cache has unrelated ownership restrictions, pass `-Dmaven.repo.local=/path/to/a/writable/probe-cache`. + +The fixture registers the actual `CaldavBWServlet` in Spring Boot's embedded Tomcat and executes real HTTP with `java.net.http.HttpClient` through Spring Security's Basic filter and a precise DAV firewall method allowlist. The `SysIntf` fixture delegates to Mockito objects and has **no persistent backend**. A generated forwarding class is needed because the released servlet instantiates that interface implementation by configured class name and no-argument constructor. + +Production permissions, storage, calendar parsing, credentials, and recurrence semantics cannot be inferred from this fixture. An expected-defect assertion records an upstream behavior that must be fixed before adoption; its passing result does not qualify that behavior for BusyStack. + +See [ADR 0001](../../docs/adr/0001-protocol-engine.md) for evidence and the remaining decision gate. diff --git a/investigation/protocol/pom.xml b/investigation/protocol/pom.xml new file mode 100644 index 0000000..b44e70b --- /dev/null +++ b/investigation/protocol/pom.xml @@ -0,0 +1,20 @@ + + 4.0.0 + org.springframework.bootspring-boot-starter-parent4.1.2 + org.busystack.caldav.investigationbedework-probe0.0.1-SNAPSHOT + 25 + + org.springframework.bootspring-boot-starter-webmvc + org.springframework.bootspring-boot-starter-security + org.springframework.bootspring-boot-starter-testtest + org.springframework.bootspring-boot-starter-webmvc-testtest + org.glassfish.jaxbjaxb-runtime + org.bedeworkbw-caldav-server6.0.1 + + + org.apache.maven.pluginsmaven-compiler-plugin3.14.1 + org.apache.maven.pluginsmaven-surefire-plugin3.5.4true + org.apache.maven.pluginsmaven-resources-plugin3.5.0 + org.apache.maven.pluginsmaven-dependency-plugin3.10.0 + + diff --git a/investigation/protocol/settings.xml b/investigation/protocol/settings.xml new file mode 100644 index 0000000..2bff93e --- /dev/null +++ b/investigation/protocol/settings.xml @@ -0,0 +1,3 @@ + + central-only*https://repo.maven.apache.org/maven2 + diff --git a/investigation/protocol/src/test/java/org/busystack/caldav/investigation/BedeworkHttpTest.java b/investigation/protocol/src/test/java/org/busystack/caldav/investigation/BedeworkHttpTest.java new file mode 100644 index 0000000..69aee6c --- /dev/null +++ b/investigation/protocol/src/test/java/org/busystack/caldav/investigation/BedeworkHttpTest.java @@ -0,0 +1,209 @@ +package org.busystack.caldav.investigation; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.*; +import static org.mockito.Mockito.*; + +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Arrays; +import java.util.Base64; +import java.util.List; +import java.util.Map; +import javax.tools.ToolProvider; + +import jakarta.servlet.http.HttpServletRequest; +import org.bedework.access.CurrentAccess; +import org.bedework.caldav.server.CalDAVCollection; +import org.bedework.caldav.server.CaldavBWServlet; +import org.bedework.caldav.server.sysinterface.CalDAVAuthProperties; +import org.bedework.caldav.server.sysinterface.CalDAVSystemProperties; +import org.bedework.caldav.server.sysinterface.SysIntf; +import org.bedework.webdav.servlet.shared.UrlHandler; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.springframework.boot.SpringBootConfiguration; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.server.LocalServerPort; +import org.springframework.boot.web.servlet.ServletRegistrationBean; +import org.springframework.context.annotation.Bean; +import org.springframework.security.config.Customizer; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer; +import org.springframework.security.core.userdetails.User; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.provisioning.InMemoryUserDetailsManager; +import org.springframework.security.web.SecurityFilterChain; +import org.springframework.security.web.firewall.StrictHttpFirewall; + +/** Compatibility investigation only. The mock backend has no production path. */ +@SpringBootTest(classes = BedeworkHttpTest.Application.class, + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, + properties = {"spring.main.banner-mode=off", "logging.level.root=WARN"}) +public class BedeworkHttpTest { + public static SysIntf backend; + static volatile String lastPrincipal; + static CalDAVCollection probeCollection; + @LocalServerPort int port; + + @BeforeAll static void backendAdapter() throws Exception { + backend = mock(SysIntf.class); + when(backend.init(any(), any(), anyBoolean(), anyBoolean(), anyBoolean(), anyBoolean(), anyBoolean(), nullable(String.class))) + .thenAnswer(call -> { + HttpServletRequest request = call.getArgument(0); + lastPrincipal = request.getUserPrincipal().getName(); + return lastPrincipal; + }); + when(backend.getSystemProperties()).thenReturn(mock(CalDAVSystemProperties.class)); + when(backend.getAuthProperties()).thenReturn(mock(CalDAVAuthProperties.class)); + when(backend.getUrlHandler()).thenReturn(new UrlHandler("https://calendar.example.test", "/dav", false)); + when(backend.getDefaultContentType()).thenReturn("text/calendar"); + doReturn(List.of()).when(backend).getEvents(any(), any(), any(), any()); + CalDAVCollection collection = mock(CalDAVCollection.class); + probeCollection = collection; + doReturn(collection).when(collection).resolveAlias(anyBoolean()); + when(collection.getPath()).thenReturn("/"); + when(collection.getName()).thenReturn("probe"); + when(collection.getCalType()).thenReturn(CalDAVCollection.calTypeCalendarCollection); + when(collection.getSupportedComponents()).thenReturn(List.of("VEVENT", "VTODO")); + doReturn(collection).when(backend).getCollection("/"); + doReturn(collection).when(backend).getCollection("/cal"); + CurrentAccess access = mock(CurrentAccess.class); + when(access.getAccessAllowed()).thenReturn(true); + when(backend.checkAccess(any(), anyInt(), anyBoolean())).thenReturn(access); + + // Bedework requires a public concrete class with a no-argument constructor. + // Generate a transparent forwarding adapter, using reflection for its broad interface. + Path output = Path.of("target/test-classes").toAbsolutePath(); + Path source = Path.of("target/generated-probe/ProbeBackend.java"); + Files.createDirectories(source.getParent()); + StringBuilder generated = new StringBuilder("package org.busystack.caldav.investigation; public class ProbeBackend implements org.bedework.caldav.server.sysinterface.SysIntf {"); + for (var method : SysIntf.class.getMethods()) { + if (java.lang.reflect.Modifier.isStatic(method.getModifiers()) || method.isDefault()) continue; + generated.append("public ").append(method.getReturnType().getCanonicalName()).append(' ').append(method.getName()).append('('); + var parameters = method.getParameterTypes(); + for (int i = 0; i < parameters.length; i++) { + if (i > 0) generated.append(','); + generated.append(parameters[i].getCanonicalName()).append(" p").append(i); + } + generated.append(") {"); + if (method.getReturnType() != void.class) generated.append("return "); + generated.append("BedeworkHttpTest.backend.").append(method.getName()).append('('); + for (int i = 0; i < parameters.length; i++) { + if (i > 0) generated.append(','); + generated.append('p').append(i); + } + generated.append(");}"); + } + Files.writeString(source, generated.append('}')); + assertEquals(0, ToolProvider.getSystemJavaCompiler().run(null, null, null, + "-classpath", System.getProperty("java.class.path"), "-d", output.toString(), source.toString())); + } + + @Test void realContainerAuthenticatesBeforeBedeworkAndPassesPrincipal() throws Exception { + var unauthorized = request("OPTIONS", "/dav/", false, ""); + assertEquals(401, unauthorized.statusCode()); + assertTrue(unauthorized.headers().firstValue("WWW-Authenticate").orElse("").startsWith("Basic")); + var authorized = request("OPTIONS", "/dav/", true, ""); + assertEquals(200, authorized.statusCode(), authorized.body()); + assertEquals("probe-user", lastPrincipal); + assertTrue(authorized.headers().firstValue("Allow").orElse("").contains("REPORT")); + // This is an observed mismatch with the BusyStack profile, not desired product behavior. + assertTrue(authorized.headers().firstValue("DAV").orElse("").contains("calendar-auto-schedule")); + } + + @Test void propfindRunsThroughRealServletAndSecurityFirewall() throws Exception { + var response = request("PROPFIND", "/dav/", true, + ""); + assertEquals(207, response.statusCode(), response.body()); + assertTrue(response.body().contains("VTODO"), response.body()); + } + + @Test void queryAndExpandPropertyReachReportHandlers() throws Exception { + var query = request("REPORT", "/dav/", true, + ""); + assertEquals(207, query.statusCode(), query.body()); + var expand = request("REPORT", "/dav/", true, + ""); + assertEquals(207, expand.statusCode(), expand.body()); + } + + @Test void failedProppatchStillCallsBackendUpdateInReleasedEngine() throws Exception { + clearInvocations(backend, probeCollection); + var response = request("PROPPATCH", "/dav/", true, + "partial-changeforbidden"); + assertEquals(207, response.statusCode(), response.body()); + assertTrue(response.body().contains("403"), response.body()); + // Records an upstream integration defect, not accepted BusyStack behavior: + // the successful setter is followed by update despite the protected-property failure. + verify(probeCollection).setDisplayName("partial-change"); + verify(backend).updateCollection(probeCollection); + verify(backend, never()).rollback(); + assertFalse(response.body().contains("424"), response.body()); + } + + @Test void putEncodesQuotedEtagInReleasedEngine() throws Exception { + var calendar = mock(org.bedework.caldav.server.SysiIcalendar.class); + var event = mock(org.bedework.caldav.server.CalDAVEvent.class); + when(event.isNew()).thenReturn(true); + when(event.getName()).thenReturn("probe.ics"); + when(event.getUid()).thenReturn("probe-uid"); + when(event.getEtag()).thenReturn("\"probe-etag\""); + doReturn(event).when(calendar).getOnlyEvent(); + when(backend.fromIcal(any(), any(java.io.Reader.class), anyString(), any(), anyBoolean())).thenReturn(calendar); + var request = HttpRequest.newBuilder(URI.create("http://127.0.0.1:" + port + "/dav/cal/probe.ics")) + .timeout(java.time.Duration.ofSeconds(20)) + .header("Content-Type", "text/calendar") + .header("Authorization", "Basic " + Base64.getEncoder().encodeToString("probe-user:probe-password".getBytes(java.nio.charset.StandardCharsets.UTF_8))) + .PUT(HttpRequest.BodyPublishers.ofString("BEGIN:VCALENDAR\r\nVERSION:2.0\r\nEND:VCALENDAR\r\n")).build(); + var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString()); + assertEquals(201, response.statusCode(), response.body()); + // Records the header defect. An HTTP entity-tag must retain the surrounding quotes. + assertEquals("%22probe-etag%22", response.headers().firstValue("ETag").orElse("")); + } + + @Test void multigetDecodesLiteralPlusAsSpaceInReleasedEngine() throws Exception { + clearInvocations(backend); + var response = request("REPORT", "/dav/cal/", true, + "/dav/cal/a+b.ics"); + assertEquals(207, response.statusCode(), response.body()); + verify(backend).getCollection("/dav/cal/a b.ics"); + verify(backend, never()).getCollection("/dav/cal/a+b.ics"); + } + + HttpResponse request(String method, String path, boolean authenticated, String body) throws Exception { + var request = HttpRequest.newBuilder(URI.create("http://127.0.0.1:" + port + path)) + .timeout(java.time.Duration.ofSeconds(20)).header("Depth", "0").header("Content-Type", "application/xml") + .method(method, HttpRequest.BodyPublishers.ofString(body)); + if (authenticated) request.header("Authorization", "Basic " + Base64.getEncoder().encodeToString("probe-user:probe-password".getBytes(java.nio.charset.StandardCharsets.UTF_8))); + return HttpClient.newBuilder().connectTimeout(java.time.Duration.ofSeconds(10)).build().send(request.build(), HttpResponse.BodyHandlers.ofString()); + } + + @SpringBootConfiguration @EnableAutoConfiguration + static class Application { + @Bean ServletRegistrationBean bedework() { + var registration = new ServletRegistrationBean<>(new CaldavBWServlet(), "/dav/*"); + registration.setInitParameters(Map.of("sysintfImpl", "org.busystack.caldav.investigation.ProbeBackend", "preserve-session", "true")); + return registration; + } + @Bean UserDetailsService users() { + var encoder = org.springframework.security.crypto.factory.PasswordEncoderFactories.createDelegatingPasswordEncoder(); + return new InMemoryUserDetailsManager(User.withUsername("probe-user").password(encoder.encode("probe-password")).roles("USER").build()); + } + @Bean SecurityFilterChain security(HttpSecurity http) throws Exception { + return http.authorizeHttpRequests(a -> a.anyRequest().authenticated()).httpBasic(Customizer.withDefaults()) + .csrf(c -> c.ignoringRequestMatchers("/dav/**")) + .sessionManagement(s -> s.sessionCreationPolicy(org.springframework.security.config.http.SessionCreationPolicy.STATELESS)).build(); + } + @Bean WebSecurityCustomizer methods() { + var firewall = new StrictHttpFirewall(); + firewall.setAllowedHttpMethods(List.of("GET", "HEAD", "POST", "OPTIONS", "PUT", "DELETE", "PROPFIND", "PROPPATCH", "REPORT", "MKCALENDAR", "MKCOL", "COPY", "MOVE", "ACL")); + return web -> web.httpFirewall(firewall); + } + } +} diff --git a/investigation/protocol/src/test/resources/application.properties b/investigation/protocol/src/test/resources/application.properties new file mode 100644 index 0000000..e89f0d2 --- /dev/null +++ b/investigation/protocol/src/test/resources/application.properties @@ -0,0 +1,2 @@ +# The servlet compatibility probe contains disposable demonstration credentials. +server.address=127.0.0.1 diff --git a/licenses/EPL-2.0.txt b/licenses/EPL-2.0.txt new file mode 100644 index 0000000..e23ece2 --- /dev/null +++ b/licenses/EPL-2.0.txt @@ -0,0 +1,277 @@ +Eclipse Public License - v 2.0 + + THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE + PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION + OF THE PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. + +1. DEFINITIONS + +"Contribution" means: + + a) in the case of the initial Contributor, the initial content + Distributed under this Agreement, and + + b) in the case of each subsequent Contributor: + i) changes to the Program, and + ii) additions to the Program; + where such changes and/or additions to the Program originate from + and are Distributed by that particular Contributor. A Contribution + "originates" from a Contributor if it was added to the Program by + such Contributor itself or anyone acting on such Contributor's behalf. + Contributions do not include changes or additions to the Program that + are not Modified Works. + +"Contributor" means any person or entity that Distributes the Program. + +"Licensed Patents" mean patent claims licensable by a Contributor which +are necessarily infringed by the use or sale of its Contribution alone +or when combined with the Program. + +"Program" means the Contributions Distributed in accordance with this +Agreement. + +"Recipient" means anyone who receives the Program under this Agreement +or any Secondary License (as applicable), including Contributors. + +"Derivative Works" shall mean any work, whether in Source Code or other +form, that is based on (or derived from) the Program and for which the +editorial revisions, annotations, elaborations, or other modifications +represent, as a whole, an original work of authorship. + +"Modified Works" shall mean any work in Source Code or other form that +results from an addition to, deletion from, or modification of the +contents of the Program, including, for purposes of clarity any new file +in Source Code form that contains any contents of the Program. Modified +Works shall not include works that contain only declarations, +interfaces, types, classes, structures, or files of the Program solely +in each case in order to link to, bind by name, or subclass the Program +or Modified Works thereof. + +"Distribute" means the acts of a) distributing or b) making available +in any manner that enables the transfer of a copy. + +"Source Code" means the form of a Program preferred for making +modifications, including but not limited to software source code, +documentation source, and configuration files. + +"Secondary License" means either the GNU General Public License, +Version 2.0, or any later versions of that license, including any +exceptions or additional permissions as identified by the initial +Contributor. + +2. GRANT OF RIGHTS + + a) Subject to the terms of this Agreement, each Contributor hereby + grants Recipient a non-exclusive, worldwide, royalty-free copyright + license to reproduce, prepare Derivative Works of, publicly display, + publicly perform, Distribute and sublicense the Contribution of such + Contributor, if any, and such Derivative Works. + + b) Subject to the terms of this Agreement, each Contributor hereby + grants Recipient a non-exclusive, worldwide, royalty-free patent + license under Licensed Patents to make, use, sell, offer to sell, + import and otherwise transfer the Contribution of such Contributor, + if any, in Source Code or other form. This patent license shall + apply to the combination of the Contribution and the Program if, at + the time the Contribution is added by the Contributor, such addition + of the Contribution causes such combination to be covered by the + Licensed Patents. The patent license shall not apply to any other + combinations which include the Contribution. No hardware per se is + licensed hereunder. + + c) Recipient understands that although each Contributor grants the + licenses to its Contributions set forth herein, no assurances are + provided by any Contributor that the Program does not infringe the + patent or other intellectual property rights of any other entity. + Each Contributor disclaims any liability to Recipient for claims + brought by any other entity based on infringement of intellectual + property rights or otherwise. As a condition to exercising the + rights and licenses granted hereunder, each Recipient hereby + assumes sole responsibility to secure any other intellectual + property rights needed, if any. For example, if a third party + patent license is required to allow Recipient to Distribute the + Program, it is Recipient's responsibility to acquire that license + before distributing the Program. + + d) Each Contributor represents that to its knowledge it has + sufficient copyright rights in its Contribution, if any, to grant + the copyright license set forth in this Agreement. + + e) Notwithstanding the terms of any Secondary License, no + Contributor makes additional grants to any Recipient (other than + those set forth in this Agreement) as a result of such Recipient's + receipt of the Program under the terms of a Secondary License + (if permitted under the terms of Section 3). + +3. REQUIREMENTS + +3.1 If a Contributor Distributes the Program in any form, then: + + a) the Program must also be made available as Source Code, in + accordance with section 3.2, and the Contributor must accompany + the Program with a statement that the Source Code for the Program + is available under this Agreement, and informs Recipients how to + obtain it in a reasonable manner on or through a medium customarily + used for software exchange; and + + b) the Contributor may Distribute the Program under a license + different than this Agreement, provided that such license: + i) effectively disclaims on behalf of all other Contributors all + warranties and conditions, express and implied, including + warranties or conditions of title and non-infringement, and + implied warranties or conditions of merchantability and fitness + for a particular purpose; + + ii) effectively excludes on behalf of all other Contributors all + liability for damages, including direct, indirect, special, + incidental and consequential damages, such as lost profits; + + iii) does not attempt to limit or alter the recipients' rights + in the Source Code under section 3.2; and + + iv) requires any subsequent distribution of the Program by any + party to be under a license that satisfies the requirements + of this section 3. + +3.2 When the Program is Distributed as Source Code: + + a) it must be made available under this Agreement, or if the + Program (i) is combined with other material in a separate file or + files made available under a Secondary License, and (ii) the initial + Contributor attached to the Source Code the notice described in + Exhibit A of this Agreement, then the Program may be made available + under the terms of such Secondary Licenses, and + + b) a copy of this Agreement must be included with each copy of + the Program. + +3.3 Contributors may not remove or alter any copyright, patent, +trademark, attribution notices, disclaimers of warranty, or limitations +of liability ("notices") contained within the Program from any copy of +the Program which they Distribute, provided that Contributors may add +their own appropriate notices. + +4. COMMERCIAL DISTRIBUTION + +Commercial distributors of software may accept certain responsibilities +with respect to end users, business partners and the like. While this +license is intended to facilitate the commercial use of the Program, +the Contributor who includes the Program in a commercial product +offering should do so in a manner which does not create potential +liability for other Contributors. Therefore, if a Contributor includes +the Program in a commercial product offering, such Contributor +("Commercial Contributor") hereby agrees to defend and indemnify every +other Contributor ("Indemnified Contributor") against any losses, +damages and costs (collectively "Losses") arising from claims, lawsuits +and other legal actions brought by a third party against the Indemnified +Contributor to the extent caused by the acts or omissions of such +Commercial Contributor in connection with its distribution of the Program +in a commercial product offering. The obligations in this section do not +apply to any claims or Losses relating to any actual or alleged +intellectual property infringement. In order to qualify, an Indemnified +Contributor must: a) promptly notify the Commercial Contributor in +writing of such claim, and b) allow the Commercial Contributor to control, +and cooperate with the Commercial Contributor in, the defense and any +related settlement negotiations. The Indemnified Contributor may +participate in any such claim at its own expense. + +For example, a Contributor might include the Program in a commercial +product offering, Product X. That Contributor is then a Commercial +Contributor. If that Commercial Contributor then makes performance +claims, or offers warranties related to Product X, those performance +claims and warranties are such Commercial Contributor's responsibility +alone. Under this section, the Commercial Contributor would have to +defend claims against the other Contributors related to those performance +claims and warranties, and if a court requires any other Contributor to +pay any damages as a result, the Commercial Contributor must pay +those damages. + +5. NO WARRANTY + +EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, AND TO THE EXTENT +PERMITTED BY APPLICABLE LAW, THE PROGRAM IS PROVIDED ON AN "AS IS" +BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR +IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS OF +TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR +PURPOSE. Each Recipient is solely responsible for determining the +appropriateness of using and distributing the Program and assumes all +risks associated with its exercise of rights under this Agreement, +including but not limited to the risks and costs of program errors, +compliance with applicable laws, damage to or loss of data, programs +or equipment, and unavailability or interruption of operations. + +6. DISCLAIMER OF LIABILITY + +EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, AND TO THE EXTENT +PERMITTED BY APPLICABLE LAW, NEITHER RECIPIENT NOR ANY CONTRIBUTORS +SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION LOST +PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE +EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + +7. GENERAL + +If any provision of this Agreement is invalid or unenforceable under +applicable law, it shall not affect the validity or enforceability of +the remainder of the terms of this Agreement, and without further +action by the parties hereto, such provision shall be reformed to the +minimum extent necessary to make such provision valid and enforceable. + +If Recipient institutes patent litigation against any entity +(including a cross-claim or counterclaim in a lawsuit) alleging that the +Program itself (excluding combinations of the Program with other software +or hardware) infringes such Recipient's patent(s), then such Recipient's +rights granted under Section 2(b) shall terminate as of the date such +litigation is filed. + +All Recipient's rights under this Agreement shall terminate if it +fails to comply with any of the material terms or conditions of this +Agreement and does not cure such failure in a reasonable period of +time after becoming aware of such noncompliance. If all Recipient's +rights under this Agreement terminate, Recipient agrees to cease use +and distribution of the Program as soon as reasonably practicable. +However, Recipient's obligations under this Agreement and any licenses +granted by Recipient relating to the Program shall continue and survive. + +Everyone is permitted to copy and distribute copies of this Agreement, +but in order to avoid inconsistency the Agreement is copyrighted and +may only be modified in the following manner. The Agreement Steward +reserves the right to publish new versions (including revisions) of +this Agreement from time to time. No one other than the Agreement +Steward has the right to modify this Agreement. The Eclipse Foundation +is the initial Agreement Steward. The Eclipse Foundation may assign the +responsibility to serve as the Agreement Steward to a suitable separate +entity. Each new version of the Agreement will be given a distinguishing +version number. The Program (including Contributions) may always be +Distributed subject to the version of the Agreement under which it was +received. In addition, after a new version of the Agreement is published, +Contributor may elect to Distribute the Program (including its +Contributions) under the new version. + +Except as expressly stated in Sections 2(a) and 2(b) above, Recipient +receives no rights or licenses to the intellectual property of any +Contributor under this Agreement, whether expressly, by implication, +estoppel or otherwise. All rights in the Program not expressly granted +under this Agreement are reserved. Nothing in this Agreement is intended +to be enforceable by any entity that is not a Contributor or Recipient. +No third-party beneficiary rights are created under this Agreement. + +Exhibit A - Form of Secondary Licenses Notice + +"This Source Code may also be made available under the following +Secondary Licenses when the conditions for such availability set forth +in the Eclipse Public License, v. 2.0 are satisfied: {name license(s), +version(s), and exceptions or additional permissions here}." + + Simply including a copy of this Agreement, including this Exhibit A + is not sufficient to license the Source Code under Secondary Licenses. + + If it is not possible or desirable to put the notice in a particular + file, then You may include the notice in a location (such as a LICENSE + file in a relevant directory) where a recipient would be likely to + look for such a notice. + + You may add additional accurate notices of copyright ownership. \ No newline at end of file diff --git a/licenses/LGPL-2.1.txt b/licenses/LGPL-2.1.txt new file mode 100644 index 0000000..f6683e7 --- /dev/null +++ b/licenses/LGPL-2.1.txt @@ -0,0 +1,501 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Libraries + + If you develop a new library, and you want it to be of the greatest +possible use to the public, we recommend making it free software that +everyone can redistribute and change. You can do so by permitting +redistribution under these terms (or, alternatively, under the terms of the +ordinary General Public License). + + To apply these terms, attach the following notices to the library. It is +safest to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least the +"copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, see . + +Also add information on how to contact you by electronic and paper mail. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the library, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the + library `Frob' (a library for tweaking knobs) written by James Random Hacker. + + , 1 April 1990 + Moe Ghoul, President of Vice + +That's all there is to it! diff --git a/licenses/asm-9.7.1-copyright-and-license.txt b/licenses/asm-9.7.1-copyright-and-license.txt new file mode 100644 index 0000000..83f57b9 --- /dev/null +++ b/licenses/asm-9.7.1-copyright-and-license.txt @@ -0,0 +1,27 @@ +// ASM: a very small and fast Java bytecode manipulation framework +// Copyright (c) 2000-2011 INRIA, France Telecom +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions +// are met: +// 1. Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// 3. Neither the name of the copyright holders nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF +// THE POSSIBILITY OF SUCH DAMAGE. diff --git a/licenses/logback-LICENSE.txt b/licenses/logback-LICENSE.txt new file mode 100644 index 0000000..55db596 --- /dev/null +++ b/licenses/logback-LICENSE.txt @@ -0,0 +1,15 @@ +Logback LICENSE +--------------- + +Logback: the reliable, generic, fast and flexible logging framework. +Copyright (C) 1999-2024, QOS.ch. All rights reserved. + +This program and the accompanying materials are dual-licensed under +either the terms of the Eclipse Public License v2.0 as published by +the Eclipse Foundation + + or (per the licensee's choosing) + +under the terms of the GNU Lesser General Public License version 2.1 +as published by the Free Software Foundation. + diff --git a/licenses/provenance.json b/licenses/provenance.json new file mode 100644 index 0000000..445c15b --- /dev/null +++ b/licenses/provenance.json @@ -0,0 +1,23 @@ +{ + "EPL-2.0.txt": { + "conclusion": "EPL-2.0", + "sha256": "0becf16567beb77fa252b7664631dd177c8f9a1889e48995b45379c7130e5303", + "source": "https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt" + }, + "LGPL-2.1.txt": { + "conclusion": "LGPL-2.1-only alternative delivered without changing the selected EPL conclusion", + "sha256": "20e50fe7aae3e56378ebf0417d9de904f55a0e61e4df315333e632a4d3555d95", + "source": "https://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt" + }, + "asm-9.7.1-copyright-and-license.txt": { + "conclusion": "BSD-3-Clause", + "entry": "org/objectweb/asm/ClassReader.java initial copyright/license comment, retained verbatim", + "sha256": "34b8dd2e1b4b5f822ba633602676297fa82328d9438ba4d7336ec7393d212e06", + "source": "https://repo.maven.apache.org/maven2/org/ow2/asm/asm/9.7.1/asm-9.7.1-sources.jar" + }, + "logback-LICENSE.txt": { + "conclusion": "EPL-2.0 OR LGPL-2.1-only; this distribution selects EPL-2.0", + "sha256": "f1eedf4f2cb9e901d6ae549a06d0c20dcde0136d37e38b50adaf51d4673f66d2", + "source": "https://raw.githubusercontent.com/qos-ch/logback/v_1.5.38/LICENSE.txt" + } +} diff --git a/mvnw b/mvnw new file mode 100755 index 0000000..eb33d0d --- /dev/null +++ b/mvnw @@ -0,0 +1,297 @@ +#!/bin/sh +# ---------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +# ---------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- +# Apache Maven Wrapper startup batch script, version 3.3.4 +# +# Optional ENV vars +# ----------------- +# JAVA_HOME - location of a JDK home dir, required when download maven via java source +# MVNW_REPOURL - repo url base for downloading maven distribution +# MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven +# MVNW_VERBOSE - true: enable verbose log; debug: trace the mvnw script; others: silence the output +# ---------------------------------------------------------------------------- + +set -euf +[ "${MVNW_VERBOSE-}" != debug ] || set -x + +# OS specific support. +native_path() { printf %s\\n "$1"; } +case "$(uname)" in +CYGWIN* | MINGW*) + [ -z "${JAVA_HOME-}" ] || JAVA_HOME="$(cygpath --unix "$JAVA_HOME")" + native_path() { cygpath --path --windows "$1"; } + ;; +esac + +# set JAVACMD and JAVACCMD +set_java_home() { + # For Cygwin and MinGW, ensure paths are in Unix format before anything is touched + if [ -n "${JAVA_HOME-}" ]; then + if [ -x "$JAVA_HOME/jre/sh/java" ]; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + JAVACCMD="$JAVA_HOME/jre/sh/javac" + else + JAVACMD="$JAVA_HOME/bin/java" + JAVACCMD="$JAVA_HOME/bin/javac" + + if [ ! -x "$JAVACMD" ] || [ ! -x "$JAVACCMD" ]; then + echo "The JAVA_HOME environment variable is not defined correctly, so mvnw cannot run." >&2 + echo "JAVA_HOME is set to \"$JAVA_HOME\", but \"\$JAVA_HOME/bin/java\" or \"\$JAVA_HOME/bin/javac\" does not exist." >&2 + return 1 + fi + fi + else + JAVACMD="$( + 'set' +e + 'unset' -f command 2>/dev/null + 'command' -v java + )" || : + JAVACCMD="$( + 'set' +e + 'unset' -f command 2>/dev/null + 'command' -v javac + )" || : + + if [ ! -x "${JAVACMD-}" ] || [ ! -x "${JAVACCMD-}" ]; then + echo "The java/javac command does not exist in PATH nor is JAVA_HOME set, so mvnw cannot run." >&2 + return 1 + fi + fi +} + +# hash string like Java String::hashCode +hash_string() { + str="${1:-}" h=0 + while [ -n "$str" ]; do + char="${str%"${str#?}"}" + h=$(((h * 31 + $(LC_CTYPE=C printf %d "'$char")) % 4294967296)) + str="${str#?}" + done + printf %x\\n $h +} + +verbose() { :; } +[ "${MVNW_VERBOSE-}" != true ] || verbose() { printf %s\\n "${1-}"; } + +die() { + printf %s\\n "$1" >&2 + exit 1 +} + +trim() { + # MWRAPPER-139: + # Trims trailing and leading whitespace, carriage returns, tabs, and linefeeds. + # Needed for removing poorly interpreted newline sequences when running in more + # exotic environments such as mingw bash on Windows. + printf "%s" "${1}" | tr -d '[:space:]' +} + +scriptDir="$(dirname "$0")" +scriptName="$(basename "$0")" + +# parse distributionUrl and optional distributionSha256Sum, requires .mvn/wrapper/maven-wrapper.properties +while IFS="=" read -r key value; do + case "${key-}" in + distributionUrl) distributionUrl=$(trim "${value-}") ;; + distributionSha256Sum) distributionSha256Sum=$(trim "${value-}") ;; + esac +done <"$scriptDir/.mvn/wrapper/maven-wrapper.properties" +[ -n "${distributionUrl-}" ] || die "cannot read distributionUrl property in $scriptDir/.mvn/wrapper/maven-wrapper.properties" + +case "${distributionUrl##*/}" in +maven-mvnd-*bin.*) + MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/ + case "${PROCESSOR_ARCHITECTURE-}${PROCESSOR_ARCHITEW6432-}:$(uname -a)" in + *AMD64:CYGWIN* | *AMD64:MINGW*) distributionPlatform=windows-amd64 ;; + :Darwin*x86_64) distributionPlatform=darwin-amd64 ;; + :Darwin*arm64) distributionPlatform=darwin-aarch64 ;; + :Linux*x86_64*) distributionPlatform=linux-amd64 ;; + *) + echo "Cannot detect native platform for mvnd on $(uname)-$(uname -m), use pure java version" >&2 + distributionPlatform=linux-amd64 + ;; + esac + distributionUrl="${distributionUrl%-bin.*}-$distributionPlatform.zip" + ;; +maven-mvnd-*) MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/ ;; +*) MVN_CMD="mvn${scriptName#mvnw}" _MVNW_REPO_PATTERN=/org/apache/maven/ ;; +esac + +# apply MVNW_REPOURL and calculate MAVEN_HOME +# maven home pattern: ~/.m2/wrapper/dists/{apache-maven-,maven-mvnd--}/ +[ -z "${MVNW_REPOURL-}" ] || distributionUrl="$MVNW_REPOURL$_MVNW_REPO_PATTERN${distributionUrl#*"$_MVNW_REPO_PATTERN"}" +distributionUrlName="${distributionUrl##*/}" +distributionUrlNameMain="${distributionUrlName%.*}" +distributionUrlNameMain="${distributionUrlNameMain%-bin}" +MAVEN_USER_HOME="${MAVEN_USER_HOME:-${HOME}/.m2}" +MAVEN_HOME="${MAVEN_USER_HOME}/wrapper/dists/${distributionUrlNameMain-}/$(hash_string "$distributionUrl")" + +exec_maven() { + unset MVNW_VERBOSE MVNW_USERNAME MVNW_PASSWORD MVNW_REPOURL || : + exec "$MAVEN_HOME/bin/$MVN_CMD" "$@" || die "cannot exec $MAVEN_HOME/bin/$MVN_CMD" +} + +if [ -d "$MAVEN_HOME" ]; then + verbose "found existing MAVEN_HOME at $MAVEN_HOME" + exec_maven "$@" +fi + +case "${distributionUrl-}" in +*?-bin.zip | *?maven-mvnd-?*-?*.zip) ;; +*) die "distributionUrl is not valid, must match *-bin.zip or maven-mvnd-*.zip, but found '${distributionUrl-}'" ;; +esac + +# prepare tmp dir +if TMP_DOWNLOAD_DIR="$(mktemp -d)" && [ -d "$TMP_DOWNLOAD_DIR" ]; then + clean() { rm -rf -- "$TMP_DOWNLOAD_DIR"; } + trap clean HUP INT TERM EXIT +else + die "cannot create temp dir" +fi + +mkdir -p -- "${MAVEN_HOME%/*}" + +# Download and Install Apache Maven +verbose "Couldn't find MAVEN_HOME, downloading and installing it ..." +verbose "Downloading from: $distributionUrl" +verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName" + +# select .zip or .tar.gz +if ! command -v unzip >/dev/null; then + # BusyStack pins the ZIP checksum; replacing the archive would invalidate that pin. + [ -z "${distributionSha256Sum-}" ] || die "unzip is required to verify and extract the pinned Maven ZIP distribution" + distributionUrl="${distributionUrl%.zip}.tar.gz" + distributionUrlName="${distributionUrl##*/}" +fi + +# verbose opt +__MVNW_QUIET_WGET=--quiet __MVNW_QUIET_CURL=--silent __MVNW_QUIET_UNZIP=-q __MVNW_QUIET_TAR='' +[ "${MVNW_VERBOSE-}" != true ] || __MVNW_QUIET_WGET='' __MVNW_QUIET_CURL='' __MVNW_QUIET_UNZIP='' __MVNW_QUIET_TAR=v + +# normalize http auth +case "${MVNW_PASSWORD:+has-password}" in +'') MVNW_USERNAME='' MVNW_PASSWORD='' ;; +has-password) [ -n "${MVNW_USERNAME-}" ] || MVNW_USERNAME='' MVNW_PASSWORD='' ;; +esac + +if [ -z "${MVNW_USERNAME-}" ] && command -v wget >/dev/null; then + verbose "Found wget ... using wget" + wget ${__MVNW_QUIET_WGET:+"$__MVNW_QUIET_WGET"} "$distributionUrl" -O "$TMP_DOWNLOAD_DIR/$distributionUrlName" || die "wget: Failed to fetch $distributionUrl" +elif [ -z "${MVNW_USERNAME-}" ] && command -v curl >/dev/null; then + verbose "Found curl ... using curl" + curl ${__MVNW_QUIET_CURL:+"$__MVNW_QUIET_CURL"} -f -L -o "$TMP_DOWNLOAD_DIR/$distributionUrlName" "$distributionUrl" || die "curl: Failed to fetch $distributionUrl" +elif set_java_home; then + verbose "Falling back to use Java to download" + javaSource="$TMP_DOWNLOAD_DIR/Downloader.java" + targetZip="$TMP_DOWNLOAD_DIR/$distributionUrlName" + cat >"$javaSource" <<-END + public class Downloader extends java.net.Authenticator + { + protected java.net.PasswordAuthentication getPasswordAuthentication() + { + return new java.net.PasswordAuthentication( System.getenv( "MVNW_USERNAME" ), System.getenv( "MVNW_PASSWORD" ).toCharArray() ); + } + public static void main( String[] args ) throws Exception + { + setDefault( new Downloader() ); + java.nio.file.Files.copy( java.net.URI.create( args[0] ).toURL().openStream(), java.nio.file.Paths.get( args[1] ).toAbsolutePath().normalize() ); + } + } + END + # For Cygwin/MinGW, switch paths to Windows format before running javac and java + verbose " - Compiling Downloader.java ..." + "$(native_path "$JAVACCMD")" "$(native_path "$javaSource")" || die "Failed to compile Downloader.java" + verbose " - Running Downloader.java ..." + "$(native_path "$JAVACMD")" -cp "$(native_path "$TMP_DOWNLOAD_DIR")" Downloader "$distributionUrl" "$(native_path "$targetZip")" +fi + +# If specified, validate the SHA-256 sum of the Maven distribution zip file +if [ -n "${distributionSha256Sum-}" ]; then + distributionSha256Result=false + if [ "$MVN_CMD" = mvnd.sh ]; then + echo "Checksum validation is not supported for maven-mvnd." >&2 + echo "Please disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2 + exit 1 + elif command -v sha256sum >/dev/null; then + if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | sha256sum -c - >/dev/null 2>&1; then + distributionSha256Result=true + fi + elif command -v shasum >/dev/null; then + if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | shasum -a 256 -c >/dev/null 2>&1; then + distributionSha256Result=true + fi + else + echo "Checksum validation was requested but neither 'sha256sum' or 'shasum' are available." >&2 + echo "Please install either command, or disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2 + exit 1 + fi + if [ $distributionSha256Result = false ]; then + echo "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised." >&2 + echo "If you updated your Maven version, you need to update the specified distributionSha256Sum property." >&2 + exit 1 + fi +fi + +# unzip and move +if command -v unzip >/dev/null; then + unzip ${__MVNW_QUIET_UNZIP:+"$__MVNW_QUIET_UNZIP"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -d "$TMP_DOWNLOAD_DIR" || die "failed to unzip" +else + tar xzf${__MVNW_QUIET_TAR:+"$__MVNW_QUIET_TAR"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -C "$TMP_DOWNLOAD_DIR" || die "failed to untar" +fi + +# Find the actual extracted directory name (handles snapshots where filename != directory name) +actualDistributionDir="" + +# First try the expected directory name (for regular distributions) +if [ -d "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" ]; then + if [ -f "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain/bin/$MVN_CMD" ]; then + actualDistributionDir="$distributionUrlNameMain" + fi +fi + +# If not found, search for any directory with the Maven executable (for snapshots) +if [ -z "$actualDistributionDir" ]; then + # enable globbing to iterate over items + set +f + for dir in "$TMP_DOWNLOAD_DIR"/*; do + if [ -d "$dir" ]; then + if [ -f "$dir/bin/$MVN_CMD" ]; then + actualDistributionDir="$(basename "$dir")" + break + fi + fi + done + set -f +fi + +if [ -z "$actualDistributionDir" ]; then + verbose "Contents of $TMP_DOWNLOAD_DIR:" + verbose "$(ls -la "$TMP_DOWNLOAD_DIR")" + die "Could not find Maven distribution directory in extracted archive" +fi + +verbose "Found extracted Maven distribution directory: $actualDistributionDir" +printf %s\\n "$distributionUrl" >"$TMP_DOWNLOAD_DIR/$actualDistributionDir/mvnw.url" +mv -- "$TMP_DOWNLOAD_DIR/$actualDistributionDir" "$MAVEN_HOME" || [ -d "$MAVEN_HOME" ] || die "fail to move MAVEN_HOME" + +clean || : +exec_maven "$@" diff --git a/mvnw.cmd b/mvnw.cmd new file mode 100644 index 0000000..92450f9 --- /dev/null +++ b/mvnw.cmd @@ -0,0 +1,189 @@ +<# : batch portion +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM http://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Apache Maven Wrapper startup batch script, version 3.3.4 +@REM +@REM Optional ENV vars +@REM MVNW_REPOURL - repo url base for downloading maven distribution +@REM MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven +@REM MVNW_VERBOSE - true: enable verbose log; others: silence the output +@REM ---------------------------------------------------------------------------- + +@IF "%__MVNW_ARG0_NAME__%"=="" (SET __MVNW_ARG0_NAME__=%~nx0) +@SET __MVNW_CMD__= +@SET __MVNW_ERROR__= +@SET __MVNW_PSMODULEP_SAVE=%PSModulePath% +@SET PSModulePath= +@FOR /F "usebackq tokens=1* delims==" %%A IN (`powershell -noprofile "& {$scriptDir='%~dp0'; $script='%__MVNW_ARG0_NAME__%'; icm -ScriptBlock ([Scriptblock]::Create((Get-Content -Raw '%~f0'))) -NoNewScope}"`) DO @( + IF "%%A"=="MVN_CMD" (set __MVNW_CMD__=%%B) ELSE IF "%%B"=="" (echo %%A) ELSE (echo %%A=%%B) +) +@SET PSModulePath=%__MVNW_PSMODULEP_SAVE% +@SET __MVNW_PSMODULEP_SAVE= +@SET __MVNW_ARG0_NAME__= +@SET MVNW_USERNAME= +@SET MVNW_PASSWORD= +@IF NOT "%__MVNW_CMD__%"=="" ("%__MVNW_CMD__%" %*) +@echo Cannot start maven from wrapper >&2 && exit /b 1 +@GOTO :EOF +: end batch / begin powershell #> + +$ErrorActionPreference = "Stop" +if ($env:MVNW_VERBOSE -eq "true") { + $VerbosePreference = "Continue" +} + +# calculate distributionUrl, requires .mvn/wrapper/maven-wrapper.properties +$distributionUrl = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionUrl +if (!$distributionUrl) { + Write-Error "cannot read distributionUrl property in $scriptDir/.mvn/wrapper/maven-wrapper.properties" +} + +switch -wildcard -casesensitive ( $($distributionUrl -replace '^.*/','') ) { + "maven-mvnd-*" { + $USE_MVND = $true + $distributionUrl = $distributionUrl -replace '-bin\.[^.]*$',"-windows-amd64.zip" + $MVN_CMD = "mvnd.cmd" + break + } + default { + $USE_MVND = $false + $MVN_CMD = $script -replace '^mvnw','mvn' + break + } +} + +# apply MVNW_REPOURL and calculate MAVEN_HOME +# maven home pattern: ~/.m2/wrapper/dists/{apache-maven-,maven-mvnd--}/ +if ($env:MVNW_REPOURL) { + $MVNW_REPO_PATTERN = if ($USE_MVND -eq $False) { "/org/apache/maven/" } else { "/maven/mvnd/" } + $distributionUrl = "$env:MVNW_REPOURL$MVNW_REPO_PATTERN$($distributionUrl -replace "^.*$MVNW_REPO_PATTERN",'')" +} +$distributionUrlName = $distributionUrl -replace '^.*/','' +$distributionUrlNameMain = $distributionUrlName -replace '\.[^.]*$','' -replace '-bin$','' + +$MAVEN_M2_PATH = "$HOME/.m2" +if ($env:MAVEN_USER_HOME) { + $MAVEN_M2_PATH = "$env:MAVEN_USER_HOME" +} + +if (-not (Test-Path -Path $MAVEN_M2_PATH)) { + New-Item -Path $MAVEN_M2_PATH -ItemType Directory | Out-Null +} + +$MAVEN_WRAPPER_DISTS = $null +if ((Get-Item $MAVEN_M2_PATH).Target[0] -eq $null) { + $MAVEN_WRAPPER_DISTS = "$MAVEN_M2_PATH/wrapper/dists" +} else { + $MAVEN_WRAPPER_DISTS = (Get-Item $MAVEN_M2_PATH).Target[0] + "/wrapper/dists" +} + +$MAVEN_HOME_PARENT = "$MAVEN_WRAPPER_DISTS/$distributionUrlNameMain" +$MAVEN_HOME_NAME = ([System.Security.Cryptography.SHA256]::Create().ComputeHash([byte[]][char[]]$distributionUrl) | ForEach-Object {$_.ToString("x2")}) -join '' +$MAVEN_HOME = "$MAVEN_HOME_PARENT/$MAVEN_HOME_NAME" + +if (Test-Path -Path "$MAVEN_HOME" -PathType Container) { + Write-Verbose "found existing MAVEN_HOME at $MAVEN_HOME" + Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD" + exit $? +} + +if (! $distributionUrlNameMain -or ($distributionUrlName -eq $distributionUrlNameMain)) { + Write-Error "distributionUrl is not valid, must end with *-bin.zip, but found $distributionUrl" +} + +# prepare tmp dir +$TMP_DOWNLOAD_DIR_HOLDER = New-TemporaryFile +$TMP_DOWNLOAD_DIR = New-Item -Itemtype Directory -Path "$TMP_DOWNLOAD_DIR_HOLDER.dir" +$TMP_DOWNLOAD_DIR_HOLDER.Delete() | Out-Null +trap { + if ($TMP_DOWNLOAD_DIR.Exists) { + try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null } + catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" } + } +} + +New-Item -Itemtype Directory -Path "$MAVEN_HOME_PARENT" -Force | Out-Null + +# Download and Install Apache Maven +Write-Verbose "Couldn't find MAVEN_HOME, downloading and installing it ..." +Write-Verbose "Downloading from: $distributionUrl" +Write-Verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName" + +$webclient = New-Object System.Net.WebClient +if ($env:MVNW_USERNAME -and $env:MVNW_PASSWORD) { + $webclient.Credentials = New-Object System.Net.NetworkCredential($env:MVNW_USERNAME, $env:MVNW_PASSWORD) +} +[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 +$webclient.DownloadFile($distributionUrl, "$TMP_DOWNLOAD_DIR/$distributionUrlName") | Out-Null + +# If specified, validate the SHA-256 sum of the Maven distribution zip file +$distributionSha256Sum = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionSha256Sum +if ($distributionSha256Sum) { + if ($USE_MVND) { + Write-Error "Checksum validation is not supported for maven-mvnd. `nPlease disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." + } + Import-Module $PSHOME\Modules\Microsoft.PowerShell.Utility -Function Get-FileHash + if ((Get-FileHash "$TMP_DOWNLOAD_DIR/$distributionUrlName" -Algorithm SHA256).Hash.ToLower() -ne $distributionSha256Sum) { + Write-Error "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised. If you updated your Maven version, you need to update the specified distributionSha256Sum property." + } +} + +# unzip and move +Expand-Archive "$TMP_DOWNLOAD_DIR/$distributionUrlName" -DestinationPath "$TMP_DOWNLOAD_DIR" | Out-Null + +# Find the actual extracted directory name (handles snapshots where filename != directory name) +$actualDistributionDir = "" + +# First try the expected directory name (for regular distributions) +$expectedPath = Join-Path "$TMP_DOWNLOAD_DIR" "$distributionUrlNameMain" +$expectedMvnPath = Join-Path "$expectedPath" "bin/$MVN_CMD" +if ((Test-Path -Path $expectedPath -PathType Container) -and (Test-Path -Path $expectedMvnPath -PathType Leaf)) { + $actualDistributionDir = $distributionUrlNameMain +} + +# If not found, search for any directory with the Maven executable (for snapshots) +if (!$actualDistributionDir) { + Get-ChildItem -Path "$TMP_DOWNLOAD_DIR" -Directory | ForEach-Object { + $testPath = Join-Path $_.FullName "bin/$MVN_CMD" + if (Test-Path -Path $testPath -PathType Leaf) { + $actualDistributionDir = $_.Name + } + } +} + +if (!$actualDistributionDir) { + Write-Error "Could not find Maven distribution directory in extracted archive" +} + +Write-Verbose "Found extracted Maven distribution directory: $actualDistributionDir" +Rename-Item -Path "$TMP_DOWNLOAD_DIR/$actualDistributionDir" -NewName $MAVEN_HOME_NAME | Out-Null +try { + Move-Item -Path "$TMP_DOWNLOAD_DIR/$MAVEN_HOME_NAME" -Destination $MAVEN_HOME_PARENT | Out-Null +} catch { + if (! (Test-Path -Path "$MAVEN_HOME" -PathType Container)) { + Write-Error "fail to move MAVEN_HOME" + } +} finally { + try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null } + catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" } +} + +Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD" diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..706f3b2 --- /dev/null +++ b/pom.xml @@ -0,0 +1,58 @@ + + + 4.0.0 + org.springframework.bootspring-boot-starter-parent4.1.2 + org.busystackcaldav-server0.1.0 + BusyStack CalDAV Server + Independent calendar and task server + https://github.com/busystack/caldav-server + Apache License, Version 2.0https://www.apache.org/licenses/LICENSE-2.0repo + BusyStack contributors + scm:git:https://github.com/busystack/caldav-server.gitscm:git:ssh://git@github.com/busystack/caldav-server.githttps://github.com/busystack/caldav-server + 252026-10-27T00:00:00Z + + org.springframework.bootspring-boot-starter-session-jdbc + org.springframework.bootspring-boot-starter-webmvc + org.springframework.bootspring-boot-starter-thymeleaf + org.springframework.bootspring-boot-starter-security + org.springframework.securityspring-security-webauthn + org.springframework.bootspring-boot-starter-security-oauth2-authorization-server + org.springframework.bootspring-boot-starter-security-oauth2-resource-server + org.springframework.bootspring-boot-starter-security-oauth2-client + org.springframework.bootspring-boot-starter-jdbc + org.springframework.bootspring-boot-starter-validation + org.springframework.bootspring-boot-starter-actuator + org.springframework.bootspring-boot-starter-flyway + org.flywaydbflyway-database-postgresql + org.postgresqlpostgresqlruntime + org.mnode.ical4jical4j4.4.0 + org.springframework.bootspring-boot-starter-testtest + org.springframework.bootspring-boot-starter-webmvc-testtest + org.springframework.securityspring-security-testtest + org.testcontainerstestcontainers-postgresqltest + org.testcontainerstestcontainers-junit-jupitertest + + + + src/main/resources + ${project.basedir}META-INFLICENSENOTICETHIRD-PARTY-NOTICES + licensesMETA-INF/licenses + + + org.apache.maven.pluginsmaven-clean-plugin3.5.0 + org.apache.maven.pluginsmaven-resources-plugin3.3.1 + org.apache.maven.pluginsmaven-compiler-plugin3.15.025truetrue + org.apache.maven.pluginsmaven-surefire-plugin3.5.6 + org.apache.maven.pluginsmaven-failsafe-plugin3.5.6-Xmx256mintegration-testverify + org.apache.maven.pluginsmaven-jar-plugin3.4.2 + org.springframework.bootspring-boot-maven-plugin4.1.2 + org.apache.maven.pluginsmaven-enforcer-plugin3.6.2toolchainenforce[25,26)[3.9.12,4) + com.diffplug.spotlessspotless-maven-plugin3.10.41.32.0checkvalidate + org.cyclonedxcyclonedx-maven-plugin2.9.3prepare-packagemakeAggregateBomfalsejson + org.apache.maven.pluginsmaven-dependency-plugin3.9.0 + com.github.spotbugsspotbugs-maven-plugin4.10.5.0MaxHightrueverifycheck + org.apache.maven.pluginsmaven-install-plugin3.1.4 + org.apache.maven.pluginsmaven-deploy-plugin3.1.4 + + + diff --git a/qualification/clients/README.md b/qualification/clients/README.md new file mode 100644 index 0000000..9dc78c5 --- /dev/null +++ b/qualification/clients/README.md @@ -0,0 +1,41 @@ +# Independent clients + +`IndependentClientIT` starts the actual application with PostgreSQL 18.6 and TLS signed by a disposable CA. Its account and named DAV credential are fixture setup through application services; the separate Python process performs discovery, calendar/task creation, reads, writes, queries and synchronization through `python-caldav:3.6.0` and `icalendar:7.3.0`. It shares neither HTTP/XML request builders nor calendar parsing with the server. Missing Python/dependencies fail this required test. + +The independent-client fixture leaves the synchronization-depth setting **absent**. Both python-caldav 3.6.0 and Thunderbird 157 send HTTP `Depth: 1`, whereas [RFC 6578 section 3.2](https://www.rfc-editor.org/rfc/rfc6578.html#section-3.2) requires rejection of nonzero Depth. The server default deliberately accommodates Depth 1 only with XML sync-level 1. Missing Depth and Depth 0 work normally; unsupported/malformed/duplicate/infinite values remain rejected. Explicit `BUSYSTACK_DAV_ALLOW_SYNC_DEPTH_ONE=false` selects strict mode and has a separate real-HTTPS qualification profile. Scope, authorization, continuation and computation limits are unchanged. This is a documented exception to that RFC requirement, not strict compliance with it. Each independent runner performs actual authenticated missing/0/1 REPORTs and wrong-level rejection, records observed effective behavior, and separately records the calling harness's configuration input. Client synchronization fallback remains disabled. + +```sh +python3 -m venv .client-venv +.client-venv/bin/pip install -r qualification/clients/requirements.txt +BUSYSTACK_CLIENT_PYTHON="$PWD/.client-venv/bin/python" ./mvnw verify +``` + +For the desktop-client provider profile, install the additional pinned Mozilla driver dependencies and a published Thunderbird 157.0.1 Linux binary. The installer checks the exact [official SHA512SUMS](https://archive.mozilla.org/pub/thunderbird/releases/157.0.1/SHA512SUMS) entry before extraction. It requires an empty tooling destination and changes no installed Thunderbird/profile. + +```sh +.client-venv/bin/pip install -r qualification/clients/desktop-requirements.txt +bash qualification/clients/install-thunderbird.sh .client-tools +BUSYSTACK_CLIENT_PYTHON="$PWD/.client-venv/bin/python" \ +BUSYSTACK_THUNDERBIRD_BINARY="$PWD/.client-tools/thunderbird/thunderbird" \ + ./mvnw -Dit.test=IndependentClientIT verify +``` + +This launches Thunderbird's native CalDAV provider in its real desktop executable under headless Marionette control, in a new private temporary profile. The fixture adds its test CA to that profile's normal certificate database; it never disables certificate validation. It exercises recurring-event and undated-VTODO CRUD, task completion, Unicode, alarms, unknown properties, refresh and deletion. It does not qualify the graphical account wizard, visual rendering, or a person using the interface. Without the binary setting, desktop qualification is explicitly recorded as unexecuted. + +The same-object round-trip profile also requires Thunderbird to create a recurring event, a completed undated task, an all-day event and a floating-time event. The separate python-caldav/icalendar client reads and modifies those exact resources; Thunderbird refreshes and verifies the modifications, edits them again, and Python verifies the final data and unchanged resource URLs. Assertions retain recurrence count, alarm trigger, extension properties, task completion timestamp, exclusive all-day end, and floating values without invented UTC/TZID qualifiers. A passing JSON result includes `crossClient.sameResources: 4`. The recorded IndependentClientIT run passed this complete profile; subsequent application artifacts must run the process again before extending that claim. This profile uses isolated test-CA trust and the unmodified server defaults, with observed depth behavior recorded alongside configuration provenance. + +Results and native-client serialized fixtures are written under +`target/qualification` and retained as CI artifacts. The +[qualification index](../evidence/README.md) identifies concise version-specific +records and archived historical results. A result qualifies only its executed +source, artifact and configuration after the process exits successfully. + +The [local observance UNTIL repair](../../docs/observance-until-compatibility.md) +has separate real-editor qualification for Thunderbird 159.0 (system/explicit +Vancouver and explicit Berlin) and GNOME Calendar 50.0 / EDS 3.56.2 Vancouver. +The provider harness retains bounded, credential-redacted startup diagnostics under +`target/qualification`; these generated logs do not belong in the source tree. +The packaged restore corpus admits the captured uploads, verifies corrected hashes +and transformed-PUT validator omission, and checks bytes/history/UTC expansion after +restoration. Missing prerequisites, startup failures and assertion failures remain +failures; earlier attempts are not relabeled as successful runs. diff --git a/qualification/clients/capacity.py b/qualification/clients/capacity.py new file mode 100644 index 0000000..f9a902f --- /dev/null +++ b/qualification/clients/capacity.py @@ -0,0 +1,161 @@ +#!/usr/bin/env python3 +"""Independent raw RFC6578 traversal of a disposable SQL-seeded, valid iCalendar dataset.""" +import base64 +from concurrent.futures import ThreadPoolExecutor +import hashlib +import json +import os +from pathlib import Path +import ssl +import sys +import time +import threading +from urllib.error import HTTPError +from urllib.parse import quote, urljoin, urlsplit +from urllib.request import Request, urlopen +import xml.etree.ElementTree as XML + +D='{DAV:}'; C='{urn:ietf:params:xml:ns:caldav}' + + +def open_response(request, context, timeout=60): + try: return urlopen(request,context=context,timeout=timeout) + except HTTPError as error: return error + + +def selective_query(request): + query=XML.Element(C+'calendar-query');XML.SubElement(XML.SubElement(query,D+'prop'),D+'getetag') + filters=XML.SubElement(query,C+'filter');top=XML.SubElement(filters,C+'comp-filter',name='VCALENDAR');event=XML.SubElement(top,C+'comp-filter',name='VEVENT') + XML.SubElement(event,C+'time-range',start='20261027T000000Z',end='20261028T000000Z') + return request('REPORT',body=XML.tostring(query),headers={'Depth':'1','Content-Type':'application/xml'},timeout=150) + + +def main(): + import icalendar + url=os.environ['BUSYSTACK_CAPACITY_URL'] + auth='Basic '+base64.b64encode((os.environ['BUSYSTACK_CLIENT_USER']+':'+Path(os.environ['BUSYSTACK_CLIENT_SECRET_FILE']).read_text().strip()).encode()).decode() + context=ssl.create_default_context(cafile=os.environ['BUSYSTACK_CLIENT_CA']) + metrics={'requests':0,'responseBytes':0,'largestResponseBytes':0,'pageLimit':97,'syncFallbackDisabled':True,'configurationInput':os.environ['BUSYSTACK_CLIENT_SYNC_DEPTH_INPUT']} + metrics_lock=threading.Lock() + def request(method,target=url,body=None,headers=None,timeout=60): + response=open_response(Request(target,data=body,method=method,headers={'Authorization':auth,**(headers or {})}),context,timeout=timeout) + with response: + data=response.read(9*1024*1024) + assert len(data)<9*1024*1024, 'Response exceeded supported serialized budget' + with metrics_lock: + metrics['requests']+=1;metrics['responseBytes']+=len(data);metrics['largestResponseBytes']=max(metrics['largestResponseBytes'],len(data)) + return response.status,response.headers,data + def page(token,content): + document=XML.Element(D+'sync-collection');XML.SubElement(document,D+'sync-token').text=token + XML.SubElement(document,D+'sync-level').text='1' + prop=XML.SubElement(document,D+'prop');XML.SubElement(prop,D+'getetag') + if content: XML.SubElement(prop,C+'calendar-data') + XML.SubElement(XML.SubElement(document,D+'limit'),D+'nresults').text='97' + status,_,body=request('REPORT',body=XML.tostring(document),headers={'Depth':'1','Content-Type':'application/xml'}) + assert status==207, (status,body[:200]) + root=XML.fromstring(body);next_token=root.findtext(D+'sync-token');assert next_token + rows=[];partial=False + for response in root.findall(D+'response'): + href=urljoin(url,response.findtext(D+'href')) + status=response.findtext(D+'status','') + if href.rstrip('/')==url.rstrip('/'): + assert ' 507 ' in status, status + assert response.find('.//'+D+'number-of-matches-within-limits') is not None + partial=True;continue + rows.append({'href':href,'deleted':' 404 ' in status,'etag':response.findtext('.//'+D+'getetag'),'content':response.findtext('.//'+C+'calendar-data')}) + assert len(rows)<=97 + if partial: assert rows and next_token!=token, 'Truncated sync must advance and make progress' + return next_token,rows,partial + def resource(name):return url+quote(name,safe='') + def calendar(uid,summary): + return ('BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//BusyStack capacity fixture//EN\r\nBEGIN:VEVENT\r\nUID:'+uid+'\r\nDTSTAMP:20261027T000000Z\r\nDTSTART:20000101T000000Z\r\nDTEND:20000101T010000Z\r\nSUMMARY:'+summary+'\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n').encode() + began=time.monotonic();token=None;seen={};pages=0;body_bytes=0 + while True: + previous=token;token,rows,partial=page(token,True);pages+=1 + for row in rows: + assert not row['deleted'] and row['content'] is not None + assert row['href'] not in seen,'Duplicate resource in initial snapshot' + parsed=icalendar.Calendar.from_ical(row['content']) + assert len(parsed.walk('VEVENT'))==1 + seen[row['href']]=row['etag'];body_bytes+=len(row['content'].encode()) + if pages==1: + # The entire multi-request view must stay pinned despite names changing behind/ahead of its cursor. + assert request('PUT',resource('capacity-00001.ics'),calendar('capacity-1','first update'),{'Content-Type':'text/calendar'})[0]==204 + assert request('PUT',resource('capacity-00001.ics'),calendar('capacity-1','second update'),{'Content-Type':'text/calendar'})[0]==204 + assert request('DELETE',resource('capacity-00002.ics'))[0]==204 + assert request('DELETE',resource('capacity-10000.ics'))[0]==204 + assert request('PUT',resource('capacity-10000.ics'),calendar('capacity-10000-new','recreated'),{'Content-Type':'text/calendar','If-None-Match':'*'})[0]==201 + assert request('PUT',resource('000-added-雪.ics'),calendar('capacity-added','inserted before boundary'),{'Content-Type':'text/calendar','If-None-Match':'*'})[0]==201 + if pages==2: + # Replaying a continuation must not consume it or silently change the page. + again,again_rows,again_partial=page(previous,True) + assert again_rows==rows and again_partial==partial + print('CAPACITY_READY_RESTART',flush=True) + assert sys.stdin.readline().strip()=='CONTINUE_AFTER_RESTART' + again,again_rows,again_partial=page(previous,True) + assert again_rows==rows and again_partial==partial,'Restart must preserve continuation state' + if not partial:break + assert pages<200, 'No finite initial-sync completion' + assert len(seen)==10037 and body_bytes>32*1024*1024 + assert resource('capacity-00002.ics') in seen and resource('000-added-雪.ics') not in seen + delta=[];delta_pages=0 + while True: + token,rows,partial=page(token,True);delta+=rows;delta_pages+=1 + if not partial:break + assert delta_pages<200 + changes={row['href']:row for row in delta} + assert changes[resource('capacity-00002.ics')]['deleted'] + assert 'second update' in changes[resource('capacity-00001.ics')]['content'] + assert 'recreated' in changes[resource('capacity-10000.ics')]['content'] + assert 'inserted before boundary' in changes[resource('000-added-雪.ics')]['content'] + metadata_seen=set();metadata_pages=0;metadata_token=None + while True: + metadata_token,rows,partial=page(metadata_token,False);metadata_pages+=1 + for row in rows: + assert row['content'] is None and not row['deleted'];assert row['href'] not in metadata_seen;metadata_seen.add(row['href']) + if not partial:break + assert metadata_pages<200 + assert len(metadata_seen)==10037 and resource('000-added-雪.ics') in metadata_seen + query_start=time.monotonic();status,_,body=selective_query(request) + assert status==207, (status,body[:256]);assert len(XML.fromstring(body).findall(D+'response'))==1 + query_ms=round((time.monotonic()-query_start)*1000,2) + # Many individually admitted property updates exercise aggregate storage and selected response work. + values='x'*60000 + def patch(target,index,value): + change=XML.Element(D+'propertyupdate');prop=XML.SubElement(XML.SubElement(change,D+'set'),D+'prop') + XML.SubElement(prop,'{urn:busystack:capacity}p'+str(index)).text=value + return request('PROPPATCH',target,XML.tostring(change),{'Content-Type':'application/xml'}) + targets=[resource('capacity-'+str(i).zfill(5)+'.ics') for i in range(20,32)] + for target in targets: + for index in range(17): + status,_,body=patch(target,index,values) + assert status==207 and b' 200 OK' in body + status,_,body=patch(target,'small','selected value') + assert status==207 and b' 200 OK' in body + status,_,body=patch(targets[0],17,values) + assert status in (403,507) or status==207 and b' 507 ' in body, 'Over-budget property growth must fail explicitly' + def propfind(target,mode): + root=XML.Element(D+'propfind') + if mode=='one': XML.SubElement(XML.SubElement(root,D+'prop'),'{urn:busystack:capacity}psmall') + else: XML.SubElement(root,D+mode) + status,_,body=request('PROPFIND',target,XML.tostring(root),{'Depth':'0','Content-Type':'application/xml'}) + assert status==207 + if mode=='one': assert len(body)<4096 and b'selected value' in body and b'x'*100 not in body + if mode=='propname': assert len(body)<32768 and b'x'*100 not in body + if mode=='allprop': assert len(body)>1000000 + return len(body) + with ThreadPoolExecutor(max_workers=3) as executor: + small_responses=list(executor.map(lambda target:propfind(target,'one'),targets)) + name_responses=list(executor.map(lambda target:propfind(target,'propname'),targets)) + propfind(targets[0],'allprop') + multi=XML.Element(C+'calendar-multiget');props=XML.SubElement(multi,D+'prop') + for index in range(17): XML.SubElement(props,'{urn:busystack:capacity}p'+str(index)) + for target in targets: XML.SubElement(multi,D+'href').text=target + status,_,body=request('REPORT',body=XML.tostring(multi),headers={'Content-Type':'application/xml'}) + assert status==507, 'A genuinely oversized selected-property response must fail before commit' + assert request('GET',targets[0])[0]==200, 'Server must remain usable after limit failure' + metrics['propertyStress']={'resources':len(targets),'acceptedPropertiesPerResource':17,'valueBytes':60000,'concurrentReaders':3,'largestSmallPropertyResponseBytes':max(small_responses),'largestPropertyNameResponseBytes':max(name_responses),'combinedSelectedPropertyLimitStatus':status,'growthLimitVerified':True,'healthyAfterLimit':True} + metrics.update(status='passed',seededResources=10037,seededCalendarBodyBytes=body_bytes,initialContentPages=pages,initialMetadataPages=metadata_pages,incrementalPages=delta_pages,changedResources=len(changes),restartContinuationVerified=True,continuationReplayVerified=True,selectiveQueryMs=query_ms,elapsedSeconds=round(time.monotonic()-began,2),populationMethod='owned disposable SQL fixture; independently generated valid iCalendar bytes, not a measurement of HTTP bulk create throughput') + print(json.dumps(metrics,sort_keys=True),flush=True) + +if __name__=='__main__':main() diff --git a/qualification/clients/desktop-requirements.txt b/qualification/clients/desktop-requirements.txt new file mode 100644 index 0000000..f9049c8 --- /dev/null +++ b/qualification/clients/desktop-requirements.txt @@ -0,0 +1,16 @@ +blessed==1.52.0 +distro==1.9.0 +jinxed==2.1.0 +marionette_driver==3.7.1 +mozdevice==4.2.0 +mozfile==3.0.0 +mozinfo==1.2.3 +mozlog==8.1.0 +mozprocess==1.4.0 +mozprofile==3.0.0 +mozrunner==8.4.0 +mozsystemmonitor==1.0.1 +mozterm==1.0.0 +mozversion==2.4.0 +psutil==7.2.2 +wcwidth==0.9.4 diff --git a/qualification/clients/install-thunderbird.sh b/qualification/clients/install-thunderbird.sh new file mode 100644 index 0000000..f3310b9 --- /dev/null +++ b/qualification/clients/install-thunderbird.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env bash +set -euo pipefail + +# Test tooling only. Published Mozilla binary and its pinned official SHA512SUMS entry. +destination=${1:?Usage: install-thunderbird.sh EMPTY_TOOL_DIRECTORY} +mkdir -p "$destination" +tool_root=$(cd "$destination" && pwd) +test ! -e "$tool_root/thunderbird" || { echo 'Tool directory already contains Thunderbird; choose an empty directory.' >&2; exit 1; } +archive=$(mktemp "$tool_root/thunderbird-download.XXXXXX") +trap 'rm -f "$archive"' EXIT +curl --fail --silent --show-error --location --proto '=https' --tlsv1.2 --max-time 300 \ + https://archive.mozilla.org/pub/thunderbird/releases/157.0.1/linux-x86_64/en-US/thunderbird-157.0.1.tar.xz \ + --output "$archive" +printf '%s %s\n' '8918c554924886f21ecb394cd877cf69cd3f343fb3dfab829a9793fbf558b1e90a32249a2f76afa781fdbb09e4f498b12f146f6078cf4be9339c09897c3fee27' "$archive" | sha512sum --check --status +tar --extract --xz --file "$archive" --directory "$tool_root" +printf '%s\n' "$tool_root/thunderbird/thunderbird" diff --git a/qualification/clients/python_caldav.py b/qualification/clients/python_caldav.py new file mode 100644 index 0000000..ef7a9a4 --- /dev/null +++ b/qualification/clients/python_caldav.py @@ -0,0 +1,115 @@ +#!/usr/bin/env python3 +"""Independent python-caldav operations. Called by IndependentClientIT over test-CA HTTPS. + +The fixture provisions its account/credential through application services before this +process starts. All calendar/task discovery, creation, queries and mutations use +python-caldav's own HTTP/XML and Python's independent iCalendar implementation. +""" + +import importlib.metadata +import json +import os +from datetime import datetime, timedelta, timezone +from pathlib import Path +from uuid import uuid4 + +import caldav +import icalendar +from sync_profile import verify_sync_profile + + +def main(): + assert importlib.metadata.version("caldav") == "3.6.0", "Install qualification/clients/requirements.txt" + password = Path(os.environ["BUSYSTACK_CLIENT_SECRET_FILE"]).read_text().strip() + with caldav.DAVClient( + url=os.environ["BUSYSTACK_CLIENT_URL"], + username=os.environ["BUSYSTACK_CLIENT_USER"], + password=password, + auth_type="basic", + ssl_verify_cert=os.environ["BUSYSTACK_CLIENT_CA"], + timeout=30, + ) as client: + principal = client.principal() + events = principal.make_calendar(name="Python events 雪", cal_id="python-events", supported_calendar_component_set=["VEVENT"]) + tasks = principal.make_calendar(name="Python tasks", cal_id="python-tasks", supported_calendar_component_set=["VTODO"]) + sync_profile = verify_sync_profile(client, events) + assert {str(calendar.url) for calendar in principal.calendars()} >= {str(events.url), str(tasks.url)} + start = datetime(2026, 10, 27, 10, tzinfo=timezone.utc) + event = events.save_event(dtstart=start, dtend=start + timedelta(hours=1), summary="Independent café 雪", uid=str(uuid4())) + event.load() + assert str(event.icalendar_component["SUMMARY"]) == "Independent café 雪" + event.icalendar_component.add("X-INDEPENDENT-PROPERTY", "Preserved by BusyStack") + event.save() + event.load() + assert str(event.icalendar_component["X-INDEPENDENT-PROPERTY"]) == "Preserved by BusyStack" + assert len(events.search(start=start, end=start + timedelta(days=1), event=True)) == 1 + recurring = events.save_event(dtstart=start + timedelta(days=1), dtend=start + timedelta(days=1, hours=1), summary="Independent recurring event", rrule={"FREQ": "DAILY", "COUNT": 4}, uid=str(uuid4())) + recurring.load() + recurring_url = str(recurring.url) + model = icalendar.Calendar.from_ical(recurring.data) + master = next(part for part in model.subcomponents if part.name == "VEVENT") + master.add("EXDATE", start + timedelta(days=4)) + exception = icalendar.Event() + exception.add("UID", str(master["UID"])) + exception.add("DTSTAMP", master.decoded("DTSTAMP")) + exception.add("RECURRENCE-ID", start + timedelta(days=2)) + exception.add("DTSTART", start + timedelta(days=2, hours=2)) + exception.add("DTEND", start + timedelta(days=2, hours=3)) + exception.add("SUMMARY", "Independent moved occurrence 雪") + exception.add("X-INDEPENDENT-EXCEPTION", "preserved") + model.add_component(exception) + recurring.data = model.to_ical() + recurring.save() + recurring.load() + assert str(recurring.url) == recurring_url + stored = icalendar.Calendar.from_ical(recurring.data) + components = [part for part in stored.subcomponents if part.name == "VEVENT"] + assert len(components) == 2 and {str(part["UID"]) for part in components} == {str(master["UID"])} + found = events.search(start=start + timedelta(days=1), end=start + timedelta(days=5), event=True, expand=True, server_expand=True) + assert len(found) == 3 + assert sorted(item.icalendar_component.decoded("DTSTART") for item in found) == [start + timedelta(days=1), start + timedelta(days=2, hours=2), start + timedelta(days=3)] + # Edit the detached component without changing its RECURRENCE-ID or resource URL. + exception = next(part for part in components if "RECURRENCE-ID" in part) + for property_name in ("DTSTART", "DTEND", "SUMMARY"): + exception.pop(property_name, None) + exception.add("DTSTART", start + timedelta(days=2, hours=4)) + exception.add("DTEND", start + timedelta(days=2, hours=5)) + exception.add("SUMMARY", "Independent changed exception 雪") + recurring.data = stored.to_ical() + recurring.save() + recurring.load() + assert str(recurring.url) == recurring_url + stored = icalendar.Calendar.from_ical(recurring.data) + exception = next(part for part in stored.subcomponents if part.name == "VEVENT" and "RECURRENCE-ID" in part) + assert exception.decoded("RECURRENCE-ID") == start + timedelta(days=2) + assert exception.decoded("DTSTART") == start + timedelta(days=2, hours=4) + assert str(exception["X-INDEPENDENT-EXCEPTION"]) == "preserved" + found = events.search(start=start + timedelta(days=1), end=start + timedelta(days=5), event=True, expand=True, server_expand=True) + assert len(found) == 3 + assert sorted(item.icalendar_component.decoded("DTSTART") for item in found) == [start + timedelta(days=1), start + timedelta(days=2, hours=4), start + timedelta(days=3)] + task = tasks.save_todo(summary="Independent undated task", uid=str(uuid4()), priority=2, **{"percent-complete": 25}) + task.load() + assert "DTSTART" not in task.icalendar_component and "DUE" not in task.icalendar_component + assert len(tasks.todos(include_completed=True)) == 1 + task.complete(completion_timestamp=datetime(2026, 10, 27, 12, tzinfo=timezone.utc)) + task.load() + assert str(task.icalendar_component["STATUS"]) == "COMPLETED" + initial = events.get_objects_by_sync_token(disable_fallback=True) + assert str(initial.sync_token).startswith("urn:busystack:sync:") + assert len(list(initial)) == 2 + event.delete() + updated = events.get_objects_by_sync_token(sync_token=initial.sync_token, disable_fallback=True) + assert str(updated.sync_token) != str(initial.sync_token) + assert event.url.canonical() in updated.deleted_urls, "Incremental sync must report the deleted resource" + assert len(events.events()) == 1 + recurring.delete() + task.delete() + assert len(events.events()) == 0 + assert len(tasks.todos(include_completed=True)) == 0 + events.delete() + tasks.delete() + print(json.dumps({"client": "python-caldav", "version": caldav.__version__, "icalendar": importlib.metadata.version("icalendar"), "status": "passed", "syncConfiguration": sync_profile, "syncFallbackDisabled": True, "operations": ["principal and home discovery", "calendar and VTODO collection creation", "Unicode event CRUD", "unknown-property round trip", "event time-range query", "server recurrence expansion", "same-resource detached exception CRUD and RECURRENCE-ID preservation", "EXDATE exclusion in server expansion", "undated task CRUD and completion", "initial and incremental synchronization", "resource and collection deletion"]}, sort_keys=True)) + + +if __name__ == "__main__": + main() diff --git a/qualification/clients/requirements.txt b/qualification/clients/requirements.txt new file mode 100644 index 0000000..2a430de --- /dev/null +++ b/qualification/clients/requirements.txt @@ -0,0 +1,20 @@ +caldav==3.6.0 +calendaring-config==0.5.0 +charset-normalizer==3.5.2 +click==8.5.0 +dnspython==2.9.0 +h11==0.16.0 +icalendar==7.3.0 +icalendar-searcher==1.3.0 +jh2==5.0.15 +lxml==6.1.3 +niquests==3.21.2 +python-dateutil==2.9.0.post0 +PyYAML==6.0.3 +qh3==2.1.0 +recurring-ical-events==3.8.2 +six==1.17.0 +tzdata==2026.5 +urllib3-future==2.25.904 +wassima==2.1.5 +x-wr-timezone==2.0.1 diff --git a/qualification/clients/sync_profile.py b/qualification/clients/sync_profile.py new file mode 100644 index 0000000..4e7693f --- /dev/null +++ b/qualification/clients/sync_profile.py @@ -0,0 +1,17 @@ +"""Record actual protocol behavior and separately supplied configuration provenance.""" +import os + + +def verify_sync_profile(client, calendar): + body = '1' + results = {} + for depth in (None, '0', '1'): + headers = {'Content-Type': 'application/xml; charset=utf-8'} + if depth is not None: + headers['Depth'] = depth + response = client.request(str(calendar.url), method='REPORT', body=body, headers=headers) + results['missing' if depth is None else depth] = response.status + assert results == {'missing': 207, '0': 207, '1': 207}, results + rejected = client.request(str(calendar.url), method='REPORT', body=body.replace('1', '2'), headers={'Depth': '1', 'Content-Type': 'application/xml'}) + assert rejected.status in (400, 403), rejected.status + return {'configurationInput': os.environ.get('BUSYSTACK_CLIENT_SYNC_DEPTH_INPUT', 'not reported by calling harness'), 'observedDepthStatus': results, 'depthOneWithOtherSyncLevelStatus': rejected.status, 'allowSyncDepthOne': results['1'] == 207, 'verifiedBy': 'actual authenticated REPORTs to this collection'} diff --git a/qualification/clients/thunderbird.py b/qualification/clients/thunderbird.py new file mode 100644 index 0000000..c57f4ed --- /dev/null +++ b/qualification/clients/thunderbird.py @@ -0,0 +1,364 @@ +#!/usr/bin/env python3 +"""Drive an isolated Thunderbird's native CalDAV provider through Mozilla Marionette. + +No existing profile is opened. Test CA trust is added to this disposable profile; +certificate validation remains enabled. This qualifies the desktop client's native +provider, not a person manually completing its graphical account wizard. +""" + +import base64 +import importlib.metadata +import json +import os +from datetime import date, datetime, timedelta, timezone +from pathlib import Path +import socket +import subprocess +import tempfile +import threading +import time + +import caldav +from sync_profile import verify_sync_profile +import icalendar +from marionette_driver.marionette import Marionette + + +SETUP = r""" +const [eventUrl,taskUrl,username,password,ca] = arguments; +const done=arguments[arguments.length-1]; +(async()=>{ + const {cal}=ChromeUtils.importESModule('resource:///modules/calendar/calUtils.sys.mjs'); + Cc['@mozilla.org/security/x509certdb;1'].getService(Ci.nsIX509CertDB).addCertFromBase64(ca,'CT,C,C'); + Services.prefs.setBoolPref('signon.rememberSignons',true); + Services.logins.setLoginSavingEnabled(new URL(eventUrl).origin,true); + await cal.auth.passwordManagerSave(username,password,new URL(eventUrl).origin,'BusyStack DAV'); + globalThis.busystackCalendars=[]; + globalThis.busystackMetadata=[]; + Services.obs.addObserver({observe(subject){ + const channel=subject.QueryInterface(Ci.nsIHttpChannel); + if(channel.URI.prePath!==new URL(eventUrl).origin)return; + let origin='missing',site='missing'; + try{const value=channel.getRequestHeader('Origin');origin=value==='null'?'null':value===new URL(eventUrl).origin?'same-origin':'foreign';}catch{} + try{const value=channel.getRequestHeader('Sec-Fetch-Site');site=['none','same-origin','same-site','cross-site'].includes(value)?value:'other';}catch{} + if(globalThis.busystackMetadata.length<10)globalThis.busystackMetadata.push({method:channel.requestMethod,status:channel.responseStatus,origin,site}); + }},'http-on-examine-response'); + globalThis.busystackRefresh = (calendar,register=false) => new Promise((resolve,reject)=>{ + let settled=false; + const deadline=setTimeout(()=>{if(!settled){settled=true;calendar.removeObserver(observer);reject(new Error("Native refresh timeout status="+calendar.getProperty("currentStatus")+" checked="+calendar.wrappedJSObject.checkedServerInfo+" disabled="+calendar.getProperty("disabled")+" metadata="+JSON.stringify(globalThis.busystackMetadata)));}},45000); + const observer={ + QueryInterface:ChromeUtils.generateQI(['calIObserver']), + onStartBatch(){},onEndBatch(){},onAddItem(){},onModifyItem(){},onDeleteItem(){},onPropertyChanged(){},onPropertyDeleting(){}, + onLoad(){if(!settled){settled=true;clearTimeout(deadline);calendar.removeObserver(observer);resolve();}}, + onError(_calendar,code){if(!settled){settled=true;clearTimeout(deadline);calendar.removeObserver(observer);reject(new Error('Native CalDAV load failed: '+code));}} + }; + calendar.addObserver(observer);if(register)cal.manager.registerCalendar(calendar);else calendar.refresh(); + }); + for(const [url,name] of [[eventUrl,'BusyStack qualification events'],[taskUrl,'BusyStack qualification tasks']]){ + const calendar=cal.manager.createCalendar('caldav',Services.io.newURI(url)); + calendar.setProperty('username',username);calendar.name=name; + calendar.setProperty('cache.enabled',false);calendar.setProperty('suppressAlarms',true); + calendar.id=cal.getUUID(); + globalThis.busystackCalendars.push(calendar); + await globalThis.busystackRefresh(calendar,true); + } + return {app:Services.appinfo.name,version:Services.appinfo.version}; +})().then(done,error=>done({error:String(error.message)})); +""" + +OPERATIONS = r""" +const done=arguments[arguments.length-1]; +(async()=>{ + const [events,tasks]=globalThis.busystackCalendars; + const event=Cc['@mozilla.org/calendar/event;1'].createInstance(Ci.calIEvent); + event.icalString='BEGIN:VEVENT\r\nUID:thunderbird-event\r\nDTSTAMP:20261027T000000Z\r\nDTSTART:20261027T100000Z\r\nDTEND:20261027T110000Z\r\nSUMMARY:Thunderbird café 雪\r\nRRULE:FREQ=DAILY;COUNT=4\r\nEXDATE:20261030T100000Z\r\nX-NATIVE-CLIENT:preserved\r\nBEGIN:VALARM\r\nACTION:DISPLAY\r\nDESCRIPTION:Stored alarm\r\nTRIGGER:-PT15M\r\nEND:VALARM\r\nEND:VEVENT\r\n'; + let stored=await events.addItem(event); + if(stored.title!=='Thunderbird café 雪')throw new Error('Event title changed'); + const old=stored;stored=stored.clone();stored.title='Thunderbird changed'; + const recurrenceId=Cc['@mozilla.org/calendar/datetime;1'].createInstance(Ci.calIDateTime); + recurrenceId.icalString='20261028T100000Z'; + const detached=stored.recurrenceInfo.getOccurrenceFor(recurrenceId).clone(); + detached.title='Thunderbird moved occurrence 雪'; + detached.startDate=recurrenceId.clone();detached.startDate.hour=12; + detached.endDate=recurrenceId.clone();detached.endDate.hour=13; + detached.setProperty('X-NATIVE-EXCEPTION','preserved'); + detached.clearAlarms();detached.addAlarm(stored.getAlarms()[0].clone()); + stored.recurrenceInfo.modifyException(detached,true); + stored=await events.modifyItem(stored,old); + await globalThis.busystackRefresh(events); + const reloaded=await events.getItem('thunderbird-event'); + if(reloaded.title!=='Thunderbird changed'||reloaded.getProperty('X-NATIVE-CLIENT')!=='preserved'||!reloaded.recurrenceInfo||reloaded.getAlarms().length!==1)throw new Error('Event round trip lost data'); + const reloadedException=reloaded.recurrenceInfo.getExceptionFor(recurrenceId); + if(reloaded.recurrenceInfo.getExceptionIds().length!==1||!reloadedException||reloadedException.recurrenceId.icalString!=='20261028T100000Z'||reloadedException.startDate.icalString!=='20261028T120000Z'||reloadedException.endDate.icalString!=='20261028T130000Z'||reloadedException.title!=='Thunderbird moved occurrence 雪'||reloadedException.getProperty('X-NATIVE-EXCEPTION')!=='preserved'||reloadedException.getAlarms().length!==1)throw new Error('Native detached exception lost its recurrence identity, time, title, extension or alarm'); + const task=Cc['@mozilla.org/calendar/todo;1'].createInstance(Ci.calITodo); + task.icalString='BEGIN:VTODO\r\nUID:thunderbird-task\r\nDTSTAMP:20261027T000000Z\r\nSUMMARY:Thunderbird undated task\r\nPRIORITY:2\r\nPERCENT-COMPLETE:25\r\nX-NATIVE-TASK:preserved\r\nEND:VTODO\r\n'; + let savedTask=await tasks.addItem(task); + if(savedTask.entryDate||savedTask.dueDate)throw new Error('Task dates were invented'); + const before=savedTask;savedTask=savedTask.clone();savedTask.isCompleted=true; + savedTask=await tasks.modifyItem(savedTask,before); + await globalThis.busystackRefresh(tasks); + const reloadedTask=await tasks.getItem('thunderbird-task'); + if(!reloadedTask.isCompleted||reloadedTask.percentComplete!==100||reloadedTask.getProperty('X-NATIVE-TASK')!=='preserved')throw new Error('Task completion changed'); + const allDay=Cc['@mozilla.org/calendar/event;1'].createInstance(Ci.calIEvent); + allDay.icalString='BEGIN:VEVENT\r\nUID:thunderbird-all-day\r\nDTSTAMP:20261027T000000Z\r\nDTSTART;VALUE=DATE:20261028\r\nDTEND;VALUE=DATE:20261030\r\nSUMMARY:Thunderbird all-day event\r\nX-NATIVE-SHAPE:all-day\r\nEND:VEVENT\r\n'; + await events.addItem(allDay); + const floating=Cc['@mozilla.org/calendar/event;1'].createInstance(Ci.calIEvent); + floating.icalString='BEGIN:VEVENT\r\nUID:thunderbird-floating\r\nDTSTAMP:20261027T000000Z\r\nDTSTART:20261027T133000\r\nDTEND:20261027T141500\r\nSUMMARY:Thunderbird floating event\r\nX-NATIVE-SHAPE:floating\r\nEND:VEVENT\r\n'; + await events.addItem(floating); + await globalThis.busystackRefresh(events); + const reloadedDay=await events.getItem('thunderbird-all-day'); + const reloadedFloating=await events.getItem('thunderbird-floating'); + if(!reloadedDay.startDate.isDate||!reloadedDay.endDate.isDate||reloadedDay.startDate.icalString!=='20261028'||reloadedDay.endDate.icalString!=='20261030')throw new Error('Native all-day value or exclusive end changed'); + if(!reloadedFloating.startDate.timezone.isFloating||!reloadedFloating.endDate.timezone.isFloating||reloadedFloating.startDate.icalString!=='20261027T133000'||reloadedFloating.endDate.icalString!=='20261027T141500')throw new Error('Native floating date became zoned'); + return {event:reloaded.icalString,exception:reloadedException.icalString,task:reloadedTask.icalString}; +})().then(done,error=>done({error:String(error.message)})); +""" + +CROSS_CLIENT = r""" +const done=arguments[arguments.length-1]; +(async()=>{ + const [events,tasks]=globalThis.busystackCalendars; + await globalThis.busystackRefresh(events);await globalThis.busystackRefresh(tasks); + const event=await events.getItem('thunderbird-event'); + if(event.title!=='Python changed native recurring event 雪'||event.getProperty('X-PYTHON-ROUNDTRIP')!=='event'||event.getProperty('X-NATIVE-CLIENT')!=='preserved'||!event.recurrenceInfo||event.getAlarms().length!==1)throw new Error('Native refresh lost Python event edit, recurrence, alarm or extension'); + const rangeStart=Cc['@mozilla.org/calendar/datetime;1'].createInstance(Ci.calIDateTime); + rangeStart.icalString='20261027T000000Z'; + const rangeEnd=rangeStart.clone();rangeEnd.day+=4; + const occurrences=event.recurrenceInfo.getOccurrences(rangeStart,rangeEnd,0); + if(occurrences.length!==3||occurrences.some(item=>item.getAlarms().length!==1))throw new Error('Native recurrence instances or alarms changed after Python edit'); + const recurrenceId=Cc['@mozilla.org/calendar/datetime;1'].createInstance(Ci.calIDateTime); + recurrenceId.icalString='20261028T100000Z'; + const exception=event.recurrenceInfo.getExceptionFor(recurrenceId); + if(event.recurrenceInfo.getExceptionIds().length!==1||!exception||exception.recurrenceId.icalString!=='20261028T100000Z'||exception.startDate.icalString!=='20261028T140000Z'||exception.endDate.icalString!=='20261028T150000Z'||exception.title!=='Python moved native occurrence 雪'||exception.getProperty('X-NATIVE-EXCEPTION')!=='preserved'||exception.getProperty('X-PYTHON-EXCEPTION')!=='preserved')throw new Error('Native refresh lost Python detached-exception edit'); + const starts=occurrences.map(item=>item.startDate.icalString); + if(JSON.stringify(starts)!==JSON.stringify(['20261027T100000Z','20261028T140000Z','20261029T100000Z']))throw new Error('Native occurrence expansion ignored the detached override or EXDATE'); + const task=await tasks.getItem('thunderbird-task'); + if(task.title!=='Python changed native undated task 雪'||task.getProperty('X-PYTHON-ROUNDTRIP')!=='task'||task.getProperty('X-NATIVE-TASK')!=='preserved'||!task.isCompleted||task.percentComplete!==100||task.priority!==5||task.entryDate||task.dueDate)throw new Error('Native refresh lost Python task edit or invented dates'); + const allDay=await events.getItem('thunderbird-all-day'); + if(allDay.title!=='Python changed native all-day event'||allDay.getProperty('X-PYTHON-ROUNDTRIP')!=='all-day'||allDay.getProperty('X-NATIVE-SHAPE')!=='all-day'||!allDay.startDate.isDate||!allDay.endDate.isDate||allDay.startDate.icalString!=='20261028'||allDay.endDate.icalString!=='20261030')throw new Error('Cross-client all-day end or value type changed'); + const floating=await events.getItem('thunderbird-floating'); + if(floating.title!=='Python changed native floating event'||floating.getProperty('X-PYTHON-ROUNDTRIP')!=='floating'||floating.getProperty('X-NATIVE-SHAPE')!=='floating'||!floating.startDate.timezone.isFloating||!floating.endDate.timezone.isFloating||floating.startDate.icalString!=='20261027T133000'||floating.endDate.icalString!=='20261027T141500')throw new Error('Cross-client floating value became zoned'); + for(const [calendar,item,kind] of [[events,event,'event'],[tasks,task,'task'],[events,allDay,'all-day'],[events,floating,'floating']]) { + const changed=item.clone();changed.title='Thunderbird final '+kind;changed.setProperty('X-NATIVE-RETURN',kind); + if(kind==='event'){ + const finalException=changed.recurrenceInfo.getExceptionFor(recurrenceId).clone(); + finalException.title='Thunderbird final exception'; + finalException.startDate=recurrenceId.clone();finalException.startDate.hour=16; + finalException.endDate=recurrenceId.clone();finalException.endDate.hour=17; + finalException.setProperty('X-NATIVE-RETURN-EXCEPTION','preserved'); + changed.recurrenceInfo.modifyException(finalException,true); + } + await calendar.modifyItem(changed,item); + } + await globalThis.busystackRefresh(events);await globalThis.busystackRefresh(tasks); + return {crossClient:true,nativeOccurrenceCount:occurrences.length,nativeDetachedExceptions:event.recurrenceInfo.getExceptionIds().length}; +})().then(done,error=>done({error:String(error.message)})); +""" + +DELETE = r""" +const done=arguments[arguments.length-1]; +(async()=>{ + const [events,tasks]=globalThis.busystackCalendars; + await events.deleteItem(await events.getItem('thunderbird-event')); + await events.deleteItem(await events.getItem('thunderbird-all-day')); + await events.deleteItem(await events.getItem('thunderbird-floating')); + await tasks.deleteItem(await tasks.getItem('thunderbird-task')); + await globalThis.busystackRefresh(events);await globalThis.busystackRefresh(tasks); + if(await events.getItem('thunderbird-event')||await events.getItem('thunderbird-all-day')||await events.getItem('thunderbird-floating')||await tasks.getItem('thunderbird-task'))throw new Error('Deleted item remained'); + return {deleted:true}; +})().then(done,error=>done({error:String(error.message)})); +""" + + +def independent_items(events, tasks): + result = {} + for item in [*events.events(), *tasks.todos(include_completed=True)]: + item.load() + model = icalendar.Calendar.from_ical(item.data) + primary = [part for part in model.subcomponents if part.name in {"VEVENT", "VTODO"}] + masters = [part for part in primary if "RECURRENCE-ID" not in part] + assert len(masters) == 1, "One authoritative resource must retain one recurring master" + uid = str(masters[0]["UID"]) + assert {str(part["UID"]) for part in primary} == {uid}, "Detached exceptions must share the master's UID" + assert len(primary) == (2 if uid == "thunderbird-event" else 1), "An exception must remain inside its master's resource" + assert uid not in result, "Cross-client operation created a duplicate UID resource" + result[uid] = (item, model, masters[0]) + assert set(result) == {"thunderbird-event", "thunderbird-task", "thunderbird-all-day", "thunderbird-floating"} + return result + + +def assert_independent_data(items, final=False, completed=None, exception_phase="native"): + event = items["thunderbird-event"][2] + assert event.name == "VEVENT" + assert event.decoded("DTSTART") == datetime(2026, 10, 27, 10, tzinfo=timezone.utc) + assert event.decoded("DTEND") == datetime(2026, 10, 27, 11, tzinfo=timezone.utc) + assert event["RRULE"]["FREQ"] == ["DAILY"] and event["RRULE"]["COUNT"] == [4] + excluded = event["EXDATE"] if isinstance(event["EXDATE"], list) else [event["EXDATE"]] + assert [entry.dt for values in excluded for entry in values.dts] == [datetime(2026, 10, 30, 10, tzinfo=timezone.utc)] + detached = [part for part in items["thunderbird-event"][1].subcomponents if part.name == "VEVENT" and "RECURRENCE-ID" in part] + assert len(detached) == 1 + exception = detached[0] + assert exception.decoded("RECURRENCE-ID") == datetime(2026, 10, 28, 10, tzinfo=timezone.utc) + assert "RRULE" not in exception and "EXDATE" not in exception + hour = 16 if final else 14 if exception_phase == "python" else 12 + title = "Thunderbird final exception" if final else "Python moved native occurrence 雪" if exception_phase == "python" else "Thunderbird moved occurrence 雪" + assert exception.decoded("DTSTART") == datetime(2026, 10, 28, hour, tzinfo=timezone.utc) + assert exception.decoded("DTEND") == datetime(2026, 10, 28, hour + 1, tzinfo=timezone.utc) + assert str(exception["SUMMARY"]) == title and str(exception["X-NATIVE-EXCEPTION"]) == "preserved" + exception_alarms = [part for part in exception.subcomponents if part.name == "VALARM"] + assert len(exception_alarms) == 1 and exception_alarms[0].decoded("TRIGGER") == timedelta(minutes=-15) + if final or exception_phase == "python": + assert str(exception["X-PYTHON-EXCEPTION"]) == "preserved" + if final: + assert str(exception["X-NATIVE-RETURN-EXCEPTION"]) == "preserved" + assert str(event["X-NATIVE-CLIENT"]) == "preserved" + alarms = [part for part in event.subcomponents if part.name == "VALARM"] + assert len(alarms) == 1 and alarms[0].decoded("TRIGGER") == timedelta(minutes=-15) + assert str(alarms[0]["ACTION"]) == "DISPLAY" and str(alarms[0]["DESCRIPTION"]) == "Stored alarm" + task = items["thunderbird-task"][2] + assert task.name == "VTODO" and "DTSTART" not in task and "DUE" not in task + assert str(task["STATUS"]) == "COMPLETED" and int(task["PERCENT-COMPLETE"]) == 100 + assert str(task["X-NATIVE-TASK"]) == "preserved" + assert task.decoded("COMPLETED").tzinfo is not None + if completed is not None: + assert task.decoded("COMPLETED") == completed, "Summary/priority edits changed task completion timestamp" + all_day = items["thunderbird-all-day"][2] + assert all_day.decoded("DTSTART") == date(2026, 10, 28) and all_day.decoded("DTEND") == date(2026, 10, 30) + assert str(all_day["DTSTART"].params["VALUE"]) == "DATE" and str(all_day["DTEND"].params["VALUE"]) == "DATE" + assert str(all_day["X-NATIVE-SHAPE"]) == "all-day" + floating = items["thunderbird-floating"][2] + assert floating.decoded("DTSTART") == datetime(2026, 10, 27, 13, 30) + assert floating.decoded("DTEND") == datetime(2026, 10, 27, 14, 15) + assert floating.decoded("DTSTART").tzinfo is None and floating.decoded("DTEND").tzinfo is None + assert "TZID" not in floating["DTSTART"].params and "TZID" not in floating["DTEND"].params + assert str(floating["X-NATIVE-SHAPE"]) == "floating" + if final: + for uid, kind in [("thunderbird-event", "event"), ("thunderbird-task", "task"), ("thunderbird-all-day", "all-day"), ("thunderbird-floating", "floating")]: + component = items[uid][2] + assert str(component["SUMMARY"]) == "Thunderbird final " + kind + assert str(component["X-PYTHON-ROUNDTRIP"]) == kind and str(component["X-NATIVE-RETURN"]) == kind + assert int(task["PRIORITY"]) == 5 + + +def python_edits_native_items(events, tasks): + items = independent_items(events, tasks) + assert_independent_data(items) + completed = items["thunderbird-task"][2].decoded("COMPLETED") + urls = {uid: str(item.url) for uid, (item, _, _) in items.items()} + for uid, kind, summary in [ + ("thunderbird-event", "event", "Python changed native recurring event 雪"), + ("thunderbird-task", "task", "Python changed native undated task 雪"), + ("thunderbird-all-day", "all-day", "Python changed native all-day event"), + ("thunderbird-floating", "floating", "Python changed native floating event"), + ]: + item, model, component = items[uid] + component.pop("SUMMARY", None) + component.add("SUMMARY", summary) + component.add("X-PYTHON-ROUNDTRIP", kind) + if kind == "task": + component.pop("PRIORITY", None) + component.add("PRIORITY", 5) + if kind == "event": + exception = next(part for part in model.subcomponents if part.name == "VEVENT" and "RECURRENCE-ID" in part) + for property_name in ("DTSTART", "DTEND", "SUMMARY"): + exception.pop(property_name, None) + exception.add("DTSTART", datetime(2026, 10, 28, 14, tzinfo=timezone.utc)) + exception.add("DTEND", datetime(2026, 10, 28, 15, tzinfo=timezone.utc)) + exception.add("SUMMARY", "Python moved native occurrence 雪") + exception.add("X-PYTHON-EXCEPTION", "preserved") + item.data = model.to_ical() + item.save() + reloaded = independent_items(events, tasks) + assert_independent_data(reloaded, completed=completed, exception_phase="python") + assert {uid: str(item.url) for uid, (item, _, _) in reloaded.items()} == urls + return urls, completed + + +def main(): + assert importlib.metadata.version("caldav") == "3.6.0" and importlib.metadata.version("icalendar") == "7.3.0", "Install pinned independent-client dependencies" + binary = os.environ["BUSYSTACK_THUNDERBIRD_BINARY"] + password = Path(os.environ["BUSYSTACK_CLIENT_SECRET_FILE"]).read_text().strip() + with caldav.DAVClient(url=os.environ["BUSYSTACK_CLIENT_URL"], username=os.environ["BUSYSTACK_CLIENT_USER"], password=password, auth_type="basic", ssl_verify_cert=os.environ["BUSYSTACK_CLIENT_CA"], timeout=30) as client: + principal = client.principal() + events = principal.make_calendar(name="Thunderbird events", cal_id="thunderbird-events", supported_calendar_component_set=["VEVENT"]) + tasks = principal.make_calendar(name="Thunderbird tasks", cal_id="thunderbird-tasks", supported_calendar_component_set=["VTODO"]) + sync_profile = verify_sync_profile(client, events) + with tempfile.TemporaryDirectory(prefix="busystack-thunderbird-") as temporary: + profile = Path(temporary) + profile.chmod(0o700) + with socket.socket() as listener: + listener.bind(("127.0.0.1", 0)) + port = listener.getsockname()[1] + prefs = {"marionette.port": port, "marionette.host": "127.0.0.1", "marionette.enabled": True, "mail.provider.enabled": False, "mailnews.start_page.enabled": False, "datareporting.policy.dataSubmissionEnabled": False, "datareporting.healthreport.uploadEnabled": False, "toolkit.telemetry.enabled": False, "app.update.enabled": False, "calendar.timezone.local": "UTC", "network.proxy.type": 0, "mail.shell.checkDefaultClient": False, "mailnews.database.global.indexer.enabled": False} + (profile / "user.js").write_text("\n".join("user_pref(" + json.dumps(key) + "," + json.dumps(value) + ");" for key, value in prefs.items())) + env = os.environ.copy() + env.update(MOZ_HEADLESS="1", MOZ_NO_REMOTE="1", MOZ_REMOTE_ALLOW_SYSTEM_ACCESS="1") + with (profile / "browser.log").open("w") as log: + process = subprocess.Popen([binary, "--headless", "--no-remote", "--profile", str(profile), "--marionette", "--remote-allow-system-access"], env=env, stdout=log, stderr=subprocess.STDOUT) + driver = None + try: + deadline = time.monotonic() + 120 + while time.monotonic() < deadline: + if process.poll() is not None: + raise RuntimeError("Isolated Thunderbird exited before Marionette was ready") + try: + with socket.create_connection(("127.0.0.1", port), timeout=1): + break + except OSError: + threading.Event().wait(0.1) + else: + raise RuntimeError("Isolated Thunderbird did not become ready") + driver = Marionette(host="127.0.0.1", port=port, socket_timeout=120) + driver.start_session() + driver.set_context("chrome") + driver.timeout.script = 120 + pem = Path(os.environ["BUSYSTACK_CLIENT_CA"]).read_text() + ca = "".join(line for line in pem.splitlines() if not line.startswith("---")) + info = driver.execute_async_script(SETUP, new_sandbox=False, script_args=[str(events.url), str(tasks.url), os.environ["BUSYSTACK_CLIENT_USER"], password, ca]) + assert "error" not in info, info + result = driver.execute_async_script(OPERATIONS, new_sandbox=False) + assert "error" not in result, result + artifacts = Path("target/qualification") + artifacts.mkdir(parents=True, exist_ok=True) + for kind in ("event", "exception", "task"): + (artifacts / ("thunderbird-" + kind + ".ics")).write_bytes(result[kind].encode("utf-8")) + assert len(events.events()) == 3 and len(tasks.todos(include_completed=True)) == 1 + urls, completed = python_edits_native_items(events, tasks) + cross = driver.execute_async_script(CROSS_CLIENT, new_sandbox=False) + assert cross == {"crossClient": True, "nativeOccurrenceCount": 3, "nativeDetachedExceptions": 1}, cross + final = independent_items(events, tasks) + assert_independent_data(final, final=True, completed=completed) + assert {uid: str(item.url) for uid, (item, _, _) in final.items()} == urls + for uid, (_, model, _) in final.items(): + (artifacts / (uid + "-cross-client.ics")).write_bytes(model.to_ical()) + deleted = driver.execute_async_script(DELETE, new_sandbox=False) + assert deleted == {"deleted": True}, deleted + assert len(events.events()) == 0 and len(tasks.todos(include_completed=True)) == 0 + print(json.dumps({"client": info["app"], "version": info["version"], "driver": importlib.metadata.version("marionette_driver"), "crossClient": {"python-caldav": importlib.metadata.version("caldav"), "icalendar": importlib.metadata.version("icalendar"), "sameResources": 4, "nativeRecurrenceInstances": cross["nativeOccurrenceCount"], "nativeDetachedExceptions": cross["nativeDetachedExceptions"], "excludedOccurrences": 1}, "mode": "isolated headless native desktop CalDAV provider", "syncConfiguration": sync_profile, "status": "passed", "operations": ["server discovery by native provider", "recurring event CRUD", "Unicode and unknown-property round trip", "alarm preservation without execution", "undated VTODO CRUD and completion", "same-object Thunderbird to Python to Thunderbird to Python edits", "cross-client recurrence, alarm, extension and completion timestamp preservation", "same-resource detached exception identity/time/title edits in both clients", "EXDATE exclusion verified by native occurrence expansion", "all-day DATE and exclusive end preservation across both clients", "floating DATE-TIME preservation across both clients", "native refresh and deletion synchronization"]}, sort_keys=True)) + finally: + try: + if driver is not None: + driver.delete_session() + finally: + process.terminate() + try: + process.wait(timeout=15) + except subprocess.TimeoutExpired: + process.kill() + process.wait(timeout=15) + # Keep startup failures reviewable after TemporaryDirectory cleanup. + # Never retain the disposable DAV secret or its Basic authorization value. + artifacts = Path("target/qualification") + artifacts.mkdir(parents=True, exist_ok=True) + diagnostic = (profile / "browser.log").read_text(errors="replace")[-262144:] + diagnostic = diagnostic.replace(password, "[REDACTED]") + authorization = base64.b64encode( + (os.environ["BUSYSTACK_CLIENT_USER"] + ":" + password).encode()).decode() + diagnostic = diagnostic.replace(authorization, "[REDACTED]") + (artifacts / "thunderbird-process.log").write_text(diagnostic) + events.delete() + tasks.delete() + + +if __name__ == "__main__": + main() diff --git a/qualification/clients/workload.py b/qualification/clients/workload.py new file mode 100644 index 0000000..5f723e9 --- /dev/null +++ b/qualification/clients/workload.py @@ -0,0 +1,80 @@ +#!/usr/bin/env python3 +"""Measured small-installation workload through independent python-caldav, not a capacity claim.""" + +import importlib.metadata +import json +import os +from pathlib import Path +import platform +import statistics +import time +from datetime import datetime, timedelta, timezone +from uuid import uuid4 + +import caldav +from sync_profile import verify_sync_profile + + +def summary(values): + ordered = sorted(values) + return {"count": len(values), "meanMs": round(statistics.mean(values), 2), "medianMs": round(statistics.median(values), 2), "p95Ms": round(ordered[max(0, (95 * len(ordered) + 99) // 100 - 1)], 2), "maxMs": round(max(values), 2)} + + +def main(): + assert caldav.__version__ == "3.6.0" + timings = {} + + def measure(kind, operation): + start = time.perf_counter_ns() + result = operation() + timings.setdefault(kind, []).append((time.perf_counter_ns() - start) / 1_000_000) + return result + + password = Path(os.environ["BUSYSTACK_CLIENT_SECRET_FILE"]).read_text().strip() + with caldav.DAVClient(url=os.environ["BUSYSTACK_CLIENT_URL"], username=os.environ["BUSYSTACK_CLIENT_USER"], password=password, auth_type="basic", ssl_verify_cert=os.environ["BUSYSTACK_CLIENT_CA"], timeout=60) as client: + principal = client.principal() + nonce = uuid4().hex + events = principal.make_calendar(name="Disposable measured events", cal_id="workload-events-" + nonce, supported_calendar_component_set=["VEVENT"]) + tasks = principal.make_calendar(name="Disposable measured tasks", cal_id="workload-tasks-" + nonce, supported_calendar_component_set=["VTODO"]) + sync_profile = verify_sync_profile(client, events) + objects = [] + anchor = datetime(2026, 11, 1, 10, tzinfo=timezone.utc) + stored_bytes = 0 + try: + for index in range(200): + start = anchor + timedelta(days=index % 30, hours=index % 8) + item = measure("eventCreate", lambda: events.save_event(uid="workload-" + str(uuid4()), dtstart=start, dtend=start + timedelta(minutes=45), summary="Measured event " + str(index), description="Disposable synthetic workload data. " * 40)) + stored_bytes += len(item.data.encode("utf-8")) + objects.append(item) + for index in range(20): + item = measure("recurringEventCreate", lambda: events.save_event(uid="workload-" + str(uuid4()), dtstart=anchor + timedelta(days=index % 5), dtend=anchor + timedelta(days=index % 5, hours=1), summary="Measured weekly recurrence", rrule={"FREQ": "WEEKLY", "COUNT": 12})) + stored_bytes += len(item.data.encode("utf-8")) + objects.append(item) + for index in range(50): + properties = {} if index % 2 == 0 else {"due": anchor + timedelta(days=index % 30)} + item = measure("taskCreate", lambda: tasks.save_todo(uid="workload-" + str(uuid4()), summary="Measured task " + str(index), priority=5, **properties)) + stored_bytes += len(item.data.encode("utf-8")) + for item in objects[:20]: + measure("eventGet", item.load) + for _ in range(10): + found = measure("monthCalendarQuery", lambda: events.search(start=anchor - timedelta(days=1), end=anchor + timedelta(days=31), event=True, expand=False)) + assert len(found) == 220 + first = measure("initialSync", lambda: events.get_objects_by_sync_token(disable_fallback=True)) + assert len(list(first)) == 220 and str(first.sync_token).startswith("urn:busystack:sync:") + for item in objects[:10]: + item.icalendar_component["SUMMARY"] = "Measured updated event" + measure("eventUpdate", item.save) + for item in objects[10:15]: + measure("eventDelete", item.delete) + delta = measure("incrementalSync", lambda: events.get_objects_by_sync_token(sync_token=first.sync_token, disable_fallback=True)) + assert {item.url.canonical() for item in objects[10:15]} <= delta.deleted_urls + assert len(events.events()) == 215 and len(tasks.todos(include_completed=True)) == 50 + print(json.dumps({"status": "passed", "client": "python-caldav", "version": caldav.__version__, "icalendar": importlib.metadata.version("icalendar"), "python": platform.python_version(), "platform": platform.system() + " " + platform.machine(), "transport": "HTTPS with isolated test-CA validation", "syncConfiguration": sync_profile, "syncFallbackDisabled": True, "calendarQueryRepresentation": "stored resources; expand=False", "latencyScope": "End-to-end independent client calls, including serialization, TLS/network, server/database and response parsing", "dataset": {"collections": 2, "nonRecurringEvents": 200, "recurringResources": 20, "weeklyInstancesPerRecurringResource": 12, "tasks": 50, "undatedTasks": 25, "acceptedResourceBytes": stored_bytes}, "operations": {name: summary(values) for name, values in timings.items()}, "interpretation": "One bounded single-client workload; no scalability, high-availability or production sizing claim."}, sort_keys=True)) + finally: + # Both namespaces are created by this process; never remove another user's data. + events.delete() + tasks.delete() + + +if __name__ == "__main__": + main() diff --git a/qualification/distribution/runtime-artifacts.json b/qualification/distribution/runtime-artifacts.json new file mode 100644 index 0000000..2a86b93 --- /dev/null +++ b/qualification/distribution/runtime-artifacts.json @@ -0,0 +1,494 @@ +{ + "artifacts": { + "HdrHistogram-2.2.2.jar": { + "sha256": "22d1d4316c4ec13a68b559e98c8256d69071593731da96136640f864fa14fad8", + "url": "https://repo.maven.apache.org/maven2/org/hdrhistogram/HdrHistogram/2.2.2/HdrHistogram-2.2.2.jar" + }, + "HikariCP-7.0.2.jar": { + "sha256": "f1e612fa27345be3107a85431e8a8aeb205c15364ab2f2d411e40a9d7bb08095", + "url": "https://repo.maven.apache.org/maven2/com/zaxxer/HikariCP/7.0.2/HikariCP-7.0.2.jar" + }, + "accessors-smart-2.6.0.jar": { + "sha256": "222c9f547bb20a99fc486403a398352d1306fb671b38abd7ecab6401df170e61", + "url": "https://repo.maven.apache.org/maven2/net/minidev/accessors-smart/2.6.0/accessors-smart-2.6.0.jar" + }, + "asm-9.7.1.jar": { + "sha256": "8cadd43ac5eb6d09de05faecca38b917a040bb9139c7edeb4cc81c740b713281", + "url": "https://repo.maven.apache.org/maven2/org/ow2/asm/asm/9.7.1/asm-9.7.1.jar" + }, + "attoparser-2.0.7.RELEASE.jar": { + "sha256": "75dd1c045492bff8e1963aabb28bfe903c2064e11e27fe2f0f0aff1ad3d84476", + "url": "https://repo.maven.apache.org/maven2/org/attoparser/attoparser/2.0.7.RELEASE/attoparser-2.0.7.RELEASE.jar" + }, + "checker-qual-3.55.1.jar": { + "sha256": "857658c8bdcbff794949a8d9922f88d63ee3751c203c7cd7d21ea97f9e745288", + "url": "https://repo.maven.apache.org/maven2/org/checkerframework/checker-qual/3.55.1/checker-qual-3.55.1.jar" + }, + "classmate-1.7.3.jar": { + "sha256": "75fbda45456f123fb6e2028a6189442d8d0730b357adce4c0a6d7e789f70669b", + "url": "https://repo.maven.apache.org/maven2/com/fasterxml/classmate/1.7.3/classmate-1.7.3.jar" + }, + "commons-codec-1.21.0.jar": { + "sha256": "4da851cb6abfb98bfe9eb77c5e5fc47f5414fa28b94e21b7fd9a646705dc167f", + "url": "https://repo.maven.apache.org/maven2/commons-codec/commons-codec/1.21.0/commons-codec-1.21.0.jar" + }, + "commons-lang3-3.20.0.jar": { + "sha256": "69e5c9fa35da7a51a5fd2099dfe56a2d8d32cf233e2f6d770e796146440263f4", + "url": "https://repo.maven.apache.org/maven2/org/apache/commons/commons-lang3/3.20.0/commons-lang3-3.20.0.jar" + }, + "commons-logging-1.3.6.jar": { + "sha256": "f8ead8943401081dea0aa824b5b1ba40a0e4ed297a572a0f02258150a0b62357", + "url": "https://repo.maven.apache.org/maven2/commons-logging/commons-logging/1.3.6/commons-logging-1.3.6.jar" + }, + "content-type-2.3.jar": { + "sha256": "60349793e006fba96b532cb0c21e10e969fe0db8d87f91c3b9eaf82ba2998895", + "url": "https://repo.maven.apache.org/maven2/com/nimbusds/content-type/2.3/content-type-2.3.jar" + }, + "flyway-core-12.4.0.jar": { + "sha256": "c3f93df1f3e0c9d14652adb5ade97401f2ede503859ac693c2af5c06e2797799", + "url": "https://repo.maven.apache.org/maven2/org/flywaydb/flyway-core/12.4.0/flyway-core-12.4.0.jar" + }, + "flyway-database-postgresql-12.4.0.jar": { + "sha256": "abc075a5c3cbf37dc64b28fec7e2398fb14f31109f7ab6271854738797407b85", + "url": "https://repo.maven.apache.org/maven2/org/flywaydb/flyway-database-postgresql/12.4.0/flyway-database-postgresql-12.4.0.jar" + }, + "hibernate-validator-9.1.5.Final.jar": { + "sha256": "1e2c1f63050a6863613d910555a81e850a6aa313e4e5f526984512cfd2946dea", + "url": "https://repo.maven.apache.org/maven2/org/hibernate/validator/hibernate-validator/9.1.5.Final/hibernate-validator-9.1.5.Final.jar" + }, + "ical4j-4.4.0.jar": { + "sha256": "f4cac5c4a16d8c0f2228ad0e3221b90d069ae0efea73402b51888468e86405f9", + "url": "https://repo.maven.apache.org/maven2/org/mnode/ical4j/ical4j/4.4.0/ical4j-4.4.0.jar" + }, + "jackson-annotations-2.21.jar": { + "sha256": "53ca085f4a150f703f49e1aabd935bd03b43e1ea3d55d135438292af22cef56b", + "url": "https://repo.maven.apache.org/maven2/com/fasterxml/jackson/core/jackson-annotations/2.21/jackson-annotations-2.21.jar" + }, + "jackson-core-3.1.7.jar": { + "sha256": "7608c367fd8a92666d28711519599543c180298259963184ce04d2947a8f71e7", + "url": "https://repo.maven.apache.org/maven2/tools/jackson/core/jackson-core/3.1.7/jackson-core-3.1.7.jar" + }, + "jackson-databind-3.1.7.jar": { + "sha256": "48c781de87e2016e1f0d973748a27130f11643e7ac080173f40e2d9669d84be3", + "url": "https://repo.maven.apache.org/maven2/tools/jackson/core/jackson-databind/3.1.7/jackson-databind-3.1.7.jar" + }, + "jackson-dataformat-cbor-3.1.7.jar": { + "sha256": "a9121d3f8ceecd42da4380f9dd5d2bcfc5157445c477297b2ca5e560eb56f95a", + "url": "https://repo.maven.apache.org/maven2/tools/jackson/dataformat/jackson-dataformat-cbor/3.1.7/jackson-dataformat-cbor-3.1.7.jar" + }, + "jakarta.annotation-api-3.0.0.jar": { + "sha256": "b01f55552284cfb149411e64eabca75e942d26d2e1786b32914250e4330afaa2", + "url": "https://repo.maven.apache.org/maven2/jakarta/annotation/jakarta.annotation-api/3.0.0/jakarta.annotation-api-3.0.0.jar" + }, + "jakarta.validation-api-3.1.1.jar": { + "sha256": "63ce00156388c365f3ac1be71fcfaf114682fc0c452020b5df6e7ec236e142ab", + "url": "https://repo.maven.apache.org/maven2/jakarta/validation/jakarta.validation-api/3.1.1/jakarta.validation-api-3.1.1.jar" + }, + "jboss-logging-3.6.3.Final.jar": { + "sha256": "7c12ee575508f81e22b1db9334b969d0ec54ef0fd1dcba24eeab1a44235fc366", + "url": "https://repo.maven.apache.org/maven2/org/jboss/logging/jboss-logging/3.6.3.Final/jboss-logging-3.6.3.Final.jar" + }, + "jcip-annotations-1.0-1.jar": { + "sha256": "4fccff8382aafc589962c4edb262f6aa595e34f1e11e61057d1c6a96e8fc7323", + "url": "https://repo.maven.apache.org/maven2/com/github/stephenc/jcip/jcip-annotations/1.0-1/jcip-annotations-1.0-1.jar" + }, + "json-smart-2.6.0.jar": { + "sha256": "1ae4b561458afb540be8ec5c6dbb4f2e715a319a7ae64854998aaf924770d61b", + "url": "https://repo.maven.apache.org/maven2/net/minidev/json-smart/2.6.0/json-smart-2.6.0.jar" + }, + "jspecify-1.0.1.jar": { + "sha256": "070d75f261fe4c5b8202508366715f7f2d4660f88c8ef7e6d3575e48c9683b66", + "url": "https://repo.maven.apache.org/maven2/org/jspecify/jspecify/1.0.1/jspecify-1.0.1.jar" + }, + "jul-to-slf4j-2.0.20.jar": { + "sha256": "062249d0677a0e08039120a2292a8db064c69d04282bf562af2e54c53ff06976", + "url": "https://repo.maven.apache.org/maven2/org/slf4j/jul-to-slf4j/2.0.20/jul-to-slf4j-2.0.20.jar" + }, + "lang-tag-1.7.jar": { + "sha256": "e8c1c594e2425bdbea2d860de55c69b69fc5d59454452449a0f0913c2a5b8a31", + "url": "https://repo.maven.apache.org/maven2/com/nimbusds/lang-tag/1.7/lang-tag-1.7.jar" + }, + "log4j-api-2.25.5.jar": { + "sha256": "64777f73ea0b3104c04eb82befbdccc30a425a19e83ad06cb2f93aa303511863", + "url": "https://repo.maven.apache.org/maven2/org/apache/logging/log4j/log4j-api/2.25.5/log4j-api-2.25.5.jar" + }, + "log4j-to-slf4j-2.25.5.jar": { + "sha256": "b68e8b47342dbc9b35644a14fe2efc0b7bf18da703910cd05ad5501ffe07bba1", + "url": "https://repo.maven.apache.org/maven2/org/apache/logging/log4j/log4j-to-slf4j/2.25.5/log4j-to-slf4j-2.25.5.jar" + }, + "logback-classic-1.5.38.jar": { + "sha256": "afb439c61fa287c9e1925523b13baab03617e724406a389d48b61c2c3e46b700", + "url": "https://repo.maven.apache.org/maven2/ch/qos/logback/logback-classic/1.5.38/logback-classic-1.5.38.jar" + }, + "logback-core-1.5.38.jar": { + "sha256": "a855a0fc97b1d06f4efb1c3bba5ebd0eb508d8b58e68e89e4bbc23a85135c883", + "url": "https://repo.maven.apache.org/maven2/ch/qos/logback/logback-core/1.5.38/logback-core-1.5.38.jar" + }, + "micrometer-commons-1.17.2.jar": { + "sha256": "af8c85e879025ce30c381e6810e14c4f52b4150894e36c815860d14763271348", + "url": "https://repo.maven.apache.org/maven2/io/micrometer/micrometer-commons/1.17.2/micrometer-commons-1.17.2.jar" + }, + "micrometer-core-1.17.2.jar": { + "sha256": "e28ddecbd27477cc7020defd1c2061a3d8b6b2e543d90856186711fe7f346532", + "url": "https://repo.maven.apache.org/maven2/io/micrometer/micrometer-core/1.17.2/micrometer-core-1.17.2.jar" + }, + "micrometer-jakarta9-1.17.2.jar": { + "sha256": "2d76984cb7c994fd3629722f08ef5830b56a5312c0176c703207ecea9cda01b7", + "url": "https://repo.maven.apache.org/maven2/io/micrometer/micrometer-jakarta9/1.17.2/micrometer-jakarta9-1.17.2.jar" + }, + "micrometer-observation-1.17.2.jar": { + "sha256": "d3ff53cfa0532dc1af0de349073f9aeb985e01f4b88c1d904350178805859695", + "url": "https://repo.maven.apache.org/maven2/io/micrometer/micrometer-observation/1.17.2/micrometer-observation-1.17.2.jar" + }, + "nimbus-jose-jwt-10.9.1.jar": { + "sha256": "33152ea83ec50d22706fdaf3b07acbcd716f9a68edcabdd7c4d02843cbdcdcf6", + "url": "https://repo.maven.apache.org/maven2/com/nimbusds/nimbus-jose-jwt/10.9.1/nimbus-jose-jwt-10.9.1.jar" + }, + "oauth2-oidc-sdk-11.38.2.jar": { + "sha256": "30e63220c15bd93006c557bd0d614bb57a5f00232188693577c222383ea4bbe7", + "url": "https://repo.maven.apache.org/maven2/com/nimbusds/oauth2-oidc-sdk/11.38.2/oauth2-oidc-sdk-11.38.2.jar" + }, + "postgresql-42.7.14.jar": { + "sha256": "73914527305a40cce504b0d3d90b23caf565136912d607ae8ae7c5895512332c", + "url": "https://repo.maven.apache.org/maven2/org/postgresql/postgresql/42.7.14/postgresql-42.7.14.jar" + }, + "slf4j-api-2.0.20.jar": { + "sha256": "7e1446499b359675d8aabe6af2de86b85e72ae8a2a932c0b40d0e5ba57097439", + "url": "https://repo.maven.apache.org/maven2/org/slf4j/slf4j-api/2.0.20/slf4j-api-2.0.20.jar" + }, + "snakeyaml-2.6.jar": { + "sha256": "c8f7a98e7394adda02f6317249710e4d1b4c7a25aa8c7eace0c2eea52eb8bf85", + "url": "https://repo.maven.apache.org/maven2/org/yaml/snakeyaml/2.6/snakeyaml-2.6.jar" + }, + "spring-aop-7.0.10.jar": { + "sha256": "babf50a645f4f788e1267078cf891e472f657cabf65518055cf5214220574e3f", + "url": "https://repo.maven.apache.org/maven2/org/springframework/spring-aop/7.0.10/spring-aop-7.0.10.jar" + }, + "spring-beans-7.0.10.jar": { + "sha256": "bd67c6fc887afa30cbcb489ff4f82bf19f818e79ab1cf1bef98dff8a289108ec", + "url": "https://repo.maven.apache.org/maven2/org/springframework/spring-beans/7.0.10/spring-beans-7.0.10.jar" + }, + "spring-boot-4.1.2.jar": { + "sha256": "1fbf6443a301eb9a098bd024149c0edc6578b073f75da8c98bc1af84c43a8f59", + "url": "https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot/4.1.2/spring-boot-4.1.2.jar" + }, + "spring-boot-actuator-4.1.2.jar": { + "sha256": "82452c9ae022d6ac7fd8270532b655f3c7940b117e4902de25402f648e02de98", + "url": "https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-actuator/4.1.2/spring-boot-actuator-4.1.2.jar" + }, + "spring-boot-actuator-autoconfigure-4.1.2.jar": { + "sha256": "97faaf81fe3fb54e224592afdd8af154fd65cc62b303334db35b076b63e03e1e", + "url": "https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-actuator-autoconfigure/4.1.2/spring-boot-actuator-autoconfigure-4.1.2.jar" + }, + "spring-boot-autoconfigure-4.1.2.jar": { + "sha256": "a3582f417c8194a621776d14ea281e072d84e85bdbabc1e02df5c035835b5096", + "url": "https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-autoconfigure/4.1.2/spring-boot-autoconfigure-4.1.2.jar" + }, + "spring-boot-flyway-4.1.2.jar": { + "sha256": "3aa1f3a85cc7f9892a58e303e6f39073626a3c65c00688bf263c5d46355d69d6", + "url": "https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-flyway/4.1.2/spring-boot-flyway-4.1.2.jar" + }, + "spring-boot-health-4.1.2.jar": { + "sha256": "68f71d7fc65eb7dee534b172efb9687214c38f8920aa78b4685c51babc511bc3", + "url": "https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-health/4.1.2/spring-boot-health-4.1.2.jar" + }, + "spring-boot-http-converter-4.1.2.jar": { + "sha256": "d1e655ce47617315c6d87e754597928a31d21478b5c28b0e3300963b0915b551", + "url": "https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-http-converter/4.1.2/spring-boot-http-converter-4.1.2.jar" + }, + "spring-boot-jackson-4.1.2.jar": { + "sha256": "83a8d3ca9ac434f22e6d148e41ba82aeb52add220484bf7f9551c74323d9cbc7", + "url": "https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-jackson/4.1.2/spring-boot-jackson-4.1.2.jar" + }, + "spring-boot-jarmode-tools-4.1.2.jar": { + "sha256": "a0d9e74c09c81f2c8e096bc856f327605a34df2209e5cf6884351170c6aa3547", + "url": "https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-jarmode-tools/4.1.2/spring-boot-jarmode-tools-4.1.2.jar" + }, + "spring-boot-jdbc-4.1.2.jar": { + "sha256": "2c4f460e7b965fd9a3d7ee6c3d7c5f3ebbc9b447841c118abed20d7b5a72d184", + "url": "https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-jdbc/4.1.2/spring-boot-jdbc-4.1.2.jar" + }, + "spring-boot-micrometer-metrics-4.1.2.jar": { + "sha256": "9091ffdfaeb604d4865478ce596277094a5564dc180145bf50cbbc7f9b9d1174", + "url": "https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-micrometer-metrics/4.1.2/spring-boot-micrometer-metrics-4.1.2.jar" + }, + "spring-boot-micrometer-observation-4.1.2.jar": { + "sha256": "a5e31e91c42e49fedfd95c2e15ec565e51c63741b65d471a49c029db62ac117f", + "url": "https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-micrometer-observation/4.1.2/spring-boot-micrometer-observation-4.1.2.jar" + }, + "spring-boot-persistence-4.1.2.jar": { + "sha256": "fd1473df17db5d4d444eac14ed262407a971fc0a3a9631e6753343d72e7859af", + "url": "https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-persistence/4.1.2/spring-boot-persistence-4.1.2.jar" + }, + "spring-boot-security-4.1.2.jar": { + "sha256": "12b5c8478a965677aaa98ced147d15db214cfeb373eb5acfd84c1e9c25dc8ac3", + "url": "https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-security/4.1.2/spring-boot-security-4.1.2.jar" + }, + "spring-boot-security-oauth2-authorization-server-4.1.2.jar": { + "sha256": "fe33fe0bee4f1575980fe38402f7a9cdafcdb2aaddcc3df2e4d1cab23be7f44b", + "url": "https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-security-oauth2-authorization-server/4.1.2/spring-boot-security-oauth2-authorization-server-4.1.2.jar" + }, + "spring-boot-security-oauth2-client-4.1.2.jar": { + "sha256": "e6cb44e293d024858896f09823ad008ef665856ff9e504594e73f7b42fa4acfa", + "url": "https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-security-oauth2-client/4.1.2/spring-boot-security-oauth2-client-4.1.2.jar" + }, + "spring-boot-security-oauth2-resource-server-4.1.2.jar": { + "sha256": "f9cd304e41b06917c144d2c8eaf20181be1a305d06c5873b01c5c7809544e983", + "url": "https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-security-oauth2-resource-server/4.1.2/spring-boot-security-oauth2-resource-server-4.1.2.jar" + }, + "spring-boot-servlet-4.1.2.jar": { + "sha256": "0ff3618e8b37143843d1a8824e42ffd66b4ff73d6225b2087acce4dfc676d0e8", + "url": "https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-servlet/4.1.2/spring-boot-servlet-4.1.2.jar" + }, + "spring-boot-session-4.1.2.jar": { + "sha256": "61144a364e6dc67084c64b9bd8c1e0074df93edb71e1d78357fca7e6a031eb5b", + "url": "https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-session/4.1.2/spring-boot-session-4.1.2.jar" + }, + "spring-boot-session-jdbc-4.1.2.jar": { + "sha256": "d1ece0b6d717dd61f571e315495532af41a989172c732bf8a049e59d3ecd1ad7", + "url": "https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-session-jdbc/4.1.2/spring-boot-session-jdbc-4.1.2.jar" + }, + "spring-boot-sql-4.1.2.jar": { + "sha256": "6055db3a7e6f9b59814aac0a346a604d54a8638beff8a145273415b263936d45", + "url": "https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-sql/4.1.2/spring-boot-sql-4.1.2.jar" + }, + "spring-boot-thymeleaf-4.1.2.jar": { + "sha256": "57dd518993616795f073da376b0a9f250c58da7ea8c534e672370c3520bb64aa", + "url": "https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-thymeleaf/4.1.2/spring-boot-thymeleaf-4.1.2.jar" + }, + "spring-boot-tomcat-4.1.2.jar": { + "sha256": "f0b1d8d33b090bc773298005708b4df1576d1c4b1bbf9583afce065580e13002", + "url": "https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-tomcat/4.1.2/spring-boot-tomcat-4.1.2.jar" + }, + "spring-boot-transaction-4.1.2.jar": { + "sha256": "f0430406b83310c65219082ff16a9af5ee74e2c1ee19f9f478f6a0ab8ec1f70f", + "url": "https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-transaction/4.1.2/spring-boot-transaction-4.1.2.jar" + }, + "spring-boot-validation-4.1.2.jar": { + "sha256": "a5f49131fd54481c26d5a93af4f94a11abd02f11608ea231f42424824bae7702", + "url": "https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-validation/4.1.2/spring-boot-validation-4.1.2.jar" + }, + "spring-boot-web-server-4.1.2.jar": { + "sha256": "e5b2fa7222baf36621566a2d0f7d924aa92a8462680f8e67d972f965ef18356a", + "url": "https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-web-server/4.1.2/spring-boot-web-server-4.1.2.jar" + }, + "spring-boot-webmvc-4.1.2.jar": { + "sha256": "3c39a0578d1e66b5b5371641790a66c89b14f8099731987a7dd6502e2fdd32b5", + "url": "https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-webmvc/4.1.2/spring-boot-webmvc-4.1.2.jar" + }, + "spring-context-7.0.10.jar": { + "sha256": "f0065166b6a5101a871ab8702d6c4011cf39faed370a64e7ef14a3ad2bf2a99a", + "url": "https://repo.maven.apache.org/maven2/org/springframework/spring-context/7.0.10/spring-context-7.0.10.jar" + }, + "spring-core-7.0.10.jar": { + "sha256": "a236f31a7c07e3a1a8e7839ec5c0d47b5cc75709132db50416d5dad320341f12", + "url": "https://repo.maven.apache.org/maven2/org/springframework/spring-core/7.0.10/spring-core-7.0.10.jar" + }, + "spring-expression-7.0.10.jar": { + "sha256": "12067b66548f7613fa8938ca6051a6d1dd8c397bf478e0bb1adb9936358ec81d", + "url": "https://repo.maven.apache.org/maven2/org/springframework/spring-expression/7.0.10/spring-expression-7.0.10.jar" + }, + "spring-jdbc-7.0.10.jar": { + "sha256": "bc4ba6bd242f2db2642938960ae27589b53dd6a611d700a260af7b3383cd0c6c", + "url": "https://repo.maven.apache.org/maven2/org/springframework/spring-jdbc/7.0.10/spring-jdbc-7.0.10.jar" + }, + "spring-security-config-7.1.2.jar": { + "sha256": "826af4e79495d54b761a5f0313774a07d36359e83dd5518d57d69bacd7bbf478", + "url": "https://repo.maven.apache.org/maven2/org/springframework/security/spring-security-config/7.1.2/spring-security-config-7.1.2.jar" + }, + "spring-security-core-7.1.2.jar": { + "sha256": "39df3dc77436d3e9308f89056c1a42605e08f0ad9ad208f7e34c4b16102886e1", + "url": "https://repo.maven.apache.org/maven2/org/springframework/security/spring-security-core/7.1.2/spring-security-core-7.1.2.jar" + }, + "spring-security-crypto-7.1.2.jar": { + "sha256": "4aaa60bb612d5fece004c07f893b3a8d707c207bbd8585d10e0015b5301cd864", + "url": "https://repo.maven.apache.org/maven2/org/springframework/security/spring-security-crypto/7.1.2/spring-security-crypto-7.1.2.jar" + }, + "spring-security-oauth2-authorization-server-7.1.2.jar": { + "sha256": "0ab43f6fbdc07dfb7f33a8bf6c6faa142515cbbd5af76ef9ff15d8358ca0f577", + "url": "https://repo.maven.apache.org/maven2/org/springframework/security/spring-security-oauth2-authorization-server/7.1.2/spring-security-oauth2-authorization-server-7.1.2.jar" + }, + "spring-security-oauth2-client-7.1.2.jar": { + "sha256": "a9177d2d68ceec14181643944787b2707f19a6ff09eae8f720dcfc7e15fc6282", + "url": "https://repo.maven.apache.org/maven2/org/springframework/security/spring-security-oauth2-client/7.1.2/spring-security-oauth2-client-7.1.2.jar" + }, + "spring-security-oauth2-core-7.1.2.jar": { + "sha256": "aa50c141cb303bae1b87d761f9459930d75af280c43e80b7488d50e62dd1e216", + "url": "https://repo.maven.apache.org/maven2/org/springframework/security/spring-security-oauth2-core/7.1.2/spring-security-oauth2-core-7.1.2.jar" + }, + "spring-security-oauth2-jose-7.1.2.jar": { + "sha256": "e956dcaf0342699b967b5ca301d628d0808e97c7e6614b822fe832f6eba762dc", + "url": "https://repo.maven.apache.org/maven2/org/springframework/security/spring-security-oauth2-jose/7.1.2/spring-security-oauth2-jose-7.1.2.jar" + }, + "spring-security-oauth2-resource-server-7.1.2.jar": { + "sha256": "a154ce053da747c0be4144794cf2bd835b6cd56e09d1b4b2a62dfdcda6d273fb", + "url": "https://repo.maven.apache.org/maven2/org/springframework/security/spring-security-oauth2-resource-server/7.1.2/spring-security-oauth2-resource-server-7.1.2.jar" + }, + "spring-security-web-7.1.2.jar": { + "sha256": "fc36d37695462dbe4fb32bd6820043afbf83ebcffce4974929bf79a29fcb5e73", + "url": "https://repo.maven.apache.org/maven2/org/springframework/security/spring-security-web/7.1.2/spring-security-web-7.1.2.jar" + }, + "spring-security-webauthn-7.1.2.jar": { + "sha256": "e4476c7babfbe477dc592f2c4a6f04164ba94982f8cbe2e1fcff319e33a14d3d", + "url": "https://repo.maven.apache.org/maven2/org/springframework/security/spring-security-webauthn/7.1.2/spring-security-webauthn-7.1.2.jar" + }, + "spring-session-core-4.1.1.jar": { + "sha256": "01b836bfcc5b64f7039c3430980878112a10db5066eda4274a1d3eb874e49733", + "url": "https://repo.maven.apache.org/maven2/org/springframework/session/spring-session-core/4.1.1/spring-session-core-4.1.1.jar" + }, + "spring-session-jdbc-4.1.1.jar": { + "sha256": "669e4388684949edb0c5aef0a7f4e15af86d6c87ffb90df3145f073746870529", + "url": "https://repo.maven.apache.org/maven2/org/springframework/session/spring-session-jdbc/4.1.1/spring-session-jdbc-4.1.1.jar" + }, + "spring-tx-7.0.10.jar": { + "sha256": "9442610a8ed3d44e70fa97f49896ea810c831da62620101450f0feb522edb4cd", + "url": "https://repo.maven.apache.org/maven2/org/springframework/spring-tx/7.0.10/spring-tx-7.0.10.jar" + }, + "spring-web-7.0.10.jar": { + "sha256": "76ddb03d236547f6852def8cb303e1cf2cd2c1954dec5bf2abd87d11b89dfee7", + "url": "https://repo.maven.apache.org/maven2/org/springframework/spring-web/7.0.10/spring-web-7.0.10.jar" + }, + "spring-webmvc-7.0.10.jar": { + "sha256": "8ad0e813999d0ef1c65b8d5e55c21e4a6c0d6895cd3df32a33f9cc57bf9d226a", + "url": "https://repo.maven.apache.org/maven2/org/springframework/spring-webmvc/7.0.10/spring-webmvc-7.0.10.jar" + }, + "threeten-extra-1.8.0.jar": { + "sha256": "51e4d21edc4e9447f7760c050e0baee75d7d973f387ba605a17abdc4d24fd6d8", + "url": "https://repo.maven.apache.org/maven2/org/threeten/threeten-extra/1.8.0/threeten-extra-1.8.0.jar" + }, + "thymeleaf-3.1.5.RELEASE.jar": { + "sha256": "4011795f8494dd69e764b7709443dd13d3068ba8ac37624f61d7084f4429cbe2", + "url": "https://repo.maven.apache.org/maven2/org/thymeleaf/thymeleaf/3.1.5.RELEASE/thymeleaf-3.1.5.RELEASE.jar" + }, + "thymeleaf-spring6-3.1.5.RELEASE.jar": { + "sha256": "fd5d306052d7aa6769a8ec77778d328e6f7c83af5ac074df38035bbb1e9cd72b", + "url": "https://repo.maven.apache.org/maven2/org/thymeleaf/thymeleaf-spring6/3.1.5.RELEASE/thymeleaf-spring6-3.1.5.RELEASE.jar" + }, + "tomcat-embed-core-11.0.27.jar": { + "sha256": "b71785ff101c0df501064b3e3b8c4a02e4f9cc3047a7fa686c41fc4edc2f0cb4", + "url": "https://repo.maven.apache.org/maven2/org/apache/tomcat/embed/tomcat-embed-core/11.0.27/tomcat-embed-core-11.0.27.jar" + }, + "tomcat-embed-el-11.0.27.jar": { + "sha256": "206d6c6b1b6ce67b1c9af9ed0f6f43b4dbbc26164dd47bb49b9aad1583191f33", + "url": "https://repo.maven.apache.org/maven2/org/apache/tomcat/embed/tomcat-embed-el/11.0.27/tomcat-embed-el-11.0.27.jar" + }, + "tomcat-embed-websocket-11.0.27.jar": { + "sha256": "cbcd7ed6d7428fa71af6572d541a3ef74ceff42dfc523273950fe586062dc60f", + "url": "https://repo.maven.apache.org/maven2/org/apache/tomcat/embed/tomcat-embed-websocket/11.0.27/tomcat-embed-websocket-11.0.27.jar" + }, + "unbescape-1.1.6.RELEASE.jar": { + "sha256": "597cf87d5b1a4f385b9d1cec974b7b483abb3ee85fc5b3f8b62af8e4bec95c2c", + "url": "https://repo.maven.apache.org/maven2/org/unbescape/unbescape/1.1.6.RELEASE/unbescape-1.1.6.RELEASE.jar" + }, + "webauthn4j-core-0.31.10.RELEASE.jar": { + "sha256": "cc33945c4dd014554a383e0e32da3a659a12658c8077b44ddcdbc466e2464652", + "url": "https://repo.maven.apache.org/maven2/com/webauthn4j/webauthn4j-core/0.31.10.RELEASE/webauthn4j-core-0.31.10.RELEASE.jar" + } + }, + "executableLoader": { + "classes": { + "org/springframework/boot/loader/jar/JarEntriesStream$InputStreamSupplier.class": "5a1ba86e7ff23cebd1a0720caec116968939c190ae522b5aae48a7200f49728f", + "org/springframework/boot/loader/jar/JarEntriesStream.class": "bc93bb86ad6067aa88ed1fbfb4d7201961da02d112a73b505ee05e079ea7c26b", + "org/springframework/boot/loader/jar/ManifestInfo.class": "8e3ddf3198089cf9b1e35026818bb1fc1e5cb31e2dd0bb95cc43a239173fe085", + "org/springframework/boot/loader/jar/MetaInfVersionsInfo.class": "62dfc9a702f92ba6fff8c625cc0cf2eb50a0bfcf4a4bca755a5c96b24c73f507", + "org/springframework/boot/loader/jar/NestedJarFile$JarEntriesEnumeration.class": "e8efe4a63e719e7d0d9b797f066f50a971df9a66c968e844287ce0444613b255", + "org/springframework/boot/loader/jar/NestedJarFile$JarEntryInflaterInputStream.class": "b903e9205491cbce1a5e8c6980deb5d4650b808b2f22822e4c98b7f1105ee0da", + "org/springframework/boot/loader/jar/NestedJarFile$JarEntryInputStream.class": "b256609be64d419b3437793b6dc9d4555dd8b53a51403678997da0860ce8237b", + "org/springframework/boot/loader/jar/NestedJarFile$NestedJarEntry.class": "836a3cd42a6e6b3d2ddc4d930e4e87735937206e61fa01f48fcc5081a2aa31c9", + "org/springframework/boot/loader/jar/NestedJarFile$RawZipDataInputStream.class": "63f1772d7dbe3b5c9ae1d2a1de53f3efc1315427e115c3da64b298012b090aea", + "org/springframework/boot/loader/jar/NestedJarFile$ZipContentEntriesSpliterator.class": "6023872e8553ec900ab4fed5121f29eb17bd58d7e79525ca12db32a92c1cd6e1", + "org/springframework/boot/loader/jar/NestedJarFile.class": "5482f3ab0d3604bae45a33d89f5a379e2731b8570b3fb69afcf3a9a0f1ae8cb5", + "org/springframework/boot/loader/jar/NestedJarFileResources.class": "fbb86c89d815278c2836df21c99062155d2053572aad04f0302b826fbf735cbe", + "org/springframework/boot/loader/jar/SecurityInfo.class": "4dfb1e62894c7df0ac52829e85d3192f56621b7c5425b946aa6c8928ef5640dd", + "org/springframework/boot/loader/jar/ZipInflaterInputStream.class": "88bff081d28852fc886cdc2d511ddb1dd476150ef70d2af063cfd9e8d392da82", + "org/springframework/boot/loader/jarmode/JarMode.class": "e5868f37e4aa644919a0db41b432617010a6758fdd9bb14e933ad90e2dab7017", + "org/springframework/boot/loader/jarmode/JarModeErrorException.class": "86fa48ec9d0439412a8772033d60a09be272ae7f53d28d710ddb4bf2935a70fa", + "org/springframework/boot/loader/launch/Archive$Entry.class": "68232ceede5e91150c06a4e4fd92ab1e0fc8f065ee174cdaa8597c784fe7d67f", + "org/springframework/boot/loader/launch/Archive.class": "06bcf5c97694acd9ea9151307b72a88f5651ee018d0166ca3f50b13984f9af87", + "org/springframework/boot/loader/launch/ClassPathIndexFile.class": "14fd446bc4586724470ce0fe7a70c73337766b204d396ff7445ee2462649751a", + "org/springframework/boot/loader/launch/ExecutableArchiveLauncher.class": "d1e0ed4dbab5b399c6b1ce6ea0010f646192eb63ae841ac0af800236014f5c85", + "org/springframework/boot/loader/launch/ExplodedArchive$FileArchiveEntry.class": "2e8063baff70dbf2c2c3a341f98b1736325d5907885b7a60e94bc5119747d092", + "org/springframework/boot/loader/launch/ExplodedArchive.class": "db7830b0522f46928257696b48d89a4aa59bcda1b2a1870941fb299ea03c75fc", + "org/springframework/boot/loader/launch/JarFileArchive$JarArchiveEntry.class": "f08cc1208869492429a7ee2652a04d2bd2a93805580916e8428e0fbf1d1e481d", + "org/springframework/boot/loader/launch/JarFileArchive.class": "910b3abea64dd3cf9b54e59d9095fe86bdd4b8b9e4c0c2abe860ac2126b5b5c4", + "org/springframework/boot/loader/launch/JarLauncher.class": "e79ca45bc98497169652a67d62da64925b6d9f77338c724ae76ecc2e22ec77c1", + "org/springframework/boot/loader/launch/JarModeRunner.class": "10c8b1549326a7e570df383e6a8f02efa0ee15ed6b469f7ff93f3e4420addcf6", + "org/springframework/boot/loader/launch/LaunchedClassLoader$DefinePackageCallType.class": "dc9e9903cec9a88a45268abc203118068851bd2ce5f96dce8b389758f467c570", + "org/springframework/boot/loader/launch/LaunchedClassLoader.class": "330503a1bf61b61a75a2bbb11f60af4c54c6f8dfcff3c434c0ed604a447264f5", + "org/springframework/boot/loader/launch/Launcher.class": "9fdb4c159af523887bb4d35b16e6ffe8c7d2bdd55db57acc21ee9bdf9ea8422d", + "org/springframework/boot/loader/launch/PropertiesLauncher$Instantiator$Using.class": "e442a1a4376fbf0716d095e21156f765be679827b068073768a766297bf4ef9b", + "org/springframework/boot/loader/launch/PropertiesLauncher$Instantiator.class": "20bcb6c70629c60984c5340867fbf28f3c6a82ce1c647e1354f62dc9e7287774", + "org/springframework/boot/loader/launch/PropertiesLauncher.class": "7a35c728eca91fb92dd41af25f0849139dbf5bcf399de8d151172a5c5996a608", + "org/springframework/boot/loader/launch/SystemPropertyUtils.class": "9128e0b1ab0cf7cc8c38132cbc7d643e96fba1a889a7c10ff6d7a110209462c4", + "org/springframework/boot/loader/launch/WarLauncher.class": "35192183ec889356162ee5432f42349446bb904b93d244b5db36a83fe018193e", + "org/springframework/boot/loader/log/DebugLogger$DisabledDebugLogger.class": "70c2f5304651e4d64573f9b7c65d67c769c303811511eb052c12515498f6116e", + "org/springframework/boot/loader/log/DebugLogger$SystemErrDebugLogger.class": "0a3bc94f216301865f7478cf10ba848bb0863c5df16ef198987fc500f761b0e1", + "org/springframework/boot/loader/log/DebugLogger.class": "4bc6ecd29e34cc111075d4c982677360849743e765b94b5b4048b753acb73935", + "org/springframework/boot/loader/net/protocol/Handlers.class": "ca8f6e3892515d7f84358b989157d1b09cfd316156140c18b6710c9c7b89e41e", + "org/springframework/boot/loader/net/protocol/jar/Canonicalizer.class": "bbe9eac08a0c08f35b4da9d2d23597a5f7f99edbdcff979ce31de51773728014", + "org/springframework/boot/loader/net/protocol/jar/Handler.class": "8c5a2a2e1c85f368e606c4c263c3f999eead3018ef02916fa595eaaf66e39f41", + "org/springframework/boot/loader/net/protocol/jar/JarFileUrlKey.class": "68195e15e46adf2b930a8128ff6f7789731905a386c9994f4828894bb07254c7", + "org/springframework/boot/loader/net/protocol/jar/JarUrl.class": "bea9760f0200cdcd00a88318a9956cf8b978b67b615eef7b0777d89bf644b43d", + "org/springframework/boot/loader/net/protocol/jar/JarUrlClassLoader$OptimizedEnumeration.class": "38511dfc5549b47b375b9b167fd1f66584ecff002e012a492e9361889b350af0", + "org/springframework/boot/loader/net/protocol/jar/JarUrlClassLoader.class": "1b7a4eed352242b34ff1af8d036ca9f53528b93cecbfbf9a69b657ee04e72102", + "org/springframework/boot/loader/net/protocol/jar/JarUrlConnection$ConnectionInputStream.class": "53ed2535662913ff97cfaf101af89aa83fad3d38f1bba7426a3898eb6669d8b4", + "org/springframework/boot/loader/net/protocol/jar/JarUrlConnection$EmptyUrlStreamHandler.class": "04b6f12809f7b4cbe08a8d488a8078f992c8ce40022f8693e484ee6b36874d25", + "org/springframework/boot/loader/net/protocol/jar/JarUrlConnection.class": "7c35739897297b61a2d2d6ef58829a05af6e3af99833433e7ea9c8761de22f57", + "org/springframework/boot/loader/net/protocol/jar/LazyDelegatingInputStream.class": "9eacbfadac12e5030ebb65e146f35586950d1b77f34844ccb907eb8d5ac2d038", + "org/springframework/boot/loader/net/protocol/jar/Optimizations.class": "ef9ac2ce0d5f4fcd5bf5c77d81201c6888333675012dd07ef8514c2c6de5629f", + "org/springframework/boot/loader/net/protocol/jar/UrlJarEntry.class": "27d7950330b6faab02e433ac3454526566dac2313b112da2eb02de819ae0ef20", + "org/springframework/boot/loader/net/protocol/jar/UrlJarFile.class": "de6468e5b8f190ca6018770e428790d85f23a49e069d12e4a5030f7006d01653", + "org/springframework/boot/loader/net/protocol/jar/UrlJarFileFactory.class": "3ca7f07aec2c27243ef0df65b1c019c5142b4627313d8f7ba0f1a883204abde5", + "org/springframework/boot/loader/net/protocol/jar/UrlJarFiles$Cache.class": "9e2762834be07af1fbec5c9f4db69ea9ad18bdc71ac0f4bc25ac640dc2dcca34", + "org/springframework/boot/loader/net/protocol/jar/UrlJarFiles.class": "a0ba895de5d98cee996100f4c69dbd18b894b767db1553ba4750a31e82adf845", + "org/springframework/boot/loader/net/protocol/jar/UrlJarManifest$ManifestSupplier.class": "4407d5779bc4fdd6584408a41ce4448fe0d6a5ac48c4bcd589376d2bc1c6a926", + "org/springframework/boot/loader/net/protocol/jar/UrlJarManifest.class": "78c45c58de12daceddd6dde8c231cbac81b6cbbd0c11ad51ca538f0e65eba438", + "org/springframework/boot/loader/net/protocol/jar/UrlNestedJarFile.class": "d8f68086fefb3dee5c7df5f10f136ec3701f5cd007a87eb7becd1b09135f45db", + "org/springframework/boot/loader/net/protocol/nested/Handler.class": "cdb16f49a9dd57dcf4d4c91c4aa00753484ef6ed8a481a3417dd42be1e288c00", + "org/springframework/boot/loader/net/protocol/nested/NestedLocation.class": "3d9d9f34cd226dc324ab0e5efb7c3d4ab324e3b53327cd25fdb169d977c92856", + "org/springframework/boot/loader/net/protocol/nested/NestedUrlConnection$ConnectionInputStream.class": "699b4caa31c00052c80d9e61706a4543b1ac2523dda7d1d3efa6bcd646af7f95", + "org/springframework/boot/loader/net/protocol/nested/NestedUrlConnection.class": "076d6c06ca935cfdad6a85d938cb2152f429f436fa7041d2b5812d87d9870b69", + "org/springframework/boot/loader/net/protocol/nested/NestedUrlConnectionResources.class": "b8e452c52a58bc36974ea0a818838e7cc07f5e5ec0c9c3c13400fee133eb8cf7", + "org/springframework/boot/loader/net/util/UrlDecoder.class": "278bd1f1f5a671fa73f19583065fbff77033b2eccf6833d6c07e12bc5a059bee", + "org/springframework/boot/loader/nio/file/NestedByteChannel$Resources.class": "b97039607d1651d913811b7ab40bb147d8213ab64b0dcec5e480d5518613943e", + "org/springframework/boot/loader/nio/file/NestedByteChannel.class": "b5257c1ccc9267c1da0f37367c9e0471049aea0ae7165a706f1fd13b4c50539a", + "org/springframework/boot/loader/nio/file/NestedFileStore.class": "a9e997a6e21ac2d969296da5645cace2731d9e7a044a98674eca5a01454fc900", + "org/springframework/boot/loader/nio/file/NestedFileSystem.class": "7089349eaedfdd671a7b993a2190d6450a4d4023aaf1e8cfdfe424aaa3c534fd", + "org/springframework/boot/loader/nio/file/NestedFileSystemProvider.class": "d66f49a5d290f23c8d4c2d30e5124673e3d5dff7eb25de55a2e58ab2b84afbe5", + "org/springframework/boot/loader/nio/file/NestedPath.class": "c94bafce8a6c71e5303df35144c222a6d552582a1d16defb2b97624268bbc230", + "org/springframework/boot/loader/nio/file/UriPathEncoder.class": "91b0cf7b974efa4dfeb332d493c094e282c719b8b1390a0ebb4257bfecea9cda", + "org/springframework/boot/loader/ref/Cleaner.class": "b857f3eeba9c571fb538afc71c06f9413eb25646d7a06dc9b5cc1e298270710d", + "org/springframework/boot/loader/ref/DefaultCleaner.class": "00fa6c021f762bc2e2365cf860113f7d8cf136ba9798ad1a0d99c92d77df7a14", + "org/springframework/boot/loader/zip/ByteArrayDataBlock.class": "6aac332a092ad98c7d26f97c2988d99ba4eb8c646334928c4a50e30606138260", + "org/springframework/boot/loader/zip/CloseableDataBlock.class": "5892c653af80fe2834adee1908955ec3705db2925780e0af23a6363cc5627871", + "org/springframework/boot/loader/zip/DataBlock.class": "b80c80037a6a22fd87f4de0e4f10f01d0530fcd2229f80bcdb74cba22ada509e", + "org/springframework/boot/loader/zip/DataBlockInputStream.class": "bc332c4a646f071fa0dcb9cf9dd5af77c0ac7bbe9d93d8c5348ee0f9d3fcc1ca", + "org/springframework/boot/loader/zip/FileDataBlock$FileAccess.class": "ee12600d3c965516fdd7b782a72048da6b829174dc5d9e5b843eecae14805f56", + "org/springframework/boot/loader/zip/FileDataBlock$Tracker$1.class": "ecb7a7d2e1922ed97defd4b08c631e162803034abc785c45931c760401dbba9a", + "org/springframework/boot/loader/zip/FileDataBlock$Tracker.class": "c69558dcf4f9dbf49ae635017f576d12f1a1e320c04519cdc1e55176a1df5c53", + "org/springframework/boot/loader/zip/FileDataBlock.class": "fa187ee2c40e703a8537f70ec6ab62581cb890b02809d1c72c35ec13a8601755", + "org/springframework/boot/loader/zip/NameOffsetLookups.class": "12c0b6821ee9823b440709ba5db3fe20130ad8b6c799c3ba5451d403bd310f94", + "org/springframework/boot/loader/zip/VirtualDataBlock.class": "d495b5a2205644b2096deaf8d130de472268da91f43cc8a517615075120cf830", + "org/springframework/boot/loader/zip/VirtualZipDataBlock$DataPart.class": "d9a0c9f082e171b621a50b1820831fb45dee20063b3c3a57de99edb3a5e61452", + "org/springframework/boot/loader/zip/VirtualZipDataBlock.class": "79dd8baa20b87ab8b4416bab425f1000477fd889644af7f6ec99bccc0f855e81", + "org/springframework/boot/loader/zip/Zip64EndOfCentralDirectoryLocator.class": "8f4e58afcf93de817c0e2933b0b22aa6ef41026935a2c14af09e1efc9f5c581c", + "org/springframework/boot/loader/zip/Zip64EndOfCentralDirectoryRecord.class": "239ba022579323561ad191b402a9ddbd53df429cfda5cbfb0e7d9b58ceee9308", + "org/springframework/boot/loader/zip/ZipCentralDirectoryFileHeaderRecord.class": "e08ea06d420f2453f43e340cbd8127a0f7f7a730ed28410087a075491b06a27f", + "org/springframework/boot/loader/zip/ZipContent$Entry.class": "97d6270654d5fd313ab988af9f0ff0fe6d55e9af59d4bc1031621f9734a4b904", + "org/springframework/boot/loader/zip/ZipContent$Kind.class": "c76fdba8d246f7c8648b53805e8eb5f7597945838b882ce7b6f3cebd95dc8d88", + "org/springframework/boot/loader/zip/ZipContent$Loader.class": "7ffe9c40c62198bfeee3eb2c7935d7779690c4612dbc1faf703f9bbff442e395", + "org/springframework/boot/loader/zip/ZipContent$SignatureFiles.class": "e9334128e289439870afd9741efa67c784a4b7d636dc1bceae92bc0623b2b830", + "org/springframework/boot/loader/zip/ZipContent$Source.class": "653ea2cbb1b0d24cda8d07f033cd0f1dda5b597aa9cd08abe3b5ba4c103f0d1b", + "org/springframework/boot/loader/zip/ZipContent.class": "bfd46a132495649c4d4152dd744a6b2179f4b7718f42c47d9c80abd3578502b0", + "org/springframework/boot/loader/zip/ZipDataDescriptorRecord.class": "d4b425053af4048aa02a833fec876ba1d0e842abf03b14d7b558458046e69b83", + "org/springframework/boot/loader/zip/ZipEndOfCentralDirectoryRecord$Located.class": "4426f23c4ba038d4a7ba27e97a8508f056e91554211cb19c156301ba0fe0085b", + "org/springframework/boot/loader/zip/ZipEndOfCentralDirectoryRecord.class": "fca2209cc217cb5aa85b1a8fb06538ba785e22216a2a050238a89c621e1883f7", + "org/springframework/boot/loader/zip/ZipLocalFileHeaderRecord.class": "c71b035b2b2dbfa1481e6ebbef0e7bfb36299c34db92a2d0bee6c8142d412a9b", + "org/springframework/boot/loader/zip/ZipString$CompareType.class": "1482a20f857658fe071637c7520a5c1ac7905732810922b35bf78d4660b58666", + "org/springframework/boot/loader/zip/ZipString.class": "204f5541e9b6327ab2c7a348440c873f6b99a896c1728b6b1fc2b63225172f19" + }, + "sha256": "fa8195ba5c3e0f28e3db48492ad95881dd3204c5178098c9a006253c1b6766e9", + "url": "https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-loader/4.1.2/spring-boot-loader-4.1.2.jar" + }, + "provenance": "SHA-256 of complete unmodified JAR bytes retrieved directly from Maven Central over HTTPS; no local cache used to establish identities." +} diff --git a/qualification/distribution/test_distribution.py b/qualification/distribution/test_distribution.py new file mode 100644 index 0000000..d888adc --- /dev/null +++ b/qualification/distribution/test_distribution.py @@ -0,0 +1,28 @@ +"""Distribution regressions; actual archive and image checks run after packaging.""" +import importlib.util +from pathlib import Path +import unittest +import xml.etree.ElementTree as ET + +ROOT = Path(__file__).resolve().parents[2] + +class DistributionMetadataTest(unittest.TestCase): + def test_project_has_official_unmodified_apache_license(self): + import hashlib + self.assertEqual(hashlib.sha256((ROOT / 'LICENSE').read_bytes()).hexdigest(), 'cfc7749b96f63bd31c3c42b5c471bf756814053e847c10f3eb003417bc523d30') + + def test_project_pom_declares_license_separately_from_dependencies(self): + doc = ET.parse(ROOT / 'pom.xml') + ns = {'m':'http://maven.apache.org/POM/4.0.0'} + license = doc.find('m:licenses/m:license',ns) + self.assertEqual(license.find('m:name',ns).text,'Apache License, Version 2.0') + self.assertEqual(license.find('m:url',ns).text,'https://www.apache.org/licenses/LICENSE-2.0') + + def test_compose_and_executable_default_match_without_hidden_fixture_override(self): + self.assertIn('${BUSYSTACK_DAV_ALLOW_SYNC_DEPTH_ONE:-true}',(ROOT/'compose.yaml').read_text()) + self.assertIn('allow-sync-depth-one: true',(ROOT/'src/main/resources/application.yaml').read_text()) + self.assertNotIn('allow-sync-depth-one=true',(ROOT/'src/test/java/org/busystack/caldav/protocol/IndependentClientIT.java').read_text()) + self.assertNotIn('"allowSyncDepthOne": True',(ROOT/'qualification/clients/python_caldav.py').read_text()) + +if __name__ == '__main__': + unittest.main() diff --git a/qualification/distribution/test_jar_licenses.py b/qualification/distribution/test_jar_licenses.py new file mode 100644 index 0000000..e1f71f0 --- /dev/null +++ b/qualification/distribution/test_jar_licenses.py @@ -0,0 +1,71 @@ +"""Inspect real ZIP structures; CycloneDX emits SPDX IDs without requiring redundant URLs.""" +import importlib.util +import io +import json +from pathlib import Path +import tempfile +import unittest +import zipfile + +ROOT=Path(__file__).resolve().parents[2] +spec=importlib.util.spec_from_file_location('distribution_inspection',ROOT/'scripts/verify-distribution.py') +inspection=importlib.util.module_from_spec(spec);spec.loader.exec_module(inspection) + + +class JarLicenseContractTest(unittest.TestCase): + def setUp(self): + self.directory=tempfile.TemporaryDirectory(prefix='busystack-license-jar-test-') + self.addCleanup(self.directory.cleanup) + + def jar(self, project_license, dependency_license='Broadcom Foundation License', project_version=inspection.versions.resolve(ROOT), manifest_version=inspection.versions.resolve(ROOT), maven_version=inspection.versions.resolve(ROOT)): + path=Path(self.directory.name)/'application.jar' + session=[{'group':'org.springframework.session','name':module,'version':'4.1.1','licenses':[{'license':{'name':dependency_license}}]} for module in ('spring-session-core','spring-session-jdbc')] + bom={'metadata':{'component':{'version':project_version,'licenses':[{'license':project_license}]}},'components':session} + with zipfile.ZipFile(path,'w') as jar: + jar.writestr('META-INF/MANIFEST.MF','Manifest-Version: 1.0\r\nImplementation-Version: '+manifest_version+'\r\n\r\n') + jar.writestr('META-INF/maven/org.busystack/caldav-server/pom.properties','artifactId=caldav-server\ngroupId=org.busystack\nversion='+maven_version+'\n') + for name in inspection.DOCUMENTS:jar.writestr('META-INF/'+name,(ROOT/name).read_bytes()) + for name in ('ical4j-parser-BSD-3-Clause.txt','spring-derived-schemas.txt'): + jar.writestr('META-INF/licenses/'+name,(ROOT/'src/main/resources/META-INF/licenses'/name).read_bytes()) + for name in ('asm-9.7.1-copyright-and-license.txt','logback-LICENSE.txt','EPL-2.0.txt','LGPL-2.1.txt'): + jar.writestr('META-INF/licenses/'+name,(ROOT/'licenses'/name).read_bytes()) + jar.writestr('BOOT-INF/classes/application.yaml',(ROOT/'src/main/resources/application.yaml').read_bytes()) + jar.writestr('META-INF/sbom/application.cdx.json',json.dumps(bom)) + for component in session: + nested=io.BytesIO() + with zipfile.ZipFile(nested,'w') as dependency:dependency.writestr('META-INF/LICENSE.txt',(ROOT/'LICENSE').read_bytes()) + jar.writestr('BOOT-INF/lib/'+component['name']+'-4.1.1.jar',nested.getvalue()) + return path + + def test_canonical_spdx_only_project_license_is_accepted(self): + result=inspection.inspect_jar(self.jar({'id':'Apache-2.0'})) + self.assertEqual(result['runtimeComponents'],2) + self.assertEqual(len(result['nestedArtifacts']),2) + + def test_official_optional_url_is_accepted(self): + result=inspection.inspect_jar(self.jar({'id':'Apache-2.0','url':'https://www.apache.org/licenses/LICENSE-2.0'})) + self.assertEqual(result['projectLicense'][0]['license']['id'],'Apache-2.0') + + def test_wrong_project_noassertion_is_rejected(self): + with self.assertRaisesRegex(AssertionError,'Project SBOM license'): + inspection.inspect_jar(self.jar({'name':'NOASSERTION'})) + + def test_optional_project_license_url_must_be_official(self): + with self.assertRaisesRegex(AssertionError,'Project SBOM license'): + inspection.inspect_jar(self.jar({'id':'Apache-2.0','url':'https://invalid.example/license'})) + + def test_original_dependency_metadata_cannot_be_overwritten_by_project_license(self): + with self.assertRaisesRegex(AssertionError,'Original upstream POM'): + inspection.inspect_jar(self.jar({'id':'Apache-2.0'},'Apache-2.0')) + + def test_snapshot_manifest_is_rejected(self): + with self.assertRaisesRegex(AssertionError,'JAR implementation version'): + inspection.inspect_jar(self.jar({'id':'Apache-2.0'},manifest_version=inspection.versions.resolve(ROOT) + '-SNAPSHOT')) + + def test_project_sbom_version_must_match_release(self): + with self.assertRaisesRegex(AssertionError,'Project SBOM version'): + inspection.inspect_jar(self.jar({'id':'Apache-2.0'},project_version=inspection.versions.resolve(ROOT) + '-SNAPSHOT')) + + def test_maven_version_must_match_release(self): + with self.assertRaisesRegex(AssertionError,'JAR Maven version'): + inspection.inspect_jar(self.jar({'id':'Apache-2.0'},maven_version=inspection.versions.resolve(ROOT) + '-SNAPSHOT')) diff --git a/qualification/evidence/README.md b/qualification/evidence/README.md new file mode 100644 index 0000000..bd17001 --- /dev/null +++ b/qualification/evidence/README.md @@ -0,0 +1,24 @@ +# Qualification records + +Maintained tests and protocol fixtures live in `src/test`, `qualification/` and +`investigation/`. Generated logs, screenshots and intermediate execution records +belong in CI artifacts or the external project audit archive. + +- [v0.1.0 qualification](releases/0.1.0/README.md) records the qualified executable, + runtime inputs, real-client scope, results and successful CI references. +- [v0.0.1 summary](releases/0.0.1/summary.json) preserves historical identity and + scope; it is not evidence for a different executable. +- Each published release's `release-manifest.json` identifies its exact source, + successful verification run/attempt, JAR checksum, registry manifest digest and + deployment archive checksum. Local Docker IDs are not registry digests. + +## Historical records + +The detailed pre-cleanup records remain unchanged in +[Git history](https://github.com/busystack/caldav-server/tree/f3ba60d360117c44b3dfeb6dc9848778a98ef06a/qualification/evidence) +and the external audit archive `v0.1.0-public-cleanup-20261102/source-before-cleanup.tar.gz`. +Its source commit is `f3ba60d360117c44b3dfeb6dc9848778a98ef06a` and archive SHA-256 is +`d8c1ee2ddd06cdc01f882366518a023b0ec02224b329c0f937cd8549e7365783`. +The archive retains unsuccessful and interrupted attempts alongside later +successful results; removal from the current source tree changes none of them. +No credentials or private client profiles are needed to build or install a release. diff --git a/qualification/evidence/releases/0.0.1/summary.json b/qualification/evidence/releases/0.0.1/summary.json new file mode 100644 index 0000000..99d8855 --- /dev/null +++ b/qualification/evidence/releases/0.0.1/summary.json @@ -0,0 +1,51 @@ +{ + "version": "0.0.1", + "scope": "Historical qualification summary; not qualification for another executable.", + "publishedSourceCommit": "40b68299e8ad9fb6b77ce2845a7eb8c7122be5c9", + "clientSourceCommit": "9635587b7fa1e54a144355dc1579c0e4c04e1b8f", + "status": "passed", + "jarSha256": "98d9febbd849e7cd335d2f277026063765ce260264f08b65e04278936023002a", + "runtimeInputs": { + "Dockerfile": "23f03a1f531a1b52e32963465ebd131d8a6969f45951294bb93a3c8e54e22394", + "compose.yaml": "44a2b67f6a77b5eb533a3aacfdca6f6184e6d350849bacadf5316f7c1767b3bc" + }, + "verificationRun": { + "id": 41262396896, + "scope": "Application/investigation/static tests, current advisory gates, distribution integrity, six packaged HTTPS profiles and local/federated restoration", + "sourceCommit": "9635587b7fa1e54a144355dc1579c0e4c04e1b8f", + "status": "passed", + "url": "https://github.com/busystack/caldav-server/actions/runs/41262396896" + }, + "clients": { + "gnome-vancouver": { + "EDS": "3.56.2-8", + "client": "GNOME Calendar", + "status": "passed", + "timezone": "America/Vancouver", + "version": "50.0-1" + }, + "thunderbird-explicit-berlin": { + "client": "Thunderbird", + "status": "passed", + "timezone": "Europe/Berlin", + "version": "159.0" + }, + "thunderbird-explicit-vancouver": { + "client": "Thunderbird", + "status": "passed", + "timezone": "America/Vancouver", + "version": "159.0" + }, + "thunderbird-system-vancouver": { + "client": "Thunderbird", + "status": "passed", + "timezone": "America/Vancouver", + "version": "159.0" + } + }, + "originalRecord": "https://github.com/busystack/caldav-server/blob/f3ba60d360117c44b3dfeb6dc9848778a98ef06a/qualification/evidence/releases/0.0.1/official-dependencies/verification.json", + "limitations": [ + "Historical successes and failed attempts retain their original scope in the archived records.", + "This summary does not extend client or security claims beyond the recorded executable and configurations." + ] +} diff --git a/qualification/evidence/releases/0.1.0/README.md b/qualification/evidence/releases/0.1.0/README.md new file mode 100644 index 0000000..2af4551 --- /dev/null +++ b/qualification/evidence/releases/0.1.0/README.md @@ -0,0 +1,17 @@ +# v0.1.0 qualification + +[verification.json](verification.json) binds the passed native-editor profiles to +the actual Maven-versioned executable checksum and the Dockerfile/Compose input +hashes. It records Thunderbird 159.0 with system Vancouver, explicit Vancouver +and explicit Berlin, and GNOME Calendar 50.0 / EDS 3.56.2 with Vancouver. + +The profiles exercised creation, synchronization, time editing and reopening, +bounded recurrence, restart persistence and deletion; Thunderbird also exercised +task completion and edited occurrences. These are automated real-client checks, +not manual-human or physical-authenticator certification. + +The receipt links the original results and successful implementation CI runs. +Subsequent documentation-only source revisions retain this qualification only +when the JAR checksum and runtime-input hashes match. Official release source, +CI attempt, image digest and asset checksums are recorded in the release manifest. +Detailed diagnostics are archived through the [evidence index](../../README.md). diff --git a/qualification/evidence/releases/0.1.0/verification.json b/qualification/evidence/releases/0.1.0/verification.json new file mode 100644 index 0000000..c31b2cb --- /dev/null +++ b/qualification/evidence/releases/0.1.0/verification.json @@ -0,0 +1,89 @@ +{ + "version": "0.1.0", + "status": "passed", + "recordedAt": "2026-11-02T02:42:39.676240+00:00", + "qualifiedSourceCommit": "5d09ecb2e241a97f429f71aecfbfd9fcd166b861", + "verificationRun": 41357992646, + "verificationAttempt": 1, + "jarSha256": "bff028ac0072134458710e22e63ce433f96d4fd2619b217049d619c998151792", + "runtimeInputs": { + "Dockerfile": "6e1125b89524a0b873ad5d81b7defa5fc62ccdb7aa64ed9a4802841817286c13", + "compose.yaml": "17bdfbf0b5fb5ef830042311d89ce8335ac806c15275515befaf05ee7fe9bb49" + }, + "clients": { + "thunderbird-system-vancouver": { + "status": "passed", + "client": "Thunderbird", + "version": "159.0", + "timezone": "America/Vancouver", + "systemTimezone": true, + "checks": [ + "native creation and server read", + "conditional time editing and reopen", + "bounded daily recurrence and edited occurrence", + "client restart with three correct occurrences", + "task completion and deletion", + "whole-series deletion" + ], + "evidence": "https://github.com/busystack/caldav-server/blob/f3ba60d360117c44b3dfeb6dc9848778a98ef06a/qualification/evidence/releases/0.1.0/clients/thunderbird-system-vancouver/thunderbird-gui-results.json" + }, + "thunderbird-explicit-vancouver": { + "status": "passed", + "client": "Thunderbird", + "version": "159.0", + "timezone": "America/Vancouver", + "systemTimezone": false, + "checks": [ + "native creation and server read", + "conditional time editing and reopen", + "bounded daily recurrence and edited occurrence", + "client restart with three correct occurrences", + "task completion and deletion", + "whole-series deletion" + ], + "evidence": "https://github.com/busystack/caldav-server/blob/f3ba60d360117c44b3dfeb6dc9848778a98ef06a/qualification/evidence/releases/0.1.0/clients/thunderbird-explicit-vancouver/thunderbird-gui-results.json" + }, + "thunderbird-explicit-berlin": { + "status": "passed", + "client": "Thunderbird", + "version": "159.0", + "timezone": "Europe/Berlin", + "systemTimezone": false, + "checks": [ + "native creation and server read", + "conditional time editing and reopen", + "bounded daily recurrence and edited occurrence", + "client restart with three correct occurrences", + "task completion and deletion", + "whole-series deletion" + ], + "evidence": "https://github.com/busystack/caldav-server/blob/f3ba60d360117c44b3dfeb6dc9848778a98ef06a/qualification/evidence/releases/0.1.0/clients/thunderbird-explicit-berlin/thunderbird-gui-results.json" + }, + "gnome-vancouver": { + "status": "passed", + "client": "GNOME Calendar", + "version": "50.0-1", + "EDS": "3.56.2-8", + "timezone": "America/Vancouver", + "creationEditingReopening": true, + "finiteSeriesInstances": 3, + "clientServerRestarts": true, + "wholeSeriesDeleted": true, + "actualDeleteStatus": 204, + "finalGetStatus": 404, + "evidence": "https://github.com/busystack/caldav-server/blob/f3ba60d360117c44b3dfeb6dc9848778a98ef06a/qualification/evidence/releases/0.1.0/clients/gnome-vancouver/gnome-workflow.json" + } + }, + "qualificationScope": "Native-editor qualification of the identified executable and Dockerfile/Compose inputs. The release manifest records final release-source and distribution identities.", + "verifiedImplementationCi": { + "sourceCommit": "f3ba60d360117c44b3dfeb6dc9848778a98ef06a", + "pushRun": 41379618091, + "pullRequestRun": 41379622624, + "status": "passed" + }, + "limitations": [ + "Real editor controls were automated in isolated client profiles; manual human use and physical authenticators were not qualified.", + "Only the recorded client versions, platforms and timezone profiles are covered.", + "Failed and interrupted automation attempts remain preserved in the historical evidence archive; they are not counted as passing executions." + ] +} diff --git a/qualification/keycloak/Dockerfile b/qualification/keycloak/Dockerfile new file mode 100644 index 0000000..91f34df --- /dev/null +++ b/qualification/keycloak/Dockerfile @@ -0,0 +1,11 @@ +# Test fixture only: the official UBI9 runtime requires x86-64-v2. +# Copy the identical pinned Keycloak distribution onto the Java 25 runtime +# already qualified for this workspace's older CPU. No server code is changed. +FROM quay.io/keycloak/keycloak:26.8.1@sha256:a78d6b79572168cb25886b80a8b5b8d4ed4390743e3eecb810538683e209dd6b AS distribution +FROM eclipse-temurin:25-jdk@sha256:3ab7fc8d0b4ade8fef96903fb36767c1dbb4fd1535dcfad85b59c17afbbb5857 +COPY --from=distribution --chown=1000:1000 /opt/keycloak /opt/keycloak +ENV KC_RUN_IN_CONTAINER=true +USER 1000:1000 +WORKDIR /opt/keycloak +EXPOSE 8443 +ENTRYPOINT ["/opt/keycloak/bin/kc.sh"] diff --git a/qualification/passkeys/.gitignore b/qualification/passkeys/.gitignore new file mode 100644 index 0000000..c2658d7 --- /dev/null +++ b/qualification/passkeys/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/qualification/passkeys/Dockerfile b/qualification/passkeys/Dockerfile new file mode 100644 index 0000000..bc63033 --- /dev/null +++ b/qualification/passkeys/Dockerfile @@ -0,0 +1,8 @@ +FROM mcr.microsoft.com/playwright:v1.61.1-noble@sha256:5b8f294aff9041b7191c34a4bab3ac270157a28774d4b0660e9743297b697e48 +RUN apt-get update -qq && apt-get install --no-install-recommends --yes libnss3-tools=2:3.98-1ubuntu0.3 && rm -rf /var/lib/apt/lists/* +WORKDIR /qualification +COPY package.json package-lock.json ./ +ENV PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1 +RUN npm ci --ignore-scripts --no-audit --no-fund +COPY *.mjs trust-and-run.sh ./ +ENTRYPOINT ["bash", "/qualification/trust-and-run.sh"] diff --git a/qualification/passkeys/README.md b/qualification/passkeys/README.md new file mode 100644 index 0000000..89bf759 --- /dev/null +++ b/qualification/passkeys/README.md @@ -0,0 +1,54 @@ +# Browser security and deployment qualification + +The fixture exercises the packaged Java application, real PostgreSQL, server-rendered pages, Spring Security and Chromium WebAuthn over normally validated HTTPS. It uses a virtual user-verified resident CTAP2 authenticator. It does not qualify physical authenticators, other browsers or desktop CalDAV clients. + +On a Linux Docker host with Compose 2.24.4 or newer, Python 3 and OpenSSL: + +```sh +python3 scripts/build-image.py +docker build --tag busystack-passkey-browser:qualification qualification/passkeys +python3 qualification/passkeys/run.py --application-image "busystack-caldav:$(python3 scripts/release_version.py)" --groups +python3 qualification/passkeys/run.py --application-image "busystack-caldav:$(python3 scripts/release_version.py)" --proxy --groups +``` + +The default profile uses direct embedded TLS at the hostname root. `--context-path /team` tests embedded TLS under a servlet prefix. `--proxy` uses the pinned non-root Nginx fixture with TLS termination, preserved `/team` paths, host-root CalDAV discovery and RFC 8414 metadata mapping, private management routes and bounded declared/chunked bodies. The ordinary production Compose deployment still consists of the application and PostgreSQL. + +The checks cover password-only mutation denial; actual passkey enrollment/assertion and MFA session rotation; a collection explicitly selecting and displaying `VTODO`; named DAV credential issuance, display-once semantics and revocation; administrator-controlled public native client registration; real browser authorization/consent with S256 PKCE and a cookie-authenticated missing-CSRF denial; ordinary recovery-code issuance/display-once, anonymous denial, recent-primary use, replay rejection, access reset and fresh factor enrollment; code exchange without a client secret; refresh rotation and grant revocation; JDBC browser-session and passkey persistence across JVM restart; and disabled-session/passkey rejection. Authentication of `/dav/missing` proves routing and credential enforcement, not calendar/task content synchronization. + +For the optional upstream profile: + +```sh +docker build --tag busystack-keycloak-browser:26.8.1 qualification/keycloak +python3 qualification/passkeys/run.py --application-image "busystack-caldav:$(python3 scripts/release_version.py)" --oidc +``` + +This profile additionally starts a disposable real Keycloak instance and uses distinct reserved browser sites, `app.busystack.test` and `idp.identity.test`, with private-CA certificates. A fresh nonadministrator account is provisioned from an exact subject allowlist, despite its upstream `realm-admin` role. Actual organization password authentication and the cross-site callback must preserve the application's Secure, HttpOnly, Lax session cookie. Organization login alone cannot mutate collections; a local passkey completes MFA. The user cannot discover the separate local administrator's collection or enter administration. A local-password attempt with the upstream password must fail. This account then exercises DAV credential/OAuth browser workflows and persistence. Explicit identity linking and provider logout/deprovisioning have separate integration tests. Restored-provider login belongs to the restore profile below. + +Playwright is pinned to 1.61.1 in the package lock; the digest-pinned browser image currently contains Chromium 149.0.7827.55 and prints its actual version. NSS tooling is pinned to `libnss3-tools=2:3.98-1ubuntu0.3`. These dependencies are absent from production runtime. Exact image digests are listed in [versions](../../docs/versions.md). + +Each run creates a host-private temporary directory, disposable CA/certificates and randomly named Compose project. All published fixture ports bind `127.0.0.1`. Scoped reserved-domain resolution routes only the test domains to loopback, while retaining normal TLS hostname/SNI checks. A separate browser container imports only the fixture CA into its NSS database; the application's optional upstream truststore contains only that CA. Certificate-error bypasses and host trust changes are absent. A normal client lacking the CA must reject the certificate. + +Generated secrets, private keys, truststores and virtual authenticator state are disposable, never committed as fixtures or uploaded as evidence. Failure output redacts passwords, tokens and authorization parameters. Cleanup removes only the named fixture containers/networks/volumes. Missing prerequisites, TLS failures, failed assertions and explicit deadlines fail visibly. Execution status belongs in [qualification evidence](https://github.com/busystack/caldav-server/blob/f3ba60d360117c44b3dfeb6dc9848778a98ef06a/qualification/evidence/README.md); a fixture's presence does not establish a pass. Hostile ceremonies, physical devices, independent calendar/task clients and complete production readiness have separate evidence; physical devices remain unexecuted. + +The browser integration uses [Playwright CDP sessions](https://playwright.dev/docs/api/class-cdpsession) and the [Chromium WebAuthn testing protocol](https://chromedevtools.github.io/devtools-protocol/tot/WebAuthn/). + +## Group sharing, restoration and packaged-client correctness + +`--groups` requires actual CSRF-protected account provisioning, group creation/rename/membership updates/deletion and owner READ/WRITE/revoke forms. The actor retains one authoritative collection; administrator membership management is distinct from owner-controlled data grants. + +```sh +python3 qualification/passkeys/run.py --application-image "busystack-caldav:$(python3 scripts/release_version.py)" --proxy --groups --restore --clients --client-python .client-venv/bin/python +python3 qualification/passkeys/run.py --application-image "busystack-caldav:$(python3 scripts/release_version.py)" --oidc --groups --restore +``` + +`--restore` creates an actual VTODO through HTTPS using a browser-issued DAV credential, authorizes an actual native OAuth grant, records a sync token, and saves a database dump while those credentials are demonstrably valid. It then revokes access through the browser and restores the older dump into a separate empty owned project. Every restored account is initially disabled; epochs change and factors/sessions/credentials/grants are cleared. The operator reviews known fixture permissions and recovers only the selected owner and a synthetic READ recipient. The owner completes fresh local or real cross-site upstream authentication and passkey enrollment, then issues fresh DAV and OAuth access. The second principal completes actual local password/passkey enrollment and DAV issuance. HTTPS assertions cover byte-exact task/ownership, old Basic/Bearer/refresh rejection, invalid old sync state, fresh initial sync, restored READ GET permission, PUT denial, and owner removal of that share followed by recipient denial. A separate WRITE grant remains preserved in PostgreSQL while its account stays disabled. The same provider remains running during the federated restore; exact issuer/subject recovery does not create a local password fallback. + +The current correction gate uses `--clients` to run the existing small Python and native Thunderbird correctness profiles against the packaged HTTPS server with a browser-issued credential. It checks same-resource cross-client edits without collecting latency or seeding capacity workloads. `--restore` also retains nine accepted original audit fixtures, rejects all nine invalid originals atomically, and verifies original bytes/ETags, mixed properties, immutable history, shares, old stable/partial tokens and the corrected 2040 timezone reports after the actual backup/restore scripts. Original fixture bytes are in `src/test/resources/calendar/audit/`. Missing client prerequisites fail visibly. Physical devices and public production DNS/TLS remain separate verification. + +`--workload` requires the local restore profile and the locked independent-client Python environment. It leaves the sync-depth setting absent, verifies the effective default through authenticated REPORTs, runs the independent-client dataset, and captures latency distributions, environment/image identifiers, Docker memory/CPU samples and PostgreSQL statistics in `target/qualification/workload.json`. It verifies credentials remain valid immediately before the backup, so elapsed token expiry cannot masquerade as restoration revocation. Workload failure or expiry fails the qualification. Results describe one client on a shared host, not capacity or high availability. All ordinary browser profiles leave the setting absent. The required `--strict-sync-depth` profile sets explicit false and verifies actual missing/Depth 0 success with Depth 1 rejection. No client user-agent mode is used. + +The optional historical `--workload` measurement profile additionally seeds only owned disposable fixtures: 10,037 independently generated valid calendar resources exceeding 32 MiB, and 12,500 group grants across 25 calendars and 500 groups. Actual independent HTTPS requests traverse content and metadata using 97-row RFC 6578 pages, retry continuation, write/delete/recreate/insert across boundaries, restart the JVM midway and resume, then verify deltas and a selective query. Repeated valid property updates fill 12 resource targets; selected values, names, all properties, three concurrent readers, an explicit oversized-response failure and subsequent healthy GET are measured. The real browser checks first/last dashboard sharing pages, owner revocation, group membership update and deletion. Source fixture population is SQL rather than a bulk-create throughput measurement; ordinary client CRUD is separately qualified. Image, memory samples, duration and database counters remain recorded. The controlled restart creates an explicitly recorded measurement gap, not an unreported observer failure. + +`--workload` also requires the exact native Thunderbird prerequisite (`--thunderbird-binary` or `BUSYSTACK_THUNDERBIRD_BINARY`, default `.client-tools/thunderbird/thunderbird`). It re-runs both independent Python and native headless Thunderbird same-resource event/task round trips against the **final packaged server**, with no compatibility-enabling override. Missing prerequisites fail visibly. The earlier Maven integration run is separate evidence rather than a substitute for this artifact-level execution. + +The expanded fixture's selective query has a request-specific 150-second client deadline, while all other capacity requests keep 60 seconds and the process keeps its 900-second bound. The pinned proxy uses a 120-second read deadline and unchanged 5-second connect deadline. This adjustment follows a fixed 10,037-resource real-HTTP selective query taking 41.564 seconds at a 256 MiB heap, beyond the former 30-second proxy deadline; see [measurement scope](../../docs/performance.md). It does not extend authentication, MFA or token policy and does not permit unlimited query work. diff --git a/qualification/passkeys/account-recovery.mjs b/qualification/passkeys/account-recovery.mjs new file mode 100644 index 0000000..0459cfa --- /dev/null +++ b/qualification/passkeys/account-recovery.mjs @@ -0,0 +1,82 @@ +import assert from 'node:assert/strict'; +import {readFileSync} from 'node:fs'; +import {call, json} from './transport.mjs'; +import {issueDavCredential} from './dav-credentials.mjs'; +import {qualifyOAuth} from './oauth.mjs'; +import {organizationPrimary, primaryCannotMutate, signOut} from './oidc.mjs'; + +export async function qualifyAccountRecovery({page, base, privateValues, administratorPage, + username, cdp, authenticatorId}) { + const prefix = new URL(base).pathname.replace(/\/$/, ''); + const federated = process.env.BUSYSTACK_BROWSER_OIDC === 'true'; + async function primary() { + if (federated) await organizationPrimary(page, base, privateValues); + else { + await page.goto(base + '/login'); + const password = readFileSync(process.env.BUSYSTACK_BROWSER_PASSWORD_FILE, 'utf8').trimEnd(); + await page.locator('input[name=username]').fill(username); + await page.locator('input[name=password]').fill(password); + await Promise.all([page.waitForURL(base + '/'), page.getByRole('button', {name: 'Verify password', exact: true}).click()]); + } + } + const device = await issueDavCredential(page, base, privateValues, 'Recovery-code reset device', username); + const grant = await qualifyOAuth(page, base, privateValues, administratorPage, + {clientId: 'browser-code-recovery-qualification', clientName: 'Browser Recovery Reset', retainGrant: true}); + await page.goto(base + '/security/recovery'); + const [issued] = await Promise.all([page.waitForResponse(response => response.request().method() === 'POST' + && response.url() === base + '/security/recovery/codes'), page.getByRole('button', {name: 'Generate ten codes', exact: true}).click()]); + assert.ok((issued.headers()['cache-control'] || '').includes('no-store')); + await page.getByRole('heading', {name: 'Save your recovery codes', exact: true}).waitFor(); + const codes = await page.locator('ol code').allInnerTexts(); + privateValues.push(...codes); + assert.equal(codes.length, 10); + assert.equal(new Set(codes).size, 10); + assert.ok(codes.every(value => value.length === 80)); + await page.goto(base + '/security/recovery'); + for (const code of codes) assert.ok(!(await page.content()).includes(code), 'Recovery codes must not reappear in settings'); + await signOut(page, base); + const anonymousContext = await page.context().browser().newContext(); + try { + const anonymous = await anonymousContext.newPage(); + await anonymous.goto(base + '/login'); + const status = await anonymous.evaluate(async ({target, code}) => { + const csrf = document.querySelector('meta[name=csrf-token]').content; + const response = await fetch(target, {method: 'POST', redirect: 'manual', + headers: {'Content-Type': 'application/x-www-form-urlencoded'}, + body: new URLSearchParams({_csrf: csrf, code})}); + return response.status; + }, {target: prefix + '/security/recovery/use', code: codes[0]}); + assert.ok([0, 401, 403].includes(status), 'A recovery code alone must not authenticate an anonymous browser'); + } finally { await anonymousContext.close(); } + await primary(); + await primaryCannotMutate(page, base); + await cdp.send('WebAuthn.setAutomaticPresenceSimulation', {authenticatorId, enabled: false}); + await page.goto(base + '/security/recovery'); + assert.equal(await page.locator('main strong').innerText(), '10', 'Anonymous recovery must not consume a valid code'); + await page.getByLabel('Recovery code', {exact: true}).fill(codes[0]); + await Promise.all([page.waitForURL(url => url.pathname === prefix + '/login' && url.searchParams.has('recovered')), + page.getByRole('button', {name: 'Use code and reset account access', exact: true}).click()]); + assert.equal((await call(base, '/dav/missing', {headers: {Authorization: 'Basic ' + device.encoded}})).status, 401); + assert.equal((await call(base, '/dav/missing', {headers: {Authorization: 'Bearer ' + grant.accessToken}})).status, 401); + const refresh = await call(base, '/oauth2/token', {method: 'POST', form: { + grant_type: 'refresh_token', client_id: grant.clientId, refresh_token: grant.refreshToken + }}); + assert.equal(refresh.status, 400); + assert.equal(json(refresh).error, 'invalid_grant'); + await cdp.send('WebAuthn.setAutomaticPresenceSimulation', {authenticatorId, enabled: true}); + await page.getByRole('button', {name: 'Verify passkey', exact: true}).click(); + await page.locator('#security-status').filter({hasText: '(401)'}).waitFor(); + await cdp.send('WebAuthn.clearCredentials', {authenticatorId}); + await primary(); + await page.goto(base + '/security/recovery'); + assert.equal(await page.locator('main strong').innerText(), '9'); + await page.getByLabel('Recovery code', {exact: true}).fill(codes[0]); + const [replay] = await Promise.all([page.waitForResponse(response => response.request().method() === 'POST' + && response.url() === base + '/security/recovery/use'), page.getByRole('button', {name: 'Use code and reset account access', exact: true}).click()]); + assert.equal(replay.status(), 403, 'A consumed recovery code must not be reusable'); + await page.goto(base + '/security/passkeys'); + await page.getByRole('button', {name: 'Register passkey', exact: true}).click(); + await page.locator('#security-status').filter({hasText: 'Passkey registered.'}).waitFor(); + await Promise.all([page.waitForURL(base + '/'), page.getByRole('button', {name: 'Verify passkey to complete MFA', exact: true}).click()]); + console.log('BROWSER_SINGLE_USE_PRIMARY_PLUS_RECOVERY_CODE_RESET_AND_FRESH_MFA_PASSED'); +} diff --git a/qualification/passkeys/audit_corpus.py b/qualification/passkeys/audit_corpus.py new file mode 100644 index 0000000..5af2164 --- /dev/null +++ b/qualification/passkeys/audit_corpus.py @@ -0,0 +1,222 @@ +"""Small original-audit corpus across the actual packaged HTTPS backup/restore workflow.""" + +import hashlib +import json +import xml.etree.ElementTree as XML +from pathlib import Path + + +class Corpus: + VALID = ( + "control-event.ics", "control-last-ordinary-second.ics", "future-zone.ics", + "valid-lowercase-delimiters.ics", "valid-rfc9073-location.ics", + "valid-rfc9074-alarm.ics", "valid-rfc9253-relationship.ics", + "valid-text-list.ics", "valid-unknown-x-properties.ics", + ) + # Independently derived from the unchanged captured uploads by UTC = local - TZOFFSETFROM. + # These are fixed expected digests, not a second runtime repair used to prepare requests. + OBSERVANCE_UPLOADS = { + "vancouver": ( + "3f9aa2efae75716aa40272fe96534fb9cb87af3068c8b8cac3fc6d9741d3387d", + "46aa8ea5c501d7c7243f58fd63a3dbc7eab78c5bca4cab189a9eb592ed99ec18"), + "berlin": ( + "6b884299d3e11a8c7856895b10743f92862b0ec0f273fe9e46cea2287ced237d", + "e063263ef823cb2ab181a9773078f2dc9a6254fe63f5611567e6acb775ad4c60"), + } + + def __init__(self, checkpoint): + self.checkpoint = checkpoint + # These are controller test inputs, independent of the deployment root. + # A downloaded deployment intentionally contains no application source. + self.fixture = Path(__file__).resolve().parents[2] / "src/test/resources/calendar/audit" + self.resources, self.collections, self.tokens = {}, {}, {} + + def call(self, encoded, path, method="GET", body=None, **headers): + return self.checkpoint.request(path, method, + {"Authorization": "Basic " + encoded, **headers}, body) + + def seed(self, environment): + point = self.checkpoint + encoded = point.saved["encoded"] + for component, suffix in (("VEVENT", "events"), ("VTODO", "tasks")): + name = "independent-audit-restore-" + suffix + path = "/dav/calendars/" + point.owner + "/" + name + "/" + body = ('' + '' + '').encode() + assert self.call(encoded, path, "MKCALENDAR", body, **{"Content-Type": "application/xml"})[0] == 201 + identifier = point.operations.sql(environment, + "SELECT collection_id FROM dav_bindings WHERE home_id='" + point.owner + "' AND path='" + name + "';") + assert identifier + self.collections[component] = {"path": path, "id": identifier} + for name in self.VALID: + content = (self.fixture / name).read_bytes() + component = "VTODO" if b"BEGIN:VTODO" in content else "VEVENT" + path = self.collections[component]["path"] + name + assert self.call(encoded, path, "PUT", content, + **{"Content-Type": "text/calendar", "If-None-Match": "*"})[0] == 201, name + status, headers, raw = self.call(encoded, path) + assert status == 200 and raw == content, name + self.resources[path] = {"content": content, "etag": headers["ETag"], "component": component} + for zone, (original_digest, corrected_digest) in self.OBSERVANCE_UPLOADS.items(): + name = "thunderbird159-" + zone + ".ics" + original = (self.fixture.parent / "observance-until" / name).read_bytes() + assert hashlib.sha256(original).hexdigest() == original_digest + path = self.collections["VEVENT"]["path"] + name + status, headers, _ = self.call(encoded, path, "PUT", original, + **{"Content-Type": "text/calendar", "If-None-Match": "*"}) + assert status == 201 + assert not {key.lower() for key in headers} & {"etag", "last-modified"} + status, headers, corrected = self.call(encoded, path) + assert status == 200 and hashlib.sha256(corrected).hexdigest() == corrected_digest + assert headers["ETag"] == '"' + corrected_digest + '"' + self.resources[path] = {"content": corrected, "etag": headers["ETag"], "component": "VEVENT"} + # The invalid originals are rejection controls, never silently introduced into this dump. + for fixture in sorted(self.fixture.glob("invalid-*.ics")): + content = fixture.read_bytes() + component = "VTODO" if b"BEGIN:VTODO" in content else "VEVENT" + collection = self.collections[component] + before = point.report(encoded, path=collection["path"])[2] + old = XML.fromstring(before).findtext("{DAV:}sync-token") + path = collection["path"] + fixture.name + status, _, body = self.call(encoded, path, "PUT", content, **{"Content-Type": "text/calendar"}) + assert status == 403 and XML.fromstring(body).find("{urn:ietf:params:xml:ns:caldav}valid-calendar-data") is not None + assert self.call(encoded, path)[0] == 404 + after = point.report(encoded, path=collection["path"])[2] + assert XML.fromstring(after).findtext("{DAV:}sync-token") == old + # Keep one immutable deletion history entry in the actual archive. + self.deleted_path = self.collections["VTODO"]["path"] + "deleted-history.ics" + assert self.call(encoded, self.deleted_path, "PUT", point.content, + **{"Content-Type": "text/calendar"})[0] == 201 + assert self.call(encoded, self.deleted_path, "DELETE")[0] == 204 + assert self.call(encoded, self.deleted_path)[0] == 404 + # Explicit resource properties add real history and historical property snapshots. + properties = (b'' + b'before apres after' + b'') + self.property_path = self.collections["VEVENT"]["path"] + "future-zone.ics" + assert self.call(encoded, self.property_path, "PROPPATCH", properties, + **{"Content-Type": "application/xml"})[0] == 207 + for collection in self.collections.values(): + assert self.call(encoded, collection["path"], "PROPPATCH", properties, + **{"Content-Type": "application/xml"})[0] == 207 + self.grant(encoded, collection["path"], point.reader, point.writer) + status, _, body = point.report(encoded, path=collection["path"]) + assert status == 207 + self.tokens[collection["path"]] = XML.fromstring(body).findtext("{DAV:}sync-token") + # Leave a real immutable partial cursor in the archive as well as final stable tokens. + status, _, body = point.report(encoded, path=self.collections["VEVENT"]["path"], limit=1) + assert status == 207 and b"507 Insufficient Storage" in body + self.cursor = XML.fromstring(body).findtext("{DAV:}sync-token") + self.verify_owner(encoded) + self.database_snapshot = self.database_state(environment) + print("HTTPS_AUDIT_CORPUS_CREATED=" + json.dumps({ + "acceptedOriginals": len(self.VALID), "rejectedOriginals": 9, + "repairedObservanceUploads": len(self.OBSERVANCE_UPLOADS), + "contentSha256": {Path(path).name: hashlib.sha256(value["content"]).hexdigest() + for path, value in self.resources.items()}, + "historyAndPropertySnapshots": True, "deletedResourceHistory": True, + "collectionPropertiesAndDirectReadWriteGrants": True, "durablePartialCursor": True, + }, sort_keys=True), flush=True) + + def grant(self, encoded, path, *principals): + acl = XML.Element("{DAV:}acl") + for index, principal in enumerate(principals): + ace = XML.SubElement(acl, "{DAV:}ace") + XML.SubElement(XML.SubElement(ace, "{DAV:}principal"), "{DAV:}href").text = ( + self.checkpoint.base + "/dav/principals/" + principal + "/") + grant = XML.SubElement(ace, "{DAV:}grant") + XML.SubElement(XML.SubElement(grant, "{DAV:}privilege"), "{DAV:}read") + if index: + XML.SubElement(XML.SubElement(grant, "{DAV:}privilege"), "{DAV:}write") + assert self.call(encoded, path, "ACL", XML.tostring(acl), **{"Content-Type": "application/xml"})[0] == 200 + + def database_state(self, environment): + ids = ",".join("'" + value["id"] + "'" for value in self.collections.values()) + query = ("SELECT json_build_object('versions',(SELECT json_agg(row_to_json(v) ORDER BY collection_id,name,revision) " + "FROM (SELECT collection_id,name,revision,deleted,encode(sha256(content),'hex') AS sha256,etag,updated_at " + "FROM calendar_resource_versions WHERE collection_id IN (" + ids + ")) v)," + "'properties',(SELECT json_agg(row_to_json(p) ORDER BY collection_id,resource_name,revision,namespace,property_name) " + "FROM calendar_resource_version_properties p WHERE collection_id IN (" + ids + "))," + "'collectionProperties',(SELECT json_agg(row_to_json(p) ORDER BY collection_id,namespace,name) " + "FROM dav_collection_properties p WHERE collection_id IN (" + ids + "))," + "'directGrants',(SELECT json_agg(row_to_json(m) ORDER BY collection_id,account_id) " + "FROM collection_members m WHERE collection_id IN (" + ids + ")));") + return json.loads(self.checkpoint.operations.sql(environment, query)) + + def assert_database(self, environment): + assert self.database_state(environment) == self.database_snapshot + print("HTTPS_AUDIT_CORPUS_RESTORED_IMMUTABLE_HISTORY_AND_PROPERTIES_PASSED", flush=True) + + def verify_owner(self, encoded): + assert self.call(encoded, self.deleted_path)[0] == 404 + for path, value in self.resources.items(): + status, headers, content = self.call(encoded, path) + assert status == 200 and content == value["content"] and headers["ETag"] == value["etag"], path + path = self.collections["VEVENT"]["path"] + for name, expected in (("future-query-correct.xml", True), ("future-query-incorrect.xml", False)): + status, _, body = self.call(encoded, path, "REPORT", (self.fixture / name).read_bytes(), + **{"Depth": "1", "Content-Type": "application/xml"}) + assert status == 207 + found = any((node.text or "").endswith("/future-zone.ics") for node in XML.fromstring(body).findall(".//{DAV:}href")) + assert found == expected, name + free = (b'' + b'') + status, _, busy = self.call(encoded, path, "REPORT", free, **{"Depth": "1", "Content-Type": "application/xml"}) + assert status == 200 and b"20400401T100000Z/20400401T110000Z" in busy + assert busy.count(b"DTSTAMP:") == busy.count(b"UID:") == busy.count(b"BEGIN:VFREEBUSY") == 1 + root = XML.Element("{urn:ietf:params:xml:ns:caldav}calendar-multiget") + prop = XML.SubElement(root, "{DAV:}prop") + XML.SubElement(XML.SubElement(prop, "{urn:ietf:params:xml:ns:caldav}calendar-data"), + "{urn:ietf:params:xml:ns:caldav}expand", start="20400401T090000Z", end="20400401T130000Z") + XML.SubElement(root, "{DAV:}href").text = self.checkpoint.base + self.property_path + status, _, data = self.call(encoded, path, "REPORT", XML.tostring(root), **{"Content-Type": "application/xml"}) + assert status == 207 + expanded = XML.fromstring(data).findtext(".//{urn:ietf:params:xml:ns:caldav}calendar-data") + assert "DTSTART:20400401T100000Z" in expanded and "DTEND:20400401T110000Z" in expanded + for zone, start, end in (("vancouver", "170000", "180000"), ("berlin", "090000", "100000")): + repaired = self.collections["VEVENT"]["path"] + "thunderbird159-" + zone + ".ics" + root.find("{DAV:}href").text = self.checkpoint.base + repaired + expansion = root.find(".//{urn:ietf:params:xml:ns:caldav}expand") + expansion.set("start", "20261109T000000Z") + expansion.set("end", "20261110T000000Z") + status, _, data = self.call(encoded, path, "REPORT", XML.tostring(root), + **{"Content-Type": "application/xml"}) + assert status == 207 + expanded = XML.fromstring(data).findtext(".//{urn:ietf:params:xml:ns:caldav}calendar-data") + assert "DTSTART:20261109T" + start + "Z" in expanded + assert "DTEND:20261109T" + end + "Z" in expanded + status, _, body = self.call(encoded, self.property_path, "PROPFIND", + b'', + **{"Content-Type": "application/xml", "Depth": "0"}) + assert status == 207 + proof = XML.fromstring(body).find(".//{urn:busystack:audit-restore}proof") + assert proof.text == "before " and proof[0].text == "apres" and proof[0].tail == " after" + assert proof.attrib["{http://www.w3.org/XML/1998/namespace}lang"] == "en" + for resource, value in self.resources.items(): + assert self.call(encoded, resource)[2] == value["content"] + + def verify_restored(self, encoded, reader_encoded): + self.verify_owner(encoded) + for component, collection in self.collections.items(): + for token in [self.tokens[collection["path"]]] + ([self.cursor] if component == "VEVENT" else []): + status, _, body = self.checkpoint.report(encoded, token, path=collection["path"]) + assert status == 403 and XML.fromstring(body).find("{DAV:}valid-sync-token") is not None + assert self.checkpoint.report(encoded, path=collection["path"])[0] == 207 + for path, value in self.resources.items(): + collection = self.collections[value["component"]] + reader_path = "/dav/calendars/" + self.checkpoint.reader + "/" + collection["id"] + "/" + Path(path).name + assert self.call(reader_encoded, reader_path)[2] == value["content"] + assert self.call(reader_encoded, reader_path, "PUT", value["content"], + **{"Content-Type": "text/calendar", "If-Match": value["etag"]})[0] == 403 + print("HTTPS_AUDIT_CORPUS_BYTE_EXACT_TIMEZONE_REPORTS_READ_SHARES_AND_OLD_TOKENS_PASSED", flush=True) + + def revoke(self, encoded, reader_encoded): + for collection in self.collections.values(): + self.grant(encoded, collection["path"]) + for path, value in self.resources.items(): + collection = self.collections[value["component"]] + reader_path = "/dav/calendars/" + self.checkpoint.reader + "/" + collection["id"] + "/" + Path(path).name + assert self.call(reader_encoded, reader_path)[0] in (403, 404) + assert self.call(encoded, path)[2] == value["content"] + print("HTTPS_AUDIT_CORPUS_REVOKED_SHARES_DENIED_OWNER_BYTES_PRESERVED", flush=True) diff --git a/qualification/passkeys/dav-credentials.mjs b/qualification/passkeys/dav-credentials.mjs new file mode 100644 index 0000000..98e0a8e --- /dev/null +++ b/qualification/passkeys/dav-credentials.mjs @@ -0,0 +1,71 @@ +import assert from 'node:assert/strict'; +import {randomUUID} from 'node:crypto'; +import {call} from './transport.mjs'; + +export async function issueDavCredential(page, base, privateValues, label, username = process.env.BUSYSTACK_BROWSER_USERNAME) { + await page.goto(base + '/'); + const form = page.locator('form').filter({has: page.locator('input[name=label]')}); + await form.locator('input[name=label]').fill(label); + await form.locator('input[name=days]').fill('1'); + const [response] = await Promise.all([page.waitForResponse(value => value.request().method() === 'POST' && value.url() === base + '/credentials'), + form.getByRole('button', {name: 'Issue credential', exact: true}).click()]); + assert.ok((response.headers()['cache-control'] || '').includes('no-store')); + await page.getByRole('heading', {name: 'New DAV credential', exact: true}).waitFor(); + const credential = await page.locator('code').innerText(); + assert.equal(credential.length, 80); + const encoded = Buffer.from(username + ':' + credential).toString('base64'); + privateValues.push(credential, encoded); + await page.goto(base + '/'); + assert.ok(!(await page.content()).includes(credential), 'A device secret must not appear again in management'); + return {credential, encoded}; +} + +export async function qualifyDavCredentials(page, base, privateValues, username = process.env.BUSYSTACK_BROWSER_USERNAME) { + const label = 'Browser-qualified DAV device'; + const {credential, encoded} = await issueDavCredential(page, base, privateValues, label, username); + assert.equal((await call(base, '/dav/missing', {headers: {Authorization: 'Basic ' + encoded}})).status, 404); + await verifySyncDepth(page, base, encoded); + await page.goto(base + '/'); + assert.ok(!(await page.content()).includes(credential), 'A device secret must not appear again in the management page'); + const entry = page.locator('li').filter({hasText: label}); + await Promise.all([page.waitForURL(base + '/'), entry.getByRole('button', {name: 'Revoke', exact: true}).click()]); + assert.equal((await call(base, '/dav/missing', {headers: {Authorization: 'Basic ' + encoded}})).status, 401); + console.log('BROWSER_DAV_CREDENTIAL_ISSUANCE_DISPLAY_ONCE_AND_REVOCATION_PASSED'); +} + + +async function verifySyncDepth(page, base, encoded) { + const headers = {Authorization: 'Basic ' + encoded}; + const discovery = await call(base, '/dav/', {method: 'PROPFIND', headers: {...headers, Depth: '0'}, + body: ''}); + assert.equal(discovery.status, 207); + const principal = await page.evaluate(xml => { + const doc = new DOMParser().parseFromString(xml, 'application/xml'); + const property = doc.getElementsByTagNameNS('DAV:', 'current-user-principal')[0]; + return property?.getElementsByTagNameNS('DAV:', 'href')[0]?.textContent; + }, discovery.body); + assert.ok(principal); + const id = new URL(principal, base).pathname.split('/').filter(Boolean).at(-1); + assert.match(id, /^[0-9a-f-]{36}$/); + const path = '/dav/calendars/' + id + '/depth-probe-' + randomUUID() + '/'; + assert.equal((await call(base, path, {method: 'MKCALENDAR', headers})).status, 201); + try { + const body = '1'; + const observed = {}; + for (const depth of [undefined, '0', '1']) { + const response = await call(base, path, {method: 'REPORT', headers: {...headers, ...(depth === undefined ? {} : {Depth: depth})}, body}); + observed[depth === undefined ? 'missing' : depth] = response.status; + } + assert.equal(observed.missing, 207); + assert.equal(observed['0'], 207); + const expected = process.env.BUSYSTACK_SYNC_DEPTH_EXPECTED; + assert.ok(expected === 'true' || expected === 'false', 'Calling harness must record the intended configuration'); + assert.equal(observed['1'], expected === 'true' ? 207 : 400); + const other = await call(base, path, {method: 'REPORT', headers: {...headers, Depth: '1'}, body: body.replace('1', '2')}); + assert.ok([400, 403].includes(other.status)); + console.log('DAV_SYNC_CONFIGURATION=' + JSON.stringify({configurationInput: process.env.BUSYSTACK_SYNC_DEPTH_INPUT, + observedDepthStatus: observed, depthOneWithOtherSyncLevelStatus: other.status, allowSyncDepthOne: observed['1'] === 207})); + } finally { + assert.equal((await call(base, path, {method: 'DELETE', headers})).status, 204); + } +} diff --git a/qualification/passkeys/groups.mjs b/qualification/passkeys/groups.mjs new file mode 100644 index 0000000..aaad1ee --- /dev/null +++ b/qualification/passkeys/groups.mjs @@ -0,0 +1,83 @@ +import assert from 'node:assert/strict'; +import {randomBytes} from 'node:crypto'; + +export async function qualifyGroups(page, base, privateValues) { + const prefix = new URL(base).pathname.replace(/\/$/, ''); + const member = 'browsergroupmember'; + const password = randomBytes(32).toString('base64url'); + privateValues.push(password); + await page.goto(base + '/admin'); + const provision = page.locator(`form[action="${prefix}/admin/accounts"]`); + await provision.locator('input[name=username]').fill(member); + await provision.locator('input[name=displayName]').fill('Browser Group Member'); + await provision.locator('input[name=password]').fill(password); + await Promise.all([page.waitForURL(base + '/admin'), provision.getByRole('button', {name: 'Create account', exact: true}).click()]); + await page.getByText(member, {exact: true}).waitFor(); + await page.getByRole('link', {name: 'Sharing groups', exact: true}).click(); + await page.getByRole('heading', {name: 'Sharing groups', exact: true}).waitFor(); + const create = page.locator(`form[action="${prefix}/admin/groups"]`); + await create.locator('input[name=name]').fill('Browser sharing group'); + await create.locator('textarea[name=usernames]').fill(member); + await Promise.all([page.waitForURL(base + '/admin/groups'), create.getByRole('button', {name: 'Create group', exact: true}).click()]); + let card = page.locator('article').filter({has: page.getByRole('heading', {name: 'Browser sharing group', exact: true})}); + assert.equal(await card.locator('textarea[name=usernames]').inputValue(), member); + const group = await card.locator('code').innerText(); + assert.match(group, /^[a-f0-9-]{36}$/i); + const edit = card.locator(`form[action="${prefix}/admin/groups/${group}"]`); + await edit.locator('input[name=name]').fill('Browser group renamed'); + await edit.locator('textarea[name=usernames]').fill(''); + await Promise.all([page.waitForURL(base + '/admin/groups'), edit.getByRole('button', {name: 'Save group', exact: true}).click()]); + card = page.locator('article').filter({has: page.getByRole('heading', {name: 'Browser group renamed', exact: true})}); + assert.equal(await card.locator('textarea[name=usernames]').inputValue(), ''); + await card.locator('textarea[name=usernames]').fill(member); + await Promise.all([page.waitForURL(base + '/admin/groups'), card.getByRole('button', {name: 'Save group', exact: true}).click()]); + await page.goto(base + '/'); + const sharing = page.locator(`form[action="${prefix}/group-shares"]`).filter({has: page.locator('select[name=group]')}); + await sharing.locator('select[name=collection]').selectOption({label: 'Browser-qualified tasks'}); + await sharing.locator('select[name=group]').selectOption(group); + for (const permission of ['READ', 'WRITE']) { + await sharing.locator('select[name=permission]').selectOption(permission); + await Promise.all([page.waitForURL(base + '/'), sharing.getByRole('button', {name: 'Save group access', exact: true}).click()]); + const grant = page.locator('tbody tr').filter({hasText: 'Browser group renamed'}); + assert.ok((await grant.innerText()).includes(permission)); + } + let grant = page.locator('tbody tr').filter({hasText: 'Browser group renamed'}); + await Promise.all([page.waitForURL(base + '/'), grant.getByRole('button', {name: 'Remove group access', exact: true}).click()]); + assert.equal(await page.locator('tbody tr').filter({hasText: 'Browser group renamed'}).count(), 0); + await page.goto(base + '/admin/groups'); + card = page.locator('article').filter({has: page.getByRole('heading', {name: 'Browser group renamed', exact: true})}); + await Promise.all([page.waitForURL(base + '/admin/groups'), card.getByRole('button', {name: 'Delete group', exact: true}).click()]); + assert.equal(await page.getByRole('heading', {name: 'Browser group renamed', exact: true}).count(), 0); + await page.goto(base + '/'); + assert.equal(await page.locator(`option[value="${group}"]`).count(), 0); + console.log('BROWSER_GROUP_MEMBERSHIP_UPDATE_DELETE_AND_OWNER_GRANTS_PASSED'); +} + + +export async function qualifySharingCapacity(page, administratorPage, base, fixture) { + const prefix = new URL(base).pathname.replace(/\/$/, ''); + await page.goto(base + '/'); + const rows = page.locator('table').filter({has: page.locator('caption').filter({hasText: 'Groups with access to your owned collections'})}).locator('tbody tr'); + assert.equal(await rows.count(), 50); + await page.getByRole('link', {name: 'Next group sharing page', exact: true}).waitFor(); + await page.goto(base + '/?group-shares-page=249'); + assert.equal(await rows.count(), 50); + assert.equal(await page.getByRole('link', {name: 'Next group sharing page', exact: true}).count(), 0); + const remove = rows.first().locator('form'); + const removedGroup = await remove.locator('input[name=group]').inputValue(); + await Promise.all([page.waitForURL(base + '/'), remove.getByRole('button', {name: 'Remove group access', exact: true}).click()]); + assert.equal(await rows.count(), 50); + await administratorPage.goto(base + '/admin/groups'); + let card = administratorPage.locator('article').filter({has: administratorPage.getByRole('heading', {name: fixture.groupName, exact: true})}); + await card.locator('input[name=name]').fill('Capacity group updated'); + await card.locator('textarea[name=usernames]').fill('restorewriter'); + await Promise.all([administratorPage.waitForURL(base + '/admin/groups'), card.getByRole('button', {name: 'Save group', exact: true}).click()]); + card = administratorPage.locator('article').filter({has: administratorPage.getByRole('heading', {name: 'Capacity group updated', exact: true})}); + assert.equal(await card.locator('textarea[name=usernames]').inputValue(), 'restorewriter'); + await Promise.all([administratorPage.waitForURL(base + '/admin/groups'), card.getByRole('button', {name: 'Delete group', exact: true}).click()]); + assert.equal(await administratorPage.getByRole('heading', {name: 'Capacity group updated', exact: true}).count(), 0); + await page.goto(base + '/'); + await page.getByRole('heading', {name: 'BusyStack account', exact: true}).waitFor(); + console.log('BROWSER_12500_GRANTS_PAGINATION_REVOCATION_MEMBERSHIP_AND_GROUP_DELETE_PASSED'); + return {...fixture, browserPassed: true, firstPageRows: 50, lastPage: 249, lastPageRows: 50, removedGroup}; +} diff --git a/qualification/passkeys/oauth.mjs b/qualification/passkeys/oauth.mjs new file mode 100644 index 0000000..81e7ca7 --- /dev/null +++ b/qualification/passkeys/oauth.mjs @@ -0,0 +1,108 @@ +import assert from 'node:assert/strict'; +import {createServer} from 'node:http'; +import {randomBytes, createHash} from 'node:crypto'; +import {call, json} from './transport.mjs'; + +export async function qualifyOAuth(page, base, privateValues, administratorPage = page, options = {}) { + const clientId = options.clientId || 'browser-native-qualification'; + const clientName = options.clientName || 'Browser Native Calendar'; + const state = randomBytes(32).toString('base64url'); + const verifier = randomBytes(32).toString('base64url'); + const challenge = createHash('sha256').update(verifier).digest('base64url'); + privateValues.push(state, verifier); + let resolveCode; + const codeReceived = new Promise(resolve => { resolveCode = resolve; }); + const callback = createServer((incoming, outgoing) => { + const url = new URL(incoming.url, 'http://127.0.0.1'); + const code = url.searchParams.get('code'); + if (incoming.method !== 'GET' || url.pathname !== '/callback' || url.searchParams.get('state') !== state || !code) { + outgoing.writeHead(400, {'Content-Type': 'text/plain'}); + outgoing.end('Invalid authorization callback.'); + return; + } + privateValues.push(code); + outgoing.writeHead(200, {'Content-Type': 'text/plain', 'Cache-Control': 'no-store'}); + outgoing.end('Authorization completed. Return to your account.'); + resolveCode(code); + }); + await new Promise((resolve, reject) => { callback.once('error', reject); callback.listen(0, '127.0.0.1', resolve); }); + let deadline; + try { + const redirect = `http://127.0.0.1:${callback.address().port}/callback`; + if (!options.existingClient) { + await administratorPage.goto(base + '/admin/oauth-clients'); + await administratorPage.getByLabel('Client ID', {exact: true}).fill(clientId); + await administratorPage.getByLabel('Name', {exact: true}).fill(clientName); + await administratorPage.getByLabel('Redirect URIs, one per line', {exact: true}).fill(redirect); + await Promise.all([administratorPage.waitForURL(base + '/admin/oauth-clients'), administratorPage.getByRole('button', {name: 'Register application', exact: true}).click()]); + await administratorPage.getByRole('cell', {name: clientId, exact: true}).waitFor(); + } + const parameters = new URLSearchParams({response_type: 'code', client_id: clientId, redirect_uri: redirect, + scope: 'dav', state, code_challenge: challenge, code_challenge_method: 'S256'}); + const authorizationPage = await page.goto(base + '/oauth2/authorize?' + parameters); + try { + await page.getByRole('heading', {name: 'Authorize application', exact: true}).waitFor(); + } catch { + const headings = await page.locator('h1').allInnerTexts(); + throw new Error(`Consent page unavailable: status=${authorizationPage.status()}, path=${new URL(page.url()).pathname}, headings=${JSON.stringify(headings)}`); + } + const consentState = await page.locator('form').filter({has: page.getByRole('button', {name: 'Authorize application', exact: true})}).locator('input[name=state]').inputValue(); + privateValues.push(consentState); + const missingCsrf = await page.evaluate(async values => (await fetch(values.target, { + method: 'POST', credentials: 'same-origin', redirect: 'manual', headers: {'Content-Type': 'application/x-www-form-urlencoded'}, + body: new URLSearchParams({client_id: values.clientId, state: values.state, scope: 'dav'}) + })).status, {target: base + '/oauth2/authorize', clientId, state: consentState}); + assert.equal(missingCsrf, 403, 'A consent POST with the session and state still requires its CSRF token'); + console.log('BROWSER_CONSENT_WITHOUT_CSRF_REJECTED'); + await page.getByRole('checkbox', {name: 'Calendar and task access', exact: true}).check(); + const timeout = new Promise((_, reject) => { deadline = setTimeout(() => reject(new Error('Authorization callback timed out')), 30000); }); + await page.getByRole('button', {name: 'Authorize application', exact: true}).click(); + const code = await Promise.race([codeReceived, timeout]); + clearTimeout(deadline); + const exchange = await call(base, '/oauth2/token', {method: 'POST', form: { + grant_type: 'authorization_code', client_id: clientId, redirect_uri: redirect, code, code_verifier: verifier + }}); + assert.equal(exchange.status, 200, 'Public S256 authorization-code exchange must succeed without a client secret'); + const initial = json(exchange); + for (const key of ['access_token', 'refresh_token']) { + assert.ok(typeof initial[key] === 'string' && initial[key].length > 40, 'Expected an opaque client credential'); + privateValues.push(initial[key]); + } + assert.equal(initial.token_type.toLowerCase(), 'bearer'); + if (initial.scope !== undefined) assert.ok(initial.scope.split(' ').includes('dav')); + assert.equal((await call(base, '/dav/missing', {headers: {Authorization: 'Bearer ' + initial.access_token}})).status, 404, + 'A valid DAV bearer must reach the application rather than an authentication challenge'); + const refreshed = await call(base, '/oauth2/token', {method: 'POST', form: { + grant_type: 'refresh_token', client_id: clientId, refresh_token: initial.refresh_token + }}); + assert.equal(refreshed.status, 200, 'Public native refresh must succeed'); + const current = json(refreshed); + for (const key of ['access_token', 'refresh_token']) { + assert.ok(typeof current[key] === 'string' && current[key].length > 40, 'Expected a rotated opaque credential'); + privateValues.push(current[key]); + assert.notEqual(current[key], initial[key]); + } + assert.equal((await call(base, '/dav/missing', {headers: {Authorization: 'Bearer ' + current.access_token}})).status, 404); + if (options.retainGrant) { + await page.goto(base + '/'); + assert.ok(Number.isInteger(current.expires_in) && current.expires_in > 0); + return {clientId, accessToken: current.access_token, refreshToken: current.refresh_token, + accessExpiresAt: Date.now() + current.expires_in * 1000}; + } + await page.goto(base + '/oauth-grants'); + const grant = page.locator('tbody tr').filter({hasText: clientId}); + await Promise.all([page.waitForURL(base + '/oauth-grants'), grant.getByRole('button', {name: 'Revoke access', exact: true}).click()]); + assert.equal((await call(base, '/dav/missing', {headers: {Authorization: 'Bearer ' + current.access_token}})).status, 401); + const revokedRefresh = await call(base, '/oauth2/token', {method: 'POST', form: { + grant_type: 'refresh_token', client_id: clientId, refresh_token: current.refresh_token + }}); + assert.equal(revokedRefresh.status, 400); + assert.equal(json(revokedRefresh).error, 'invalid_grant'); + await page.goto(base + '/'); + console.log('BROWSER_S256_CONSENT_TOKEN_REFRESH_AND_GRANT_REVOCATION_PASSED'); + } finally { + clearTimeout(deadline); + callback.closeAllConnections(); + await new Promise(resolve => callback.close(resolve)); + } +} diff --git a/qualification/passkeys/oidc.mjs b/qualification/passkeys/oidc.mjs new file mode 100644 index 0000000..5bdd4e0 --- /dev/null +++ b/qualification/passkeys/oidc.mjs @@ -0,0 +1,106 @@ +import assert from 'node:assert/strict'; +import {readFileSync} from 'node:fs'; + +export function accountLocation(url, base) { + const publicUrl = new URL(base); + const prefix = publicUrl.pathname.replace(/\/$/, ''); + return url.origin === publicUrl.origin && url.pathname === prefix + '/' && !url.hash + && (url.search === '' || (url.searchParams.size === 1 && url.searchParams.get('continue') === '')); +} + +export async function organizationPrimary(page, base, privateValues, requirePasswordForm = false) { + const password = readFileSync(process.env.BUSYSTACK_UPSTREAM_PASSWORD_FILE, 'utf8').trimEnd(); + privateValues.push(password); + await page.goto(base + '/login'); + await page.getByRole('link', {name: "Sign in with your organization's identity provider", exact: true}).click(); + const dashboard = page.getByRole('heading', {name: 'BusyStack account', exact: true}); + let destination; + try { + destination = await Promise.race([ + page.locator('#kc-login').waitFor().then(() => 'provider-password'), + dashboard.waitFor().then(() => 'application') + ]); + } catch (error) { + // No URL query, form values, cookies or tokens enter retained diagnostics. + const location = new URL(page.url()); + const headings = await page.locator('h1, h2').allTextContents(); + console.error('ORGANIZATION_PRIMARY_FAILURE=' + JSON.stringify({ + origin: location.origin, path: location.pathname, + headings: headings.map(value => privateValues.reduce((text, secret) => text.replaceAll(secret, '[redacted]'), value)).slice(0, 8), + requirePasswordForm + })); + throw error; + } + if (requirePasswordForm) assert.equal(destination, 'provider-password', 'The first organization login must exercise real upstream password authentication'); + if (destination === 'provider-password') { + assert.equal(new URL(page.url()).hostname, 'idp.identity.test'); + const username = page.locator('#username'); + if (await username.count()) { + if (await username.isEditable()) await username.fill(process.env.BUSYSTACK_UPSTREAM_USERNAME); + else assert.equal(await username.inputValue(), process.env.BUSYSTACK_UPSTREAM_USERNAME); + } + await page.locator('#password').fill(password); + await Promise.all([page.waitForURL(url => accountLocation(url, base)), page.locator('#kc-login').click()]); + } + await page.waitForURL(url => accountLocation(url, base)); + await dashboard.waitFor(); + await page.locator('main > p').first().getByText(process.env.BUSYSTACK_FEDERATED_USERNAME, {exact: true}).waitFor(); + const cookie = (await page.context().cookies()).find(value => value.name === 'JSESSIONID'); + assert.ok(cookie && cookie.secure && cookie.httpOnly && cookie.sameSite === 'Lax', 'Cross-site OIDC callback must retain a protected Lax session'); +} + +export async function signOut(page, base) { + await page.goto(base + '/'); + const prefix = new URL(base).pathname.replace(/\/$/, ''); + await Promise.all([page.waitForURL(url => url.pathname === prefix + '/login'), + page.locator(`form[action="${prefix}/logout"]`).getByRole('button', {name: 'Sign out', exact: true}).click()]); +} + +export async function primaryCannotMutate(page, base) { + const prefix = new URL(base).pathname.replace(/\/$/, ''); + const status = await page.evaluate(async target => { + const csrf = document.querySelector('input[name=_csrf]').value; + return (await fetch(target, {method: 'POST', redirect: 'manual', headers: {'Content-Type': 'application/x-www-form-urlencoded'}, + body: new URLSearchParams({_csrf: csrf, name: 'must fail', component: 'VTODO'})})).status; + }, prefix + '/collections'); + assert.equal(status, 403, 'Organization login alone must not authorize a protected mutation'); +} + +export async function newFederatedUser(browser, base, privateValues) { + const context = await browser.newContext(); + const page = await context.newPage(); + const cdp = await context.newCDPSession(page); + await cdp.send('WebAuthn.enable'); + const authenticator = await cdp.send('WebAuthn.addVirtualAuthenticator', {options: {protocol: 'ctap2', transport: 'internal', + hasResidentKey: true, hasUserVerification: true, isUserVerified: true, automaticPresenceSimulation: true}}); + await organizationPrimary(page, base, privateValues, true); + await primaryCannotMutate(page, base); + await page.goto(base + '/security/passkeys'); + await page.getByRole('button', {name: 'Register passkey', exact: true}).click(); + await page.locator('#security-status').filter({hasText: 'Passkey registered.'}).waitFor(); + await Promise.all([page.waitForURL(base + '/'), page.getByRole('button', {name: 'Verify passkey to complete MFA', exact: true}).click()]); + const prefix = new URL(base).pathname.replace(/\/$/, ''); + const form = page.locator(`form[action="${prefix}/collections"]`); + await form.locator('input[name=name]').fill('Federated browser-qualified tasks'); + await form.locator('select[name=component]').selectOption('VTODO'); + await Promise.all([page.waitForURL(base + '/'), form.getByRole('button', {name: 'Create', exact: true}).click()]); + const collection = page.locator('li').filter({has: page.getByText('Federated browser-qualified tasks', {exact: true})}); + assert.ok((await collection.innerText()).includes('VTODO')); + assert.equal(await page.getByText('Browser-qualified tasks', {exact: true}).count(), 0, 'An organization user must not discover the local administrator collection'); + const forbidden = await page.goto(base + '/admin'); + assert.equal(forbidden.status(), 403, 'Upstream realm roles must not grant application administration'); + await page.goto(base + '/'); + await signOut(page, base); + await page.locator('input[name=username]').fill(process.env.BUSYSTACK_FEDERATED_USERNAME); + const upstreamPassword = readFileSync(process.env.BUSYSTACK_UPSTREAM_PASSWORD_FILE, 'utf8').trimEnd(); + await page.locator('input[name=password]').fill(upstreamPassword); + await Promise.all([page.waitForURL(url => url.pathname === prefix + '/login' && url.searchParams.has('error')), + page.getByRole('button', {name: 'Verify password', exact: true}).click()]); + await organizationPrimary(page, base, privateValues); + await primaryCannotMutate(page, base); + await page.goto(base + '/login'); + await Promise.all([page.waitForURL(base + '/'), page.getByRole('button', {name: 'Verify passkey', exact: true}).click()]); + console.log('FEDERATED_LOCAL_PASSWORD_FALLBACK_DENIED'); + console.log('CROSS_SITE_ORGANIZATION_PLUS_PASSKEY_AND_NONADMIN_PROVISIONING_PASSED'); + return {context, page, cdp, authenticatorId: authenticator.authenticatorId}; +} diff --git a/qualification/passkeys/oidc.py b/qualification/passkeys/oidc.py new file mode 100644 index 0000000..5ecb014 --- /dev/null +++ b/qualification/passkeys/oidc.py @@ -0,0 +1,97 @@ +"""Optional real Keycloak fixture with distinct browser sites and scoped CA trust.""" + +import json +import os +from pathlib import Path +import secrets +import socket + + +IMAGE = "busystack-keycloak-browser:26.8.1" +REALM = "browser-qualification" +USERNAME = "realm-browser" +INTERNAL_USERNAME = "federated-browser" +SUBJECT = "d0880a2f-bb8e-4f40-84dd-f0707fe08c77" + + +def configure(root, directory, base, execute, environment): + with socket.socket() as reservation: + reservation.bind(("127.0.0.1", 0)) + port = reservation.getsockname()[1] + issuer = f"https://idp.identity.test:{port}/realms/{REALM}" + secret = secrets.token_urlsafe(32) + upstream_password = secrets.token_urlsafe(32) + secret_file = directory / "upstream-client-secret" + secret_file.write_text(secret) + secret_file.chmod(0o444) + (directory / "upstream-password").write_text(upstream_password) + (directory / "upstream-password").chmod(0o600) + realm = { + "realm": REALM, "enabled": True, "sslRequired": "all", "registrationAllowed": False, + "roles": {"realm": [{"name": "realm-admin"}]}, + "clients": [{"clientId": "busystack-browser-upstream", "enabled": True, "protocol": "openid-connect", + "publicClient": False, "secret": secret, "standardFlowEnabled": True, + "directAccessGrantsEnabled": False, "implicitFlowEnabled": False, + "serviceAccountsEnabled": False, "redirectUris": [base + "/login/oauth2/code/upstream"], + "attributes": {"pkce.code.challenge.method": "S256"}}], + "users": [{"id": SUBJECT, "username": USERNAME, "enabled": True, "realmRoles": ["realm-admin"], + "email": "realm-browser@example.test", "emailVerified": True, + "firstName": "Federated", "lastName": "Browser", "requiredActions": [], + "credentials": [{"type": "password", "value": upstream_password, "temporary": False}]}] + } + realm_file = directory / "realm.json" + realm_file.write_text(json.dumps(realm)) + realm_file.chmod(0o444) + public_trust = directory / "public-trust" + public_trust.mkdir(mode=0o700) + execute([ + "docker", "run", "--rm", "--user", f"{os.getuid()}:{os.getgid()}", + "--volume", f"{directory / 'ca.pem'}:/input/ca.pem:ro", + "--volume", f"{directory / 'server-password'}:/input/store-password:ro", + "--volume", f"{public_trust}:/output", "--entrypoint", "keytool", + "eclipse-temurin:25-jdk@sha256:3ab7fc8d0b4ade8fef96903fb36767c1dbb4fd1535dcfad85b59c17afbbb5857", + "-importcert", "-noprompt", "-alias", "fixture-ca", "-file", "/input/ca.pem", + "-keystore", "/output/trust.p12", "-storetype", "PKCS12", "-storepass:file", "/input/store-password" + ], environment) + store = public_trust / "trust.p12" + store.chmod(0o444) + # The JVM launcher prints only the @argfile path, not this generated password. + arguments = directory / "client-trust.args" + arguments.write_text( + "-Djavax.net.ssl.trustStore=/tls/client-trust.p12\n" + "-Djavax.net.ssl.trustStoreType=PKCS12\n" + "-Djavax.net.ssl.trustStorePassword=" + (directory / "server-password").read_text().strip() + "\n" + ) + arguments.chmod(0o444) + configuration = { + "services": { + "application": { + "environment": { + "SPRING_APPLICATION_JSON": json.dumps({"busystack": {"oidc": { + "enabled": True, "issuer": issuer, "client-id": "busystack-browser-upstream", + "allowed-users": [{"subject": SUBJECT, "username": INTERNAL_USERNAME, "display-name": "Federated Browser"}] + }}}), + "JDK_JAVA_OPTIONS": "@/tls/client-trust.args", + }, + "volumes": [str(store) + ":/tls/client-trust.p12:ro", str(arguments) + ":/tls/client-trust.args:ro"], + "secrets": [{"source": "upstream_client_secret", "target": "busystack.oidc.client-secret"}], + }, + "upstream": { + "image": IMAGE, + "command": ["start-dev", "--import-realm", "--hostname=https://idp.identity.test:" + str(port), + "--https-port=" + str(port), "--http-enabled=false", + "--https-certificate-file=/tls/server.pem", "--https-certificate-key-file=/tls/server.key"], + "environment": {"JAVA_OPTS_KC_HEAP": "-Xms128m -Xmx512m"}, + "ports": [f"127.0.0.1:{port}:{port}"], + "volumes": [str(realm_file) + ":/opt/keycloak/data/import/browser-qualification.json:ro", + str(directory / "server.pem") + ":/tls/server.pem:ro", + str(directory / "server.key") + ":/tls/server.key:ro"], + "networks": {"default": {"aliases": ["idp.identity.test"]}}, + "mem_limit": "1g", "cap_drop": ["ALL"], "security_opt": ["no-new-privileges:true"], + }, + }, + "secrets": {"upstream_client_secret": {"file": str(secret_file)}}, + } + override = directory / "oidc.override.json" + override.write_text(json.dumps(configuration)) + return override, issuer diff --git a/qualification/passkeys/package-lock.json b/qualification/passkeys/package-lock.json new file mode 100644 index 0000000..446d3a4 --- /dev/null +++ b/qualification/passkeys/package-lock.json @@ -0,0 +1,59 @@ +{ + "name": "busystack-passkey-qualification", + "version": "0.0.1", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "busystack-passkey-qualification", + "version": "0.0.1", + "dependencies": { + "playwright": "1.61.1" + } + }, + "node_modules/fsevents": { + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz", + "integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==", + "hasInstallScript": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^8.16.0 || ^10.6.0 || >=11.0.0" + } + }, + "node_modules/playwright": { + "version": "1.61.1", + "resolved": "https://registry.npmjs.org/playwright/-/playwright-1.61.1.tgz", + "integrity": "sha512-DWnY5o3YbLWK4GovuAVwpqL+1VwGNdUGrRr++8j8PtQQzvAVZUIMjKQ90fY689sEJZJBbZVw1rXaOKSTitkzPQ==", + "license": "Apache-2.0", + "dependencies": { + "playwright-core": "1.61.1" + }, + "bin": { + "playwright": "cli.js" + }, + "engines": { + "node": ">=18" + }, + "optionalDependencies": { + "fsevents": "2.3.2" + } + }, + "node_modules/playwright-core": { + "version": "1.61.1", + "resolved": "https://registry.npmjs.org/playwright-core/-/playwright-core-1.61.1.tgz", + "integrity": "sha512-h7Qlt6m4REp25qvIdvbDtVmD4LqVXfpRxhORv9L0jzETM05p4fuPJ3dKyuSXQxDSbXnmS79HAgi9589lGSpLkg==", + "license": "Apache-2.0", + "bin": { + "playwright-core": "cli.js" + }, + "engines": { + "node": ">=18" + } + } + } +} diff --git a/qualification/passkeys/package.json b/qualification/passkeys/package.json new file mode 100644 index 0000000..d471cad --- /dev/null +++ b/qualification/passkeys/package.json @@ -0,0 +1,7 @@ +{ + "name": "busystack-passkey-qualification", + "private": true, + "version": "0.0.1", + "description": "Disposable browser qualification; no production runtime dependency", + "dependencies": {"playwright": "1.61.1"} +} diff --git a/qualification/passkeys/packaged_clients.py b/qualification/passkeys/packaged_clients.py new file mode 100644 index 0000000..e6b3a1f --- /dev/null +++ b/qualification/passkeys/packaged_clients.py @@ -0,0 +1,45 @@ +"""Run the existing small correctness profiles against the browser-issued packaged credential.""" + +import json +import os +from pathlib import Path +import subprocess + + +def verify(checkpoint, configuration): + secret = checkpoint.directory / "packaged-client-secret" + secret.write_text(checkpoint.saved["credential"]) + secret.chmod(0o600) + environment = os.environ.copy() + environment.update( + BUSYSTACK_CLIENT_URL=checkpoint.base + "/dav/", + BUSYSTACK_CLIENT_USER=checkpoint.saved["username"], + BUSYSTACK_CLIENT_SECRET_FILE=str(secret), + BUSYSTACK_CLIENT_CA=str(checkpoint.ca), + BUSYSTACK_CLIENT_SYNC_DEPTH_INPUT="setting omitted; packaged application default", + BUSYSTACK_THUNDERBIRD_BINARY=configuration["thunderbird"], + ) + results = [] + try: + for script, deadline in (("python_caldav.py", 180), ("thunderbird.py", 360)): + result = subprocess.run( + [configuration["python"], "qualification/clients/" + script], + cwd=checkpoint.root, env=environment, capture_output=True, text=True, timeout=deadline, + ) + if result.returncode: + private = [checkpoint.saved["credential"], checkpoint.saved["encoded"], + checkpoint.saved["grant"]["accessToken"], checkpoint.saved["grant"]["refreshToken"]] + message = checkpoint.operations.sanitized_diagnostic(result.stdout + result.stderr, private) + raise RuntimeError("Packaged independent client " + script + " failed: " + message) + value = json.loads(result.stdout.strip().splitlines()[-1]) + assert value["status"] == "passed", script + results.append(value) + print("PACKAGED_INDEPENDENT_CLIENT=" + json.dumps(value, sort_keys=True), flush=True) + finally: + secret.unlink(missing_ok=True) + output = checkpoint.root / "target/qualification/standards-corrections-packaged-clients.json" + output.parent.mkdir(parents=True, exist_ok=True) + output.write_text(json.dumps({"applicationImage": configuration["image"], + "sourceRevision": subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=checkpoint.root, text=True).strip(), + "profile": "actual packaged HTTPS server, default sync compatibility; small correctness profiles without performance workload", + "clients": results}, indent=2) + "\n") diff --git a/qualification/passkeys/passkeys.mjs b/qualification/passkeys/passkeys.mjs new file mode 100644 index 0000000..68b4aa2 --- /dev/null +++ b/qualification/passkeys/passkeys.mjs @@ -0,0 +1,131 @@ +import { chromium } from 'playwright'; +import assert from 'node:assert/strict'; +import { createInterface } from 'node:readline'; +import { readFileSync } from 'node:fs'; +import { qualifyOAuth } from './oauth.mjs'; +import { qualifyDavCredentials } from './dav-credentials.mjs'; +import {qualifyAccountRecovery} from './account-recovery.mjs'; +import {qualifyGroups} from './groups.mjs'; +import {qualifyRestoration} from './recovery.mjs'; +import { newFederatedUser, organizationPrimary, primaryCannotMutate, signOut } from './oidc.mjs'; + +const base = process.env.BUSYSTACK_BROWSER_URL; +assert.ok(base && new URL(base).protocol === 'https:' && ['localhost', 'app.busystack.test'].includes(new URL(base).hostname), 'Browser fixture must use isolated test-site HTTPS'); +const password = readFileSync(process.env.BUSYSTACK_BROWSER_PASSWORD_FILE, 'utf8').trimEnd(); +const privateValues = [password]; +const prefix = new URL(base).pathname.replace(/\/$/, ''); +const input = createInterface({input: process.stdin}); +const lines = input[Symbol.asyncIterator](); +async function waitFor(expected) { + const result = await lines.next(); + assert.ok(!result.done && result.value === expected, `Expected harness signal ${expected}`); +} +const browser = await chromium.launch({headless: true}); +console.log('PLAYWRIGHT_VERSION=1.61.1'); +console.log('CHROMIUM_VERSION=' + browser.version()); +try { + let context = await browser.newContext(); + let page = await context.newPage(); + const cdp = await context.newCDPSession(page); + await cdp.send('WebAuthn.enable'); + const localAuthenticator = await cdp.send('WebAuthn.addVirtualAuthenticator', {options: {protocol: 'ctap2', transport: 'internal', + hasResidentKey: true, hasUserVerification: true, isUserVerified: true, automaticPresenceSimulation: true}}); + await page.goto(base + '/login'); + assert.equal(await page.locator('meta[name=csrf-token]').count(), 1); + await page.locator('input[name=username]').fill(process.env.BUSYSTACK_BROWSER_USERNAME); + await page.locator('input[name=password]').fill(password); + await Promise.all([page.waitForURL(base + '/'), page.getByRole('button', {name: 'Verify password', exact: true}).click()]); + const passwordSession = (await context.cookies()).find(cookie => cookie.name === 'JSESSIONID')?.value; + assert.ok(passwordSession); + privateValues.push(passwordSession); + const passwordOnly = await page.evaluate(async path => { + const csrf = document.querySelector('input[name=_csrf]').value; + const response = await fetch(path, {method: 'POST', redirect: 'manual', + headers: {'Content-Type': 'application/x-www-form-urlencoded'}, + body: new URLSearchParams({_csrf: csrf, name: 'must fail', component: 'VTODO'})}); + return response.status; + }, prefix + '/collections'); + assert.equal(passwordOnly, 403, 'Password alone must not authorize account mutations'); + await page.goto(base + '/security/passkeys'); + await page.getByRole('button', {name: 'Register passkey', exact: true}).click(); + await page.locator('#security-status').filter({hasText: 'Passkey registered.'}).waitFor(); + await Promise.all([page.waitForURL(base + '/'), page.getByRole('button', {name: 'Verify passkey to complete MFA', exact: true}).click()]); + const mfaSession = (await context.cookies()).find(cookie => cookie.name === 'JSESSIONID')?.value; + if (mfaSession) privateValues.push(mfaSession); + assert.ok(mfaSession && mfaSession !== passwordSession, 'Passkey authentication must rotate session ID'); + const collectionForm = page.locator(`form[action="${prefix}/collections"]`); + await collectionForm.locator('input[name=name]').fill('Browser-qualified tasks'); + await collectionForm.locator('select[name=component]').selectOption('VTODO'); + await Promise.all([page.waitForURL(base + '/'), collectionForm.locator('button').click()]); + const createdCollection = page.locator('li').filter({has: page.getByText('Browser-qualified tasks', {exact: true})}); + await createdCollection.waitFor(); + assert.ok((await createdCollection.innerText()).includes('VTODO'), 'The created collection must actually advertise VTODO'); + console.log('PASSWORD_PLUS_WEBAUTHN_AND_MUTATION_PASSED'); + if (process.env.BUSYSTACK_BROWSER_GROUPS === 'true') await qualifyGroups(page, base, privateValues); + const administratorPage = page; + let activeCdp = cdp; + let activeAuthenticatorId = localAuthenticator.authenticatorId; + let currentUsername = process.env.BUSYSTACK_BROWSER_USERNAME; + let collectionName = 'Browser-qualified tasks'; + if (process.env.BUSYSTACK_BROWSER_OIDC === 'true') { + await cdp.send('WebAuthn.setAutomaticPresenceSimulation', {authenticatorId: localAuthenticator.authenticatorId, enabled: false}); + const federated = await newFederatedUser(browser, base, privateValues); + context = federated.context; + page = federated.page; + activeCdp = federated.cdp; + activeAuthenticatorId = federated.authenticatorId; + currentUsername = process.env.BUSYSTACK_FEDERATED_USERNAME; + collectionName = 'Federated browser-qualified tasks'; + } + await qualifyAccountRecovery({page, base, privateValues, administratorPage, username: currentUsername, + cdp: activeCdp, authenticatorId: activeAuthenticatorId}); + await qualifyDavCredentials(page, base, privateValues, currentUsername); + await qualifyOAuth(page, base, privateValues, administratorPage); + if (process.env.BUSYSTACK_BROWSER_RESTORE === 'true') { + await qualifyRestoration({page, base, privateValues, administratorPage, username: currentUsername, + collectionName, cdp: activeCdp, authenticatorId: activeAuthenticatorId, waitFor}); + } + const persistentCookie = (await context.cookies()).find(value => value.name === 'JSESSIONID')?.value; + if (persistentCookie) privateValues.push(persistentCookie); + console.log('READY_RESTART'); + await waitFor('RESTARTED'); + await page.goto(base + '/'); + await page.getByRole('heading', {name: 'BusyStack account', exact: true}).waitFor(); + assert.equal((await context.cookies()).find(value => value.name === 'JSESSIONID')?.value, persistentCookie, + 'A persisted browser session must survive the application restart'); + console.log('PERSISTED_BROWSER_SESSION_AFTER_RESTART_PASSED'); + if (process.env.BUSYSTACK_BROWSER_OIDC === 'true') { + await signOut(page, base); + await organizationPrimary(page, base, privateValues); + await primaryCannotMutate(page, base); + } else { + await page.goto(base + '/login'); + await page.locator('input[name=username]').fill(process.env.BUSYSTACK_BROWSER_USERNAME); + const currentPassword = process.env.BUSYSTACK_BROWSER_RESTORE === 'true' ? readFileSync(process.env.BUSYSTACK_BROWSER_RECOVERY_PASSWORD_FILE, 'utf8').trimEnd() : password; + privateValues.push(currentPassword); + await page.locator('input[name=password]').fill(currentPassword); + await Promise.all([page.waitForURL(base + '/'), page.getByRole('button', {name: 'Verify password', exact: true}).click()]); + } + await page.goto(base + '/login'); + await Promise.all([page.waitForURL(base + '/'), page.getByRole('button', {name: 'Verify passkey', exact: true}).click()]); + await page.locator('li').getByText(collectionName, {exact: true}).waitFor(); + console.log('PERSISTED_PASSKEY_AFTER_RESTART_PASSED'); + console.log('READY_DISABLE'); + await waitFor('DISABLED'); + const disabled = await page.goto(base + '/'); + assert.equal(disabled.status(), 403); + // Existing sessions and independent passkey authentication must both reject disabled users. + await page.goto(base + '/login'); + await page.getByRole('button', {name: 'Verify passkey', exact: true}).click(); + await page.locator('#security-status').filter({hasText: '(401)'}).waitFor(); + console.log('DISABLED_SESSION_AND_PASSKEY_PASSED'); +} catch (error) { + let diagnostic = String(error.stack || error); + for (const value of privateValues) diagnostic = diagnostic.replaceAll(value, '[redacted]'); + diagnostic = diagnostic.replace(/([?&](?:code|state|code_challenge)=)[^&\s)"]+/g, '$1[redacted]'); + console.error(diagnostic); + process.exitCode = 1; +} finally { + input.close(); + await browser.close(); +} diff --git a/qualification/passkeys/proxy.py b/qualification/passkeys/proxy.py new file mode 100644 index 0000000..0e8ade4 --- /dev/null +++ b/qualification/passkeys/proxy.py @@ -0,0 +1,94 @@ +"""Optional isolated HTTPS reverse proxy for the same real-browser qualification.""" + +import http.client +import json +from pathlib import Path +import ssl +import urllib.error +import urllib.parse +import urllib.request +import tls + + +IMAGE = "nginx:1.30.6-alpine@sha256:47928bf67eb62a1863cb08d9ecb52d3e704304ba614eaee17884204677702951" + + +def override(root, directory, application_image, base, port, template=None): + public = urllib.parse.urlsplit(base) + assert public.path == "/team" + # A host-private directory protects the bind-mounted fixture key from other host users. + (directory / "server.key").chmod(0o444) + if template is None: + template = (root / "qualification/proxy/nginx.conf.template").read_text() + config = template.replace("@@PREFIX@@", public.path).replace("@@ORIGIN@@", f"{public.scheme}://{public.netloc}").replace("@@AUTHORITY@@", public.netloc).replace("@@HOSTNAME@@", public.hostname) + (directory / "nginx.conf").write_text(config) + return ( + "services:\n application:\n" + f" image: {json.dumps(application_image)}\n" + " ports: !override []\n" + " environment:\n SERVER_PORT: '8080'\n SERVER_SSL_ENABLED: 'false'\n" + " reverse-proxy:\n" + f" image: {IMAGE}\n" + " user: '101:101'\n" + " entrypoint: ['nginx', '-g', 'daemon off;']\n" + " depends_on:\n application:\n condition: service_healthy\n" + f" ports:\n - '127.0.0.1:{port}:8443'\n" + " volumes:\n" + f" - {json.dumps(str(directory / 'nginx.conf') + ':/etc/nginx/nginx.conf:ro')}\n" + f" - {json.dumps(str(directory / 'server.pem') + ':/tls/server.pem:ro')}\n" + f" - {json.dumps(str(directory / 'server.key') + ':/tls/server.key:ro')}\n" + " read_only: true\n tmpfs:\n - /tmp:rw,nosuid,nodev,noexec,size=32m\n" + " mem_limit: 128m\n cap_drop: [ALL]\n security_opt: ['no-new-privileges:true']\n" + ) + + +class NoRedirect(urllib.request.HTTPRedirectHandler): + def redirect_request(self, req, fp, code, msg, headers, newurl): + return None + + +def verify(base, ca): + public = urllib.parse.urlsplit(base) + origin = f"{public.scheme}://{public.netloc}" + context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) + context.load_verify_locations(cafile=ca) + opener = tls.opener(ca, NoRedirect) + + def call(url, method="GET", headers=None, data=None): + try: + response = opener.open(urllib.request.Request(url, method=method, headers=headers or {}, data=data), timeout=15) + except urllib.error.HTTPError as failure: + response = failure + with response: + return response.status, response.headers, response.read(65536) + + status, headers, _ = call(origin + "/.well-known/caldav") + assert status == 308 and headers["Location"] == base + "/dav/" + forged = {"Host": "attacker.invalid", "Forwarded": "host=attacker.invalid;proto=http", "X-Forwarded-Host": "attacker.invalid", "X-Forwarded-Proto": "http"} + status, _, body = call(origin + "/.well-known/oauth-authorization-server" + public.path, headers=forged) + assert status == 200 and json.loads(body)["issuer"] == base + status, headers, _ = call(base, headers=forged) + assert status == 308 and headers["Location"] == base + "/" + status, headers, _ = call(base + "/security/passkeys", headers={**forged, "Accept": "text/html"}) + target = urllib.parse.urlsplit(headers.get("Location", "")) + assert status == 302 and (target.scheme, target.netloc, target.path) == (public.scheme, public.netloc, public.path + "/login"), f"Expected configured browser login redirect; status={status}, location={headers.get('Location')}" + assert urllib.parse.parse_qs(target.query) in ({}, {"factor": ["required"]}) and not target.fragment + for path in ("/actuator", "/actuator/health", public.path + "/actuator", public.path + "/actuator/health/readiness"): + assert call(origin + path)[0] == 404 + for method in ("OPTIONS", "PROPFIND", "PROPPATCH", "REPORT", "MKCALENDAR", "MKCOL", "GET", "HEAD", "PUT", "DELETE", "COPY", "MOVE", "ACL"): + status, headers, _ = call(base + "/dav/", method=method) + assert status == 401 and "Basic" in headers.get("WWW-Authenticate", ""), method + assert call(base + "/dav/", method="LOCK")[0] in (400, 405, 501) + assert call(base + "/dav/", method="PUT", data=b"x" * (1048576 + 1))[0] == 413 + connection = tls.Connection(public.hostname, public.port, context=context, timeout=15) + try: + try: + connection.request("PUT", public.path + "/dav/", body=(b"x" * 65536 for _ in range(17)), headers={"Content-Type": "application/xml"}, encode_chunked=True) + except BrokenPipeError: + pass # The server may reject an oversized stream before the client finishes writing it. + response = connection.getresponse() + assert response.status == 413 + response.read() + finally: + connection.close() + print("PROXY_TEAM_DISCOVERY_METADATA_PRIVACY_METHODS_AND_BODY_LIMITS_PASSED", flush=True) diff --git a/qualification/passkeys/recovery.mjs b/qualification/passkeys/recovery.mjs new file mode 100644 index 0000000..1249a93 --- /dev/null +++ b/qualification/passkeys/recovery.mjs @@ -0,0 +1,177 @@ +import assert from 'node:assert/strict'; +import {readFileSync, writeFileSync} from 'node:fs'; +import {randomBytes} from 'node:crypto'; +import {call} from './transport.mjs'; +import {issueDavCredential} from './dav-credentials.mjs'; +import {qualifySharingCapacity} from './groups.mjs'; +import {qualifyOAuth} from './oauth.mjs'; +import {accountLocation, organizationPrimary, primaryCannotMutate, signOut} from './oidc.mjs'; + +export async function qualifyRestoration({page, base, privateValues, administratorPage, username, + collectionName, cdp, authenticatorId, waitFor}) { + const exchange = process.env.BUSYSTACK_BROWSER_RECOVERY_FILE; + const prefix = new URL(base).pathname.replace(/\/$/, ''); + const federated = process.env.BUSYSTACK_BROWSER_OIDC === 'true'; + await page.goto(base + '/'); + const collection = await page.locator('li').filter({has: page.getByText(collectionName, {exact: true})}).locator('code').innerText(); + const readerUsername = 'restorereader'; + const writerUsername = 'restorewriter'; + for (const member of [readerUsername, writerUsername]) { + await administratorPage.goto(base + '/admin'); + const provision = administratorPage.locator(`form[action="${prefix}/admin/accounts"]`); + const initialPassword = randomBytes(32).toString('base64url'); + privateValues.push(initialPassword); + await provision.locator('input[name=username]').fill(member); + await provision.locator('input[name=displayName]').fill('Restore ' + member); + await provision.locator('input[name=password]').fill(initialPassword); + await Promise.all([administratorPage.waitForURL(base + '/admin'), + provision.getByRole('button', {name: 'Create account', exact: true}).click()]); + await administratorPage.getByText(member, {exact: true}).waitFor(); + } + await page.goto(base + '/'); + for (const [member, permission] of [[readerUsername, 'READ'], [writerUsername, 'WRITE']]) { + const share = page.locator(`form[action="${prefix}/shares"]`).filter({has: page.locator('select[name=collection]')}); + await share.locator('select[name=collection]').selectOption(collection); + await share.locator('input[name=username]').fill(member); + await share.locator('select[name=permission]').selectOption(permission); + await Promise.all([page.waitForURL(base + '/'), share.getByRole('button', {name: 'Save access', exact: true}).click()]); + const row = page.locator('tbody tr').filter({has: page.getByText(member, {exact: true})}); + await row.getByRole('button', {name: 'Remove individual access', exact: true}).waitFor(); + } + const credential = await issueDavCredential(page, base, privateValues, 'Recovery snapshot device', username); + const grant = await qualifyOAuth(page, base, privateValues, administratorPage, + {clientId: 'browser-native-restore-qualification', clientName: 'Browser Native Restore', retainGrant: true}); + const oldCookie = (await page.context().cookies()).find(value => value.name === 'JSESSIONID')?.value; + if (oldCookie) privateValues.push(oldCookie); + assert.ok(oldCookie, 'A live authenticated snapshot session is required'); + const initial = {username, collection, readerUsername, writerUsername, credential: credential.credential, encoded: credential.encoded, grant, snapshotCookie: oldCookie}; + writeFileSync(exchange, JSON.stringify(initial), {mode: 0o600}); + if (process.env.BUSYSTACK_BROWSER_CAPACITY === 'true') { + console.log('READY_CAPACITY_SHARING'); + await waitFor('CAPACITY_SHARING_SEEDED'); + const state = JSON.parse(readFileSync(exchange, 'utf8')); + state.sharingCapacity = await qualifySharingCapacity(page, administratorPage, base, state.sharingCapacity); + writeFileSync(exchange, JSON.stringify(state), {mode: 0o600}); + console.log('READY_CAPACITY_SHARING_CLEANUP'); + await waitFor('CAPACITY_SHARING_CLEANED'); + } + console.log('READY_RESTORE_BACKUP'); + await waitFor('BACKED_UP'); + const backedUp = JSON.parse(readFileSync(exchange, 'utf8')); + assert.equal(backedUp.snapshotCookie, oldCookie); + assert.equal(backedUp.grant.clientId, grant.clientId); + privateValues.push(backedUp.grant.accessToken, backedUp.grant.refreshToken); + Object.assign(grant, backedUp.grant); + // A long measured workload must not bypass the normal recent password/organization + passkey requirement. + await signOut(page, base); + if (federated) { + await page.context().clearCookies({domain: /(^|\.)idp\.identity\.test$/}); + await organizationPrimary(page, base, privateValues, true); + } else { + const primaryPassword = readFileSync(process.env.BUSYSTACK_BROWSER_PASSWORD_FILE, 'utf8').trimEnd(); + privateValues.push(primaryPassword); + await page.locator('input[name=username]').fill(username); + await page.locator('input[name=password]').fill(primaryPassword); + await Promise.all([page.waitForURL(url => accountLocation(url, base)), + page.getByRole('button', {name: 'Verify password', exact: true}).click()]); + } + await page.goto(base + '/login'); + await Promise.all([page.waitForURL(base + '/'), page.getByRole('button', {name: 'Verify passkey', exact: true}).click()]); + assert.notEqual((await page.context().cookies()).find(value => value.name === 'JSESSIONID')?.value, oldCookie); + console.log('HTTPS_RECOVERY_RECENT_PRIMARY_AND_WEBAUTHN_REAUTHENTICATION_PASSED'); + await page.goto(base + '/'); + const device = page.locator('li').filter({hasText: 'Recovery snapshot device'}); + await Promise.all([page.waitForURL(base + '/'), device.getByRole('button', {name: 'Revoke', exact: true}).click()]); + await page.goto(base + '/oauth-grants'); + const authorization = page.locator('tbody tr').filter({hasText: grant.clientId}); + await Promise.all([page.waitForURL(base + '/oauth-grants'), authorization.getByRole('button', {name: 'Revoke access', exact: true}).click()]); + assert.equal((await call(base, '/dav/missing', {headers: {Authorization: 'Basic ' + credential.encoded}})).status, 401); + assert.equal((await call(base, '/dav/missing', {headers: {Authorization: 'Bearer ' + grant.accessToken}})).status, 401); + console.log('READY_RESTORE_APPLY'); + await waitFor('RESTORED'); + // Replay the exact cookie proven live at backup, separately from the later reauthenticated browser. + const replayedSession = await call(base, '/', {headers: {Cookie: 'JSESSIONID=' + oldCookie}}); + assert.ok([302, 303, 401, 403].includes(replayedSession.status), 'The snapshot session must not revive after restore'); + console.log('HTTPS_RECOVERY_SNAPSHOT_SESSION_COOKIE_REPLAY_REJECTED'); + // Restored session rows and factors cannot revive a browser which authenticated before backup. + await page.goto(base + '/'); + await page.waitForURL(url => url.pathname === prefix + '/login'); + assert.notEqual((await page.context().cookies()).find(value => value.name === 'JSESSIONID')?.value, oldCookie); + await page.getByRole('button', {name: 'Verify passkey', exact: true}).click(); + await page.locator('#security-status').filter({hasText: '(401)'}).waitFor(); + await cdp.send('WebAuthn.clearCredentials', {authenticatorId}); + await page.goto(base + '/login'); + const oldPasswordFile = federated ? process.env.BUSYSTACK_UPSTREAM_PASSWORD_FILE : process.env.BUSYSTACK_BROWSER_PASSWORD_FILE; + const oldPassword = readFileSync(oldPasswordFile, 'utf8').trimEnd(); + privateValues.push(oldPassword); + await page.locator('input[name=username]').fill(username); + await page.locator('input[name=password]').fill(oldPassword); + await Promise.all([page.waitForURL(url => url.pathname === prefix + '/login' && url.searchParams.has('error')), + page.getByRole('button', {name: 'Verify password', exact: true}).click()]); + if (federated) { + await page.context().clearCookies({domain: /(^|\.)idp\.identity\.test$/}); + await organizationPrimary(page, base, privateValues, true); + } else { + const password = readFileSync(process.env.BUSYSTACK_BROWSER_RECOVERY_PASSWORD_FILE, 'utf8').trimEnd(); + privateValues.push(password); + await page.locator('input[name=username]').fill(username); + await page.locator('input[name=password]').fill(password); + await Promise.all([page.waitForURL(url => accountLocation(url, base)), + page.getByRole('button', {name: 'Verify password', exact: true}).click()]); + await page.getByRole('heading', {name: 'BusyStack account', exact: true}).waitFor(); + } + await primaryCannotMutate(page, base); + await page.goto(base + '/security/passkeys'); + await page.getByRole('button', {name: 'Register passkey', exact: true}).click(); + await page.locator('#security-status').filter({hasText: 'Passkey registered.'}).waitFor(); + await Promise.all([page.waitForURL(base + '/'), page.getByRole('button', {name: 'Verify passkey to complete MFA', exact: true}).click()]); + const fresh = await issueDavCredential(page, base, privateValues, 'Recovered device', username); + await qualifyOAuth(page, base, privateValues, administratorPage, + {clientId: grant.clientId, clientName: 'Browser Native Restore', existingClient: true}); + // Recover a second principal through its real browser factor enrollment, then enforce its restored READ grant. + const readerContext = await page.context().browser().newContext(); + const readerPage = await readerContext.newPage(); + const readerCdp = await readerContext.newCDPSession(readerPage); + await cdp.send('WebAuthn.setAutomaticPresenceSimulation', {authenticatorId, enabled: false}); + await readerCdp.send('WebAuthn.enable'); + const readerAuthenticator = await readerCdp.send('WebAuthn.addVirtualAuthenticator', {options: { + protocol: 'ctap2', transport: 'internal', hasResidentKey: true, hasUserVerification: true, + isUserVerified: true, automaticPresenceSimulation: true}}); + let readerCredential; + try { + const readerPassword = readFileSync(process.env.BUSYSTACK_BROWSER_READER_PASSWORD_FILE, 'utf8').trimEnd(); + privateValues.push(readerPassword); + await readerPage.goto(base + '/login'); + await readerPage.locator('input[name=username]').fill(readerUsername); + await readerPage.locator('input[name=password]').fill(readerPassword); + await Promise.all([readerPage.waitForURL(base + '/'), readerPage.getByRole('button', {name: 'Verify password', exact: true}).click()]); + await primaryCannotMutate(readerPage, base); + await readerPage.goto(base + '/security/passkeys'); + await readerPage.getByRole('button', {name: 'Register passkey', exact: true}).click(); + await readerPage.locator('#security-status').filter({hasText: 'Passkey registered.'}).waitFor(); + await Promise.all([readerPage.waitForURL(base + '/'), readerPage.getByRole('button', {name: 'Verify passkey to complete MFA', exact: true}).click()]); + await readerPage.locator('li').getByText(collectionName, {exact: true}).waitFor(); + readerCredential = await issueDavCredential(readerPage, base, privateValues, 'Recovered shared read device', readerUsername); + } finally { + await readerCdp.send('WebAuthn.setAutomaticPresenceSimulation', {authenticatorId: readerAuthenticator.authenticatorId, enabled: false}); + await readerContext.close(); + await cdp.send('WebAuthn.setAutomaticPresenceSimulation', {authenticatorId, enabled: true}); + } + writeFileSync(exchange, JSON.stringify({...initial, recoveredEncoded: fresh.encoded, + readerEncoded: readerCredential.encoded}), {mode: 0o600}); + console.log('READY_RESTORE_VERIFY'); + await waitFor('RESTORE_VERIFIED'); + await page.goto(base + '/'); + const readerShares = page.locator('tbody tr').filter({has: page.getByText(readerUsername, {exact: true})}); + const readerShareCount = await readerShares.count(); + const restoredShare = readerShares.filter({has: page.locator(`input[name="collection"][value="${collection}"]`)}); + assert.equal(await restoredShare.count(), 1); + await Promise.all([page.waitForURL(base + '/'), restoredShare.getByRole('button', {name: 'Remove individual access', exact: true}).click()]); + assert.equal(await restoredShare.count(), 0); + assert.equal(await readerShares.count(), readerShareCount - 1); + console.log('READY_RESTORE_REVOKED_READER_VERIFY'); + await waitFor('RESTORE_REVOKED_READER_VERIFIED'); + console.log('HTTPS_RESTORE_SHARED_RECIPIENT_FRESH_MFA_READ_WRITE_DENIAL_AND_REVOCATION_PASSED'); + console.log('HTTPS_RESTORE_OLD_ACCESS_REJECTED_FRESH_MFA_DAV_OAUTH_CONTENT_AND_SYNC_PASSED'); + await page.goto(base + '/'); +} diff --git a/qualification/passkeys/recovery.py b/qualification/passkeys/recovery.py new file mode 100644 index 0000000..a887426 --- /dev/null +++ b/qualification/passkeys/recovery.py @@ -0,0 +1,257 @@ +"""Real HTTPS recovery checkpoint; state and credentials stay in the private fixture directory.""" + +import hashlib +import json +import time +import urllib.error +import urllib.parse +import urllib.request +import xml.etree.ElementTree as XML +from uuid import uuid4 +import tls +import workload_measurement +import audit_corpus +import packaged_clients + + +class NoRedirect(urllib.request.HTTPRedirectHandler): + def redirect_request(self, req, fp, code, msg, headers, newurl): + return None + + +class Checkpoint: + def __init__(self, root, directory, operations, base, oidc, issuer, environments): + self.root, self.directory, self.operations, self.base = root, directory, operations, base + self.oidc, self.issuer, self.environments = oidc, issuer, environments + self.ca = directory / 'ca.pem' + self.exchange = directory / 'recovery.json' + self.attachments = [] + self.workload = None + self.clients = None + self.audit_corpus = audit_corpus.Corpus(self) + self.content = ('BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//BusyStack independent recovery fixture//EN\r\n' + 'BEGIN:VTODO\r\nUID:https-recovery-fixture\r\nDTSTAMP:20261027T000000Z\r\n' + 'SUMMARY:Restored task ✓\r\nSTATUS:IN-PROCESS\r\nPRIORITY:5\r\nPERCENT-COMPLETE:25\r\n' + 'X-RESTORE-PRESERVED;X-ORIGIN=independent:opaque client data\r\nEND:VTODO\r\nEND:VCALENDAR\r\n').encode() + + def request(self, path, method='GET', headers=None, body=None): + request = urllib.request.Request(self.base + path, method=method, headers=headers or {}, data=body) + try: + response = tls.opener(self.ca, NoRedirect).open(request, timeout=20) + except urllib.error.HTTPError as error: + response = error + with response: + data = response.read(1048577) + assert len(data) <= 1048576, 'Recovery response exceeded its limit' + return response.status, response.headers, data + + def report(self, encoded, token=None, path=None, limit=None): + request = XML.Element('{DAV:}sync-collection') + XML.SubElement(request, '{DAV:}sync-token').text = token + XML.SubElement(request, '{DAV:}sync-level').text = '1' + XML.SubElement(XML.SubElement(request, '{DAV:}prop'), '{DAV:}getetag') + if limit is not None: + XML.SubElement(XML.SubElement(request, '{DAV:}limit'), '{DAV:}nresults').text = str(limit) + return self.request(path or self.collection_path, 'REPORT', + {'Authorization': 'Basic ' + encoded, 'Content-Type': 'application/xml', 'Depth': '0'}, XML.tostring(request)) + + def backup(self, environment): + self.saved = json.loads(self.exchange.read_text()) + username = self.saved['username'].replace("'", "''") + self.owner = self.operations.sql(environment, "SELECT id FROM accounts WHERE username='" + username + "';") + self.collection_path = '/dav/calendars/' + self.owner + '/' + self.saved['collection'] + '/' + self.resource_path = self.collection_path + 'restoration-fixture.ics' + basic = {'Authorization': 'Basic ' + self.saved['encoded']} + status, _, _ = self.request(self.resource_path, 'PUT', + {**basic, 'Content-Type': 'text/calendar;charset=utf-8', 'If-None-Match': '*'}, self.content) + assert status == 201, 'Recovery VTODO creation failed' + status, _, data = self.request(self.resource_path, headers=basic) + assert status == 200 and data == self.content + self.store_language(self.saved['encoded']) + status, _, data = self.report(self.saved['encoded']) + assert status == 207 + document = XML.fromstring(data) + self.token = document.findtext('{DAV:}sync-token') + assert self.token and any(value.text and value.text.endswith('/restoration-fixture.ics') for value in document.findall('.//{DAV:}href')) + assert self.request(self.resource_path, headers={'Authorization': 'Bearer ' + self.saved['grant']['accessToken']})[0] == 200 + self.reader = self.operations.sql(environment, "SELECT id FROM accounts WHERE username='" + self.saved['readerUsername'] + "';") + self.writer = self.operations.sql(environment, "SELECT id FROM accounts WHERE username='" + self.saved['writerUsername'] + "';") + self.reader_path = '/dav/calendars/' + self.reader + '/' + self.saved['collection'] + '/restoration-fixture.ics' + self.permissions(environment) + self.audit_corpus.seed(environment) + self.epoch = self.operations.sql(environment, 'SELECT recovery_epoch FROM system_state WHERE id=1;') + if self.clients is not None: + packaged_clients.verify(self, self.clients) + if self.workload is not None: + workload_measurement.observe(self, environment, self.workload['python'], self.workload['output']) + # Use the supported public-client refresh flow after long measurements; never extend token policy. + self.refresh_retained_grant() + # The dump must contain a demonstrably live authenticated session, not an expired cookie. + cookie=self.saved['snapshotCookie'] + status,_,body=self.request('/', headers={'Cookie':'JSESSIONID='+cookie}) + assert status==200 and b'BusyStack account' in body, 'Snapshot browser session must be valid at backup' + print('HTTPS_RECOVERY_LIVE_SNAPSHOT_SESSION_VERIFIED',flush=True) + # Benchmarking must not turn the revoked-credential proof into an accidental expiry test. + assert self.request(self.resource_path, headers=basic)[0] == 200, 'Snapshot DAV credential expired before backup' + assert self.request(self.resource_path, headers={'Authorization': 'Bearer ' + self.saved['grant']['accessToken']})[0] == 200, 'Snapshot access token expired before backup; issue a fresh grant before qualification' + self.archive = self.directory / 'https-recovery.dump' + self.operations.execute(['./scripts/backup.sh', str(self.archive)], environment) + print('HTTPS_RECOVERY_SNAPSHOT_WITH_VALID_DAV_OAUTH_AND_SYNC_CREATED', flush=True) + + def refresh_retained_grant(self): + old=self.saved['grant'] + status,_,body=self.request('/oauth2/token','POST',{'Content-Type':'application/x-www-form-urlencoded'}, + urllib.parse.urlencode({'grant_type':'refresh_token','client_id':old['clientId'], + 'refresh_token':old['refreshToken']}).encode()) + assert status==200, 'Retained public grant must refresh through the real token endpoint' + current=json.loads(body) + assert current.get('token_type','').lower()=='bearer' + assert type(current.get('expires_in')) is int and current['expires_in']>0 + for key,previous in [('access_token','accessToken'),('refresh_token','refreshToken')]: + assert isinstance(current.get(key),str) and len(current[key])>40 and current[key]!=old[previous], 'Expected actual opaque credential rotation' + if 'scope' in current: assert 'dav' in current['scope'].split() + self.saved['grant']={**old,'accessToken':current['access_token'],'refreshToken':current['refresh_token'], + 'accessExpiresAt':int(time.time()*1000)+current['expires_in']*1000} + self.exchange.write_text(json.dumps(self.saved)) + self.exchange.chmod(0o600) + print('HTTPS_RECOVERY_RETAINED_GRANT_REFRESHED_BEFORE_BACKUP',flush=True) + + def restore(self, environment): + source = environment.copy() + self.operations.compose(source, 'stop', 'application', 'reverse-proxy') + restored = source.copy() + restored['COMPOSE_PROJECT_NAME'] += '-restored' + self.environments.append(restored) + self.operations.compose(restored, 'up', '-d', '--wait', '--wait-timeout', '120', 'database') + if self.oidc is not None: + # Keep the existing independent provider running; connect only our fixture container/network. + upstream = self.operations.compose(source, 'ps', '--quiet', 'upstream') + network = restored['COMPOSE_PROJECT_NAME'] + '_default' + metadata = json.loads(self.operations.execute(['docker', 'inspect', upstream], source))[0] + assert metadata['Config']['Labels']['com.docker.compose.project'] == source['COMPOSE_PROJECT_NAME'] + graph = json.loads(self.operations.execute(['docker', 'network', 'inspect', network], source))[0] + assert graph['Labels']['com.docker.compose.project'] == restored['COMPOSE_PROJECT_NAME'] + self.operations.execute(['docker', 'network', 'connect', '--alias', 'idp.identity.test', network, upstream], source) + self.attachments.append((network, upstream)) + self.operations.execute(['./scripts/restore.sh', str(self.archive)], restored) + assert self.operations.sql(restored, 'SELECT count(*) FROM accounts WHERE enabled;') == '0' + for table in ['dav_credentials', 'oauth2_authorization', 'oauth2_authorization_consent', + 'spring_session', 'user_credentials', 'account_recovery_codes', 'webauthn_ceremonies', 'sync_continuations']: + assert self.operations.sql(restored, 'SELECT count(*) FROM ' + table + ';') == '0', table + assert self.operations.sql(restored, 'SELECT recovery_epoch FROM system_state WHERE id=1;') != self.epoch + assert self.operations.sql(restored, "SELECT encode(sha256(content),'hex') FROM calendar_resources WHERE name='restoration-fixture.ics';") == hashlib.sha256(self.content).hexdigest() + assert self.operations.sql(restored, "SELECT owner_id FROM collections WHERE id='" + self.saved['collection'] + "';") == self.owner + self.permissions(restored) + self.audit_corpus.assert_database(restored) + # The fixture identities/grants are known and reviewed; recover exactly the selected user. + if self.oidc is None: + self.operations.compose(restored, 'run', '--rm', '--no-deps', '--user', self.operator, + '--volume', str(self.directory / 'restore-password') + ':/recovery-password:ro', 'application', + '--spring.main.web-application-type=none', '--busystack.recover-account=true', + '--busystack.recovery-username=' + self.saved['username'], '--busystack.recovery-password-file=/recovery-password') + else: + self.operations.compose(restored, 'run', '--rm', '--no-deps', 'application', + '--spring.main.web-application-type=none', '--busystack.recover-external-account=true', + '--busystack.recovery-username=' + self.saved['username'], '--busystack.recovery-issuer=' + self.issuer, + '--busystack.recovery-subject=' + self.oidc.SUBJECT) + self.operations.compose(restored, 'run', '--rm', '--no-deps', '--user', self.operator, + '--volume', str(self.directory / 'reader-restore-password') + ':/recovery-password:ro', 'application', + '--spring.main.web-application-type=none', '--busystack.recover-account=true', + '--busystack.recovery-username=' + self.saved['readerUsername'], '--busystack.recovery-password-file=/recovery-password') + assert self.operations.sql(restored, 'SELECT count(*) FROM accounts WHERE enabled;') == '2' + assert self.operations.sql(restored, "SELECT enabled FROM accounts WHERE id='" + self.writer + "';") == 'f' + self.operations.compose(restored, 'up', '-d', '--no-build', 'application') + self.operations.compose(restored, 'up', '-d', '--no-build', '--wait', '--wait-timeout', '240', 'reverse-proxy') + return restored + + def store_language(self, encoded): + headers={'Authorization':'Basic '+encoded,'Content-Type':'application/xml'} + body=('' + ' before bonjour after ' + 'operation' + 'propempty' + '').encode() + status,_,response=self.request(self.resource_path,'PROPPATCH',headers,body) + assert status==207 and b' 200 OK' in response + self.verify_language(encoded) + temporary=self.collection_path.rsplit('/',2)[0]+'/language-copy-'+uuid4().hex+'/' + create=('' + 'inherited' + '').encode() + assert self.request(temporary,'MKCALENDAR',headers,create)[0]==201 + try: + copied=temporary+'copied.ics';moved=temporary+'moved.ics' + assert self.request(self.resource_path,'COPY',{**headers,'Destination':self.base+copied})[0]==201 + self.verify_language(encoded,copied) + assert self.request(copied,'MOVE',{**headers,'Destination':self.base+moved})[0]==201 + self.verify_language(encoded,moved) + status,_,data=self.request(temporary,'PROPFIND',{**headers,'Depth':'0'}, + b'') + assert status==207 and XML.fromstring(data).find('.//{urn:busystack:recovery}created').attrib['{http://www.w3.org/XML/1998/namespace}lang']=='es' + finally: + assert self.request(temporary,'DELETE',headers)[0]==204 + print('HTTPS_INHERITED_XML_LANGUAGE_MIXED_CONTENT_MKCALENDAR_PROPPATCH_COPY_MOVE_PASSED',flush=True) + + def verify_language(self, encoded, path=None): + status,_,data=self.request(path or self.resource_path,'PROPFIND', + {'Authorization':'Basic '+encoded,'Content-Type':'application/xml','Depth':'0'}, + b'') + assert status==207 + doc=XML.fromstring(data);lang='{http://www.w3.org/XML/1998/namespace}lang';ns='{urn:busystack:recovery}' + for name,value in [('root','en'),('operation','fr'),('prop','de'),('empty','')]: + assert doc.find('.//'+ns+name).attrib[lang]==value + prop=doc.find('.//'+ns+'root');assert prop.text==' before ' and prop[0].text=='bonjour' and prop[0].tail==' after ' and prop[0].attrib[lang]=='fr' + + def permissions(self, environment): + for account, permission in [(self.reader, 'READ'), (self.writer, 'WRITE')]: + assert self.operations.sql(environment, "SELECT permission FROM collection_members WHERE collection_id='" + + self.saved['collection'] + "' AND account_id='" + account + "';") == permission + + @property + def operator(self): + import os + return str(os.getuid()) + ':' + str(os.getgid()) + + def verify(self): + current = json.loads(self.exchange.read_text()) + status,_,_=self.request('/', headers={'Cookie':'JSESSIONID='+self.saved['snapshotCookie']}) + assert status in (302,303,401,403), 'The actual session valid at backup must not revive after restoration' + assert self.request(self.resource_path, headers={'Authorization': 'Basic ' + self.saved['encoded']})[0] == 401 + assert time.time() * 1000 < self.saved['grant']['accessExpiresAt'], 'Restore access-token proof exceeded natural expiry; rerun with fresh credentials' + assert self.request(self.resource_path, headers={'Authorization': 'Bearer ' + self.saved['grant']['accessToken']})[0] == 401 + status, _, data = self.request('/oauth2/token', 'POST', {'Content-Type': 'application/x-www-form-urlencoded'}, + urllib.parse.urlencode({'grant_type': 'refresh_token', 'client_id': self.saved['grant']['clientId'], + 'refresh_token': self.saved['grant']['refreshToken']}).encode()) + assert status == 400 and json.loads(data)['error'] == 'invalid_grant' + status, _, data = self.request(self.resource_path, headers={'Authorization': 'Basic ' + current['recoveredEncoded']}) + assert status == 200 and data == self.content + self.verify_language(current['recoveredEncoded']) + status, _, data = self.report(current['recoveredEncoded'], self.token) + assert status == 403 and XML.fromstring(data).find('{DAV:}valid-sync-token') is not None + status, _, data = self.report(current['recoveredEncoded']) + assert status == 207 + report = XML.fromstring(data) + assert report.findtext('{DAV:}sync-token') and report.findtext('{DAV:}sync-token') != self.token + assert any(value.text and value.text.endswith('/restoration-fixture.ics') for value in report.findall('.//{DAV:}href')) + reader = {'Authorization': 'Basic ' + current['readerEncoded']} + status, headers, data = self.request(self.reader_path, headers=reader) + assert status == 200 and data == self.content, 'Restored READ share must serve the authoritative task' + changed = self.content.replace(b'PERCENT-COMPLETE:25', b'PERCENT-COMPLETE:50') + assert self.request(self.reader_path, 'PUT', {**reader, 'Content-Type': 'text/calendar', + 'If-Match': headers['ETag']}, changed)[0] == 403, 'Restored READ recipient must not write' + assert self.request(self.resource_path, headers={'Authorization': 'Basic ' + current['recoveredEncoded']})[2] == self.content + print('HTTPS_RECOVERY_RESTORED_READ_SHARE_GET_ALLOWED_PUT_DENIED', flush=True) + print('HTTPS_RECOVERY_OLD_CREDENTIALS_AND_SYNC_REJECTED_FRESH_ACCESS_PRESERVES_TASK', flush=True) + self.audit_corpus.verify_restored(current['recoveredEncoded'], current['readerEncoded']) + + def verify_revoked_reader(self): + current = json.loads(self.exchange.read_text()) + assert self.request(self.reader_path, headers={'Authorization': 'Basic ' + current['readerEncoded']})[0] in (403, 404) + status, _, content = self.request(self.resource_path, headers={'Authorization': 'Basic ' + current['recoveredEncoded']}) + assert status == 200 and content == self.content + print('HTTPS_RECOVERY_REVOKED_READER_DENIED_OWNER_TASK_PRESERVED', flush=True) + self.audit_corpus.revoke(current['recoveredEncoded'], current['readerEncoded']) + + def disconnect(self, environment): + for network, upstream in reversed(self.attachments): + self.operations.execute(['docker', 'network', 'disconnect', network, upstream], environment) diff --git a/qualification/passkeys/run.py b/qualification/passkeys/run.py new file mode 100644 index 0000000..cea6b12 --- /dev/null +++ b/qualification/passkeys/run.py @@ -0,0 +1,341 @@ +#!/usr/bin/env python3 +"""Qualify actual browser WebAuthn/MFA over disposable localhost TLS on Linux.""" + +import argparse +import importlib.util +import json +import os +import re +from pathlib import Path +import secrets +import selectors +import socket +import ssl +import subprocess +import tempfile +import threading +import time +import urllib.error +import urllib.request +import proxy +import tls +import oidc +import recovery +import sharing_capacity + + +ROOT = Path(__file__).resolve().parents[2] +spec = importlib.util.spec_from_file_location("operations", ROOT / "scripts/verify-operations.py") +operations = importlib.util.module_from_spec(spec) +spec.loader.exec_module(operations) + + +def ready(base, ca, public_proxy=False, check_path=None, timeout=240): + deadline = time.monotonic() + timeout + while time.monotonic() < deadline: + try: + path = check_path or ("/login" if public_proxy else "/actuator/health/readiness") + with tls.opener(ca).open(base + path, timeout=2) as response: + if response.status == 200 and (public_proxy or check_path is not None or json.load(response)["status"] == "UP"): + return + except urllib.error.HTTPError as error: + if error.code != 503 and not (public_proxy and error.code in (502, 504)): + raise + except urllib.error.URLError as error: + if isinstance(error.reason, ssl.SSLCertVerificationError): + raise + except (TimeoutError, ConnectionError): + pass + # Poll a concrete readiness condition with an explicit deadline. + threading.Event().wait(0.25) + raise RuntimeError("Packaged TLS endpoint did not become ready within " + str(timeout) + " seconds") + + +def certificates(directory, environment): + password = directory / "server-password" + password.write_text(secrets.token_urlsafe(32)) + password.chmod(0o444) + execute = lambda arguments: operations.execute(["openssl", *arguments], environment) + execute(["req", "-x509", "-newkey", "ec", "-pkeyopt", "ec_paramgen_curve:P-256", "-nodes", "-keyout", str(directory / "ca.key"), "-out", str(directory / "ca.pem"), "-days", "2", "-subj", "/CN=BusyStack disposable browser CA", "-addext", "basicConstraints=critical,CA:TRUE", "-addext", "keyUsage=critical,keyCertSign,cRLSign"]) + execute(["req", "-new", "-newkey", "ec", "-pkeyopt", "ec_paramgen_curve:P-256", "-nodes", "-keyout", str(directory / "server.key"), "-out", str(directory / "server.csr"), "-subj", "/CN=localhost"]) + (directory / "extensions").write_text("subjectAltName=DNS:localhost,IP:127.0.0.1,DNS:app.busystack.test,DNS:idp.identity.test\nextendedKeyUsage=serverAuth\nkeyUsage=digitalSignature\nbasicConstraints=CA:FALSE\n") + execute(["x509", "-req", "-in", str(directory / "server.csr"), "-CA", str(directory / "ca.pem"), "-CAkey", str(directory / "ca.key"), "-CAcreateserial", "-out", str(directory / "server.pem"), "-days", "2", "-sha256", "-extfile", str(directory / "extensions")]) + execute(["pkcs12", "-export", "-name", "server", "-inkey", str(directory / "server.key"), "-in", str(directory / "server.pem"), "-certfile", str(directory / "ca.pem"), "-out", str(directory / "server.p12"), "-passout", "file:" + str(password)]) + (directory / "server.p12").chmod(0o444) + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--application-image", required=True) + parser.add_argument("--browser-image", default="busystack-passkey-browser:qualification") + parser.add_argument("--context-path", choices=("", "/team"), default="") + parser.add_argument("--proxy", action="store_true", help="Use pinned Nginx HTTPS termination and the /team prefix") + parser.add_argument("--oidc", action="store_true", help="Also qualify a real Keycloak login across distinct reserved test sites") + parser.add_argument("--restore", action="store_true", help="Qualify actual HTTPS authentication, content and sync after restoring an older database") + parser.add_argument("--groups", action="store_true", help="Require the V9 group-management and owner-sharing browser workflow") + parser.add_argument("--workload", action="store_true", help="Measure the bounded independent-client workload during local HTTPS restoration") + parser.add_argument("--clients", action="store_true", help="Run existing Python/native Thunderbird correctness profiles on the packaged server, without performance workload") + parser.add_argument("--client-python", default=os.environ.get("BUSYSTACK_CLIENT_PYTHON", str(ROOT / ".client-venv/bin/python"))) + parser.add_argument("--workload-output", default=str(ROOT / "target/qualification/workload.json")) + parser.add_argument("--strict-sync-depth", action="store_true", help="Explicit false override; default qualification leaves the setting absent") + parser.add_argument("--thunderbird-binary", default=os.environ.get("BUSYSTACK_THUNDERBIRD_BINARY", str(ROOT / ".client-tools/thunderbird/thunderbird"))) + args = parser.parse_args() + if args.clients and (not args.restore or args.oidc or args.strict_sync_depth): + parser.error("--clients requires local --restore and the default synchronization compatibility profile") + if args.clients and (not Path(args.client_python).is_file() or not os.access(args.thunderbird_binary, os.X_OK)): + parser.error("--clients requires the pinned Python environment and native Thunderbird binary") + if args.workload and args.strict_sync_depth: + parser.error("Independent client workload requires the default sync accommodation; strict override has its own protocol checks") + if args.workload and (not args.restore or args.oidc): + parser.error("--workload requires the local --restore profile") + if args.workload and not Path(args.client_python).is_file(): + parser.error("--workload requires the pinned independent-client Python environment") + if args.workload and not os.access(args.thunderbird_binary, os.X_OK): + parser.error("--workload requires the pinned native Thunderbird binary; use install-thunderbird.sh or --thunderbird-binary") + if args.restore: + args.proxy = True + if args.oidc: + args.proxy = True + if args.proxy: + args.context_path = "/team" + if os.uname().sysname != "Linux": + parser.error("This fixture requires Linux Docker host networking; it does not silently skip") + for image in (args.application_image, args.browser_image): + if not image or any(character.isspace() for character in image): + parser.error("Image references must not contain whitespace") + environment = os.environ.copy() + environment.update(operations.development_environment(args.application_image)) + operations.execute(["docker", "info"], environment) + args.application_image = operations.execute(["docker", "image", "inspect", "--format", "{{.Id}}", args.application_image], environment) + args.browser_image = operations.execute(["docker", "image", "inspect", "--format", "{{.Id}}", args.browser_image], environment) + print("BROWSER_APPLICATION_IMAGE=" + args.application_image, flush=True) + print("BROWSER_FIXTURE_IMAGE=" + args.browser_image, flush=True) + print("BROWSER_PROFILE=" + ("proxy-cross-site-oidc" if args.oidc else "proxy-team" if args.proxy else "embedded-tls" + args.context_path), flush=True) + print("BROWSER_GROUP_WORKFLOW=" + ("required" if args.groups else "unexecuted"), flush=True) + print("HTTPS_RESTORE_WORKFLOW=" + ("required" if args.restore else "unexecuted"), flush=True) + print("DAV_SYNC_DEPTH_INPUT=" + ("explicit false strict override" if args.strict_sync_depth else "setting absent; Compose/application default"), flush=True) + with tempfile.TemporaryDirectory(prefix="busystack-browser-") as temporary: + directory = Path(temporary) + directory.chmod(0o700) + for name in ("database-password", "admin-password", "bootstrap-password", "restore-password", "reader-restore-password"): + path = directory / name + path.write_text(secrets.token_urlsafe(32) + "\n") + path.chmod(0o600 if name in ("bootstrap-password", "restore-password", "reader-restore-password") else 0o444) + certificates(directory, environment) + with socket.socket() as listener: + listener.bind(("127.0.0.1", 0)) + port = listener.getsockname()[1] + hostname = "app.busystack.test" if args.oidc else "localhost" + base = f"https://{hostname}:{port}{args.context_path}" + override = directory / "compose.override.yaml" + override.write_text( + "services:\n application:\n" + f" image: {json.dumps(args.application_image)}\n" + f" ports: !override\n - '127.0.0.1:{port}:8443'\n" + " environment:\n SERVER_PORT: '8443'\n SERVER_SSL_ENABLED: 'true'\n" + " SERVER_SSL_KEY_STORE: file:/tls/server.p12\n SERVER_SSL_KEY_STORE_TYPE: PKCS12\n" + f" volumes:\n - {json.dumps(str(directory / 'server.p12') + ':/tls/server.p12:ro')}\n" + " secrets:\n - source: test_keystore_password\n target: server.ssl.key-store-password\n" + " healthcheck:\n disable: true\n" + f"secrets:\n test_keystore_password:\n file: {json.dumps(str(directory / 'server-password'))}\n" + ) + if args.proxy: + override.write_text(proxy.override(ROOT, directory, args.application_image, base, port)) + project = "busystack-browser-" + secrets.token_hex(6) + browser_name = project + "-chromium" + environment.update( + COMPOSE_FILE=f"{ROOT / 'compose.yaml'}:{override}", + COMPOSE_PROJECT_NAME=project, + COMPOSE_PATH_SEPARATOR=":", + BUSYSTACK_DATABASE_PASSWORD_FILE=str(directory / "database-password"), + BUSYSTACK_POSTGRES_ADMIN_PASSWORD_FILE=str(directory / "admin-password"), + BUSYSTACK_PUBLIC_BASE_URL=base, + BUSYSTACK_ISSUER=base, + BUSYSTACK_HTTP_PORT="8080", + BUSYSTACK_ALLOW_INSECURE_LOCAL="false", + ) + environment.pop("BUSYSTACK_DAV_ALLOW_SYNC_DEPTH_ONE", None) + if args.strict_sync_depth: + environment["BUSYSTACK_DAV_ALLOW_SYNC_DEPTH_ONE"] = "false" + issuer = None + if args.oidc: + operations.execute(["docker", "image", "inspect", oidc.IMAGE], environment) + upstream_override, issuer = oidc.configure(ROOT, directory, base, operations.execute, environment) + environment["COMPOSE_FILE"] += ":" + str(upstream_override) + environments = [environment.copy()] + checkpoint = recovery.Checkpoint(ROOT, directory, operations, base, oidc if args.oidc else None, issuer, environments) if args.restore else None + if checkpoint is not None: + if args.clients: + checkpoint.clients = {"python": args.client_python, "thunderbird": args.thunderbird_binary, + "image": args.application_image} + checkpoint.exchange.touch(mode=0o600) + if args.workload: + checkpoint.workload = {"python": args.client_python, "output": args.workload_output, "thunderbird": args.thunderbird_binary} + browser = None + sharing_load = None + try: + operations.compose(environment, "up", "-d", "--wait", "--wait-timeout", "120", "database") + if args.oidc: + operations.compose(environment, "up", "-d", "--no-build", "upstream") + # The application may resolve trusted issuer metadata at startup; establish the + # real provider readiness condition first, rather than depending on restart timing. + ready(issuer, directory / "ca.pem", False, "/.well-known/openid-configuration", timeout=480) + with tls.opener(directory / "ca.pem").open(issuer + "/.well-known/openid-configuration", timeout=10) as response: + assert json.load(response)["issuer"] == issuer + print("UPSTREAM_HTTPS_METADATA_READY", flush=True) + operations.compose(environment, "run", "--rm", "--no-deps", "--user", f"{os.getuid()}:{os.getgid()}", "--volume", f"{directory / 'bootstrap-password'}:/bootstrap-password:ro", "application", "--spring.main.web-application-type=none", "--busystack.bootstrap=true", "--busystack.bootstrap-username=browseradmin", "--busystack.bootstrap-password-file=/bootstrap-password") + operations.compose(environment, "up", "-d", "--no-build", "application") + if args.proxy: + operations.compose(environment, "up", "-d", "--no-build", "--wait", "--wait-timeout", "240", "reverse-proxy") + ready(base, directory / "ca.pem", args.proxy) + tls.verify_profile(base, directory / "ca.pem") + if args.proxy: + proxy.verify(base, directory / "ca.pem") + try: + tls.opener().open(base + "/actuator/health/readiness", timeout=3) + except urllib.error.URLError as error: + assert isinstance(error.reason, ssl.SSLCertVerificationError) + else: + raise AssertionError("Disposable CA was unexpectedly trusted without isolated configuration") + browser_command = ["docker", "run", "--rm", "--name", browser_name, "--network", "host", "--interactive", "--volume", f"{directory / 'ca.pem'}:/fixture/ca.pem:ro", "--volume", f"{directory / 'bootstrap-password'}:/fixture/password:ro", "--env", "BUSYSTACK_BROWSER_CA_FILE=/fixture/ca.pem", "--env", "BUSYSTACK_BROWSER_PASSWORD_FILE=/fixture/password", "--env", "BUSYSTACK_BROWSER_USERNAME=browseradmin", "--env", "BUSYSTACK_BROWSER_URL=" + base] + if args.oidc: + browser_command += ["--add-host", "app.busystack.test:127.0.0.1", "--add-host", "idp.identity.test:127.0.0.1", + "--volume", f"{directory / 'upstream-password'}:/fixture/upstream-password:ro", "--env", "BUSYSTACK_BROWSER_OIDC=true", + "--env", "BUSYSTACK_UPSTREAM_USERNAME=" + oidc.USERNAME, "--env", "BUSYSTACK_FEDERATED_USERNAME=" + oidc.INTERNAL_USERNAME, + "--env", "BUSYSTACK_UPSTREAM_PASSWORD_FILE=/fixture/upstream-password"] + browser_command += ["--env", "BUSYSTACK_SYNC_DEPTH_INPUT=" + ("explicit false strict override" if args.strict_sync_depth else "setting absent; Compose/application default"), "--env", "BUSYSTACK_SYNC_DEPTH_EXPECTED=" + ("false" if args.strict_sync_depth else "true")] + if args.workload: + browser_command += ["--env", "BUSYSTACK_BROWSER_CAPACITY=true"] + if args.groups: + browser_command += ["--env", "BUSYSTACK_BROWSER_GROUPS=true"] + if checkpoint is not None: + browser_command += ["--env", "BUSYSTACK_BROWSER_RESTORE=true", "--env", "BUSYSTACK_BROWSER_RECOVERY_FILE=/fixture/recovery.json", + "--env", "BUSYSTACK_BROWSER_RECOVERY_PASSWORD_FILE=/fixture/restore-password", + "--env", "BUSYSTACK_BROWSER_READER_PASSWORD_FILE=/fixture/reader-restore-password", + "--volume", f"{directory / 'reader-restore-password'}:/fixture/reader-restore-password:ro", + "--volume", f"{checkpoint.exchange}:/fixture/recovery.json:rw", "--volume", f"{directory / 'restore-password'}:/fixture/restore-password:ro"] + browser_command.append(args.browser_image) + browser = subprocess.Popen( + browser_command, + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + bufsize=0, + ) + selector = selectors.DefaultSelector() + selector.register(browser.stdout, selectors.EVENT_READ) + pending = b"" + signals = set() + deadline = time.monotonic() + (1200 if args.restore else 600) + password = (directory / "bootstrap-password").read_text().strip() + while time.monotonic() < deadline: + events = selector.select(timeout=1) + if not events: + if browser.poll() is not None: + break + continue + chunk = os.read(browser.stdout.fileno(), 8192) + if not chunk: + break + pending += chunk + while b"\n" in pending: + raw, pending = pending.split(b"\n", 1) + line = raw.decode(errors="replace") + print(line.replace(password, "[redacted]"), flush=True) + signals.add(line) + if line == "READY_CAPACITY_SHARING" and checkpoint is not None and args.workload: + sharing_load = sharing_capacity.SharingCapacity(checkpoint, environment) + sharing_load.seed() + browser.stdin.write(b"CAPACITY_SHARING_SEEDED\n") + browser.stdin.flush() + elif line == "READY_CAPACITY_SHARING_CLEANUP" and sharing_load is not None: + sharing_load.finish() + browser.stdin.write(b"CAPACITY_SHARING_CLEANED\n") + browser.stdin.flush() + elif line == "READY_RESTORE_BACKUP" and checkpoint is not None: + checkpoint.backup(environment) + browser.stdin.write(b"BACKED_UP\n") + browser.stdin.flush() + elif line == "READY_RESTORE_APPLY" and checkpoint is not None: + environment = checkpoint.restore(environment) + ready(base, directory / "ca.pem", True) + browser.stdin.write(b"RESTORED\n") + browser.stdin.flush() + elif line == "READY_RESTORE_VERIFY" and checkpoint is not None: + checkpoint.verify() + browser.stdin.write(b"RESTORE_VERIFIED\n") + browser.stdin.flush() + elif line == "READY_RESTORE_REVOKED_READER_VERIFY" and checkpoint is not None: + checkpoint.verify_revoked_reader() + browser.stdin.write(b"RESTORE_REVOKED_READER_VERIFIED\n") + browser.stdin.flush() + elif line == "READY_RESTART": + assert int(operations.sql(environment, "SELECT count(*) FROM user_credentials;")) == (2 if args.restore else 2 if args.oidc else 1) + operations.compose(environment, "restart", "application") + ready(base, directory / "ca.pem", args.proxy) + browser.stdin.write(b"RESTARTED\n") + browser.stdin.flush() + elif line == "READY_DISABLE": + username = oidc.INTERNAL_USERNAME if args.oidc else "browseradmin" + operations.sql(environment, "UPDATE accounts SET enabled=false WHERE username='" + username + "';") + browser.stdin.write(b"DISABLED\n") + browser.stdin.flush() + selector.close() + try: + result = browser.wait(timeout=15) + except subprocess.TimeoutExpired as error: + raise RuntimeError("Browser qualification exceeded its deadline") from error + assert result == 0, "Browser qualification failed" + for expected in ("PASSWORD_PLUS_WEBAUTHN_AND_MUTATION_PASSED", "BROWSER_SINGLE_USE_PRIMARY_PLUS_RECOVERY_CODE_RESET_AND_FRESH_MFA_PASSED", "BROWSER_DAV_CREDENTIAL_ISSUANCE_DISPLAY_ONCE_AND_REVOCATION_PASSED", "BROWSER_S256_CONSENT_TOKEN_REFRESH_AND_GRANT_REVOCATION_PASSED", "BROWSER_CONSENT_WITHOUT_CSRF_REJECTED", "PERSISTED_PASSKEY_AFTER_RESTART_PASSED", "DISABLED_SESSION_AND_PASSKEY_PASSED"): + assert expected in signals, expected + if args.oidc: + assert "CROSS_SITE_ORGANIZATION_PLUS_PASSKEY_AND_NONADMIN_PROVISIONING_PASSED" in signals + if args.groups: + assert "BROWSER_GROUP_MEMBERSHIP_UPDATE_DELETE_AND_OWNER_GRANTS_PASSED" in signals + if args.restore: + assert "HTTPS_RESTORE_OLD_ACCESS_REJECTED_FRESH_MFA_DAV_OAUTH_CONTENT_AND_SYNC_PASSED" in signals + assert "HTTPS_RESTORE_SHARED_RECIPIENT_FRESH_MFA_READ_WRITE_DENIAL_AND_REVOCATION_PASSED" in signals + print("PASS: trusted test-CA HTTPS, Chromium virtual CTAP2 enrollment/assertion, password+passkey MFA enforcement, session rotation, persistence across restart, disabled-session/passkey denial.") + except Exception: + # Emit only sanitized error diagnostics from this disposable application, before cleanup. + try: + raw = operations.compose(environment, "logs", "--no-log-prefix", "--tail", "120", "application") + except (RuntimeError, subprocess.TimeoutExpired): + raw = "" + print("APPLICATION_DIAGNOSTICS_UNAVAILABLE", flush=True) + for name in ("bootstrap-password", "restore-password", "reader-restore-password", "server-password", "database-password", "admin-password", "upstream-password", "upstream-client-secret"): + path = directory / name + if path.exists(): + value = path.read_text().strip() + if value: raw = raw.replace(value, "[redacted]") + raw = re.sub(r"[A-Za-z0-9_+/=-]{40,}", "[redacted-value]", raw) + raw = re.sub(r"([?&](?:code|state|code_challenge)=)[^&\s)\"]+", r"\1[redacted]", raw) + for line in raw.splitlines(): + if any(marker in line for marker in (" ERROR ", "Caused by:", "APPLICATION FAILED", "Description:", "Reason:", "Action:")): + print("APPLICATION_DIAGNOSTIC " + line, flush=True) + raise + finally: + if sharing_load is not None: + sharing_load.stop.set() + if hasattr(sharing_load, "thread"): + sharing_load.thread.join(timeout=12) + try: + subprocess.run(["docker", "rm", "--force", browser_name], capture_output=True) + if browser is not None and browser.poll() is None: + browser.wait(timeout=15) + finally: + try: + if checkpoint is not None: checkpoint.disconnect(environment) + finally: + cleanup_errors = [] + for created in reversed(environments): + try: + operations.compose(created, "down", "--volumes", "--remove-orphans", "--timeout", "10") + except (RuntimeError, subprocess.TimeoutExpired) as failure: + cleanup_errors.append(failure) + if cleanup_errors: + raise RuntimeError("A disposable qualification project could not be cleaned up") from cleanup_errors[0] + + +if __name__ == "__main__": + main() diff --git a/qualification/passkeys/sharing_capacity.py b/qualification/passkeys/sharing_capacity.py new file mode 100644 index 0000000..742a3a4 --- /dev/null +++ b/qualification/passkeys/sharing_capacity.py @@ -0,0 +1,70 @@ +"""Observe real-browser management of a large, owned disposable grant fixture.""" +from datetime import datetime, timezone +import json +from pathlib import Path +import threading +import time +from uuid import uuid4 + + +class SharingCapacity: + def __init__(self, checkpoint, environment): + self.checkpoint=checkpoint;self.environment=environment;self.operations=checkpoint.operations + self.groups=[str(uuid4()) for _ in range(500)];self.collections=[str(uuid4()) for _ in range(25)] + self.stop=threading.Event();self.samples=[];self.errors=[];self.started=time.monotonic() + + def seed(self): + current=json.loads(self.checkpoint.exchange.read_text()) + user=current['username'].replace("'","''") + owner=self.operations.sql(self.environment,"SELECT id FROM accounts WHERE username='"+user+"';") + member=self.operations.sql(self.environment,"SELECT id FROM accounts WHERE username='"+current['readerUsername']+"';") + groups=','.join("('"+i+"','Capacity group "+str(n)+"','"+owner+"')" for n,i in enumerate(self.groups)) + collections=','.join("('"+i+"','"+owner+"','Capacity sharing "+str(n)+"','VEVENT')" for n,i in enumerate(self.collections)) + group_ids=','.join("'"+i+"'::uuid" for i in self.groups);collection_ids=','.join("'"+i+"'::uuid" for i in self.collections) + self.operations.sql(self.environment,f"""BEGIN; +SELECT pg_advisory_xact_lock(4779818222654407499); +SELECT set_config('busystack.defer_dav_bindings','on',true); +INSERT INTO sharing_groups(id,name,created_by) VALUES {groups}; +INSERT INTO collections(id,owner_id,name,component) VALUES {collections}; +INSERT INTO sharing_group_members(group_id,account_id) SELECT id,'{member}' FROM sharing_groups WHERE id IN ({group_ids}); +INSERT INTO collection_group_members(collection_id,group_id,permission) +SELECT c.id,g.id,'READ' FROM collections c CROSS JOIN sharing_groups g WHERE c.id IN ({collection_ids}) AND g.id IN ({group_ids}); +SELECT set_config('busystack.defer_dav_bindings','off',true); +SELECT dav_reconcile_memberships(id) FROM collections WHERE id IN ({collection_ids}); +COMMIT;""") + self.started=time.monotonic() + current['sharingCapacity']={'firstGroup':self.groups[0],'groupName':'Capacity group 0','initialGrants':12500} + self.checkpoint.exchange.write_text(json.dumps(current)) + self.application=self.operations.compose(self.environment,'ps','--quiet','application') + inspected=json.loads(self.operations.execute(['docker','inspect',self.application],self.environment))[0] + self.record={'status':'running','recordedAt':datetime.now(timezone.utc).isoformat(),'applicationImage':inspected['Image'], + 'memoryLimitBytes':inspected['HostConfig']['Memory'],'groups':500,'collections':25,'initialGrants':12500, + 'population':'Owned isolated SQL fixture; assertions are actual CSRF-protected browser forms', + 'databaseBefore':self.operations.sql(self.environment,'SELECT pg_database_size(current_database());')} + def sample(): + while not self.stop.is_set(): + try: + value=json.loads(self.operations.execute(['docker','stats','--no-stream','--format','{{json .}}',self.application],self.environment)) + self.samples.append({'elapsedSeconds':round(time.monotonic()-self.started,2),'memoryUsage':value['MemUsage'],'cpuPercent':value['CPUPerc']}) + except (ValueError,RuntimeError,KeyError,OSError) as e:self.errors.append(type(e).__name__) + self.stop.wait(2) + self.thread=threading.Thread(target=sample,daemon=True);self.thread.start() + + def finish(self): + current=json.loads(self.checkpoint.exchange.read_text()) + result=current['sharingCapacity'];assert result['browserPassed'] + group_ids=','.join("'"+i+"'::uuid" for i in self.groups);collection_ids=','.join("'"+i+"'::uuid" for i in self.collections) + count=int(self.operations.sql(self.environment,f'SELECT count(*) FROM collection_group_members WHERE collection_id IN ({collection_ids});')) + expected=12475 if result['removedGroup']==self.groups[0] else 12474 + assert count==expected,(count,expected) + assert self.operations.sql(self.environment,"SELECT count(*) FROM sharing_groups WHERE id='"+self.groups[0]+"';")=='0' + assert self.operations.sql(self.environment,f"SELECT count(*) FROM dav_bindings b JOIN accounts a ON a.id=b.home_id WHERE a.username='restorewriter' AND b.collection_id IN ({collection_ids});")=='0' + assert self.operations.sql(self.environment,f"SELECT count(*) FROM dav_bindings b JOIN accounts a ON a.id=b.home_id WHERE a.username='restorereader' AND b.collection_id IN ({collection_ids});")=='25' + self.stop.set();self.thread.join(timeout=12) + self.record.update(status='passed',remainingGrantsBeforeCleanup=count,browser=result,elapsedSeconds=round(time.monotonic()-self.started,2),containerSamples=self.samples,observerErrors=self.errors, + databaseAfter=self.operations.sql(self.environment,'SELECT pg_database_size(current_database());')) + assert not self.thread.is_alive() and self.samples and not self.errors + output=Path(self.checkpoint.root/'target/qualification/sharing-capacity.json');output.parent.mkdir(parents=True,exist_ok=True);output.write_text(json.dumps(self.record,indent=2)+'\n') + # Generated fixture identifiers only; retirement records intentionally remain non-reusable. + self.operations.sql(self.environment,f"BEGIN; SELECT set_config('busystack.defer_dav_bindings','on',true); DELETE FROM collections WHERE id IN ({collection_ids}); DELETE FROM sharing_groups WHERE id IN ({group_ids}); SELECT set_config('busystack.defer_dav_bindings','off',true); COMMIT;") + print('PACKAGED_SHARING_CAPACITY_BROWSER_MANAGEMENT_PASSED artifact='+str(output),flush=True) diff --git a/qualification/passkeys/test_capacity_transport.py b/qualification/passkeys/test_capacity_transport.py new file mode 100644 index 0000000..5d4c361 --- /dev/null +++ b/qualification/passkeys/test_capacity_transport.py @@ -0,0 +1,37 @@ +"""The large selective query has its measured allowance; other HTTP requests keep 60 seconds.""" +import importlib.util +import io +from pathlib import Path +from urllib.error import HTTPError +from urllib.request import Request +import unittest +from unittest.mock import Mock, patch +import xml.etree.ElementTree as XML + +spec=importlib.util.spec_from_file_location('capacity_transport',Path(__file__).resolve().parents[1]/'clients/capacity.py') +capacity=importlib.util.module_from_spec(spec);spec.loader.exec_module(capacity) + + +class CapacityTransportTest(unittest.TestCase): + def test_ordinary_request_keeps_sixty_second_transport_timeout(self): + request=Request('https://fixture.invalid/calendar/') + context=object();response=object() + with patch.object(capacity,'urlopen',return_value=response) as open: + self.assertIs(capacity.open_response(request,context),response) + open.assert_called_once_with(request,context=context,timeout=60) + + def test_only_selective_query_supplies_its_measured_timeout(self): + request=Mock(return_value=(207,{},b'')) + self.assertEqual(capacity.selective_query(request)[0],207) + self.assertEqual(request.call_args.args,('REPORT',)) + self.assertEqual(request.call_args.kwargs['timeout'],150) + body=XML.fromstring(request.call_args.kwargs['body']) + self.assertEqual(body.tag,'{urn:ietf:params:xml:ns:caldav}calendar-query') + self.assertEqual(body.find('.//{urn:ietf:params:xml:ns:caldav}time-range').attrib,{'start':'20261027T000000Z','end':'20261028T000000Z'}) + + def test_http_limit_response_remains_a_response_not_a_timeout_retry(self): + request=Request('https://fixture.invalid/calendar/') + failure=HTTPError(request.full_url,507,'Limit',{},io.BytesIO(b'')) + with patch.object(capacity,'urlopen',side_effect=failure) as open: + self.assertIs(capacity.open_response(request,object(),timeout=150),failure) + self.assertEqual(open.call_count,1) diff --git a/qualification/passkeys/test_operation_diagnostics.py b/qualification/passkeys/test_operation_diagnostics.py new file mode 100644 index 0000000..76ccfb1 --- /dev/null +++ b/qualification/passkeys/test_operation_diagnostics.py @@ -0,0 +1,55 @@ +"""Operation failures retain bounded boot evidence while protecting fixture credentials.""" +import importlib.util +import json +from pathlib import Path +import unittest + + +spec = importlib.util.spec_from_file_location( + "verify_operations", Path(__file__).resolve().parents[2] / "scripts" / "verify-operations.py" +) +operations = importlib.util.module_from_spec(spec) +spec.loader.exec_module(operations) + + +class OperationDiagnosticsTest(unittest.TestCase): + def test_health_attempts_are_bounded_sanitized_and_preserve_timing(self): + attempts = [{"Start": str(index), "End": str(index + 1), "ExitCode": 1, + "Output": "\x1b[31mConnection refused fixture-secret password=short Bearer opaque\x1b[0m " + "bounded " * 1000} + for index in range(8)] + state = {"State": {"Status": "running", "ExitCode": 0, "OOMKilled": False, + "StartedAt": "boot-time", "Health": {"Status": "unhealthy", "FailingStreak": 5, "Log": attempts}}, + "RestartCount": 0, "Config": {"Healthcheck": {"StartPeriod": 60, "Interval": 15, "Timeout": 5, "Retries": 5, + "Test": ["credential-must-not-appear"]}}} + result = operations.failure_state(state, ["fixture-secret"]) + self.assertEqual(result["startedAt"], "boot-time") + self.assertEqual(result["healthcheck"], {"StartPeriod": 60, "Interval": 15, "Timeout": 5, "Retries": 5}) + self.assertEqual(len(result["healthLog"]), 5) + self.assertEqual(result["healthLog"][0]["start"], "3") + self.assertEqual(result["healthLog"][-1]["end"], "8") + rendered = json.dumps(result) + for secret in ("fixture-secret", "short", "opaque", "credential-must-not-appear", "\\u001b"): + self.assertNotIn(secret, rendered) + self.assertTrue(all(len(attempt["output"]) <= 1035 for attempt in result["healthLog"])) + self.assertTrue(all(attempt["output"].endswith("[truncated]") for attempt in result["healthLog"])) + self.assertIn("Connection refused", rendered) + + def test_startup_and_last_migrations_are_bounded_without_arbitrary_log_content(self): + logs = "Starting BusyStackApplication\nPrivate calendar body must not be retained\n" + logs += "\n".join("Migrating schema public to version " + str(index) for index in range(20)) + logs += "\nHikariPool-1 - Start completed\nTomcat initialized\n ERROR password=short fixture-secret" + result = operations.startup_diagnostics(logs, ["fixture-secret"]) + self.assertEqual(len(result["lastMigrations"]), 8) + self.assertTrue(result["lastMigrations"][0].endswith("12")) + self.assertIn("Starting BusyStackApplication", result["startupMilestones"]) + rendered = json.dumps(result) + self.assertNotIn("Private calendar body", rendered) + self.assertNotIn("fixture-secret", rendered) + self.assertNotIn("short", rendered) + self.assertIn("Tomcat initialized", rendered) + + def test_short_authorization_values_are_redacted_before_named_header_handling(self): + for value in ("Authorization: Basic dTpw", "Authorization=Bearer small-token"): + rendered = operations.sanitized_diagnostic(value, []) + self.assertNotIn("dTpw", rendered) + self.assertNotIn("small-token", rendered) diff --git a/qualification/passkeys/test_recovery_refresh.py b/qualification/passkeys/test_recovery_refresh.py new file mode 100644 index 0000000..267c987 --- /dev/null +++ b/qualification/passkeys/test_recovery_refresh.py @@ -0,0 +1,46 @@ +"""Retained grant bookkeeping must use actual public refresh, rotation and private storage.""" +import json +from pathlib import Path +import tempfile +import unittest +from unittest.mock import patch +from urllib.parse import parse_qs +import recovery + + +class RecoveryRefreshTest(unittest.TestCase): + def setUp(self): + self.directory=tempfile.TemporaryDirectory(prefix='busystack-recovery-refresh-test-') + self.addCleanup(self.directory.cleanup) + self.checkpoint=object.__new__(recovery.Checkpoint) + self.checkpoint.exchange=Path(self.directory.name)/'private-exchange.json' + self.checkpoint.saved={'grant':{'clientId':'public-native','accessToken':'old-access-'+'a'*50,'refreshToken':'old-refresh-'+'r'*50,'accessExpiresAt':0},'snapshotCookie':'private-session'} + self.current={'access_token':'new-access-'+'a'*50,'refresh_token':'new-refresh-'+'r'*50,'expires_in':600,'token_type':'Bearer','scope':'dav'} + self.sent=None + def request(path,method,headers,body): + self.sent=(path,method,headers,parse_qs(body.decode())) + return 200,{},json.dumps(self.current).encode() + self.checkpoint.request=request + + def test_actual_public_exchange_replaces_both_credentials_and_expiry_privately(self): + with patch('recovery.time.time',return_value=1000): self.checkpoint.refresh_retained_grant() + path,method,headers,form=self.sent + self.assertEqual((path,method),('/oauth2/token','POST')) + self.assertEqual(form,{'grant_type':['refresh_token'],'client_id':['public-native'],'refresh_token':['old-refresh-'+'r'*50]}) + stored=json.loads(self.checkpoint.exchange.read_text()) + self.assertEqual(stored['grant']['accessToken'],self.current['access_token']) + self.assertEqual(stored['grant']['refreshToken'],self.current['refresh_token']) + self.assertEqual(stored['grant']['accessExpiresAt'],1600000) + self.assertEqual(self.checkpoint.exchange.stat().st_mode&0o777,0o600) + self.assertEqual(stored['snapshotCookie'],'private-session') + + def test_missing_rotation_is_rejected_without_overwriting_exchange(self): + self.current['refresh_token']=self.checkpoint.saved['grant']['refreshToken'] + with self.assertRaises(AssertionError): self.checkpoint.refresh_retained_grant() + self.assertFalse(self.checkpoint.exchange.exists()) + + def test_failed_endpoint_response_does_not_fabricate_fresh_expiry(self): + self.checkpoint.request=lambda *args:(400,{},b'{"error":"invalid_grant"}') + with self.assertRaises(AssertionError): self.checkpoint.refresh_retained_grant() + self.assertEqual(self.checkpoint.saved['grant']['accessExpiresAt'],0) + self.assertFalse(self.checkpoint.exchange.exists()) diff --git a/qualification/passkeys/test_workload_measurement.py b/qualification/passkeys/test_workload_measurement.py new file mode 100644 index 0000000..a5c6cfe --- /dev/null +++ b/qualification/passkeys/test_workload_measurement.py @@ -0,0 +1,35 @@ +"""Heap evidence comes from the owned application JVM, never a guessed observation.""" +from subprocess import CompletedProcess, CalledProcessError +import unittest +from unittest.mock import patch +import workload_measurement + + +class HeapEvidenceTest(unittest.TestCase): + def setUp(self): + self.environment={'COMPOSE_PROJECT_NAME':'owned-fixture'} + self.inspected={'Config':{'Labels':{'com.docker.compose.project':'owned-fixture','com.docker.compose.service':'application'}, + 'Entrypoint':['java','-XX:MaxRAMPercentage=70.0','-jar','/opt/busystack/application.jar'],'Cmd':None}, + 'HostConfig':{'Memory':1073741824}} + + def test_captures_actual_pid_one_max_heap_without_tool_option_inheritance(self): + with patch('workload_measurement.subprocess.run',return_value=CompletedProcess([],0,'1:\n-XX:InitialHeapSize=16777216 -XX:MaxHeapSize=751619276 -XX:MaxRAMPercentage=70.000000\n','')) as run: + result=workload_measurement.heap_configuration('owned-app',self.inspected,self.environment) + self.assertEqual(result['maxHeapBytes'],751619276) + self.assertEqual(result['source'],'actual owned-container jcmd PID1 VM.flags') + self.assertIn('JAVA_TOOL_OPTIONS=',run.call_args.args[0]) + self.assertEqual(run.call_args.kwargs['timeout'],15) + + def test_unavailable_attach_records_command_policy_without_fabricating_actual_heap(self): + with patch('workload_measurement.subprocess.run',side_effect=CalledProcessError(1,['jcmd'])): + result=workload_measurement.heap_configuration('owned-app',self.inspected,self.environment) + self.assertIsNone(result['maxHeapBytes']) + self.assertEqual(result['configuredMaxRAMPercentage'],'70.0') + self.assertIn('-XX:MaxRAMPercentage=70.0',result['applicationJvmCommand']) + + def test_refuses_to_attach_to_an_unrelated_project(self): + self.inspected['Config']['Labels']['com.docker.compose.project']='unrelated' + with patch('workload_measurement.subprocess.run') as run: + with self.assertRaises(AssertionError): + workload_measurement.heap_configuration('unrelated-app',self.inspected,self.environment) + run.assert_not_called() diff --git a/qualification/passkeys/tls.py b/qualification/passkeys/tls.py new file mode 100644 index 0000000..157c14e --- /dev/null +++ b/qualification/passkeys/tls.py @@ -0,0 +1,84 @@ +"""Normal certificate validation with test-only routing for reserved .test domains.""" + +import http.client +import socket +import ssl +import urllib.request +import urllib.parse +import warnings +import json + + +DOMAINS = {"app.busystack.test", "idp.identity.test"} + + +class Connection(http.client.HTTPSConnection): + def connect(self): + if self.host in DOMAINS: + # Route only these reserved fixture names; HTTPSConnection retains hostname/SNI checks. + self._create_connection = lambda address, timeout, source_address=None: socket.create_connection(("127.0.0.1", address[1]), timeout, source_address) + super().connect() + + +class Handler(urllib.request.HTTPSHandler): + def https_open(self, request): + return self.do_open(Connection, request, context=self._context) + + +def context(ca=None): + return ssl.create_default_context(cafile=ca) + + +def opener(ca=None, *handlers): + return urllib.request.build_opener(urllib.request.ProxyHandler({}), Handler(context=context(ca)), *handlers) + + +def verify_profile(base, ca): + """Negotiate the actual disposable listener, retaining CA, SNI and name checks.""" + public = urllib.parse.urlsplit(base) + host = "127.0.0.1" if public.hostname in DOMAINS else public.hostname + address = (host, public.port or 443) + + def handshake(value, name=None): + with socket.create_connection(address, timeout=10) as raw: + with value.wrap_socket(raw, server_hostname=name or public.hostname) as connection: + return {"version": connection.version(), "cipher": connection.cipher()[0]} + + results = {} + for version in (ssl.TLSVersion.TLSv1_2, ssl.TLSVersion.TLSv1_3): + value = context(ca) + value.minimum_version = value.maximum_version = version + results[version.name] = handshake(value) + assert handshake(context(ca))["version"] == "TLSv1.3" + + def rejected(value, name=None, certificate=False): + try: + handshake(value, name) + except ssl.SSLCertVerificationError: + assert certificate + except ssl.SSLError as error: + assert not certificate + # A local no-ciphers failure would not establish rejection by the server. + assert error.reason in ("TLSV1_ALERT_PROTOCOL_VERSION", "SSLV3_ALERT_HANDSHAKE_FAILURE") + else: + raise AssertionError("The deliberately unsupported TLS handshake succeeded") + + for version in (ssl.TLSVersion.TLSv1, ssl.TLSVersion.TLSv1_1): + value = context(ca) + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + value.minimum_version = value.maximum_version = version + # Only this negative test client offers the obsolete version; server policy is unchanged. + value.set_ciphers("ALL:@SECLEVEL=0") + rejected(value) + results[version.name] = "server rejected" + for cipher in ("AES128-GCM-SHA256", "DHE-RSA-AES128-GCM-SHA256"): + value = context(ca) + value.minimum_version = value.maximum_version = ssl.TLSVersion.TLSv1_2 + value.set_ciphers(cipher) + rejected(value) + results[cipher] = "server rejected" + rejected(context(), certificate=True) + rejected(context(ca), name="wrong-host.invalid", certificate=True) + results["identity"] = "trusted matching name succeeds; untrusted CA and wrong name rejected" + print("HTTPS_TLS_PROFILE=" + json.dumps(results, sort_keys=True), flush=True) diff --git a/qualification/passkeys/transport.mjs b/qualification/passkeys/transport.mjs new file mode 100644 index 0000000..d1ee94e --- /dev/null +++ b/qualification/passkeys/transport.mjs @@ -0,0 +1,37 @@ +import {request} from 'node:https'; +import {readFileSync} from 'node:fs'; + +// The temporary CA is scoped to this client; system/production trust is never changed. +export function call(base, path, {method = 'GET', headers = {}, form, body} = {}) { + if (form !== undefined && body !== undefined) throw new Error('Specify form or body, not both'); + const data = body === undefined ? (form === undefined ? undefined : new URLSearchParams(form).toString()) : body; + return new Promise((resolve, reject) => { + const outgoing = request(new URL(base + path), { + method, + ca: readFileSync(process.env.BUSYSTACK_BROWSER_CA_FILE), + rejectUnauthorized: true, + headers: {...headers, ...(data === undefined ? {} : { + 'Content-Type': body === undefined ? 'application/x-www-form-urlencoded' : (headers['Content-Type'] || 'application/xml; charset=utf-8'), + 'Content-Length': Buffer.byteLength(data) + })} + }, incoming => { + const chunks = []; + let bytes = 0; + incoming.on('data', value => { + bytes += value.length; + if (bytes > 65536) outgoing.destroy(new Error('Qualification response exceeds its limit')); + else chunks.push(value); + }); + incoming.on('error', reject); + incoming.on('end', () => resolve({status: incoming.statusCode, body: Buffer.concat(chunks).toString('utf8')})); + }); + outgoing.on('error', reject); + outgoing.setTimeout(15000, () => outgoing.destroy(new Error('Qualification HTTPS request timed out'))); + outgoing.end(data); + }); +} + +export function json(response) { + try { return JSON.parse(response.body); } + catch { throw new Error('Qualification endpoint returned malformed JSON'); } +} diff --git a/qualification/passkeys/trust-and-run.sh b/qualification/passkeys/trust-and-run.sh new file mode 100644 index 0000000..fac101f --- /dev/null +++ b/qualification/passkeys/trust-and-run.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash +set -euo pipefail +test -r "${BUSYSTACK_BROWSER_CA_FILE:?Mount the disposable test CA}" +install -d -m 700 /root/.pki/nssdb +certutil -N --empty-password -d sql:/root/.pki/nssdb +certutil -A -d sql:/root/.pki/nssdb -n 'BusyStack disposable qualification CA' -t 'C,,' -i "$BUSYSTACK_BROWSER_CA_FILE" +exec node /qualification/passkeys.mjs diff --git a/qualification/passkeys/workload_measurement.py b/qualification/passkeys/workload_measurement.py new file mode 100644 index 0000000..63562c9 --- /dev/null +++ b/qualification/passkeys/workload_measurement.py @@ -0,0 +1,237 @@ +"""Observe a bounded independent-client workload on this driver's disposable application.""" + +from datetime import datetime, timezone +import json +import os +from pathlib import Path +import platform +import re +import subprocess +import threading +import selectors +from uuid import uuid4 +import time + + +def database(operations, environment): + return json.loads(operations.sql(environment, """SELECT json_build_object( +'databaseBytes',pg_database_size(current_database()), +'resourceTableBytes',pg_total_relation_size('calendar_resources'), +'commits',xact_commit,'rollbacks',xact_rollback,'blocksRead',blks_read,'blocksHit',blks_hit, +'rowsReturned',tup_returned,'rowsFetched',tup_fetched,'rowsInserted',tup_inserted, +'rowsUpdated',tup_updated,'rowsDeleted',tup_deleted,'tempFiles',temp_files,'tempBytes',temp_bytes, +'deadlocks',deadlocks,'checksumFailures',checksum_failures) +FROM pg_stat_database WHERE datname=current_database();""")) + + +def heap_configuration(application, inspected, environment): + labels=inspected['Config'].get('Labels') or {} + assert labels.get('com.docker.compose.project')==environment['COMPOSE_PROJECT_NAME'] + assert labels.get('com.docker.compose.service')=='application' + command=[*inspected['Config'].get('Entrypoint',[]),*(inspected['Config'].get('Cmd') or [])] + percentage=next((value.split('=',1)[1] for value in command if value.startswith('-XX:MaxRAMPercentage=')),None) + record={'containerMemoryLimitBytes':inspected['HostConfig']['Memory'], + 'applicationJvmCommand':command,'configuredMaxRAMPercentage':percentage} + try: + # Attach only to PID1 in this explicitly owned synthetic fixture. Clear tool-only Java + # options so the diagnostic process neither inherits nor echoes test trust-store secrets. + result=subprocess.run(['docker','exec','--env','JAVA_TOOL_OPTIONS=','--env','JDK_JAVA_OPTIONS=', + application,'/opt/java/openjdk/bin/jcmd','1','VM.flags'],env=environment, + text=True,capture_output=True,timeout=15,check=True) + if len(result.stdout)>65536: raise ValueError('VM flags exceed diagnostic output bound') + maximum=re.search(r'(?:^|\s)-XX:MaxHeapSize=(\d+)(?=\s|$)',result.stdout) + if maximum is None: raise ValueError('VM flags lack actual MaxHeapSize') + record.update(source='actual owned-container jcmd PID1 VM.flags',maxHeapBytes=int(maximum.group(1)), + vmFlags=result.stdout.strip()) + except (OSError,ValueError,subprocess.CalledProcessError,subprocess.TimeoutExpired) as error: + # An unavailable attach mechanism is explicit. This fallback records the actual launched + # command and configured percentage, rather than inventing an observed heap size. + assert percentage is not None and record['containerMemoryLimitBytes']>0 + record.update(source='actual launched command; attach diagnostic unavailable',maxHeapBytes=None, + attachFailure=type(error).__name__) + return record + + +def observe(checkpoint, environment, python, output): + operations = checkpoint.operations + application = operations.compose(environment, 'ps', '--quiet', 'application') + inspected = json.loads(operations.execute(['docker', 'inspect', application], environment))[0] + secret = checkpoint.directory / 'workload-secret' + secret.write_text(checkpoint.saved['credential']) + secret.chmod(0o600) + variables = environment.copy() + variables.update(BUSYSTACK_CLIENT_URL=checkpoint.base + '/dav/', + BUSYSTACK_CLIENT_USER=checkpoint.saved['username'], BUSYSTACK_CLIENT_SECRET_FILE=str(secret), + BUSYSTACK_CLIENT_CA=str(checkpoint.ca), + BUSYSTACK_CLIENT_SYNC_DEPTH_INPUT='setting absent; Compose/application default') + output = Path(output) + output.parent.mkdir(parents=True, exist_ok=True) + metadata = {'status': 'running', 'startedAt': datetime.now(timezone.utc).isoformat(), + 'applicationImage': inspected['Image'], 'applicationMemoryLimitBytes': inspected['HostConfig']['Memory'], + 'applicationNanoCpuLimit': inspected['HostConfig']['NanoCpus'], + 'jvmHeap':heap_configuration(application,inspected,environment), + 'host': {'system': platform.system(), 'architecture': platform.machine(), 'kernel': platform.release(), + 'logicalCpus': os.cpu_count(), 'loadAverage': list(os.getloadavg())}, + 'postgresVersion': operations.sql(environment, 'SHOW server_version;'), + 'dockerVersion': operations.execute(['docker', 'version', '--format', '{{.Server.Version}}'], environment), + 'databaseBefore': database(operations, environment), 'containerSamples': [], + 'interpretation': 'One warmed application and one sequential independent client on a shared host. ' + 'Docker samples include observer overhead; PostgreSQL cumulative statistics update asynchronously. ' + 'Calendar/task data is synthetic. No capacity, scalability or high-availability claim.'} + if hasattr(os, 'sched_getaffinity'): + metadata['host']['affinityCpuCount'] = len(os.sched_getaffinity(0)) + quota = Path('/sys/fs/cgroup/cpu.max') + if quota.exists(): metadata['host']['cgroupCpuMax'] = quota.read_text().strip() + cpuinfo = Path('/proc/cpuinfo') + if cpuinfo.exists(): + for line in cpuinfo.read_text().splitlines(): + if line.startswith('model name'): + metadata['host']['cpuModel'] = line.split(':', 1)[1].strip() + break + meminfo = Path('/proc/meminfo') + if meminfo.exists(): + value = next(line for line in meminfo.read_text().splitlines() if line.startswith('MemTotal:')) + metadata['host']['memoryBytes'] = int(value.split()[1]) * 1024 + output.write_text(json.dumps(metadata, indent=2) + '\n') + stop = threading.Event() + restarting = threading.Event() + observations, errors = [], [] + started = time.monotonic() + + def sample(): + if restarting.is_set(): + return + try: + measured = subprocess.run(['docker', 'stats', '--no-stream', '--format', '{{json .}}', application], + env=environment, text=True, capture_output=True, timeout=10, check=True) + values = json.loads(measured.stdout) + observations.append({'elapsedSeconds': round(time.monotonic() - started, 2), + 'cpuPercent': values['CPUPerc'], 'memoryUsage': values['MemUsage'], + 'memoryPercent': values['MemPerc'], 'networkIo': values['NetIO'], 'blockIo': values['BlockIO'], 'pids': values['PIDs']}) + if len(observations) % 10 == 0: + print('WORKLOAD_OBSERVATION elapsedSeconds=' + str(observations[-1]['elapsedSeconds']) + + ' memory=' + values['MemUsage'] + ' cpu=' + values['CPUPerc'], flush=True) + except (RuntimeError, ValueError, subprocess.TimeoutExpired, subprocess.CalledProcessError, OSError, KeyError) as error: + if not restarting.is_set(): + errors.append(type(error).__name__) + + def sampling(): + while not stop.is_set(): + sample() + # This is a measurement interval, not synchronization of application mutations. + stop.wait(2) + + sample() + thread = threading.Thread(target=sampling, daemon=True) + thread.start() + try: + result = subprocess.run([python, str(checkpoint.root / 'qualification/clients/workload.py')], + cwd=checkpoint.root, env=variables, text=True, capture_output=True, timeout=900) + if result.returncode != 0: + raise RuntimeError('Independent measured workload failed; no performance pass is recorded') + measured = json.loads(result.stdout) + assert measured['status'] == 'passed' + metadata['workload'] = measured + metadata['packagedIndependentClients'] = {} + for client_script in ('python_caldav.py', 'thunderbird.py'): + client_variables=variables.copy() + client_variables['BUSYSTACK_THUNDERBIRD_BINARY']=checkpoint.workload['thunderbird'] + client_result=subprocess.run([python,str(checkpoint.root/'qualification/clients'/client_script)], + cwd=checkpoint.root,env=client_variables,text=True,capture_output=True,timeout=360) + if client_result.returncode!=0: + raise RuntimeError('Required independent packaged-client qualification failed: '+client_script+' '+client_result.stdout[-2000:]) + client_record=json.loads(client_result.stdout);assert client_record['status']=='passed' + metadata['packagedIndependentClients'][client_script]=client_record + print('FINAL_PACKAGED_PYTHON_AND_THUNDERBIRD_CLIENTS_PASSED',flush=True) + metadata['capacity'] = capacity(checkpoint, environment, python, variables, restarting) + metadata['databaseAfter'] = database(operations, environment) + metadata['status'] = 'passed' + except Exception: + metadata['status'] = 'failed' + raise + finally: + stop.set() + thread.join(timeout=12) + sample() + metadata['finishedAt'] = datetime.now(timezone.utc).isoformat() + metadata['elapsedSeconds'] = round(time.monotonic() - started, 2) + metadata['containerSamples'] = observations + metadata['observerErrors'] = errors + if thread.is_alive() or errors or not observations: + metadata['status'] = 'failed' + output.write_text(json.dumps(metadata, indent=2) + '\n') + secret.unlink(missing_ok=True) + if metadata['status'] != 'passed': + raise RuntimeError('Required container measurements did not complete') + print('MEASURED_INDEPENDENT_CLIENT_WORKLOAD_PASSED artifact=' + str(output), flush=True) + + +def capacity(checkpoint, environment, python, variables, restarting): + """Owned disposable SQL population; every sync/query/property assertion uses real HTTPS.""" + operations=checkpoint.operations + identifier=str(uuid4()) + owner=checkpoint.owner + operations.sql(environment, f"""BEGIN; +INSERT INTO collections(id,owner_id,name,component) VALUES ('{identifier}','{owner}','Disposable measured capacity','VEVENT'); +WITH fixture AS ( + SELECT i,convert_to('BEGIN:VCALENDAR' || E'\r\n' || 'VERSION:2.0' || E'\r\n' + || 'PRODID:-//BusyStack capacity fixture//EN' || E'\r\n' || 'BEGIN:VEVENT' || E'\r\n' + || 'UID:capacity-' || i || E'\r\n' || 'DTSTAMP:20261027T000000Z' || E'\r\n' + || 'DTSTART:' || CASE WHEN i=5000 THEN '20261027' ELSE '20000101' END || 'T000000Z' || E'\r\n' + || 'DTEND:' || CASE WHEN i=5000 THEN '20261027' ELSE '20000101' END || 'T010000Z' || E'\r\n' + || 'SUMMARY:Capacity fixture ' || i || E'\r\n' || 'DESCRIPTION:Independent synthetic data' || E'\r\n' + || repeat(' ' || repeat('x',69) || E'\r\n',60) || 'END:VEVENT' || E'\r\n' || 'END:VCALENDAR' || E'\r\n','UTF8') AS body + FROM generate_series(1,10037) i +) +INSERT INTO calendar_resources(collection_id,name,uid,content,etag,revision) + SELECT '{identifier}','capacity-' || lpad(i::text,5,'0') || '.ics','capacity-' || i,body,'"' || encode(sha256(body),'hex') || '"',i FROM fixture; +INSERT INTO collection_changes(collection_id,revision,name,deleted) + SELECT collection_id,revision,name,false FROM calendar_resources WHERE collection_id='{identifier}'; +UPDATE collections SET revision=10037 WHERE id='{identifier}'; +COMMIT;""") + configured=variables.copy() + configured['BUSYSTACK_CAPACITY_URL']=checkpoint.base+'/dav/calendars/'+owner+'/'+identifier+'/' + process=None + try: + process=subprocess.Popen([python,str(checkpoint.root/'qualification/clients/capacity.py')],cwd=checkpoint.root,env=configured, + stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,text=True,bufsize=1) + selector=selectors.DefaultSelector();selector.register(process.stdout,selectors.EVENT_READ) + deadline=time.monotonic()+900;result=None + while time.monotonic()caldav-server' + real_version, 'caldav-server' + version)) + (root / '.gitignore').write_text('target/\n') + notes = root / f'docs/releases/{version}.md' + notes.parent.mkdir(parents=True, exist_ok=True) + notes.write_text('Qualified ' + version + ' notes. [Operations](../operations.md)\n') + source = directory / 'candidate' + source.mkdir() + sbom = make_jar(source / versions.jar_name(version), version) + (source / 'sbom.cdx.json').write_bytes(sbom) + receipt = {'version': version, 'jarSha256': candidate.sha256(source / versions.jar_name(version)), 'status': 'passed', + 'runtimeInputs': {name: candidate.sha256(root / name) for name in ('Dockerfile', 'compose.yaml')}, + 'clients': {name: {'status': 'passed'} for name in CLIENTS}} + receipt_file = root / versions.receipt_path(version) + receipt_file.parent.mkdir(parents=True, exist_ok=True) + candidate.write_json(receipt_file, receipt) + git(root, 'add', '.') + git(root, 'commit', '--quiet', '-m', 'Qualified application source') + sha = git(root, 'rev-parse', 'HEAD') + image_config, _ = make_image_archive(source / 'container.tar.gz', version, sha) + release_source.retain(sha, source / 'source.tar.gz', root) + record = {'schemaVersion': 2, 'version': version, 'repository': versions.REPOSITORY, + 'sourceCommit': sha, 'sourceTree': git(root, 'rev-parse', 'HEAD^{tree}'), 'platform': 'linux/amd64', + 'ciRun': 123, 'ciAttempt': 2, 'workflow': '.github/workflows/ci.yml', 'imageId': image_config, 'imageConfigDigest': image_config, + 'files': {p.name: candidate.sha256(p) for p in source.iterdir()}} + candidate.write_json(source / 'candidate.json', record) + for module in (candidate, bundle.candidate, publish.candidate, publish, builder): + stack.enter_context(patch.object(module, 'ROOT', root)) + yield directory, root, source, record + + +class VersionAndProvenanceTest(unittest.TestCase): + def test_final_image_promotion_preserves_exact_staged_manifest_bytes_for_each_version(self): + for version in VERSIONS: + with self.subTest(version=version): + data = b'{ "schemaVersion": 2, "mediaType": "application/vnd.oci.image.index.v1+json", "manifests": [] }\n' + digest = 'sha256:' + hashlib.sha256(data).hexdigest() + def response(request, timeout): + result = io.BytesIO(data) + result.headers = {'Content-Type': 'application/vnd.oci.image.index.v1+json', 'Docker-Content-Digest': digest} + if request.get_method() == 'PUT': + self.assertEqual(request.data, data) + self.assertTrue(request.full_url.endswith('/manifests/' + version)) + else: + self.assertTrue(request.full_url.endswith('/manifests/' + digest)) + return result + with patch.object(publish, 'manifest_digest', side_effect=[None, None, digest]), patch.object(publish, 'registry_headers', return_value={'Accept': 'application/vnd.oci.image.index.v1+json'}) as auth, patch.object(publish.urllib.request, 'urlopen', side_effect=response) as requests: + publish.promote_manifest(digest, version) + auth.assert_called_once_with(actions='pull,push') + self.assertEqual(requests.call_count, 2) + + def test_manifest_promotion_rejects_changed_bytes_and_conflicting_tags(self): + digest = 'sha256:' + '1' * 64 + result = io.BytesIO(b'{"mediaType":"application/vnd.oci.image.manifest.v1+json"}') + result.headers = {'Content-Type': 'application/vnd.oci.image.manifest.v1+json', 'Docker-Content-Digest': digest} + with patch.object(publish, 'manifest_digest', return_value=None), patch.object(publish, 'registry_headers', return_value={'Accept': result.headers['Content-Type']}), patch.object(publish.urllib.request, 'urlopen', return_value=result) as requests, self.assertRaisesRegex(AssertionError, 'bytes do not match'): + publish.promote_manifest(digest, '0.1.0') + self.assertEqual(requests.call_count, 1) + with patch.object(publish, 'manifest_digest', return_value='sha256:' + '2' * 64), patch.object(publish.urllib.request, 'urlopen') as requests, self.assertRaisesRegex(AssertionError, 'Conflicting'): + publish.promote_manifest(digest, '0.1.0') + requests.assert_not_called() + with patch.object(publish, 'manifest_digest', return_value=digest), patch.object(publish.urllib.request, 'urlopen') as requests: + publish.promote_manifest(digest, '0.1.0') + requests.assert_not_called() + + def test_saved_image_version_and_revision_are_verified(self): + for version, revision in (('0.2.0', SHA), ('0.1.0', SHA)): + with fixture() as (_, _, source, record): + make_image_archive(source / 'container.tar.gz', version, revision) + with self.assertRaises(AssertionError): + release_image.saved_identity(source / 'container.tar.gz', record['version'], record['sourceCommit']) + + def test_daemon_local_loaded_id_can_differ_without_rebuilding_or_changing_runtime(self): + with fixture() as (_, _, source, record): + digest, saved = release_image.saved_identity(source / 'container.tar.gz', record['version'], record['sourceCommit']) + loaded_id = 'sha256:' + 'b' * 64 + actual = {'Id': loaded_id, 'Os': 'linux', 'Architecture': 'amd64', 'Config': saved['config'], 'RootFS': {'Layers': []}} + with patch.object(release_image.subprocess, 'check_output', return_value='Loaded image ID: ' + loaded_id + '\n') as load, patch.object(candidate.builder, 'identity', return_value=actual) as inspect: + result = candidate.load_image(source, record) + self.assertEqual(result['Id'], loaded_id) + self.assertNotEqual(loaded_id, record['imageId']) + self.assertEqual(record['imageConfigDigest'], digest) + self.assertEqual(load.call_args.args[0][:3], ['docker', 'image', 'load']) + self.assertEqual(inspect.call_args.args[0], loaded_id) + actual['RootFS']['Layers'] = ['sha256:' + 'c' * 64] + with patch.object(release_image.subprocess, 'check_output', return_value='Loaded image ID: ' + loaded_id + '\n'), patch.object(candidate.builder, 'identity', return_value=actual), self.assertRaisesRegex(AssertionError, 'root filesystem'): + candidate.load_image(source, record) + actual['RootFS']['Layers'] = [] + actual['Config'] = {**saved['config'], 'Entrypoint': ['unqualified-command']} + with patch.object(release_image.subprocess, 'check_output', return_value='Loaded image ID: ' + loaded_id + '\n'), patch.object(candidate.builder, 'identity', return_value=actual), self.assertRaisesRegex(AssertionError, 'runtime configuration'): + candidate.load_image(source, record) + + def test_snapshot_uses_git_blobs_without_archive_line_ending_or_export_filters(self): + with fixture() as (_, root, _, _): + (root / '.gitattributes').write_text('*.cmd text eol=crlf\nNOTICE export-ignore\n') + (root / 'windows.cmd').write_bytes(b'@echo qualified\n') + git(root, 'add', '.') + git(root, 'commit', '--quiet', '-m', 'Qualified source with archive attributes') + sha = git(root, 'rev-parse', 'HEAD') + source = release_source.Snapshot(sha, root) + self.assertEqual(source.read('windows.cmd'), b'@echo qualified\n') + self.assertEqual(source.read('NOTICE'), (root / 'NOTICE').read_bytes()) + + def test_cli_rejects_explicitly_empty_candidate_and_derives_each_version(self): + for version in VERSIONS: + with self.subTest(version=version), fixture(version) as (_, root, _, _): + entry = root / 'scripts/release_version.py' + entry.write_bytes((ROOT / 'scripts/release_version.py').read_bytes()) + result = subprocess.run([sys.executable, str(entry), '--candidate', ''], capture_output=True, text=True) + self.assertNotEqual(result.returncode, 0) + self.assertIn('Expected a release candidate tag', result.stderr) + result = subprocess.run([sys.executable, str(entry), '--candidate', version + '-rc.1'], capture_output=True, text=True, check=True) + self.assertEqual(result.stdout.strip(), version + '-rc.1') + + def test_versions_paths_tags_and_maven_extraction(self): + for version in VERSIONS: + with self.subTest(version=version), fixture(version) as (_, root, source, record): + self.assertEqual(versions.resolve(root), version) + self.assertEqual(versions.resolve(root, source=record['sourceCommit']), version) + self.assertEqual(versions.tag(version), 'v' + version) + self.assertEqual(versions.jar_name(version), 'caldav-server-' + version + '.jar') + self.assertEqual(versions.candidate_tag(version, version + '-rc.12'), version + '-rc.12') + candidate.validate(source, version, record['sourceCommit']) + publish.check_client_receipt(record['files'][versions.jar_name(version)], record['sourceCommit'], version) + + def test_invalid_versions_and_candidates_reject(self): + for value in ('', '01.2.3', '1.02.3', '1.2.03', '1.2', '-1.2.3', '1.2.3-SNAPSHOT', 'v1.2.3', '1.2.3\n', '1.2.3', None): + with self.subTest(value=value), self.assertRaises(ValueError): + versions.stable(value) + for version in VERSIONS: + for tag in (None, '', 'latest', version, '9.9.9-rc.1', version + '-rc.0', version + '-rc.01', version + '-rc.1/other', version + '-rc.' + '1' * 128): + with self.subTest(version=version, tag=tag), self.assertRaises(ValueError): + publish.candidate_version(version, tag) + + def test_controller_version_cannot_replace_qualified_application(self): + with fixture() as (_, root, source, record): + sha = record['sourceCommit'] + pom = root / 'pom.xml' + pom.write_text(pom.read_text().replace('caldav-server0.1.0', 'caldav-server0.2.0')) + git(root, 'add', '.') + git(root, 'commit', '--quiet', '-m', 'Newer controller version') + controller = git(root, 'rev-parse', 'HEAD') + self.assertEqual(versions.resolve(root), '0.2.0') + self.assertEqual(versions.resolve(root, source=sha, requested='0.1.0'), '0.1.0') + with self.assertRaisesRegex(ValueError, 'differs from qualified'): + versions.resolve(root, source=sha, requested='0.2.0') + publish.verify_source(sha, controller, 'publish') + with self.assertRaises(AssertionError): + publish.verify_source(sha, controller, 'stage') + with self.assertRaises(AssertionError): + publish.verify_source(sha, '0' * 40, 'publish') + candidate.validate(source, '0.1.0', sha) + with self.assertRaises(ValueError): + candidate.validate(source, '0.2.0', sha) + + def test_every_bundle_input_is_isolated_from_newer_controller(self): + for version in VERSIONS: + with self.subTest(version=version), fixture(version) as (directory, root, source, record): + digest = 'sha256:' + '1' * 64 + first = bundle.build(source, digest, directory / 'first') + changed = ['compose.yaml', 'deployment/INSTALL.md', 'deployment/nginx-root.conf.example', + 'deployment/nginx-subpath.conf.example', f'docs/releases/{version}.md', + 'scripts/init-db.sh', 'scripts/backup.sh', 'scripts/restore.sh', 'LICENSE', + 'NOTICE', 'THIRD-PARTY-NOTICES', 'docs/operations.md'] + for name in changed: + (root / name).write_text('Unqualified controller replacement\n') + git(root, 'add', '.') + git(root, 'commit', '--quiet', '-m', 'Change controller payload files') + second = bundle.build(source, digest, directory / 'second') + self.assertEqual(first, second) + for path in (directory / 'first').iterdir(): + self.assertEqual(path.read_bytes(), (directory / 'second' / path.name).read_bytes(), path.name) + notes = publish.release_notes(record['sourceCommit'], version) + self.assertIn('Qualified ' + version, notes) + self.assertNotIn('Unqualified', notes) + self.assertIn('/blob/' + record['sourceCommit'] + '/docs/operations.md', notes) + manifest = bundle.validate_assets(directory / 'first', version, record['sourceCommit']) + with tarfile.open(directory / 'first' / versions.deployment_name(version)) as archive: + prefix = 'caldav-server-' + version + '-deploy/' + compose = archive.extractfile(prefix + 'compose.yaml').read().decode() + self.assertIn(':' + version + '@' + digest, compose) + self.assertNotIn('build:', compose) + installation = archive.extractfile(prefix + 'INSTALL.md').read().decode() + self.assertIn(versions.jar_name(version), installation) + self.assertIn(versions.deployment_name(version), installation) + self.assertNotIn('@VERSION@', installation) + self.assertIn(prefix + versions.receipt_path(version), archive.getnames()) + self.assertFalse(any('website-handoff' in n or 'docs/development/' in n for n in archive.getnames())) + self.assertEqual(manifest['container']['localTestedImageId'], record['imageId']) + + def test_snapshot_changes_and_missing_source_inputs_reject(self): + with fixture() as (directory, root, source, record): + snapshot = source / 'source.tar.gz' + data = release_source.files(snapshot.read_bytes()) + data['deployment/INSTALL.md'] = b'changed' + snapshot.write_bytes(bundle.tar_bytes(data)) + record['files']['source.tar.gz'] = candidate.sha256(snapshot) + candidate.write_json(source / 'candidate.json', record) + with self.assertRaisesRegex(AssertionError, 'differs from qualified'): + candidate.validate(source) + qualified = release_source.Snapshot(record['sourceCommit'], root) + with self.assertRaisesRegex(AssertionError, 'Missing qualified source input'): + qualified.read('missing-required-deployment-file') + + def test_missing_artifacts_wrong_jar_version_and_checksums_reject(self): + with fixture() as (_, root, source, record): + jar = source / versions.jar_name(record['version']) + make_jar(jar, '1.0.0') + with self.assertRaisesRegex(AssertionError, 'checksum'): + candidate.validate(source) + record['files'][jar.name] = candidate.sha256(jar) + candidate.write_json(source / 'candidate.json', record) + with self.assertRaisesRegex(AssertionError, 'JAR implementation version'): + candidate.validate(source) + jar.unlink() + with self.assertRaisesRegex(AssertionError, 'Unexpected candidate contents'): + candidate.validate(source) + + def test_client_qualification_cannot_use_different_executable_or_runtime(self): + with fixture() as (_, root, source, record): + sha = record['sourceCommit'] + with self.assertRaisesRegex(AssertionError, 'another executable'): + publish.check_client_receipt('8' * 64, sha, record['version']) + receipt_path = root / versions.receipt_path(record['version']) + receipt = json.loads(receipt_path.read_text()) + receipt['runtimeInputs']['Dockerfile'] = '0' * 64 + candidate.write_json(receipt_path, receipt) + git(root, 'add', '.') + git(root, 'commit', '--quiet', '-m', 'Invalid qualification input') + with self.assertRaisesRegex(AssertionError, 'runtime inputs changed'): + publish.check_client_receipt(record['files'][versions.jar_name(record['version'])], git(root, 'rev-parse', 'HEAD'), record['version']) + + def test_developer_build_resolves_identity_without_version_input(self): + for version in VERSIONS: + with self.subTest(version=version), fixture(version) as (_, root, source, record): + target = root / 'target' + target.mkdir() + jar = target / versions.jar_name(version) + shutil.copyfile(source / jar.name, jar) + # Include a real license entry for the generated image input directory. + with zipfile.ZipFile(jar, 'a') as archive: + archive.writestr('META-INF/licenses/example.txt', 'fixture license') + real_run = subprocess.run + def execute(command, *args, **kwargs): + return subprocess.CompletedProcess(command, 0) if command[0] == 'docker' else real_run(command, *args, **kwargs) + with patch.object(sys, 'argv', ['build-image', '--jar', str(jar)]), patch.object(builder.subprocess, 'run', side_effect=execute) as run, patch.object(builder, 'identity', return_value={'Id': record['imageId']}): + builder.main() + command = run.call_args.args[0] + self.assertIn('APPLICATION_VERSION=' + version, command) + self.assertIn('SOURCE_REVISION=' + record['sourceCommit'], command) + self.assertIn('busystack-caldav:' + version, command) + generated = root / 'target/docker-context/.image-input' + self.assertEqual((generated / 'application.jar').read_bytes(), jar.read_bytes()) + environment = builder.compose_environment('busystack-caldav:' + version, generated.parent, version, record['sourceCommit']) + self.assertEqual(environment['BUSYSTACK_APPLICATION_IMAGE'], 'busystack-caldav:' + version) + self.assertEqual(environment['BUSYSTACK_BUILD_VERSION'], version) + + def test_image_metadata_and_packaged_checksum_must_agree(self): + labels = {'org.opencontainers.image.version': '0.1.0', 'org.opencontainers.image.revision': SHA, + 'org.opencontainers.image.source': 'https://github.com/' + versions.REPOSITORY, 'org.opencontainers.image.licenses': 'Apache-2.0'} + config = {'Config': {'Labels': labels}, 'Os': 'linux', 'Architecture': 'amd64'} + for key in ('org.opencontainers.image.version', 'org.opencontainers.image.revision'): + bad = copy.deepcopy(config) + bad['Config']['Labels'][key] = '' + with patch.object(builder.subprocess, 'check_output', return_value=json.dumps([bad]).encode()), self.assertRaises(AssertionError): + builder.identity('image', '0.1.0', SHA, '1' * 64) + with patch.object(builder.subprocess, 'check_output', side_effect=[json.dumps([config]).encode(), '2' * 64 + ' application.jar']), self.assertRaisesRegex(AssertionError, 'exact expected JAR'): + builder.identity('image', '0.1.0', SHA, '1' * 64) + + def test_staged_assets_are_consumed_exactly_and_tampering_rejects(self): + with fixture() as (directory, root, source, record): + staged = directory / 'stage' + staged.mkdir() + digest = 'sha256:' + '1' * 64 + manifest = bundle.build(source, digest, staged / 'assets') + notes = staged / 'release-notes.md' + notes.write_text(publish.release_notes(record['sourceCommit'], record['version'])) + installation = {'status': 'passed', 'version': record['version'], 'sourceCommit': record['sourceCommit'], + 'jarSha256': manifest['jar']['sha256'], 'registryManifestDigest': digest, + 'deploymentSha256': manifest['deployment']['sha256']} + candidate.write_json(staged / 'installation.json', installation) + stage = {'version': record['version'], 'candidateVersion': record['version'] + '-rc.1', 'sourceCommit': record['sourceCommit'], 'verificationRun': 123, + 'verificationAttempt': 2, 'sourceSnapshotSha256': record['files']['source.tar.gz'], 'registryDigest': digest, + 'assets': {p.name: candidate.sha256(p) for p in (staged / 'assets').iterdir()}, + 'installationSha256': candidate.sha256(staged / 'installation.json'), 'releaseNotesSha256': candidate.sha256(notes)} + publish.validate_stage(staged, stage, record, {'id': 123, 'run_attempt': 2}) + (root / 'deployment/INSTALL.md').write_text('new controller instructions') + git(root, 'add', '.') + git(root, 'commit', '--quiet', '-m', 'Later controller') + publish.validate_stage(staged, stage, record, {'id': 123, 'run_attempt': 2}) + for field, value in (('version', '0.2.0'), ('sourceCommit', SHA), ('verificationAttempt', 3)): + with self.subTest(field=field), self.assertRaises(AssertionError): + publish.validate_stage(staged, stage | {field: value}, record, {'id': 123, 'run_attempt': 2}) + notes.write_text('changed staging notes') + with self.assertRaisesRegex(AssertionError, 'notes changed'): + publish.validate_stage(staged, stage, record, {'id': 123, 'run_attempt': 2}) + + def test_publication_resumes_interrupted_upload_with_newer_controller_and_exact_staged_assets(self): + with fixture() as (directory, root, source, record): + staged = directory / 'stage' + staged.mkdir() + digest = 'sha256:' + '1' * 64 + manifest = bundle.build(source, digest, staged / 'assets') + notes = staged / 'release-notes.md' + notes.write_text(publish.release_notes(record['sourceCommit'], record['version'])) + candidate.write_json(staged / 'installation.json', {'status': 'passed', 'version': record['version'], + 'sourceCommit': record['sourceCommit'], 'jarSha256': manifest['jar']['sha256'], + 'deploymentSha256': manifest['deployment']['sha256'], 'registryManifestDigest': digest}) + stage = {'version': record['version'], 'candidateVersion': record['version'] + '-rc.1', + 'sourceCommit': record['sourceCommit'], 'verificationRun': 123, 'verificationAttempt': 2, + 'sourceSnapshotSha256': record['files']['source.tar.gz'], 'registryDigest': digest, + 'assets': {p.name: candidate.sha256(p) for p in (staged / 'assets').iterdir()}, + 'releaseNotesSha256': candidate.sha256(notes), 'installationSha256': candidate.sha256(staged / 'installation.json')} + candidate.write_json(staged / 'stage.json', stage) + pom = root / 'pom.xml' + pom.write_text(pom.read_text().replace('caldav-server0.1.0', 'caldav-server0.2.0')) + (root / 'deployment/INSTALL.md').write_text('Unqualified controller content') + git(root, 'add', '.') + git(root, 'commit', '--quiet', '-m', 'Later trusted controller') + controller = git(root, 'rev-parse', 'HEAD') + state = {'tag': False, 'image': False, 'release': None, 'interrupted': False} + run = {'repository': {'full_name': publish.REPO}, 'head_repository': {'full_name': publish.REPO}, + 'head_sha': record['sourceCommit'], 'head_branch': 'main', 'status': 'completed', 'conclusion': 'success', + 'event': 'push', 'name': 'verify', 'path': '.github/workflows/ci.yml', 'id': 123, 'run_attempt': 2} + def api(path, missing=False, method='GET', fields=None): + if path.endswith('/actions/runs/123'): return run + if path.endswith('/attempts/2/jobs?per_page=100&page=1'): + return {'jobs': [{'name': name, 'status': 'completed', 'conclusion': 'success'} for name in publish.REQUIRED]} + if path.endswith('/actions/runs/456'): + return run | {'id': 456, 'run_attempt': 1, 'event': 'workflow_dispatch', 'name': 'release', 'path': '.github/workflows/release.yml'} + if 'packages/container/' in path: return {'visibility': 'public', 'repository': {'full_name': publish.REPO}} + if '/git/ref/tags/' in path: return {'object': {'type': 'commit', 'sha': record['sourceCommit']}} if state['tag'] else None + if path.endswith('/git/refs') and method == 'POST': + self.assertEqual(fields, {'ref': 'refs/tags/' + versions.tag(record['version']), 'sha': record['sourceCommit']}) + state['tag'] = True + return {} + if '/releases?per_page=' in path: return [state['release']] if state['release'] else [] + if path.endswith('/releases/77'): return state['release'] + self.fail('Unexpected API operation: ' + path) + def download(run_id, name, destination): + shutil.copytree(source if run_id == 123 else staged, destination) + real_command = publish.command + config = {'Id': record['imageId'], 'Os': 'linux', 'Architecture': 'amd64', 'Config': {'Labels': { + 'org.opencontainers.image.version': record['version'], 'org.opencontainers.image.revision': record['sourceCommit']}}} + def command(*args): + if args[:3] == ('docker', 'image', 'inspect'): return json.dumps([config]) + if args[:2] == ('docker', 'run'): return record['files'][versions.jar_name(record['version'])] + ' application.jar' + if args[:3] == ('gh', 'release', 'verify'): return '{}' + return real_command(*args) + real_run = subprocess.run + def execute(command, *args, **kwargs): + if command[0] == 'git': return real_run(command, *args, **kwargs) + self.assertNotIn(command[:2], (['docker', 'push'], ['docker', 'tag'])) + if command[:3] == ['gh', 'release', 'create']: + self.assertEqual(command[3], 'v0.1.0') + state['release'] = {'id': 77, 'tag_name': 'v0.1.0', 'draft': True, 'assets': [], + 'body': notes.read_text(), 'html_url': 'https://example.invalid/release'} + if command[:3] == ['gh', 'release', 'upload']: + asset = Path(command[4]) + if len(state['release']['assets']) == 1 and not state['interrupted']: + state['interrupted'] = True + raise subprocess.CalledProcessError(1, command) + state['release']['assets'].append({'name': asset.name, 'digest': 'sha256:' + candidate.sha256(asset)}) + if command[:3] == ['gh', 'release', 'edit']: + state['release'].update(draft=False, immutable=True) + return subprocess.CompletedProcess(command, 0) + environment = {'GITHUB_REPOSITORY': publish.REPO, 'GITHUB_REF': 'refs/heads/main', 'GITHUB_SHA': controller} + def promote(staged_digest, version): + self.assertEqual((staged_digest, version), (digest, record['version'])) + state['image'] = True + with patch.dict('os.environ', environment), patch.object(publish, 'api', side_effect=api), patch.object(publish, 'download', side_effect=download), patch.object(publish, 'command', side_effect=command), patch.object(publish.subprocess, 'run', side_effect=execute), patch.object(publish.candidate, 'load_image', return_value=config), patch.object(publish, 'manifest_digest', side_effect=lambda tag, anonymous=False: digest if tag != record['version'] or state['image'] else None), patch.object(publish, 'promote_manifest', side_effect=promote) as promotion, patch.object(publish.bundle, 'build', side_effect=AssertionError('Publication must not regenerate assets')): + for attempt in (1, 2): + arguments = ['publish', '--version', record['version'], '--verification-run', '123', '--source-sha', record['sourceCommit'], + '--phase', 'publish', '--stage-run', '456', '--immutability-confirmed', '--work', str(directory / ('publish-' + str(attempt)))] + with patch.object(sys, 'argv', arguments): + if attempt == 1: + with self.assertRaises(subprocess.CalledProcessError): publish.main() + else: + publish.main() + promotion.assert_called_once_with(digest, record['version']) + self.assertTrue(state['release']['immutable']) + self.assertFalse(state['release']['draft']) + self.assertEqual({a['name']: a['digest'] for a in state['release']['assets']}, {name: 'sha256:' + checksum for name, checksum in stage['assets'].items()}) + + +class PublicationGateTest(unittest.TestCase): + def setUp(self): + self.run = {'repository': {'full_name': publish.REPO}, 'head_repository': {'full_name': publish.REPO}, + 'path': '.github/workflows/ci.yml', 'name': 'verify', 'status': 'completed', + 'conclusion': 'success', 'head_sha': SHA, 'head_branch': 'main', 'event': 'push'} + self.jobs = [{'name': name, 'status': 'completed', 'conclusion': 'success'} for name in publish.REQUIRED] + + def test_correct_run_passes(self): + publish.verification(self.run, self.jobs, SHA) + + def test_release_lookup_finds_drafts_and_handles_pagination(self): + draft = {'id': 123, 'tag_name': 'v0.2.0', 'draft': True, 'assets': []} + published = [{'id': index, 'tag_name': 'v9.' + str(index), 'draft': False} for index in range(100)] + with patch.object(publish, 'api', side_effect=[published, [draft]]) as api: + self.assertEqual(publish.release_for_tag('v0.2.0'), draft) + self.assertEqual(api.call_count, 2) + with patch.object(publish, 'api', return_value=[]): + self.assertIsNone(publish.release_for_tag('v0.2.0')) + with patch.object(publish, 'api', return_value=[draft, draft | {'id': 124}]), self.assertRaises(AssertionError): + publish.release_for_tag('v0.2.0') + + def test_wrong_sha_failed_incomplete_and_untrusted_runs_reject(self): + for key, value in [('head_sha', 'b' * 40), ('status', 'in_progress'), ('conclusion', 'failure'), + ('path', '.github/workflows/other.yml'), ('head_branch', 'fork'), ('event', 'pull_request')]: + with self.subTest(key=key), self.assertRaises(AssertionError): + publish.verification(self.run | {key: value}, self.jobs, SHA) + for key in ('repository', 'head_repository'): + with self.subTest(key=key), self.assertRaises(AssertionError): + publish.verification(self.run | {key: {'full_name': 'attacker/repo'}}, self.jobs, SHA) + + def test_missing_failed_or_skipped_required_job_rejects(self): + with self.assertRaises(AssertionError): + publish.verification(self.run, self.jobs[:-1], SHA) + for conclusion in ('failure', 'skipped', 'cancelled', None): + jobs = copy.deepcopy(self.jobs) + jobs[0]['conclusion'] = conclusion + with self.subTest(conclusion=conclusion), self.assertRaises(AssertionError): + publish.verification(self.run, jobs, SHA) + + def test_conflicting_version_rejects_matching_retry_passes(self): + publish.matching_version(None, 'sha256:' + '1' * 64) + publish.matching_version('sha256:' + '1' * 64, 'sha256:' + '1' * 64) + with self.assertRaises(AssertionError): + publish.matching_version('sha256:' + '2' * 64, 'sha256:' + '1' * 64) + + def test_conflicting_tag_rejects(self): + with patch.object(publish, 'api', return_value={'object': {'type': 'commit', 'sha': 'b' * 40}}), self.assertRaisesRegex(AssertionError, 'Conflicting tag'): + publish.check_release(SHA, '1.0.0') + + def test_existing_published_release_must_be_immutable(self): + tag = {'object': {'type': 'commit', 'sha': SHA}} + with patch.object(publish, 'api', return_value=tag), patch.object(publish, 'release_for_tag', return_value={'draft': False, 'immutable': False}), self.assertRaisesRegex(AssertionError, 'not immutable'): + publish.check_release(SHA, '1.0.0') + + def test_registry_index_accepts_only_tested_amd64_and_attestations(self): + digest = 'sha256:' + '4' * 64 + index = {'manifests': [{'digest': 'sha256:' + '5' * 64, 'platform': {'os': 'linux', 'architecture': 'amd64'}}, + {'digest': 'sha256:' + '6' * 64, 'platform': {'os': 'unknown', 'architecture': 'unknown'}, + 'annotations': {'vnd.docker.reference.type': 'attestation-manifest'}}]} + with patch.object(publish, 'command', side_effect=[json.dumps(index), json.dumps({'config': {'digest': 'sha256:' + '7' * 64}})]): + publish.remote_image_matches({'imageId': digest, 'imageConfigDigest': 'sha256:' + '7' * 64}, digest) + index['manifests'][0]['platform']['architecture'] = 'arm64' + with patch.object(publish, 'command', return_value=json.dumps(index)), self.assertRaises(AssertionError): + publish.remote_image_matches({'imageId': digest, 'imageConfigDigest': 'sha256:' + '7' * 64}, digest) + + def test_missing_remote_artifact_rejects_before_download(self): + with patch.object(publish, 'api', return_value={'artifacts': []}), patch.object(publish.subprocess, 'run') as run: + with self.assertRaisesRegex(AssertionError, 'Missing/ambiguous artifact'): + publish.download(123, 'verified-candidate-123-1', Path('/unused')) + run.assert_not_called() + def test_active_source_has_no_removed_copies_or_patch_payload(self): + integrity.source() + + def test_restore_corpus_remains_available_for_source_free_deployment(self): + from types import SimpleNamespace + import hashlib + spec = importlib.util.spec_from_file_location('bundle_audit_corpus', ROOT / 'qualification/passkeys/audit_corpus.py') + corpus_module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(corpus_module) + with tempfile.TemporaryDirectory(prefix='extracted deployment ') as temporary: + deployment = Path(temporary) + self.assertFalse((deployment / 'src').exists()) + corpus = corpus_module.Corpus(SimpleNamespace(root=deployment)) + for name in corpus.VALID: + self.assertIn(b'BEGIN:VCALENDAR', (corpus.fixture / name).read_bytes().upper()) + for zone, (original, _) in corpus.OBSERVANCE_UPLOADS.items(): + data = (corpus.fixture.parent / 'observance-until' / ('thunderbird159-' + zone + '.ics')).read_bytes() + self.assertEqual(hashlib.sha256(data).hexdigest(), original) + + def test_packaged_modified_dependency_and_removed_classes_reject(self): + with tempfile.TemporaryDirectory() as temporary: + root = Path(temporary) + lock = root / 'lock.json' + import hashlib + payload = io.BytesIO() + with zipfile.ZipFile(payload, 'w') as nested: + nested.writestr('Official.class', b'official upstream bytecode fixture') + original = payload.getvalue() + candidate.write_json(lock, {'artifacts': {'example.jar': {'sha256': hashlib.sha256(original).hexdigest()}}}) + jar = root / 'application.jar' + with zipfile.ZipFile(jar, 'w') as archive: + archive.writestr('BOOT-INF/lib/example.jar', original) + integrity.inspect(jar, lock) + with zipfile.ZipFile(jar, 'w') as archive: + archive.writestr('BOOT-INF/lib/example.jar', original + b'patched') + with self.assertRaises(AssertionError): + integrity.inspect(jar, lock) + with zipfile.ZipFile(jar, 'w') as archive: + archive.writestr('BOOT-INF/lib/example.jar', original) + archive.writestr('BOOT-INF/classes/renamed/Ical4jZoneRulesBuilder.class', b'stale') + with self.assertRaises(AssertionError): + integrity.inspect(jar, lock) + + +if __name__ == '__main__': + unittest.main() diff --git a/qualification/security/README.md b/qualification/security/README.md new file mode 100644 index 0000000..c874682 --- /dev/null +++ b/qualification/security/README.md @@ -0,0 +1,38 @@ +# Runtime dependency advisory checks + +The [qualification index](../evidence/README.md) identifies version-specific verification and archived historical results. + +Generate the runtime CycloneDX inventory with `./mvnw package`, then run: + +```sh +python3 -m unittest discover -s qualification/security -p 'test_*.py' +python3 scripts/check-advisories.py +``` + +The required CI gate reads resolved Maven group/artifact/version coordinates from the generated JSON and queries the official [OSV batch API](https://google.github.io/osv.dev/post-v1-querybatch/). It sends public dependency coordinates only. It follows each query's pagination independently, verifies complete result cardinality, rejects nonadvancing pagination, and bounds response sizes and page counts. Feed/configuration errors return exit 2 and an error report; any returned advisory returns exit 1. Exit 0 means the API returned no advisories for those exact queried versions at the recorded time. It does not mean the code is certified safe or that the database knows every vulnerability. + +`target/osv-runtime-advisories.json` records the inventory SHA-256, coordinates, completeness/page metadata, advisory identifiers and timestamp. Non-Maven components are counted separately; this check covers the Maven runtime graph, not container operating-system packages or application-code review. The job does not use an advisory-service account, secret, cached green result or a network-error bypass. + +The positive control is inventory data for `org.apache.logging.log4j:log4j-core:2.14.1`. It does not download, execute or add that dependency to the application. It must return exit 1 and include the known Log4j advisory identifier `GHSA-jfh8-c2jp-5v3q`; an API error or an arbitrary nonzero exit is insufficient. Run it explicitly with: + +```sh +python3 scripts/check-advisories.py \ + --sbom qualification/security/positive-control.cdx.json \ + --output target/osv-positive-control.json +``` + +The gate's unit tests cover partial pagination, missing results, repeated tokens, feed errors, detected advisories and empty inventories. CI runs the real positive control and the current application inventory, and retains the reports. + +## Additional NVD audit + +CI additionally invokes pinned OWASP Dependency-Check Maven 13.0.0 if an `NVD_API_KEY` secret is configured. This is additional coverage; OSV is the required advisory gate. A missing key is reported as an unexecuted NVD audit, not a passed NVD audit. When invoked, NVD errors or CVSS scores at least 7 fail its step. OSS Index is explicitly disabled because that source needs a separate credential. + +Earlier unauthenticated NVD attempts failed during feed initialization and did +not produce a passing audit. Their diagnostics remain in the historical archive. +A working configured feed is required for this optional additional check. + +## Required final-container OS package scan + +After building the final source image, run `python3 scripts/check-container-advisories.py --image busystack-caldav:ci`. The runner downloads **Trivy 0.76.1** from its official release, verifies archive SHA-256 `c7cbe3137fb8a48a17574bede3dd6beb16742d1e9772929940fc90082c39d9c0`, then scans only the locally identified Docker image's OS packages. It records immutable image identity/labels, scanner version, complete package inventory, all reported findings, feed timestamps, database and raw-report hashes. The feed must be no more than 48 hours old and identify actual OS packages; update failures or missing output fail with exit 2. HIGH, CRITICAL and UNKNOWN findings fail with exit 1, including unfixed vulnerabilities. Missing or unrecognized severity fails closed; lower recognized severities remain visible for disposition. The scanner receives an empty private config/ignore file, all standard severities and explicit ignore-unfixed=false, with TRIVY_* shell overrides removed; no package exclusion or advisory suppression is used. + +This coverage concerns Ubuntu OS packages, distinct from the required Maven advisory gate. It is not an analysis of application source or a complete vulnerability analysis of manually installed JDK binaries. CI retains raw OS inventory/findings and the separate scan summary; a failed advisory download is never recorded as a clean scan. Trivy's own version source is the [official release](https://github.com/aquasecurity/trivy/releases/tag/v0.76.1). diff --git a/qualification/security/positive-control.cdx.json b/qualification/security/positive-control.cdx.json new file mode 100644 index 0000000..f887d07 --- /dev/null +++ b/qualification/security/positive-control.cdx.json @@ -0,0 +1,14 @@ +{ + "bomFormat": "CycloneDX", + "specVersion": "1.6", + "version": 1, + "components": [ + { + "type": "library", + "group": "org.apache.logging.log4j", + "name": "log4j-core", + "version": "2.14.1", + "purl": "pkg:maven/org.apache.logging.log4j/log4j-core@2.14.1" + } + ] +} diff --git a/qualification/security/test_advisory_gate.py b/qualification/security/test_advisory_gate.py new file mode 100644 index 0000000..2b7806c --- /dev/null +++ b/qualification/security/test_advisory_gate.py @@ -0,0 +1,66 @@ +"""Adversarial feed cases for a CI gate that must never report partial data as success.""" + +import importlib.util +import json +from pathlib import Path +import tempfile +import unittest +import urllib.error + +ROOT = Path(__file__).resolve().parents[2] +SPEC = importlib.util.spec_from_file_location("advisories", ROOT / "scripts/check-advisories.py") +ADVISORIES = importlib.util.module_from_spec(SPEC) +SPEC.loader.exec_module(ADVISORIES) + + +class AdvisoryGateTest(unittest.TestCase): + def test_only_pending_query_is_paginated_and_advisories_are_retained(self): + packages, _ = ADVISORIES.coordinates({"bomFormat": "CycloneDX", "components": [ + {"purl": "pkg:maven/test/a@1", "group": "test", "name": "a", "version": "1"}, + {"purl": "pkg:maven/test/b@1", "group": "test", "name": "b", "version": "1"}, + ]}) + requests = [] + + def feed(payload): + requests.append(payload) + if len(requests) == 1: + return {"results": [{"vulns": [{"id": "ADVISORY-1"}], "next_page_token": "next"}, {}]} + self.assertEqual(payload["queries"], [{"package": {"ecosystem": "Maven", "name": "test:a"}, "version": "1", "page_token": "next"}]) + return {"results": [{"vulns": [{"id": "ADVISORY-2"}]}]} + + ADVISORIES.query(packages, feed) + self.assertTrue(all(package["complete"] for package in packages)) + self.assertEqual([value["id"] for value in packages[0]["advisories"]], ["ADVISORY-1", "ADVISORY-2"]) + + def test_missing_results_and_repeated_pagination_tokens_fail(self): + for feed in ( + lambda _: {"results": []}, + lambda _: {"results": [{"next_page_token": "unchanged"}]}, + ): + with self.subTest(feed=feed): + packages = [{"name": "test:a", "version": "1", "advisories": [], "pages": 0, "complete": False}] + with self.assertRaises(ValueError): + ADVISORIES.query(packages, feed) + + def test_feed_failure_and_returned_advisory_have_distinct_nonzero_outcomes(self): + with tempfile.TemporaryDirectory() as directory: + sbom, output = Path(directory) / "input.json", Path(directory) / "result.json" + sbom.write_text(json.dumps({"bomFormat": "CycloneDX", "components": [ + {"purl": "pkg:maven/test/a@1", "group": "test", "name": "a", "version": "1"}, + ]})) + + def unavailable(_): + raise urllib.error.URLError("feed unavailable") + + self.assertEqual(ADVISORIES.run(sbom, output, unavailable), 2) + self.assertEqual(json.loads(output.read_text())["status"], "error") + self.assertEqual(ADVISORIES.run(sbom, output, lambda _: {"results": [{"vulns": [{"id": "ADVISORY-1"}]}]}), 1) + self.assertEqual(json.loads(output.read_text())["status"], "advisories-returned") + + def test_empty_inventory_cannot_pass(self): + with self.assertRaises(ValueError): + ADVISORIES.coordinates({"bomFormat": "CycloneDX", "components": []}) + + +if __name__ == "__main__": + unittest.main() diff --git a/qualification/security/test_container_advisories.py b/qualification/security/test_container_advisories.py new file mode 100644 index 0000000..e7d68ab --- /dev/null +++ b/qualification/security/test_container_advisories.py @@ -0,0 +1,44 @@ +import importlib.util +from pathlib import Path +import unittest +import os +from unittest.mock import patch +from datetime import datetime, timedelta, timezone + +spec=importlib.util.spec_from_file_location('container_gate',Path(__file__).resolve().parents[2]/'scripts/check-container-advisories.py') +gate=importlib.util.module_from_spec(spec);spec.loader.exec_module(gate) + +class ContainerGateTest(unittest.TestCase): + def setUp(self): + self.now=datetime.now(timezone.utc) + self.feed={'UpdatedAt':self.now.isoformat()} + self.doc={'Metadata':{'OS':{'Family':'ubuntu','Name':'26.04'}},'Results':[{'Class':'os-pkgs','Packages':[{'Name':'libc6'}]}]} + def test_valid_inventory_is_not_empty_clean_placeholder(self): + self.assertEqual(gate.analyze(self.doc,self.feed,self.now)['status'],'passed') + def test_missing_inventory_fails_closed(self): + self.doc['Results'][0]['Packages']=[] + with self.assertRaises(ValueError): gate.analyze(self.doc,self.feed,self.now) + def test_stale_feed_is_not_passed(self): + self.feed['UpdatedAt']=(self.now-timedelta(days=3)).isoformat() + with self.assertRaises(ValueError): gate.analyze(self.doc,self.feed,self.now) + def test_high_unfixed_advisory_blocks_without_suppression(self): + self.doc['Results'][0]['Vulnerabilities']=[{'VulnerabilityID':'test','Severity':'HIGH','Status':'affected'}] + result=gate.analyze(self.doc,self.feed,self.now) + self.assertEqual(result['status'],'advisories-returned');self.assertEqual(result['blockingFindings'],1) + def test_lower_findings_remain_visible(self): + self.doc['Results'][0]['Vulnerabilities']=[{'VulnerabilityID':'test','Severity':'MEDIUM'}] + result=gate.analyze(self.doc,self.feed,self.now) + self.assertEqual(result['status'],'passed');self.assertEqual(len(result['findings']),1) + + def test_missing_or_unrecognized_severity_fails_closed(self): + for severity in (None, '', 'UNREVIEWED', 'high', 17): + with self.subTest(severity=severity): + item={'VulnerabilityID':'test'} + if severity is not None: item['Severity']=severity + self.doc['Results'][0]['Vulnerabilities']=[item] + with self.assertRaisesRegex(ValueError,'severity'): + gate.analyze(self.doc,self.feed,self.now) + + def test_shell_overrides_cannot_silently_suppress_or_weaken_the_scan(self): + with patch.dict(os.environ,{'TRIVY_IGNORE_UNFIXED':'true','TRIVY_SEVERITY':'CRITICAL','TRIVY_INSECURE':'true','DOCKER_HOST':'unix:///fixture/docker.sock'},clear=True): + self.assertEqual(gate.scanner_environment(),{'DOCKER_HOST':'unix:///fixture/docker.sock'}) diff --git a/scripts/backup.sh b/scripts/backup.sh new file mode 100755 index 0000000..2a7ccb4 --- /dev/null +++ b/scripts/backup.sh @@ -0,0 +1,23 @@ +#!/usr/bin/env bash +set -euo pipefail +umask 077 + +if [[ $# != 1 ]]; then + printf 'Usage: %s NEW_ARCHIVE_PATH\n' "$0" >&2 + exit 2 +fi +archive=$1 +if [[ -e "$archive" || -e "$archive.sha256" ]]; then + printf 'Refusing to replace an existing backup or checksum.\n' >&2 + exit 2 +fi +temporary=$(mktemp "${archive}.partial.XXXXXXXX") +trap 'rm -f -- "$temporary"' EXIT +docker compose exec -T database pg_dump --username postgres --dbname busystack --format custom --no-owner --no-acl > "$temporary" +test -s "$temporary" +ln -- "$temporary" "$archive" +rm -- "$temporary" +archive_directory=$(dirname -- "$archive") +archive_filename=$(basename -- "$archive") +(cd "$archive_directory" && sha256sum -- "$archive_filename") > "$archive.sha256" +printf 'Created database archive and checksum. Back up deployment configuration and secret files separately.\n' diff --git a/scripts/build-image.py b/scripts/build-image.py new file mode 100644 index 0000000..66801ab --- /dev/null +++ b/scripts/build-image.py @@ -0,0 +1,102 @@ +#!/usr/bin/env python3 +"""Build and verify the exact Maven JAR and OCI identity; optionally run development Compose.""" +import argparse +import hashlib +import json +import os +from pathlib import Path +import shutil +import subprocess +import sys +import zipfile + +sys.path.insert(0, str(Path(__file__).resolve().parent)) +import release_version as versions + +ROOT = versions.ROOT + + +def inspect_jar(path, version): + with zipfile.ZipFile(path) as jar: + names = jar.namelist() + assert len(names) == len(set(names)), 'Duplicate JAR entries' + manifest = dict(line.split(': ', 1) for line in jar.read('META-INF/MANIFEST.MF').decode().splitlines() if ': ' in line) + assert manifest.get('Implementation-Version') == version, 'JAR implementation version differs from Maven' + coordinates = dict(line.split('=', 1) for line in jar.read('META-INF/maven/org.busystack/caldav-server/pom.properties').decode().splitlines() if '=' in line and not line.startswith('#')) + assert coordinates == {'groupId': 'org.busystack', 'artifactId': 'caldav-server', 'version': version}, 'JAR Maven coordinates differ from qualified project' + sbom = jar.read('META-INF/sbom/application.cdx.json') + component = json.loads(sbom)['metadata']['component'] + assert component['version'] == version, 'Project SBOM version differs from Maven' + assert component['name'] == 'caldav-server' and component['group'] == 'org.busystack', 'Project SBOM coordinates differ from Maven' + return hashlib.sha256(Path(path).read_bytes()).hexdigest(), sbom + + +def identity(image, version, revision, jar_sha): + config = json.loads(subprocess.check_output(['docker', 'image', 'inspect', image]))[0] + labels = config['Config']['Labels'] or {} + assert labels.get('org.opencontainers.image.version') == version, 'Image version differs from Maven' + assert labels.get('org.opencontainers.image.revision') == revision, 'Image source revision differs from qualified source' + assert labels.get('org.opencontainers.image.source') == 'https://github.com/' + versions.REPOSITORY + assert labels.get('org.opencontainers.image.licenses') == 'Apache-2.0' + assert config['Os'] == 'linux' and config['Architecture'] == 'amd64', 'Expected qualified Linux AMD64 image' + checksum = subprocess.check_output(['docker', 'run', '--rm', '--entrypoint', 'sha256sum', image, '/opt/busystack/application.jar'], text=True).split()[0] + assert checksum == jar_sha, 'Container does not contain the exact expected JAR' + return config + + +def compose_environment(image, context, version, revision): + versions.stable(version) + assert len(revision) == 40 and all(c in '0123456789abcdef' for c in revision), 'Invalid source revision' + return {'BUSYSTACK_APPLICATION_IMAGE': image, 'BUSYSTACK_BUILD_CONTEXT': str(context), + 'BUSYSTACK_BUILD_VERSION': version, 'BUSYSTACK_SOURCE_REVISION': revision} + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--image', help='Optional local image name; Maven still supplies the application version') + parser.add_argument('--jar', help='Use an already tested JAR instead of running clean package') + parser.add_argument('--compose', nargs=argparse.REMAINDER, help='Compose arguments, for example up -d --no-build --wait') + args = parser.parse_args() + assert not subprocess.check_output(['git', 'status', '--porcelain=v1', '--untracked-files=all'], cwd=ROOT), 'Commit source changes before building an image with an exact Git revision' + revision = versions.revision(root=ROOT) + version = versions.resolve(ROOT, source=revision) + if not args.jar: + subprocess.run([str(ROOT / 'mvnw'), '--batch-mode', '--no-transfer-progress', '-DskipTests', 'clean', 'package'], cwd=ROOT, check=True) + jar = Path(args.jar).resolve() if args.jar else ROOT / 'target' / versions.jar_name(version) + assert jar.name == versions.jar_name(version), 'Unexpected JAR filename' + assert sorted(p.name for p in jar.parent.glob('*.jar')) == [jar.name], 'Missing or ambiguous packaged JARs; clean the build directory' + jar_sha, sbom = inspect_jar(jar, version) + context = ROOT / 'target/docker-context' + if context.exists(): + shutil.rmtree(context) + inputs = context / '.image-input' + inputs.mkdir(parents=True) + shutil.copyfile(ROOT / 'Dockerfile', context / 'Dockerfile') + shutil.copyfile(jar, inputs / 'application.jar') + (inputs / 'runtime-dependency-inventory.json').write_bytes(sbom) + for name in ('LICENSE', 'NOTICE', 'THIRD-PARTY-NOTICES'): + shutil.copyfile(ROOT / name, inputs / name) + with zipfile.ZipFile(jar) as archive: + for name in archive.namelist(): + if name.startswith('META-INF/licenses/') and not name.endswith('/'): + path = inputs / 'third-party' / name.removeprefix('META-INF/licenses/') + path.parent.mkdir(parents=True, exist_ok=True) + path.write_bytes(archive.read(name)) + (inputs / 'version').write_text(version + '\n') + (inputs / 'revision').write_text(revision + '\n') + (inputs / 'SHA256SUMS').write_text(jar_sha + ' application.jar\n') + image = args.image or 'busystack-caldav:' + version + subprocess.run(['docker', 'build', '--platform', 'linux/amd64', '--build-arg', 'APPLICATION_VERSION=' + version, + '--build-arg', 'SOURCE_REVISION=' + revision, '--tag', image, str(context)], check=True) + config = identity(image, version, revision, jar_sha) + result = {'version': version, 'sourceCommit': revision, 'jarSha256': jar_sha, 'imageId': config['Id'], 'image': image} + (ROOT / 'target/image-build.json').write_text(json.dumps(result, indent=2) + '\n') + print(json.dumps(result), flush=True) + if args.compose is not None: + assert args.compose, 'Supply Compose arguments after --compose' + environment = os.environ | compose_environment(image, context, version, revision) + subprocess.run(['docker', 'compose', *args.compose], cwd=ROOT, env=environment, check=True) + + +if __name__ == '__main__': + main() diff --git a/scripts/check-advisories.py b/scripts/check-advisories.py new file mode 100644 index 0000000..ef5ad32 --- /dev/null +++ b/scripts/check-advisories.py @@ -0,0 +1,149 @@ +#!/usr/bin/env python3 +"""Check runtime Maven coordinates against the official OSV API; fail closed on incomplete data.""" + +import argparse +from datetime import datetime, timezone +import hashlib +import json +import os +from pathlib import Path +import sys +import tempfile +import urllib.error +import urllib.request + + +API = "https://api.osv.dev/v1/querybatch" +MAX_RESPONSE = 32 * 1024 * 1024 + + +def coordinates(document): + if not isinstance(document, dict) or document.get("bomFormat") != "CycloneDX": + raise ValueError("Input must be a generated CycloneDX JSON inventory") + found = set() + ignored = 0 + for component in document.get("components", []): + if not isinstance(component, dict) or not isinstance(component.get("purl"), str): + raise ValueError("Inventory contains an invalid component") + if not component["purl"].startswith("pkg:maven/"): + ignored += 1 + continue + values = tuple(component.get(key) for key in ("group", "name", "version")) + if any(not isinstance(value, str) or not value or "${" in value for value in values): + raise ValueError("Maven component has missing or unresolved coordinates") + if values[2].endswith("-SNAPSHOT"): + raise ValueError("Runtime dependency inventory contains a snapshot") + found.add((values[0] + ":" + values[1], values[2])) + if not found: + raise ValueError("Inventory contains no resolved Maven runtime dependencies") + return [ + {"ecosystem": "Maven", "name": name, "version": version, "advisories": [], "pages": 0, "complete": False} + for name, version in sorted(found) + ], ignored + + +def request(payload): + query = urllib.request.Request( + API, + data=json.dumps(payload).encode(), + headers={"Content-Type": "application/json", "User-Agent": "BusyStack-runtime-advisory-check/1"}, + method="POST", + ) + with urllib.request.urlopen(query, timeout=45) as response: + if response.status != 200: + raise ValueError("OSV returned a non-success status") + data = response.read(MAX_RESPONSE + 1) + if len(data) > MAX_RESPONSE: + raise ValueError("OSV response exceeded the supported size") + return json.loads(data) + + +def query(packages, post=request): + pending = [ + (index, {"package": {"ecosystem": "Maven", "name": package["name"]}, "version": package["version"]}) + for index, package in enumerate(packages) + ] + tokens = [set() for _ in packages] + requests = 0 + while pending: + following = [] + for start in range(0, len(pending), 100): + batch = pending[start:start + 100] + response = post({"queries": [item for _, item in batch]}) + requests += 1 + if not isinstance(response, dict): + raise ValueError("OSV returned an invalid response envelope") + results = response.get("results") + if not isinstance(results, list) or len(results) != len(batch): + raise ValueError("OSV response omitted query results") + for (index, sent), result in zip(batch, results): + if not isinstance(result, dict) or "error" in result: + raise ValueError("OSV returned an invalid or failed query result") + vulnerabilities = result.get("vulns", []) + if not isinstance(vulnerabilities, list): + raise ValueError("OSV returned invalid advisory data") + existing = {value["id"]: value for value in packages[index]["advisories"]} + for value in vulnerabilities: + if not isinstance(value, dict) or not isinstance(value.get("id"), str) or not value["id"]: + raise ValueError("OSV returned an advisory without an identifier") + existing[value["id"]] = value + packages[index]["advisories"] = sorted(existing.values(), key=lambda value: value["id"]) + packages[index]["pages"] += 1 + token = result.get("next_page_token") + if token is not None and not isinstance(token, str): + raise ValueError("OSV returned an invalid pagination token") + if token: + if token in tokens[index] or packages[index]["pages"] >= 100: + raise ValueError("OSV pagination did not complete within supported bounds") + tokens[index].add(token) + following.append((index, dict(sent, page_token=token))) + else: + packages[index]["complete"] = True + pending = following + return requests + + +def write(path, report): + path.parent.mkdir(parents=True, exist_ok=True) + descriptor, temporary = tempfile.mkstemp(prefix=".osv-report-", dir=path.parent) + try: + with os.fdopen(descriptor, "w") as output: + json.dump(report, output, indent=2) + output.write("\n") + os.replace(temporary, path) + finally: + if os.path.exists(temporary): + os.unlink(temporary) + + +def run(sbom, output, post=request): + report = {"api": API, "checkedAt": datetime.now(timezone.utc).isoformat(), "status": "error"} + try: + data = sbom.read_bytes() + packages, ignored = coordinates(json.loads(data)) + report.update(sbomSha256=hashlib.sha256(data).hexdigest(), packages=packages, ignoredNonMavenComponents=ignored) + report["requestCount"] = query(packages, post) + if not all(package["complete"] for package in packages): + raise ValueError("Advisory response was incomplete") + affected = sum(bool(package["advisories"]) for package in packages) + report.update(status="advisories-returned" if affected else "no-advisories-returned", packageCount=len(packages), affectedPackageCount=affected) + write(output, report) + print(f"OSV: {len(packages)} Maven package versions checked; {affected} with returned advisories; report={output}") + return 1 if affected else 0 + except (OSError, ValueError, TypeError, urllib.error.URLError) as error: + report["error"] = str(error) + write(output, report) + print("Advisory check could not complete: " + str(error), file=sys.stderr) + return 2 + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--sbom", type=Path, default=Path("target/classes/META-INF/sbom/application.cdx.json")) + parser.add_argument("--output", type=Path, default=Path("target/osv-runtime-advisories.json")) + args = parser.parse_args() + return run(args.sbom, args.output) + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/scripts/check-container-advisories.py b/scripts/check-container-advisories.py new file mode 100644 index 0000000..ecb6206 --- /dev/null +++ b/scripts/check-container-advisories.py @@ -0,0 +1,91 @@ +#!/usr/bin/env python3 +"""Pinned Trivy OS-package scan, with current feed metadata and no advisory suppression.""" +import argparse +from datetime import datetime, timedelta, timezone +import hashlib +import json +import os +from pathlib import Path +import subprocess +import sys +import tarfile +import tempfile +import urllib.request + +VERSION = '0.76.1' +ARCHIVE_SHA256 = 'c7cbe3137fb8a48a17574bede3dd6beb16742d1e9772929940fc90082c39d9c0' +URL = f'https://github.com/aquasecurity/trivy/releases/download/v{VERSION}/trivy_{VERSION}_Linux-64bit.tar.gz' +ROOT = Path(__file__).resolve().parents[1] + + +def scanner_environment(): + # Operator shells must not silently narrow this required qualification scan. + return {key:value for key,value in os.environ.items() if not key.startswith('TRIVY_')} + + +def analyze(document, feed, now): + updated = datetime.fromisoformat(feed['UpdatedAt'].replace('Z', '+00:00')) + if not now - timedelta(hours=48) <= updated <= now + timedelta(hours=2): + raise ValueError('Vulnerability database is stale or future dated') + if not document.get('Metadata', {}).get('OS', {}).get('Family'): + raise ValueError('Scanner did not identify the image operating system') + results = [value for value in document.get('Results', []) if value.get('Class') == 'os-pkgs'] + if not results or not any(value.get('Packages') for value in results): + raise ValueError('OS package inventory is missing; empty output is not a clean scan') + findings = [] + for result in results: + for item in result.get('Vulnerabilities') or []: + if item.get('Severity') not in ('UNKNOWN','LOW','MEDIUM','HIGH','CRITICAL'): + raise ValueError('Scanner advisory severity is missing or unrecognized') + findings.append({key: item.get(key) for key in ('VulnerabilityID','PkgName','InstalledVersion','FixedVersion','Severity','Status','PrimaryURL','Title')}) + for finding in findings: + finding['disposition'] = ('blocking; update or individually justified resolution required' if finding['Severity'] in ('HIGH','CRITICAL','UNKNOWN') else 'reported below the documented HIGH/CRITICAL/UNKNOWN gate; ' + ('upstream fixed version is recorded' if finding.get('FixedVersion') else 'no fixed version reported by this feed')) + blocking = [value for value in findings if value['Severity'] in ('HIGH','CRITICAL','UNKNOWN')] + return {'os': document['Metadata']['OS'], 'packageCount': sum(len(r['Packages']) for r in results), + 'findings': findings, 'blockingFindings': len(blocking), + 'gate': 'HIGH, CRITICAL or UNKNOWN fail, including unfixed findings; all other severities remain reported', + 'suppressions': [], 'database': feed, 'status': 'advisories-returned' if blocking else 'passed'} + + +def main(): + parser=argparse.ArgumentParser(description=__doc__) + parser.add_argument('--image',required=True) + parser.add_argument('--output',default=str(ROOT/'target/container-os-advisories.json')) + parser.add_argument('--cache-dir',default=str(ROOT/'target/trivy-cache')) + args=parser.parse_args() + output=Path(args.output).resolve();output.parent.mkdir(parents=True,exist_ok=True) + cache_dir=Path(args.cache_dir).resolve() + report={'status':'error','recordedAt':datetime.now(timezone.utc).isoformat(),'scanner':{'name':'Trivy','version':VERSION,'archiveSha256':ARCHIVE_SHA256},'coverage':'final-container OS packages; separate from Maven/JAR advisories'} + try: + image=json.loads(subprocess.check_output(['docker','image','inspect',args.image],text=True))[0] + report['image']={'id':image['Id'],'repoDigests':image.get('RepoDigests',[]),'labels':image['Config'].get('Labels')} + with tempfile.TemporaryDirectory(prefix='busystack-os-scan-') as temporary: + directory=Path(temporary); archive=directory/'scanner.tar.gz' + with urllib.request.urlopen(URL,timeout=60) as response, archive.open('wb') as destination: + while chunk:=response.read(1024*1024): destination.write(chunk) + if hashlib.sha256(archive.read_bytes()).hexdigest()!=ARCHIVE_SHA256: + raise ValueError('Scanner download checksum mismatch') + with tarfile.open(archive) as source: binary=source.extractfile('trivy').read() + scanner=directory/'trivy';scanner.write_bytes(binary);scanner.chmod(0o700) + scanner_env=scanner_environment() + empty_config=directory/'scan-config.yaml';empty_config.write_text('{}\n') + empty_ignore=directory/'scan.ignore';empty_ignore.write_text('') + actual=json.loads(subprocess.check_output([str(scanner),'version','--format','json'],text=True,env=scanner_env,cwd=directory)) + if actual.get('Version')!=VERSION: raise ValueError('Scanner version mismatch') + raw=output.with_name(output.stem+'-raw.json') + subprocess.run([str(scanner),'image','--image-src','docker','--scanners','vuln','--pkg-types','os','--config',str(empty_config),'--ignorefile',str(empty_ignore),'--ignore-unfixed=false','--severity','UNKNOWN,LOW,MEDIUM,HIGH,CRITICAL','--list-all-pkgs','--no-progress','--format','json','--output',str(raw),'--cache-dir',str(cache_dir),'--timeout','10m',image['Id']],check=True,timeout=660,env=scanner_env,cwd=directory) + feed_path=cache_dir/'db/metadata.json' + feed=json.loads(feed_path.read_text()) + db=feed_path.with_name('trivy.db') + report.update(analyze(json.loads(raw.read_text()),feed,datetime.now(timezone.utc))) + report['scannerConfiguration']={'environment':'TRIVY_* overrides removed','config':'empty private config','ignoreFile':'empty private ignore file','ignoreUnfixed':False,'includedSeverities':['UNKNOWN','LOW','MEDIUM','HIGH','CRITICAL']} + with db.open('rb') as source: + report['databaseSha256']=hashlib.file_digest(source,'sha256').hexdigest() + report['rawReportSha256']=hashlib.sha256(raw.read_bytes()).hexdigest() + except (OSError,ValueError,KeyError,subprocess.CalledProcessError,subprocess.TimeoutExpired) as error: + report['status']='error';report['error']=str(error) + output.write_text(json.dumps(report,sort_keys=True,indent=2)+'\n') + print(json.dumps({k:v for k,v in report.items() if k!='findings'},sort_keys=True)) + return 2 if report['status']=='error' else 1 if report['status']=='advisories-returned' else 0 + +if __name__=='__main__': sys.exit(main()) diff --git a/scripts/init-db.sh b/scripts/init-db.sh new file mode 100755 index 0000000..f375d13 --- /dev/null +++ b/scripts/init-db.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash +set -euo pipefail + +# This script is run only by the official image on an empty PostgreSQL volume. +BUSYSTACK_DATABASE_PASSWORD=$(cat /run/secrets/spring.datasource.password) +export BUSYSTACK_DATABASE_PASSWORD +psql --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" --no-psqlrc --set ON_ERROR_STOP=1 <<'SQL' +\getenv app_password BUSYSTACK_DATABASE_PASSWORD +CREATE ROLE busystack LOGIN PASSWORD :'app_password' NOSUPERUSER NOCREATEDB NOCREATEROLE NOREPLICATION; +ALTER DATABASE busystack OWNER TO busystack; +ALTER SCHEMA public OWNER TO busystack; +SQL +unset BUSYSTACK_DATABASE_PASSWORD diff --git a/scripts/install-ci-jdk.sh b/scripts/install-ci-jdk.sh new file mode 100755 index 0000000..386e8bb --- /dev/null +++ b/scripts/install-ci-jdk.sh @@ -0,0 +1,54 @@ +#!/usr/bin/env bash +set -euo pipefail + +# Use the same immutable Temurin distribution as the application image. setup-java's +# SemVer input cannot represent this four-part Java maintenance release exactly. +: "${RUNNER_TEMP:?RUNNER_TEMP must be set by the GitHub runner}" +: "${GITHUB_ENV:?GITHUB_ENV must be set by the GitHub runner}" +: "${GITHUB_PATH:?GITHUB_PATH must be set by the GitHub runner}" +jdk_image='eclipse-temurin:25-jdk@sha256:3ab7fc8d0b4ade8fef96903fb36767c1dbb4fd1535dcfad85b59c17afbbb5857' +jdk_directory=$(mktemp -d "${RUNNER_TEMP}/busystack-jdk.XXXXXXXX") +jdk_container='' +cleanup() { + if [[ -n "$jdk_container" ]]; then + docker container rm "$jdk_container" > /dev/null + fi +} +trap cleanup EXIT + +docker info > /dev/null +docker pull "$jdk_image" +jdk_container=$(docker container create --network none --entrypoint /bin/true "$jdk_image") +docker container cp "${jdk_container}:/opt/java/openjdk/." "$jdk_directory" + +python3 - "$jdk_directory" <<'PY' +from pathlib import Path +import subprocess +import sys + +jdk = Path(sys.argv[1]) +release = dict(line.split('=', 1) for line in (jdk / 'release').read_text().splitlines() if '=' in line) +expected = { + 'IMPLEMENTOR': 'Eclipse Adoptium', + 'IMPLEMENTOR_VERSION': 'Temurin-25.0.4.1+1', + 'JAVA_VERSION': '25.0.4.1', + 'JAVA_RUNTIME_VERSION': '25.0.4.1+1-LTS', + 'SEMANTIC_VERSION': '25.0.4.1+1', + 'IMAGE_TYPE': 'JDK', +} +for name, value in expected.items(): + if release.get(name, '').strip('"') != value: + raise SystemExit(f'Unexpected pinned JDK release field: {name}') +result = subprocess.run([str(jdk / 'bin/java'), '-XshowSettings:properties', '-version'], + check=True, capture_output=True, text=True) +properties = dict(line.lstrip().split(' = ', 1) for line in result.stderr.splitlines() if ' = ' in line) +if properties.get('java.runtime.version') != expected['JAVA_RUNTIME_VERSION']: + raise SystemExit('Pinned JDK executable does not match the selected runtime version') +if properties.get('java.vendor') != expected['IMPLEMENTOR']: + raise SystemExit('Pinned JDK executable does not match the selected distribution') +subprocess.run([str(jdk / 'bin/javac'), '-version'], check=True) +print(f"Selected Eclipse Temurin {expected['SEMANTIC_VERSION']} from the pinned application JDK image") +PY + +printf 'JAVA_HOME=%s\n' "$jdk_directory" >> "$GITHUB_ENV" +printf '%s\n' "$jdk_directory/bin" >> "$GITHUB_PATH" diff --git a/scripts/install-release-gh.sh b/scripts/install-release-gh.sh new file mode 100644 index 0000000..42869ba --- /dev/null +++ b/scripts/install-release-gh.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash +set -euo pipefail +# Isolated official CLI with release-attestation verification support. No global upgrade. +: "${RUNNER_TEMP:?RUNNER_TEMP must name a writable task directory}" +: "${GITHUB_PATH:?GITHUB_PATH must name the workflow path file}" +release_cli=$(mktemp -d "${RUNNER_TEMP}/busystack-release-gh.XXXXXXXX") +curl --fail --silent --show-error --location --retry 3 \ + https://github.com/cli/cli/releases/download/v2.103.3/gh_2.103.3_linux_amd64.tar.gz \ + --output "$release_cli/gh.tar.gz" +printf '%s %s\n' 4c806978ffce1cec70853302a56d70ac6da4f19bae7adca4e1a8ab00633dd91d "$release_cli/gh.tar.gz" | sha256sum --check --status +tar -xzf "$release_cli/gh.tar.gz" -C "$release_cli" gh_2.103.3_linux_amd64/bin/gh +"$release_cli/gh_2.103.3_linux_amd64/bin/gh" release verify --help > /dev/null +"$release_cli/gh_2.103.3_linux_amd64/bin/gh" release verify-asset --help > /dev/null +printf '%s\n' "$release_cli/gh_2.103.3_linux_amd64/bin" >> "$GITHUB_PATH" diff --git a/scripts/publish-release.py b/scripts/publish-release.py new file mode 100644 index 0000000..40f57fc --- /dev/null +++ b/scripts/publish-release.py @@ -0,0 +1,400 @@ +#!/usr/bin/env python3 +"""Explicit tested-artifact promotion. Stage first; publish only the verified staged assets.""" +import argparse +import base64 +import hashlib +import importlib.util +import json +import os +from pathlib import Path +import posixpath +import re +import subprocess +import urllib.error +import urllib.parse +import urllib.request +import sys +sys.path.insert(0, str(Path(__file__).resolve().parent)) +import release_version as versions + +ROOT = Path(__file__).resolve().parents[1] +REPO = 'busystack/caldav-server' +REGISTRY = 'ghcr.io/' + REPO +REQUIRED = {'release-tools', 'application', 'dependency-audit'} + + +def module(name, filename): + spec = importlib.util.spec_from_file_location(name, ROOT / 'scripts' / filename) + result = importlib.util.module_from_spec(spec) + spec.loader.exec_module(result) + return result + + +candidate = module('candidate', 'release-candidate.py') +bundle = module('bundle', 'release-bundle.py') + + +def command(*args): + return subprocess.check_output(list(args), text=True).strip() + + +def api(path, missing=False, method='GET', fields=None): + arguments = ['gh', 'api', '--method', method, path] + for key, value in (fields or {}).items(): + arguments += ['-f', f'{key}={value}'] + result = subprocess.run(arguments, capture_output=True, text=True) + if result.returncode: + if missing and '(HTTP 404)' in result.stderr: + return None + raise RuntimeError('GitHub API failed: ' + path + ': ' + result.stderr[:500]) + return json.loads(result.stdout) if result.stdout else None + + +def release_for_tag(tag): + # The by-tag endpoint returns published releases only. Listings include drafts + # for this job's contents:write token, allowing interrupted uploads to resume. + matches = [] + page = 1 + while True: + releases = api(f'repos/{REPO}/releases?per_page=100&page={page}') + matches.extend(release for release in releases if release['tag_name'] == tag) + if len(releases) < 100: + break + page += 1 + assert len(matches) <= 1, 'Ambiguous release tag' + return matches[0] if matches else None + + +def verify_source(source_sha, controller_sha, phase): + assert phase in ('stage', 'publish'), 'Invalid release phase' + assert re.fullmatch(r'[0-9a-f]{40}', source_sha), 'Invalid source SHA' + head = command('git', '-C', str(ROOT), 'rev-parse', 'HEAD') + assert head == controller_sha, 'Dispatch/controller mismatch' + if phase == 'stage': + assert source_sha == head, 'Staging requires the current verified main commit' + else: + ancestry = subprocess.run(['git', '-C', str(ROOT), 'merge-base', '--is-ancestor', source_sha, head], capture_output=True) + assert ancestry.returncode == 0, 'Release source must belong to trusted main history' + + +def source_bytes(sha, filename): + return subprocess.check_output(['git', '-C', str(ROOT), 'show', f'{sha}:{filename}']) + + +def verification(run, jobs, sha): + assert run['repository']['full_name'] == REPO and run['head_repository']['full_name'] == REPO, 'Wrong repository' + assert run['path'] == '.github/workflows/ci.yml' and run['name'] == 'verify', 'Wrong verification workflow' + assert run['status'] == 'completed' and run['conclusion'] == 'success', 'Verification not successful' + assert run['head_sha'] == sha, 'Wrong verification SHA' + assert run['head_branch'] == 'main' and run['event'] in ('push', 'workflow_dispatch'), 'Untrusted verification source' + by_name = {job['name']: job for job in jobs} + assert REQUIRED <= by_name.keys(), 'Missing required job' + assert all(by_name[name]['status'] == 'completed' and by_name[name]['conclusion'] == 'success' for name in REQUIRED), 'Required job failed/incomplete' + + +def verify_run(run_id, sha): + run = api(f'repos/{REPO}/actions/runs/{run_id}') + jobs = [] + page = 1 + while True: + data = api(f'repos/{REPO}/actions/runs/{run_id}/attempts/{run["run_attempt"]}/jobs?per_page=100&page={page}') + jobs.extend(data['jobs']) + if len(data['jobs']) < 100: + break + page += 1 + verification(run, jobs, sha) + return run + + +def download(run_id, name, destination): + data = api(f'repos/{REPO}/actions/runs/{run_id}/artifacts?per_page=100') + matches = [item for item in data['artifacts'] if item['name'] == name and not item['expired']] + assert len(matches) == 1, 'Missing/ambiguous artifact: ' + name + subprocess.run(['gh', 'run', 'download', str(run_id), '--repo', REPO, '--name', name, '--dir', str(destination)], check=True) + + +def registry_headers(anonymous=False, actions='pull'): + # Documented OCI distribution authentication. Never print the registry bearer token. + query = urllib.parse.urlencode({'service': 'ghcr.io', 'scope': 'repository:' + REPO + ':' + actions}) + headers = {} + if not anonymous: + credential = os.environ['GITHUB_ACTOR'] + ':' + os.environ['GH_TOKEN'] + headers['Authorization'] = 'Basic ' + base64.b64encode(credential.encode()).decode() + token_request = urllib.request.Request('https://ghcr.io/token?' + query, headers=headers) + with urllib.request.urlopen(token_request, timeout=60) as response: + token = json.load(response)['token'] + return {'Authorization': 'Bearer ' + token, + 'Accept': 'application/vnd.docker.distribution.manifest.v2+json, application/vnd.oci.image.manifest.v1+json, application/vnd.oci.image.index.v1+json, application/vnd.docker.distribution.manifest.list.v2+json'} + + +def manifest_digest(tag, anonymous=False): + headers = registry_headers(anonymous=anonymous) + try: + request = urllib.request.Request('https://ghcr.io/v2/' + REPO + '/manifests/' + tag, headers=headers) + with urllib.request.urlopen(request, timeout=60) as response: + digest = response.headers['Docker-Content-Digest'] + assert re.fullmatch(r'sha256:[0-9a-f]{64}', digest) + return digest + except urllib.error.HTTPError as error: + if error.code == 404: + return None + raise + + +def promote_manifest(digest, version): + """Attach the final tag to the exact staged bytes, without a Docker re-export.""" + versions.stable(version) + assert re.fullmatch(r'sha256:[0-9a-f]{64}', digest), 'Invalid staged registry digest' + existing = manifest_digest(version) + matching_version(existing, digest) + if existing is not None: + return + headers = registry_headers(actions='pull,push') + base = 'https://ghcr.io/v2/' + REPO + '/manifests/' + with urllib.request.urlopen(urllib.request.Request(base + digest, headers=headers), timeout=60) as response: + data = response.read() + media_type = response.headers['Content-Type'].split(';', 1)[0] + assert media_type in headers['Accept'].split(', '), 'Unsupported staged manifest media type' + assert response.headers['Docker-Content-Digest'] == digest, 'Staged manifest response digest changed' + assert 'sha256:' + hashlib.sha256(data).hexdigest() == digest, 'Staged manifest bytes do not match digest' + assert json.loads(data)['mediaType'] == media_type, 'Staged manifest media type differs' + # Check again after the download; never replace a conflicting version tag. + existing = manifest_digest(version) + matching_version(existing, digest) + if existing is not None: + return + request = urllib.request.Request(base + version, data=data, method='PUT', + headers={**headers, 'Content-Type': media_type}) + with urllib.request.urlopen(request, timeout=60) as response: + assert response.headers['Docker-Content-Digest'] == digest, 'Promoted registry digest changed' + assert manifest_digest(version) == digest, 'Final tag differs from staged registry manifest' + + +def remote_image_matches(record, digest): + remote = json.loads(command('docker', 'manifest', 'inspect', REGISTRY + '@' + digest)) + if 'manifests' in remote: + runtime = [] + for item in remote['manifests']: + if item.get('annotations', {}).get('vnd.docker.reference.type') == 'attestation-manifest': + continue + assert item.get('platform') == {'architecture': 'amd64', 'os': 'linux'}, 'Unqualified registry platform' + runtime.append(item) + assert len(runtime) == 1, 'Expected exactly one Linux AMD64 runtime manifest' + remote = json.loads(command('docker', 'manifest', 'inspect', REGISTRY + '@' + runtime[0]['digest'])) + assert remote['config']['digest'] == record['imageConfigDigest'], 'Conflicting stage image configuration' + + +def matching_version(existing, expected): + assert existing is None or existing == expected, 'Conflicting existing version; refusing overwrite' + + +def candidate_version(version, value): + return versions.candidate_tag(version, value) + + +def release_notes(sha, version): + versions.resolve(ROOT, source=sha, requested=version) + notes = source_bytes(sha, f'docs/releases/{version}.md').decode() + def source_link(match): + path = posixpath.normpath('docs/releases/' + match.group(1)) + assert not path.startswith('../'), 'Release documentation link escapes repository' + return '](https://github.com/' + REPO + '/blob/' + sha + '/' + path + ')' + return re.sub(r'\]\((\.{1,2}/[^)]+)\)', source_link, notes) + + +def journal(path, operation, details): + record = {'operation': operation, **details} + with Path(path).open('a') as output: + output.write(json.dumps(record, sort_keys=True) + '\n') + print(json.dumps(record, sort_keys=True), flush=True) + + +def check_release(sha, version): + tag = api(f'repos/{REPO}/git/ref/tags/{versions.tag(version)}', missing=True) + if tag: + target = tag['object'] + if target['type'] == 'tag': + target = api(f'repos/{REPO}/git/tags/{target["sha"]}')['object'] + assert target['type'] == 'commit' and target['sha'] == sha, 'Conflicting tag' + release = release_for_tag(versions.tag(version)) + if release: + assert tag, 'Release without exact tag' + if not release['draft']: + assert release.get('immutable') is True, 'Existing release is not immutable' + return tag, release + + +def check_client_receipt(jar_sha, source_sha, version): + def contents(name): + return source_bytes(source_sha, name) + versions.resolve(ROOT, source=source_sha, requested=version) + receipt = json.loads(contents(versions.receipt_path(version))) + assert receipt['version'] == version, 'Wrong client qualification version' + assert receipt['jarSha256'] == jar_sha, 'Client qualification belongs to another executable' + assert receipt['runtimeInputs'] == {name: hashlib.sha256(contents(name)).hexdigest() for name in ('Dockerfile', 'compose.yaml')}, 'Client qualification runtime inputs changed' + assert receipt['status'] == 'passed' + assert set(receipt['clients']) == {'thunderbird-system-vancouver', 'thunderbird-explicit-vancouver', 'thunderbird-explicit-berlin', 'gnome-vancouver'} + assert all(result['status'] == 'passed' for result in receipt['clients'].values()) + + +def validate_stage(staged, stage, record, run): + staged = Path(staged) + assert stage['version'] == record['version'], 'Wrong staged version' + versions.candidate_tag(record['version'], stage['candidateVersion']) + assert stage['sourceCommit'] == record['sourceCommit'], 'Wrong staged source' + assert stage['verificationRun'] == run['id'] and stage['verificationAttempt'] == run['run_attempt'], 'Wrong staged verification attempt' + assert stage['sourceSnapshotSha256'] == record['files']['source.tar.gz'], 'Wrong staged source snapshot' + payload = staged / 'assets' + assert {p.name: candidate.sha256(p) for p in payload.iterdir()} == stage['assets'], 'Staged asset checksum mismatch' + assert candidate.sha256(staged / 'release-notes.md') == stage['releaseNotesSha256'], 'Staged release notes changed' + assert (staged / 'release-notes.md').read_text() == release_notes(record['sourceCommit'], record['version']), 'Staged notes differ from qualified source' + manifest = bundle.validate_assets(payload, record['version'], record['sourceCommit']) + assert manifest['sourceSnapshotSha256'] == stage['sourceSnapshotSha256'] + assert manifest['container']['registryManifestDigest'] == stage['registryDigest'] + assert manifest['container']['localTestedImageId'] == record['imageId'] + assert manifest['container']['imageConfigDigest'] == record['imageConfigDigest'] + assert manifest['jar']['sha256'] == record['files'][versions.jar_name(record['version'])] + for name, checksum in manifest['sourceInputs'].items(): + assert hashlib.sha256(source_bytes(record['sourceCommit'], name)).hexdigest() == checksum, 'Unqualified release payload input: ' + name + assert candidate.sha256(staged / 'installation.json') == stage['installationSha256'] + installation = json.loads((staged / 'installation.json').read_text()) + assert installation['status'] == 'passed' + assert installation['sourceCommit'] == record['sourceCommit'] + assert installation['version'] == record['version'] + assert installation['jarSha256'] == manifest['jar']['sha256'] + assert installation['deploymentSha256'] == manifest['deployment']['sha256'] + assert installation['registryManifestDigest'] == stage['registryDigest'] + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--version', required=True) + parser.add_argument('--verification-run', type=int, required=True) + parser.add_argument('--source-sha', required=True) + parser.add_argument('--phase', choices=['stage', 'publish'], required=True) + parser.add_argument('--candidate-version', help='Maven version followed by -rc.N, where N is a positive integer') + parser.add_argument('--stage-run', type=int) + parser.add_argument('--work', required=True) + parser.add_argument('--immutability-confirmed', action='store_true') + args = parser.parse_args() + args.version = versions.resolve(ROOT, source=args.source_sha, requested=args.version) + staging = candidate_version(args.version, args.candidate_version) if args.phase == 'stage' else None + assert os.environ['GITHUB_REPOSITORY'] == REPO + assert os.environ['GITHUB_REF'] == 'refs/heads/main', 'Dispatch publication from main' + verify_source(args.source_sha, os.environ['GITHUB_SHA'], args.phase) + work = Path(args.work) + work.mkdir(parents=True, exist_ok=False) + log = work / 'operations.jsonl' + journal(log, 'controller-and-release-source', {'controllerCommit': os.environ['GITHUB_SHA'], 'sourceCommit': args.source_sha}) + run = verify_run(args.verification_run, args.source_sha) + artifact_name = f'verified-candidate-{run["id"]}-{run["run_attempt"]}' + source = work / 'candidate' + download(run['id'], artifact_name, source) + record = candidate.validate(source, args.version, args.source_sha) + assert record['ciRun'] == run['id'] and record['ciAttempt'] == run['run_attempt'] and record['sourceCommit'] == args.source_sha + check_client_receipt(record['files'][versions.jar_name(args.version)], args.source_sha, args.version) + check_release(args.source_sha, args.version) + config = candidate.load_image(source, record) + loaded_id = config['Id'] + journal(log, 'verified-image-loaded', {'ciImageId': record['imageId'], 'loadedImageId': loaded_id, + 'imageConfigDigest': record['imageConfigDigest']}) + if args.phase == 'stage': + subprocess.run(['docker', 'tag', loaded_id, REGISTRY + ':' + staging], check=True) + # After the first push the linked package exists; retries never change staged content. + package = api('orgs/busystack/packages/container/caldav-server', missing=True) + prior = None + if package: + prior = manifest_digest(staging) + if prior: + remote_image_matches(record, prior) + if prior is None: + subprocess.run(['docker', 'push', REGISTRY + ':' + staging], check=True) + package = api('orgs/busystack/packages/container/caldav-server') + assert package['repository']['full_name'] == REPO, 'GHCR package is not associated with this repository' + digest = manifest_digest(staging) + assert digest + remote_image_matches(record, digest) + journal(log, 'staged-registry-image', {'candidateVersion': staging, 'digest': digest, 'imageId': record['imageId'], 'packageVisibility': package['visibility'], 'repository': package['repository']['full_name']}) + matching_version(manifest_digest(args.version), digest) + payload = work / 'assets' + manifest = bundle.build(source, digest, payload) + # This helper extracts and exercises the exact archive, with no checkout mounts. + subprocess.run(['python3', str(ROOT / 'scripts/verify-installation-bundle.py'), '--assets', str(payload), '--output', str(work / 'installation.json')], check=True) + notes = work / 'release-notes.md' + notes.write_text(release_notes(args.source_sha, args.version)) + candidate.write_json(work / 'stage.json', {'version': args.version, 'sourceSnapshotSha256': record['files']['source.tar.gz'], + 'releaseNotesSha256': candidate.sha256(notes), 'sourceCommit': args.source_sha, 'verificationRun': run['id'], + 'verificationAttempt': run['run_attempt'], 'candidateVersion': staging, 'registryDigest': digest, + 'assets': {p.name: candidate.sha256(p) for p in payload.iterdir()}, + 'installationSha256': candidate.sha256(work / 'installation.json')}) + journal(log, 'staged-installation-passed', {'digest': digest, 'assets': manifest['assets']}) + return + assert args.stage_run, 'Publish requires an exact successful staging run' + stage_run = api(f'repos/{REPO}/actions/runs/{args.stage_run}') + assert stage_run['repository']['full_name'] == REPO and stage_run['head_repository']['full_name'] == REPO + assert stage_run['path'] == '.github/workflows/release.yml' and stage_run['event'] == 'workflow_dispatch' + assert stage_run['name'] == 'release' and stage_run['head_branch'] == 'main', 'Untrusted staging run' + assert stage_run['head_sha'] == args.source_sha and stage_run['status'] == 'completed' and stage_run['conclusion'] == 'success' + staged = work / 'staged' + download(args.stage_run, f'release-stage-{args.stage_run}-{stage_run["run_attempt"]}', staged) + stage = json.loads((staged / 'stage.json').read_text()) + payload = staged / 'assets' + validate_stage(staged, stage, record, run) + digest = stage['registryDigest'] + existing_version_digest = manifest_digest(args.version) + matching_version(existing_version_digest, digest) + assert manifest_digest(digest) == digest + assert manifest_digest(digest, anonymous=True) == digest, 'Package must be public before version publication' + package = api('orgs/busystack/packages/container/caldav-server') + assert package['visibility'] == 'public' and package['repository']['full_name'] == REPO + # GITHUB_TOKEN cannot request repository Administration permission. The admin + # dispatcher must check the documented setting API immediately before dispatch. + assert args.immutability_confirmed, 'Admin must verify immutable releases before dispatch' + journal(log, 'immutable-setting-admin-confirmation', {'confirmed': True}) + tag, release = check_release(args.source_sha, args.version) + if release: + assert (release.get('body') or '').strip() == (staged / 'release-notes.md').read_text().strip(), 'Conflicting existing release notes' + expected_assets = stage['assets'] + assert set(asset['name'] for asset in release['assets']) <= expected_assets.keys(), 'Unexpected existing release asset' + assert all(asset.get('digest') == 'sha256:' + expected_assets[asset['name']] for asset in release['assets']), 'Conflicting existing release assets' + if not tag: + api(f'repos/{REPO}/git/refs', method='POST', fields={'ref': 'refs/tags/' + versions.tag(args.version), 'sha': args.source_sha}) + journal(log, 'exact-tag-created-or-matched', {'sha': args.source_sha}) + if existing_version_digest is None: + promote_manifest(digest, args.version) + assert manifest_digest(args.version) == digest + journal(log, 'version-image-created-or-matched', {'digest': digest}) + if not release: + notes = staged / 'release-notes.md' + subprocess.run(['gh', 'release', 'create', versions.tag(args.version), '--repo', REPO, '--verify-tag', '--draft', '--title', 'BusyStack CalDAV Server ' + args.version, '--notes-file', str(notes)], check=True) + release = release_for_tag(versions.tag(args.version)) + assert release, 'Created draft release is not visible' + journal(log, 'release-created-or-matched', {'releaseId': release['id'], 'draft': release['draft'], 'tag': release['tag_name']}) + expected = stage['assets'] + existing = {asset['name']: asset for asset in release['assets']} + assert set(existing) <= set(expected), 'Unexpected release asset' + for name, asset in existing.items(): + assert asset.get('digest') == 'sha256:' + expected[name], 'Conflicting release asset: ' + name + if release['draft']: + for name in sorted(set(expected) - set(existing)): + subprocess.run(['gh', 'release', 'upload', versions.tag(args.version), str(payload / name), '--repo', REPO], check=True) + remote = api(f'repos/{REPO}/releases/{release["id"]}') + assert remote['tag_name'] == versions.tag(args.version), 'Release tag changed during publication' + assert {asset['name']: asset.get('digest') for asset in remote['assets']} == {name: 'sha256:' + sha for name, sha in expected.items()} + journal(log, 'draft-assets-verified', {'assets': expected}) + if remote['draft']: + subprocess.run(['gh', 'release', 'edit', versions.tag(args.version), '--repo', REPO, '--draft=false', '--latest'], check=True) + published = api(f'repos/{REPO}/releases/{release["id"]}') + assert published['tag_name'] == versions.tag(args.version) and published['immutable'] and not published['draft'] + journal(log, 'immutable-release-published', {'url': published['html_url'], 'digest': digest}) + attestation = json.loads(command('gh', 'release', 'verify', versions.tag(args.version), '--repo', REPO, '--format', 'json')) + candidate.write_json(work / 'release-attestation.json', attestation) + for asset in sorted(payload.iterdir()): + subprocess.run(['gh', 'release', 'verify-asset', versions.tag(args.version), str(asset), '--repo', REPO], check=True) + journal(log, 'release-attestation-and-assets-verified', {'assets': sorted(expected)}) + subprocess.run(['python3', str(ROOT / 'scripts/verify-installation-bundle.py'), '--public', '--version', args.version, '--output', str(work / 'anonymous-installation.json')], check=True) + journal(log, 'anonymous-installation-passed', {'sha': args.source_sha, 'digest': digest}) + + +if __name__ == '__main__': + main() diff --git a/scripts/release-bundle.py b/scripts/release-bundle.py new file mode 100644 index 0000000..8fe6e6a --- /dev/null +++ b/scripts/release-bundle.py @@ -0,0 +1,164 @@ +#!/usr/bin/env python3 +"""Generate digest-pinned, reproducible deployment assets from a verified candidate.""" +import argparse +import gzip +import hashlib +import importlib.util +import io +import json +import os +from pathlib import Path +import re +import shutil +import subprocess +import tarfile +import sys +sys.path.insert(0, str(Path(__file__).resolve().parent)) +import release_version as versions + +ROOT = Path(__file__).resolve().parents[1] +OPERATOR_DOCUMENTS = ( + 'docs/operations.md', + 'docs/account-security.md', + 'docs/calendar-semantics.md', + 'docs/groups.md', + 'docs/keycloak.md', + 'docs/oauth.md', + 'docs/observance-until-compatibility.md', + 'docs/passkeys-and-mfa.md', + 'docs/performance.md', + 'docs/third-party-licenses.md', + 'docs/versions.md', + 'docs/adr/0004-resumable-synchronization.md', +) +spec = importlib.util.spec_from_file_location('candidate', ROOT / 'scripts/release-candidate.py') +candidate = importlib.util.module_from_spec(spec) +spec.loader.exec_module(candidate) + + +def tar_bytes(files, prefix=''): + buffer = io.BytesIO() + with tarfile.open(fileobj=buffer, mode='w') as archive: + for name, data in sorted(files.items()): + entry = tarfile.TarInfo(prefix + name) + entry.size = len(data) + entry.mtime = 0 + entry.mode = 0o755 if name.endswith('.sh') else 0o644 + archive.addfile(entry, io.BytesIO(data)) + return gzip.compress(buffer.getvalue(), mtime=0) + + +def build(source, digest, output): + assert re.fullmatch(r'sha256:[0-9a-f]{64}', digest), 'Expected registry manifest digest' + record = candidate.validate(source) + qualified = candidate.snapshot(source, record) + output = Path(output) + output.mkdir(parents=True, exist_ok=False) + version = record['version'] + image = f'ghcr.io/{candidate.REPOSITORY}:{version}@{digest}' + compose = qualified.read('compose.yaml').decode() + build_start = compose.index(' build:\n') + image_start = compose.index(' image: ${BUSYSTACK_APPLICATION_IMAGE:', build_start) + image_end = compose.index('\n', image_start) + 1 + compose = compose[:build_start] + f' image: {image}\n platform: linux/amd64\n' + compose[image_end:] + assert 'build:' not in compose and 'BUSYSTACK_BUILD_' not in compose and image in compose + identity = {**record, 'registryDigest': digest, 'image': image} + identity.pop('files') + files = {'compose.yaml': compose.encode(), 'SOURCE.json': (json.dumps(identity, indent=2, sort_keys=True) + '\n').encode()} + used = {'compose.yaml'} + def content(name, render=False): + used.add(name) + data = qualified.read(name) + return data.replace(b'@VERSION@', version.encode()) if render else data + for name in qualified.under('deployment'): + files[name.removeprefix('deployment/')] = content(name, render=True) + for name in ('init-db.sh', 'backup.sh', 'restore.sh'): + files['scripts/' + name] = content('scripts/' + name) + for name in (*OPERATOR_DOCUMENTS, f'docs/releases/{version}.md'): + files[name] = content(name, render=True) + receipt = versions.receipt_path(version) + files[receipt] = content(receipt) + notices = {name: content(name) for name in ('LICENSE', 'NOTICE', 'THIRD-PARTY-NOTICES')} + for base in ('licenses', 'src/main/resources/META-INF/licenses'): + for name in qualified.under(base): + destination = 'licenses/' + name.removeprefix(base + '/') + data = content(name) + assert destination not in notices or notices[destination] == data, 'Conflicting license input' + notices[destination] = data + notices['runtime-dependency-inventory.json'] = (Path(source) / 'sbom.cdx.json').read_bytes() + files.update(notices) + bundle_name = f'caldav-server-{version}-deploy.tar.gz' + (output / bundle_name).write_bytes(tar_bytes(files, f'caldav-server-{version}-deploy/')) + (output / 'notices.tar.gz').write_bytes(tar_bytes(notices)) + for name in (f'caldav-server-{version}.jar', 'sbom.cdx.json'): + shutil.copyfile(Path(source) / name, output / name) + assets = {path.name: candidate.sha256(path) for path in output.iterdir()} + manifest = { + 'schemaVersion': 2, 'version': version, 'sourceCommit': record['sourceCommit'], + 'sourceTree': record['sourceTree'], 'sourceSnapshotSha256': record['files']['source.tar.gz'], + 'sourceInputs': {name: hashlib.sha256(qualified.read(name)).hexdigest() for name in sorted(used)}, + 'deploymentFiles': {name: hashlib.sha256(data).hexdigest() for name, data in sorted(files.items())}, + 'repository': 'https://github.com/' + candidate.REPOSITORY, + 'verification': {'run': record['ciRun'], 'attempt': record['ciAttempt'], + 'url': f"https://github.com/{candidate.REPOSITORY}/actions/runs/{record['ciRun']}", + 'workflow': record['workflow'], 'requiredJobs': ['release-tools', 'application', 'dependency-audit']}, + 'container': {'image': image, 'registryManifestDigest': digest, 'localTestedImageId': record['imageId'], + 'imageConfigDigest': record['imageConfigDigest'], + 'testedArchiveSha256': record['files']['container.tar.gz'], 'platform': 'linux/amd64'}, + 'jar': {'name': f'caldav-server-{version}.jar', 'sha256': assets[f'caldav-server-{version}.jar']}, + 'deployment': {'name': bundle_name, 'sha256': assets[bundle_name]}, + 'assets': assets, + 'qualificationEvidence': [f"https://github.com/{candidate.REPOSITORY}/actions/runs/{record['ciRun']}", + f"https://github.com/{candidate.REPOSITORY}/blob/{record['sourceCommit']}/{versions.receipt_path(version)}"], + 'stagingRun': os.environ.get('GITHUB_RUN_ID'), + 'stagingEvidence': 'https://github.com/' + candidate.REPOSITORY + '/actions/runs/' + os.environ.get('GITHUB_RUN_ID', 'local-fixture'), + 'release': f'https://github.com/{candidate.REPOSITORY}/releases/tag/v{version}', + } + candidate.write_json(output / 'release-manifest.json', manifest) + sums = {**assets, 'release-manifest.json': candidate.sha256(output / 'release-manifest.json')} + (output / 'SHA256SUMS').write_text(''.join(f'{digest} {name}\n' for name, digest in sorted(sums.items()))) + return manifest + + +def validate_assets(directory, version=None, source_sha=None): + directory = Path(directory) + manifest = json.loads((directory / 'release-manifest.json').read_text()) + selected = versions.stable(manifest['version']) + assert version is None or selected == version, 'Wrong asset version' + assert source_sha is None or manifest['sourceCommit'] == source_sha, 'Wrong asset source' + expected = {versions.jar_name(selected), versions.deployment_name(selected), 'sbom.cdx.json', 'notices.tar.gz'} + assert set(manifest['assets']) == expected, 'Missing or unexpected release assets' + assert {p.name for p in directory.iterdir()} == expected | {'release-manifest.json', 'SHA256SUMS'}, 'Unexpected asset files' + for name, checksum in manifest['assets'].items(): + assert candidate.sha256(directory / name) == checksum, 'Release asset checksum mismatch: ' + name + sums = {**manifest['assets'], 'release-manifest.json': candidate.sha256(directory / 'release-manifest.json')} + assert (directory / 'SHA256SUMS').read_text() == ''.join(f'{sha} {name}\n' for name, sha in sorted(sums.items())), 'Incorrect release checksums' + assert manifest['jar'] == {'name': versions.jar_name(selected), 'sha256': sums[versions.jar_name(selected)]} + assert manifest['deployment'] == {'name': versions.deployment_name(selected), 'sha256': sums[versions.deployment_name(selected)]} + _, sbom = candidate.builder.inspect_jar(directory / versions.jar_name(selected), selected) + assert sbom == (directory / 'sbom.cdx.json').read_bytes() + digest = manifest['container']['registryManifestDigest'] + assert re.fullmatch(r'sha256:[0-9a-f]{64}', digest) + assert manifest['container']['image'] == f'ghcr.io/{candidate.REPOSITORY}:{selected}@{digest}' + with tarfile.open(directory / versions.deployment_name(selected)) as archive: + prefix = 'caldav-server-' + selected + '-deploy/' + members = archive.getmembers() + assert all(p.isfile() and p.name.startswith(prefix) and '..' not in Path(p.name).parts for p in members) + contents = {p.name.removeprefix(prefix): archive.extractfile(p).read() for p in members} + assert len(contents) == len(members), 'Duplicate deployment entries' + assert {name: hashlib.sha256(data).hexdigest() for name, data in contents.items()} == manifest['deploymentFiles'], 'Deployment content mismatch' + assert 'build:' not in contents['compose.yaml'].decode() and manifest['container']['image'].encode() in contents['compose.yaml'] + with tarfile.open(directory / 'notices.tar.gz') as archive: + for member in archive: + assert member.isfile() and member.name in contents, 'Unexpected notice entry' + assert archive.extractfile(member).read() == contents[member.name], 'Notice/deployment mismatch' + return manifest + + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--candidate', required=True) + parser.add_argument('--digest', required=True) + parser.add_argument('--output', required=True) + args = parser.parse_args() + build(args.candidate, args.digest, args.output) diff --git a/scripts/release-candidate.py b/scripts/release-candidate.py new file mode 100644 index 0000000..0b3c5f6 --- /dev/null +++ b/scripts/release-candidate.py @@ -0,0 +1,107 @@ +#!/usr/bin/env python3 +"""Retain the exact tested executable, image and qualified source snapshot.""" +import argparse +import hashlib +import importlib.util +import json +import os +from pathlib import Path +import shutil +import subprocess +import sys + +sys.path.insert(0, str(Path(__file__).resolve().parent)) +import release_version as versions +import release_source +import release_image + +ROOT = Path(__file__).resolve().parents[1] +REPOSITORY = versions.REPOSITORY +spec = importlib.util.spec_from_file_location('image_builder', ROOT / 'scripts/build-image.py') +builder = importlib.util.module_from_spec(spec) +spec.loader.exec_module(builder) + + +def sha256(path): + digest = hashlib.sha256() + with open(path, 'rb') as source: + for chunk in iter(lambda: source.read(1024 * 1024), b''): + digest.update(chunk) + return digest.hexdigest() + + +def write_json(path, value): + Path(path).write_text(json.dumps(value, indent=2, sort_keys=True) + '\n') + + +def snapshot(directory, record): + return release_source.Snapshot(record['sourceCommit'], ROOT, Path(directory) / 'source.tar.gz') + + +def validate(directory, requested=None, source_sha=None): + directory = Path(directory) + record = json.loads((directory / 'candidate.json').read_text()) + assert record['repository'] == REPOSITORY + version = versions.resolve(ROOT, source=record['sourceCommit'], requested=requested or record['version']) + assert record['version'] == version, 'Candidate version differs from qualified Maven version' + assert source_sha is None or record['sourceCommit'] == source_sha, 'Wrong candidate source commit' + assert record['platform'] == 'linux/amd64' + expected = {versions.jar_name(version), 'container.tar.gz', 'sbom.cdx.json', 'source.tar.gz'} + assert set(record['files']) == expected, 'Missing or unexpected candidate files' + assert {p.name for p in directory.iterdir()} == expected | {'candidate.json'}, 'Unexpected candidate contents' + for name, checksum in record['files'].items(): + assert Path(name).name == name and sha256(directory / name) == checksum, 'Candidate checksum mismatch: ' + name + source = snapshot(directory, record) + assert source.version == version + assert record['sourceTree'] == subprocess.check_output(['git', '-C', str(ROOT), 'rev-parse', record['sourceCommit'] + '^{tree}'], text=True).strip() + _, sbom = builder.inspect_jar(directory / versions.jar_name(version), version) + assert sbom == (directory / 'sbom.cdx.json').read_bytes(), 'Embedded and staged SBOM differ' + digest, _ = release_image.saved_identity(directory / 'container.tar.gz', version, record['sourceCommit']) + assert record.get('imageConfigDigest', digest) == digest, 'Saved image configuration checksum mismatch' + return {**record, 'imageConfigDigest': digest} + + +def load_image(directory, record): + actual, digest = release_image.load(Path(directory) / 'container.tar.gz', record['version'], record['sourceCommit'], + record['files'][versions.jar_name(record['version'])], builder.identity) + assert digest == record['imageConfigDigest'] + return actual + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--output', required=True) + parser.add_argument('--image', default='busystack-caldav:ci') + args = parser.parse_args() + revision = versions.revision(root=ROOT) + version = versions.resolve(ROOT, source=revision) + assert os.environ['GITHUB_REPOSITORY'] == REPOSITORY and os.environ['GITHUB_SHA'] == revision + assert not subprocess.check_output(['git', 'status', '--porcelain=v1', '--untracked-files=all'], cwd=ROOT), 'Dirty candidate source' + destination = Path(args.output) + destination.mkdir(parents=True, exist_ok=False) + jar = destination / versions.jar_name(version) + expected = ROOT / 'target' / jar.name + assert sorted(p.name for p in expected.parent.glob('*.jar')) == [jar.name], 'Ambiguous build artifacts' + checksum, sbom = builder.inspect_jar(expected, version) + config = builder.identity(args.image, version, revision, checksum) + shutil.copyfile(expected, jar) + (destination / 'sbom.cdx.json').write_bytes(sbom) + release_source.retain(revision, destination / 'source.tar.gz', ROOT) + archive = destination / 'container.tar' + subprocess.run(['docker', 'image', 'save', '--output', str(archive), config['Id']], check=True) + subprocess.run(['gzip', '-n', str(archive)], check=True) + image_config, _ = release_image.saved_identity(destination / 'container.tar.gz', version, revision) + write_json(destination / 'candidate.json', { + 'schemaVersion': 2, 'repository': REPOSITORY, 'version': version, + 'sourceCommit': revision, 'sourceTree': subprocess.check_output(['git', 'rev-parse', revision + '^{tree}'], cwd=ROOT, text=True).strip(), + 'ciRun': int(os.environ['GITHUB_RUN_ID']), 'ciAttempt': int(os.environ['GITHUB_RUN_ATTEMPT']), + 'workflow': '.github/workflows/ci.yml', 'platform': 'linux/amd64', + 'imageId': config['Id'], 'imageConfigDigest': image_config, + 'localDescriptorDigest': config.get('Descriptor', {}).get('digest'), + 'imageTag': args.image, 'files': {p.name: sha256(p) for p in destination.iterdir()}, + }) + validate(destination, version, revision) + + +if __name__ == '__main__': + main() diff --git a/scripts/release_image.py b/scripts/release_image.py new file mode 100644 index 0000000..fc75ed7 --- /dev/null +++ b/scripts/release_image.py @@ -0,0 +1,43 @@ +"""Keep saved runtime configuration identity separate from daemon-local image IDs.""" +import hashlib +import json +from pathlib import PurePosixPath +import re +import subprocess +import tarfile + + +def saved_identity(path, version, revision): + with tarfile.open(path, 'r:*') as archive: + manifest = json.load(archive.extractfile('manifest.json')) + assert len(manifest) == 1, 'Expected one saved Linux AMD64 image' + name = manifest[0]['Config'] + assert not PurePosixPath(name).is_absolute() and '..' not in PurePosixPath(name).parts, 'Unsafe image configuration path' + member = archive.getmember(name) + assert member.isfile(), 'Expected a regular image configuration blob' + data = archive.extractfile(member).read() + config = json.loads(data) + assert config['os'] == 'linux' and config['architecture'] == 'amd64', 'Unqualified saved image platform' + labels = config['config']['Labels'] + assert labels['org.opencontainers.image.version'] == version, 'Saved image version differs from Maven' + assert labels['org.opencontainers.image.revision'] == revision, 'Saved image revision differs from qualified source' + assert labels['org.opencontainers.image.source'] == 'https://github.com/busystack/caldav-server' + assert labels['org.opencontainers.image.licenses'] == 'Apache-2.0' + assert config['rootfs']['type'] == 'layers' + return 'sha256:' + hashlib.sha256(data).hexdigest(), config + + +def load(path, version, revision, jar_sha, inspect): + digest, expected = saved_identity(path, version, revision) + output = subprocess.check_output(['docker', 'image', 'load', '--input', str(path)], text=True) + references = [] + for line in output.splitlines(): + for prefix in ('Loaded image ID: ', 'Loaded image: '): + if line.startswith(prefix): + references.append(line.removeprefix(prefix)) + assert len(references) == 1, 'Missing or ambiguous loaded image identity' + actual = inspect(references[0], version, revision, jar_sha) + assert re.fullmatch(r'sha256:[0-9a-f]{64}', actual['Id']), 'Invalid loaded image ID' + assert actual['RootFS']['Layers'] == expected['rootfs']['diff_ids'], 'Loaded image root filesystem differs from verified archive' + assert actual['Config'] == expected['config'], 'Loaded image runtime configuration differs from verified archive' + return actual, digest diff --git a/scripts/release_source.py b/scripts/release_source.py new file mode 100644 index 0000000..d8e7187 --- /dev/null +++ b/scripts/release_source.py @@ -0,0 +1,93 @@ +"""Read release payloads exclusively from the exact qualified Git tree.""" +import gzip +import hashlib +import io +from pathlib import Path, PurePosixPath +import subprocess +import tarfile + +import release_version as versions + + +def tree_entries(source, root): + tree = subprocess.check_output(['git', '-C', str(root), 'ls-tree', '-rz', source]) + entries = {} + for item in tree.split(b'\0'): + if not item: + continue + metadata, name = item.split(b'\t', 1) + mode, kind, sha = metadata.decode().split() + assert kind == 'blob' and mode in ('100644', '100755'), 'Unsupported source tree entry' + entries[name.decode()] = (mode, sha) + return entries + + +def archive(source, root=versions.ROOT): + source = versions.revision(source, root) + entries = tree_entries(source, root) + shas = sorted({sha for _, sha in entries.values()}) + # git archive applies eol/export attributes. Read raw objects so even a newer + # controller's attributes cannot transform or omit qualified source bytes. + batch = subprocess.check_output(['git', '-C', str(root), 'cat-file', '--batch'], + input=('\n'.join(shas) + '\n').encode()) + blobs = {} + cursor = 0 + for expected in shas: + end = batch.index(b'\n', cursor) + sha, kind, length = batch[cursor:end].decode().split() + assert sha == expected and kind == 'blob', 'Unexpected Git object' + cursor = end + 1 + size = int(length) + blobs[sha] = batch[cursor:cursor + size] + cursor += size + assert batch[cursor:cursor + 1] == b'\n' + cursor += 1 + assert cursor == len(batch) + buffer = io.BytesIO() + with tarfile.open(fileobj=buffer, mode='w') as output: + for name, (mode, sha) in sorted(entries.items()): + entry = tarfile.TarInfo(name) + entry.size = len(blobs[sha]) + entry.mode = 0o755 if mode == '100755' else 0o644 + output.addfile(entry, io.BytesIO(blobs[sha])) + return buffer.getvalue() + + +def files(data): + result = {} + with tarfile.open(fileobj=io.BytesIO(data), mode='r:*') as stream: + for entry in stream: + if entry.isdir(): + continue + name = PurePosixPath(entry.name) + assert entry.isfile() and not name.is_absolute() and '..' not in name.parts, 'Unsafe source snapshot entry' + assert entry.name not in result, 'Duplicate source snapshot entry' + result[entry.name] = stream.extractfile(entry).read() + return result + + +class Snapshot: + def __init__(self, source, root=versions.ROOT, retained=None): + self.source = versions.revision(source, root) + expected = files(archive(self.source, root)) + self.files = files(Path(retained).read_bytes()) if retained else expected + assert self.files == expected, 'Retained source snapshot differs from qualified Git tree' + expected_blobs = {name: sha for name, (_, sha) in tree_entries(self.source, root).items()} + actual_blobs = {name: hashlib.sha1(b'blob ' + str(len(data)).encode() + b'\0' + data).hexdigest() + for name, data in self.files.items()} + assert actual_blobs == expected_blobs, 'Source archive does not reproduce qualified Git blobs' + self.version = versions.from_pom(self.read('pom.xml')) + + def read(self, name): + assert name in self.files, 'Missing qualified source input: ' + name + return self.files[name] + + def under(self, prefix): + names = sorted(name for name in self.files if name.startswith(prefix + '/')) + assert names, 'Missing qualified source directory: ' + prefix + return names + + +def retain(source, destination, root=versions.ROOT): + Snapshot(source, root) + Path(destination).write_bytes(gzip.compress(archive(source, root), mtime=0)) diff --git a/scripts/release_version.py b/scripts/release_version.py new file mode 100644 index 0000000..7de07d5 --- /dev/null +++ b/scripts/release_version.py @@ -0,0 +1,76 @@ +"""Release identity comes only from the selected source's Maven project.version.""" +import argparse +from pathlib import Path +import re +import subprocess +import xml.etree.ElementTree as ET + +ROOT = Path(__file__).resolve().parents[1] +REPOSITORY = 'busystack/caldav-server' +STABLE = r'(?:0|[1-9][0-9]*)\.(?:0|[1-9][0-9]*)\.(?:0|[1-9][0-9]*)' + + +def stable(value): + if not isinstance(value, str) or not re.fullmatch(STABLE, value): + raise ValueError('Expected a stable Maven project.version X.Y.Z without leading zeros: ' + repr(value)) + return value + + +def from_pom(data): + project = ET.fromstring(data) + ns = {'m': 'http://maven.apache.org/POM/4.0.0'} + if (project.findtext('m:groupId', namespaces=ns), project.findtext('m:artifactId', namespaces=ns)) != ('org.busystack', 'caldav-server'): + raise ValueError('Unexpected Maven project coordinates') + return stable(project.findtext('m:version', namespaces=ns)) + + +def revision(value='HEAD', root=ROOT): + sha = subprocess.check_output(['git', '-C', str(root), 'rev-parse', '--verify', value + '^{commit}'], text=True).strip() + if not re.fullmatch(r'[0-9a-f]{40}', sha): + raise ValueError('Expected an exact Git source revision') + return sha + + +def resolve(root=ROOT, source=None, requested=None): + data = (Path(root) / 'pom.xml').read_bytes() if source is None else subprocess.check_output( + ['git', '-C', str(root), 'show', revision(source, root) + ':pom.xml']) + version = from_pom(data) + if requested is not None and stable(requested) != version: + raise ValueError(f'Requested version {requested} differs from qualified Maven project.version {version}') + return version + + +def tag(version): + return 'v' + stable(version) + + +def candidate_tag(version, value): + stable(version) + if not value or len(value) > 128 or not re.fullmatch(re.escape(version) + r'-rc\.[1-9][0-9]*', value): + raise ValueError('Expected a release candidate tag such as ' + version + '-rc.1') + return value + + +def jar_name(version): + return 'caldav-server-' + stable(version) + '.jar' + + +def deployment_name(version): + return 'caldav-server-' + stable(version) + '-deploy.tar.gz' + + +def receipt_path(version): + return 'qualification/evidence/releases/' + stable(version) + '/verification.json' + + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--source-sha') + parser.add_argument('--requested') + parser.add_argument('--candidate') + parser.add_argument('--format', choices=('version', 'tag', 'jar', 'deployment', 'receipt'), default='version') + args = parser.parse_args() + version = resolve(source=args.source_sha, requested=args.requested) + print(candidate_tag(version, args.candidate) if args.candidate is not None else { + 'version': lambda v: v, 'tag': tag, 'jar': jar_name, + 'deployment': deployment_name, 'receipt': receipt_path}[args.format](version)) diff --git a/scripts/restore.sh b/scripts/restore.sh new file mode 100755 index 0000000..5bc6abe --- /dev/null +++ b/scripts/restore.sh @@ -0,0 +1,88 @@ +#!/usr/bin/env bash +set -euo pipefail +umask 077 + +if [[ $# != 1 ]]; then + printf 'Usage: %s ARCHIVE_PATH\nRestore requires a stopped application and an empty database.\n' "$0" >&2 + exit 2 +fi +archive=$1 +test -r "$archive" +test -r "$archive.sha256" +archive_directory=$(dirname -- "$archive") +archive_filename=$(basename -- "$archive") +(cd "$archive_directory" && sha256sum --check --status "$archive_filename.sha256") +running=$(docker compose ps --status running --services) +while IFS= read -r service; do + if [[ "$service" == application ]]; then + printf 'Stop the application before restoring; database must remain running.\n' >&2 + exit 2 + fi +done <<< "$running" +table_count=$(docker compose exec -T database psql --username postgres --dbname busystack --no-psqlrc --tuples-only --no-align --set ON_ERROR_STOP=1 --command "SELECT count(*) FROM pg_tables WHERE schemaname NOT IN ('pg_catalog','information_schema');") +if [[ "$table_count" != 0 ]]; then + printf 'Restore target is not empty. Use an isolated, newly initialized database; this script does not delete existing data.\n' >&2 + exit 2 +fi +docker compose exec -T database pg_restore --username postgres --role busystack --dbname busystack --exit-on-error --single-transaction --no-owner --no-acl < "$archive" +docker compose exec -T database psql --username postgres --dbname busystack --no-psqlrc --set ON_ERROR_STOP=1 --single-transaction <<'SQL' +DO $recovery$ +DECLARE + affected integer; +BEGIN + IF to_regclass('public.flyway_schema_history') IS NULL THEN + RAISE EXCEPTION 'Missing migration history; leave application stopped'; + END IF; + IF EXISTS (SELECT 1 FROM flyway_schema_history WHERE success IS NOT TRUE) THEN + RAISE EXCEPTION 'Failed migration history; leave application stopped and repair with the supported procedure'; + END IF; + IF NOT EXISTS (SELECT 1 FROM flyway_schema_history WHERE version='1' AND success) THEN + RAISE EXCEPTION 'Missing V1 migration history; leave application stopped'; + END IF; + IF EXISTS (SELECT 1 FROM flyway_schema_history WHERE version IS NOT NULL AND version NOT IN ('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13')) THEN + RAISE EXCEPTION 'This recovery script supports schema versions 1 through 13 only; leave application stopped and update recovery procedure'; + END IF; + IF EXISTS (SELECT version FROM flyway_schema_history WHERE version IS NOT NULL GROUP BY version HAVING count(*) <> 1) THEN + RAISE EXCEPTION 'Duplicate migration versions; leave application stopped'; + END IF; + IF (SELECT count(*) FROM flyway_schema_history WHERE version IS NOT NULL) <> + (SELECT max(version::integer) FROM flyway_schema_history WHERE version IS NOT NULL) THEN + RAISE EXCEPTION 'Gap in migration history; leave application stopped'; + END IF; + IF to_regclass('public.sync_continuations') IS NOT NULL THEN + DELETE FROM sync_continuations; + END IF; + UPDATE system_state SET recovery_epoch = gen_random_uuid(); + GET DIAGNOSTICS affected = ROW_COUNT; + IF affected <> 1 THEN + RAISE EXCEPTION 'Expected exactly one system_state row'; + END IF; + DELETE FROM dav_credentials; + UPDATE accounts SET enabled = false; + IF to_regclass('public.oauth_refresh_families') IS NOT NULL THEN + UPDATE oauth_refresh_families SET revoked_at = COALESCE(revoked_at, now()); + END IF; + IF to_regclass('public.oauth2_authorization') IS NOT NULL THEN + DELETE FROM oauth2_authorization; + DELETE FROM oauth2_authorization_consent; + END IF; + IF to_regclass('public.user_credentials') IS NOT NULL THEN + DELETE FROM user_credentials; + DELETE FROM user_entities; + END IF; + IF to_regclass('public.external_identities') IS NOT NULL THEN + UPDATE external_identities SET last_verified_at = timestamptz '1970-01-01 00:00:00+00'; + END IF; + IF to_regclass('public.spring_session') IS NOT NULL THEN + DELETE FROM spring_session; + DELETE FROM account_recovery_codes; + DELETE FROM webauthn_ceremonies; + DELETE FROM login_throttle; + UPDATE accounts SET security_epoch = gen_random_uuid(); + INSERT INTO security_audit(action,outcome) VALUES ('DATABASE_RESTORE','SUCCESS'); + END IF; +END; +$recovery$; +SQL +printf 'Restored database, disabled all accounts, changed recovery epoch, removed DAV credentials, passkeys and OAuth grants, and revoked refresh families.\n' +printf 'Keep every application instance stopped. Verify each user identity, then use the documented local or external offline account-recovery command to enable only that verified account. Later schemas require a reviewed recovery procedure.\n' diff --git a/scripts/validate-workflows.sh b/scripts/validate-workflows.sh new file mode 100644 index 0000000..09c8aaf --- /dev/null +++ b/scripts/validate-workflows.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash +set -euo pipefail +# Official unmodified release binaries, hashes from GitHub release asset metadata. +workflow_tools=$(mktemp -d /tmp/busystack-workflow-tools.XXXXXXXX) +trap 'rm -rf -- "$workflow_tools"' EXIT +curl --fail --silent --show-error --location --retry 3 \ + https://github.com/rhysd/actionlint/releases/download/v1.7.12/actionlint_1.7.12_linux_amd64.tar.gz \ + --output "$workflow_tools/actionlint.tar.gz" +curl --fail --silent --show-error --location --retry 3 \ + https://github.com/koalaman/shellcheck/releases/download/v0.11.0/shellcheck-v0.11.0.linux.x86_64.tar.gz \ + --output "$workflow_tools/shellcheck.tar.gz" +printf '%s %s\n' 8aca8db96f1b94770f1b0d72b6dddcb1ebb8123cb3712530b08cc387b349a3d8 "$workflow_tools/actionlint.tar.gz" \ + b7af85e41cc99489dcc21d66c6d5f3685138f06d34651e6d34b42ec6d54fe6f6 "$workflow_tools/shellcheck.tar.gz" | sha256sum --check --status +tar -xzf "$workflow_tools/actionlint.tar.gz" -C "$workflow_tools" actionlint +tar -xzf "$workflow_tools/shellcheck.tar.gz" -C "$workflow_tools" shellcheck-v0.11.0/shellcheck +export PATH="$workflow_tools:$workflow_tools/shellcheck-v0.11.0:$PATH" +actionlint +shellcheck scripts/*.sh diff --git a/scripts/verify-dependency-integrity.py b/scripts/verify-dependency-integrity.py new file mode 100644 index 0000000..314d9af --- /dev/null +++ b/scripts/verify-dependency-integrity.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python3 +"""Check every packaged runtime JAR against independently retrieved Maven Central bytes.""" +import argparse +import hashlib +import io +import json +from pathlib import Path +import zipfile +import sys +sys.path.insert(0, str(Path(__file__).resolve().parent)) +import release_version as versions + +ROOT = Path(__file__).resolve().parents[1] +LOCK = ROOT / 'qualification/distribution/runtime-artifacts.json' +REMOVED = ('Ical4jCalendarParser', 'Ical4jZoneRulesBuilder') + + +def inspect(path, lock=LOCK): + record = json.loads(Path(lock).read_text()) + expected = record['artifacts'] + with zipfile.ZipFile(path) as jar: + names = jar.namelist() + assert len(names) == len(set(names)), 'Duplicate executable archive entries' + assert not any(entry.startswith('BOOT-INF/classes/') and entry.endswith('.class') and not entry.startswith('BOOT-INF/classes/org/busystack/') for entry in names), 'Unexpected application class namespace' + assert not any(any(name in entry for name in REMOVED) for entry in names), 'Removed implementation in JAR' + assert not any(entry.startswith(('BOOT-INF/classes/net/fortuna/', 'BOOT-INF/classes/org/springframework/')) for entry in names), 'Shadowed third-party classes' + loader = {entry: hashlib.sha256(jar.read(entry)).hexdigest() for entry in names if not entry.startswith('BOOT-INF/') and entry.endswith('.class')} + assert loader == record.get('executableLoader', {}).get('classes', {}), 'Executable loader differs from official artifact' + actual = {entry.removeprefix('BOOT-INF/lib/'): hashlib.sha256(jar.read(entry)).hexdigest() + for entry in names if entry.startswith('BOOT-INF/lib/') and entry.endswith('.jar')} + assert actual == {name: item['sha256'] for name, item in expected.items()}, 'Packaged dependency differs from official artifact lock' + for entry in names: + if entry.startswith('BOOT-INF/lib/') and entry.endswith('.jar'): + with zipfile.ZipFile(io.BytesIO(jar.read(entry))) as dependency: + assert not any(any(name in member for name in REMOVED) for member in dependency.namelist()), 'Removed implementation hidden in dependency' + return {'status': 'passed', 'officialArtifacts': len(actual), 'officialLoaderClasses': len(loader), 'jarSha256': hashlib.sha256(Path(path).read_bytes()).hexdigest()} + + +def source(): + for directory in ('src', 'scripts', '.github'): + assert not any(path.name.endswith('.patch') for path in (ROOT / directory).rglob('*')), 'Patch payload in active tree' + for path in (ROOT / 'src/main').rglob('*'): + assert not any(name in path.name for name in REMOVED), 'Removed implementation reintroduced' + if path.suffix == '.java': + text = path.read_text() + assert not any(name in text for name in REMOVED), 'Removed implementation referenced' + assert 'package net.fortuna.' not in text, 'Shadowed upstream namespace' + assert 'class ParameterParser' not in text and 'class ComponentListParser' not in text, 'Copied parser internals' + assert not list((ROOT / 'docs').glob('ical4j-*.patch')), 'Obsolete patch payload reintroduced' + + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--jar', default=str(ROOT / 'target' / versions.jar_name(versions.resolve(ROOT)))) + args = parser.parse_args() + source() + print(json.dumps(inspect(args.jar), sort_keys=True)) diff --git a/scripts/verify-distribution.py b/scripts/verify-distribution.py new file mode 100644 index 0000000..6c139aa --- /dev/null +++ b/scripts/verify-distribution.py @@ -0,0 +1,109 @@ +#!/usr/bin/env python3 +"""Inspect source, executable JAR and final image license delivery; no feed or network bypass.""" +import argparse +import importlib.util +from datetime import datetime, timezone +import hashlib +import io +import json +from pathlib import Path +import subprocess +import sys +import tarfile +import zipfile + +ROOT = Path(__file__).resolve().parents[1] +APACHE_SHA = 'cfc7749b96f63bd31c3c42b5c471bf756814053e847c10f3eb003417bc523d30' +DOCUMENTS = ('LICENSE', 'NOTICE', 'THIRD-PARTY-NOTICES') +sys.path.insert(0, str(Path(__file__).resolve().parent)) +import release_version as versions + + +def inspect_jar(path, version=None): + version = version or versions.resolve(ROOT) + with zipfile.ZipFile(path) as jar: + manifest = dict(line.split(': ', 1) for line in jar.read('META-INF/MANIFEST.MF').decode().splitlines() if ': ' in line) + assert manifest.get('Implementation-Version') == version, 'JAR implementation version differs from release' + coordinates = dict(line.split('=', 1) for line in jar.read('META-INF/maven/org.busystack/caldav-server/pom.properties').decode().splitlines() if '=' in line and not line.startswith('#')) + assert coordinates.get('version') == version, 'JAR Maven version differs from release' + for name in DOCUMENTS: + assert jar.read('META-INF/' + name) == (ROOT / name).read_bytes(), 'JAR missing/different ' + name + assert jar.read('META-INF/licenses/ical4j-parser-BSD-3-Clause.txt') == (ROOT / 'src/main/resources/META-INF/licenses/ical4j-parser-BSD-3-Clause.txt').read_bytes() + assert b'Spring' in jar.read('META-INF/licenses/spring-derived-schemas.txt') + assert b'allow-sync-depth-one: true' in jar.read('BOOT-INF/classes/application.yaml'), 'Executable JAR default differs from approved interoperability default' + for name in ('asm-9.7.1-copyright-and-license.txt','logback-LICENSE.txt','EPL-2.0.txt','LGPL-2.1.txt'): + assert jar.read('META-INF/licenses/'+name)==(ROOT/'licenses'/name).read_bytes() + bom = json.loads(jar.read('META-INF/sbom/application.cdx.json')) + assert bom['metadata']['component'].get('version') == version, 'Project SBOM version differs from release' + project_licenses = bom['metadata']['component']['licenses'] + assert isinstance(project_licenses,list) and len(project_licenses)==1 and set(project_licenses[0])=={'license'}, 'Project SBOM license is incorrect' + project_license = project_licenses[0]['license'] + assert isinstance(project_license,dict) and set(project_license) in ({'id'},{'id','url'}) and project_license.get('id')=='Apache-2.0', 'Project SBOM license is incorrect' + if 'url' in project_license: + assert project_license['url'] in ('https://www.apache.org/licenses/LICENSE-2.0','https://www.apache.org/licenses/LICENSE-2.0.txt'), 'Project SBOM license URL is incorrect' + session=[c for c in bom['components'] if c.get('group')=='org.springframework.session'] + assert len(session)==2 and all(c['version']=='4.1.1' for c in session) + assert all(any(l.get('license',{}).get('name')=='Broadcom Foundation License' for l in c['licenses']) for c in session), 'Original upstream POM metadata must remain traceable; do not silently relabel components' + dependencies = [] + for name in jar.namelist(): + if not name.startswith('BOOT-INF/lib/') or not name.endswith('.jar'): + continue + with zipfile.ZipFile(io.BytesIO(jar.read(name))) as nested: + notices = {n: hashlib.sha256(nested.read(n)).hexdigest() for n in nested.namelist() if not n.endswith('/') and n.upper().split('/')[-1].startswith(('LICENSE','NOTICE','COPYING','COPYRIGHT'))} + dependencies.append({'artifact': name.split('/')[-1], 'deliveredLicenseAndNoticeFiles': notices}) + assert any(d['artifact'] == 'spring-session-core-4.1.1.jar' and d['deliveredLicenseAndNoticeFiles'] for d in dependencies) + assert any(d['artifact'] == 'spring-session-jdbc-4.1.1.jar' and d['deliveredLicenseAndNoticeFiles'] for d in dependencies) + return {'projectVersion': version, 'projectLicense': project_licenses, 'jarSha256': hashlib.sha256(Path(path).read_bytes()).hexdigest(), 'runtimeComponents': len(bom['components']), 'nestedArtifacts': dependencies} + + +def inspect_image(image, expected_jar_sha, revision, version): + config = json.loads(subprocess.check_output(['docker', 'image', 'inspect', image], text=True))[0] + labels = config['Config']['Labels'] or {} + assert labels.get('org.opencontainers.image.version') == version, 'Image version differs from release' + assert labels.get('org.opencontainers.image.licenses') == 'Apache-2.0' + assert labels.get('org.opencontainers.image.source') == 'https://github.com/busystack/caldav-server' + assert labels.get('org.opencontainers.image.revision') == revision, 'Image source revision differs from qualified revision' + for name in DOCUMENTS: + data = subprocess.check_output(['docker','run','--rm','--entrypoint','cat',image,'/opt/busystack/licenses/'+name]) + assert data == (ROOT/name).read_bytes(), 'Image missing/different ' + name + checksum = subprocess.check_output(['docker','run','--rm','--entrypoint','sha256sum',image,'/opt/busystack/application.jar'],text=True).split()[0] + assert checksum == expected_jar_sha, 'Host/container executable JAR bytes differ' + subprocess.run(['docker','run','--rm','--entrypoint','sh',image,'-ec', 'test -r /opt/java/openjdk/legal/java.base/LICENSE; test -r /opt/java/openjdk/legal/java.base/ASSEMBLY_EXCEPTION; test -r /usr/share/doc/base-files/copyright; test -r /opt/busystack/licenses/runtime-dependency-inventory.json'],check=True) + return {'id':config['Id'], 'labels':labels,'jarSha256':checksum, 'jdkLegalDirectory':'/opt/java/openjdk/legal','osLicenseDirectory':'/usr/share/doc'} + + +def main(): + parser=argparse.ArgumentParser(description=__doc__) + parser.add_argument('--jar') + parser.add_argument('--image',required=True) + parser.add_argument('--revision',default=None) + parser.add_argument('--output',default=str(ROOT/'target/qualification/distribution.json')) + args=parser.parse_args() + revision=args.revision or subprocess.check_output(['git','rev-parse','HEAD'],cwd=ROOT,text=True).strip() + version = versions.resolve(ROOT, source=revision) + args.jar = args.jar or str(ROOT / 'target' / versions.jar_name(version)) + report={'recordedAt':datetime.now(timezone.utc).isoformat(), 'sourceRevision':revision,'status':'error'} + try: + assert hashlib.sha256((ROOT/'LICENSE').read_bytes()).hexdigest()==APACHE_SHA, 'Project LICENSE must be the unmodified official Apache text' + source=subprocess.check_output(['git','archive','--format=tar',revision],cwd=ROOT) + with tarfile.open(fileobj=io.BytesIO(source)) as archive: + for name in DOCUMENTS: + assert archive.extractfile(name).read()==(ROOT/name).read_bytes(), 'Source archive missing/different '+name + report['sourceArchiveSha256']=hashlib.sha256(source).hexdigest() + spec = importlib.util.spec_from_file_location('integrity', ROOT / 'scripts/verify-dependency-integrity.py') + integrity = importlib.util.module_from_spec(spec) + spec.loader.exec_module(integrity) + integrity.source() + integrity.inspect(args.jar) + report.update(inspect_jar(args.jar, version)) + report['image']=inspect_image(args.image,report['jarSha256'],revision,version) + report['status']='passed' + except (AssertionError,OSError,KeyError,ValueError,subprocess.CalledProcessError) as error: + report['error']=str(error) + destination=Path(args.output); destination.parent.mkdir(parents=True,exist_ok=True) + destination.write_text(json.dumps(report,sort_keys=True,indent=2)+'\n') + print(json.dumps({k:v for k,v in report.items() if k!='nestedArtifacts'},sort_keys=True)) + return 0 if report['status']=='passed' else 1 + +if __name__=='__main__': + sys.exit(main()) diff --git a/scripts/verify-installation-bundle.py b/scripts/verify-installation-bundle.py new file mode 100644 index 0000000..73bebe8 --- /dev/null +++ b/scripts/verify-installation-bundle.py @@ -0,0 +1,216 @@ +#!/usr/bin/env python3 +"""Exercise exact downloaded/staged deployment assets in arbitrary disposable directories.""" +import argparse +import importlib.util +import json +import os +from pathlib import Path +import secrets +import shutil +import socket +import subprocess +import sys +import tarfile +import tempfile +import time +import urllib.error +import urllib.request + +ROOT = Path(__file__).resolve().parents[1] + + +def load(name, path): + spec = importlib.util.spec_from_file_location(name, path) + result = importlib.util.module_from_spec(spec) + spec.loader.exec_module(result) + return result + + +candidate = load('candidate', ROOT / 'scripts/release-candidate.py') +bundle = load('bundle', ROOT / 'scripts/release-bundle.py') +versions = candidate.versions + + +def port(): + with socket.socket() as listener: + listener.bind(('127.0.0.1', 0)) + return listener.getsockname()[1] + + +def jar_smoke(jar, extracted, directory): + """Run the release JAR using host Java, independently of the application container.""" + project = 'busystack-jar-' + secrets.token_hex(6) + database = directory / 'database-password' + admin = directory / 'postgres-admin-password' + bootstrap = directory / 'bootstrap-password' + for path in (database, admin, bootstrap): + path.write_text(secrets.token_urlsafe(32) + '\n') + path.chmod(0o600 if path == bootstrap else 0o444) + db_port, app_port = port(), port() + image = 'postgres:18.6@sha256:edb0c08dc33befd0262ab3b5cf1c4f3f8cfd8b09e88c05b45adee29d6761ddb6' + jar_secrets = directory / 'jar-secrets' + jar_secrets.mkdir(mode=0o700) + environment = os.environ.copy() + environment.update(SPRING_DATASOURCE_URL=f'jdbc:postgresql://127.0.0.1:{db_port}/busystack', + SPRING_DATASOURCE_USERNAME='busystack', SPRING_CONFIG_IMPORT='configtree:' + str(jar_secrets) + '/', + SERVER_ADDRESS='127.0.0.1', SERVER_PORT=str(app_port), + BUSYSTACK_PUBLIC_BASE_URL='https://calendar.example.test', BUSYSTACK_ISSUER='https://calendar.example.test') + (jar_secrets / 'spring.datasource.password').write_bytes(database.read_bytes()) + (jar_secrets / 'spring.datasource.password').chmod(0o600) + application = None + with (directory / 'jar.log').open('w') as log: + try: + subprocess.run(['docker', 'run', '-d', '--name', project, '--publish', f'127.0.0.1:{db_port}:5432', + '--env', 'POSTGRES_DB=busystack', '--env', 'POSTGRES_USER=postgres', + '--env', 'POSTGRES_PASSWORD_FILE=/run/secrets/postgres_admin_password', + '--env', 'POSTGRES_INITDB_ARGS=--auth-host=scram-sha-256 --data-checksums', + '--mount', f'type=bind,src={admin},dst=/run/secrets/postgres_admin_password,readonly', + '--mount', f'type=bind,src={database},dst=/run/secrets/spring.datasource.password,readonly', + '--mount', f'type=bind,src={extracted / "scripts/init-db.sh"},dst=/docker-entrypoint-initdb.d/10-busystack.sh,readonly', image], check=True, stdout=log, stderr=log) + deadline = time.monotonic() + 120 + while subprocess.run(['docker', 'exec', project, 'pg_isready', '-h', '127.0.0.1', '-U', 'busystack', '-d', 'busystack'], stdout=log, stderr=log).returncode: + assert time.monotonic() < deadline, 'JAR database did not become ready' + time.sleep(0.5) + subprocess.run(['java', '-jar', str(jar), '--spring.main.web-application-type=none', '--busystack.bootstrap=true', + '--busystack.bootstrap-username=jaradmin', '--busystack.bootstrap-password-file=' + str(bootstrap)], env=environment, check=True, stdout=log, stderr=log, timeout=180) + for _ in range(2): + application = subprocess.Popen(['java', '-jar', str(jar)], env=environment, stdout=log, stderr=log) + deadline = time.monotonic() + 180 + while True: + assert application.poll() is None, 'Downloaded JAR exited during startup' + try: + with urllib.request.urlopen(f'http://127.0.0.1:{app_port}/actuator/health/readiness', timeout=2) as response: + if response.status == 200 and json.load(response)['status'] == 'UP': + break + except (OSError, ValueError): + assert time.monotonic() < deadline, 'JAR startup deadline exceeded' + time.sleep(0.5) + for endpoint, expected in (('/login', 200), ('/dav/', 401), ('/.well-known/oauth-authorization-server', 200)): + try: + response = urllib.request.urlopen(f'http://127.0.0.1:{app_port}' + endpoint, timeout=10) + except urllib.error.HTTPError as error: + response = error + with response: + assert response.status == expected + if endpoint == '/dav/': + assert 'Basic' in response.headers.get('WWW-Authenticate', '') + elif endpoint.endswith('oauth-authorization-server'): + assert json.load(response)['issuer'] == 'https://calendar.example.test' + account = subprocess.check_output(['docker', 'exec', project, 'psql', '-U', 'postgres', '-d', 'busystack', '-Atc', "SELECT count(*) FROM accounts WHERE username='jaradmin' AND enabled;"]) + assert account.strip() == b'1' + application.terminate() + application.wait(timeout=30) + application = None + finally: + if application and application.poll() is None: + application.terminate() + application.wait(timeout=30) + subprocess.run(['docker', 'rm', '--force', '--volumes', project], stdout=log, stderr=log, check=True) + return {'status': 'passed', 'hostJava': subprocess.check_output(['java', '--version'], text=True).splitlines()[0], 'bootstrapAndRestartPersistence': True, 'loginDavChallengeAndIssuer': True} + + +def https_bundle_smoke(extracted, image, browser_image='busystack-passkey-browser:qualification'): + """Use the extracted deployment and proxy example with controller-owned test inputs.""" + sys.path.insert(0, str(ROOT / 'qualification/passkeys')) + browser = load('bundle_browser', ROOT / 'qualification/passkeys/run.py') + browser.ROOT = extracted + original_override = browser.proxy.override + def extracted_proxy(root, fixture, application_image, base, proxy_port): + from urllib.parse import urlsplit + public = urlsplit(base) + template = (extracted / 'nginx-subpath.conf.example').read_text() + template = template.replace('listen 443 ssl;', 'listen 8443 ssl;') + template = template.replace('server_name calendar.example.org;', 'server_name ' + public.hostname + ';') + template = template.replace('calendar.example.org', public.netloc).replace('/calendar', public.path) + template = template.replace('/etc/ssl/caldav/fullchain.pem', '/tls/server.pem').replace('/etc/ssl/caldav/privkey.pem', '/tls/server.key') + template = template.replace('http://127.0.0.1:8080', 'http://application:8080') + template = ('worker_processes 1;\nerror_log /dev/stderr warn;\npid /tmp/nginx.pid;\nevents { worker_connections 128; }\nhttp {\n' + 'access_log off;\nclient_body_temp_path /tmp/client_temp;\nproxy_temp_path /tmp/proxy_temp;\n' + 'fastcgi_temp_path /tmp/fastcgi_temp;\nuwsgi_temp_path /tmp/uwsgi_temp;\nscgi_temp_path /tmp/scgi_temp;\n' + template + '}\n') + return original_override(root, fixture, application_image, base, proxy_port, template=template) + browser.proxy.override = extracted_proxy + browser.operations.ROOT = extracted + previous_argv = sys.argv + sys.argv = ['bundle-browser', '--application-image', image, '--browser-image', browser_image, '--proxy', '--groups', '--restore'] + try: + browser.main() + finally: + sys.argv = previous_argv + browser.proxy.override = original_override + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--assets') + parser.add_argument('--public', action='store_true') + parser.add_argument('--version', help='Required for public verification; must match the asset manifest') + parser.add_argument('--output', required=True) + args = parser.parse_args() + result = {'status': 'failed', 'publicAnonymous': args.public} + try: + with tempfile.TemporaryDirectory(prefix='busystack install ') as temporary: + directory = Path(temporary) + assets = Path(args.assets).resolve() if args.assets else directory / 'download' + environment = os.environ.copy() + if args.public: + assert not args.assets and args.version, '--public requires an explicit --version' + versions.stable(args.version) + assets.mkdir() + # urllib sends neither GitHub auth headers nor cookies. + with urllib.request.urlopen('https://api.github.com/repos/busystack/caldav-server/releases/tags/' + versions.tag(args.version), timeout=60) as response: + release = json.load(response) + assert release['immutable'] and not release['draft'] + for asset in release['assets']: + assert Path(asset['name']).name == asset['name'] + with urllib.request.urlopen(asset['browser_download_url'], timeout=180) as response, (assets / asset['name']).open('wb') as output: + shutil.copyfileobj(response, output) + # Empty Docker config and removal of auth environment make pull anonymous. + docker_config = directory / 'anonymous-docker' + docker_config.mkdir() + (docker_config / 'config.json').write_text('{}\n') + environment['DOCKER_CONFIG'] = str(docker_config) + for key in ('GH_TOKEN', 'GITHUB_TOKEN', 'GH_ENTERPRISE_TOKEN', 'DOCKER_AUTH_CONFIG', 'REGISTRY_AUTH_FILE'): + environment.pop(key, None) + manifest = bundle.validate_assets(assets, args.version) + if args.public: + assert {a['name']: a['digest'] for a in release['assets']} == {p.name: 'sha256:' + candidate.sha256(p) for p in assets.iterdir()} + image = manifest['container']['image'] + assert image.endswith('@' + manifest['container']['registryManifestDigest']) + subprocess.run(['docker', 'pull', '--platform', 'linux/amd64', image], env=environment, check=True) + config = json.loads(subprocess.check_output(['docker', 'image', 'inspect', image], env=environment))[0] + assert config['Config']['Labels']['org.opencontainers.image.revision'] == manifest['sourceCommit'] + assert config['Config']['Labels']['org.opencontainers.image.version'] == manifest['version'] + jar_sha = subprocess.check_output(['docker', 'run', '--rm', '--entrypoint', 'sha256sum', image, '/opt/busystack/application.jar'], env=environment, text=True).split()[0] + assert jar_sha == manifest['jar']['sha256'] + assert config['Architecture'] == 'amd64' and config['Os'] == 'linux' + with tarfile.open(assets / manifest['deployment']['name']) as archive: + archive.extractall(directory, filter='data') + extracted = directory / ('caldav-server-' + manifest['version'] + '-deploy') + assert not (extracted / 'src').exists() and not (extracted / 'pom.xml').exists() + assert 'build:' not in (extracted / 'compose.yaml').read_text() + assert image in (extracted / 'compose.yaml').read_text() + # Reuse established procedure assertions while resolving every deployment path + # from the downloaded archive. Containers mount only extracted/generated files. + operations = load('bundle_operations', ROOT / 'scripts/verify-operations.py') + operations.ROOT = extracted + previous_environment = os.environ.copy() + os.environ.clear() + os.environ.update(environment) + try: + operations.verify(argparse.Namespace(image=image), None) + operations.verify(argparse.Namespace(image=image), '9') + https_bundle_smoke(extracted, image) + result['jarAlternative'] = jar_smoke(assets / manifest['jar']['name'], extracted, directory) + finally: + os.environ.clear() + os.environ.update(previous_environment) + result.update(status='passed', version=manifest['version'], sourceCommit=manifest['sourceCommit'], + jarSha256=manifest['jar']['sha256'], registryManifestDigest=manifest['container']['registryManifestDigest'], + deploymentSha256=manifest['deployment']['sha256'], + sourceDirectoryMounts=False, extractionDirectoryContainsSpaces=True, profiles=['same-version', 'V9-upgrade', 'HTTPS-proxy-browser-local-restore']) + finally: + candidate.write_json(args.output, result) + + +if __name__ == '__main__': + main() diff --git a/scripts/verify-operations.py b/scripts/verify-operations.py new file mode 100644 index 0000000..93ed14d --- /dev/null +++ b/scripts/verify-operations.py @@ -0,0 +1,495 @@ +#!/usr/bin/env python3 +"""Isolated packaged-application and database-recovery smoke test. + +This does not qualify browser MFA, HTTPS/proxy deployment or CalDAV clients. +The entry point always checks same-version recovery, V9 restoration/upgrade and malformed +history rejection. Only randomly named Compose projects created here are removed. +""" + +import argparse +import hashlib +import json +import os +import re +from pathlib import Path +import secrets +import subprocess +import tempfile +import urllib.error +import urllib.request + + +ROOT = Path(__file__).resolve().parent.parent + + +def sanitized_diagnostic(value, private_values, maximum=1200): + """Bound failure-only diagnostics without emitting fixture credentials or terminal controls.""" + value = str(value) + for private_value in private_values: + if private_value: + value = value.replace(private_value, "[redacted]") + value = re.sub(r"\x1b\[[0-9;]*[A-Za-z]", "", value) + value = re.sub(r"(?i)\b(?:Basic|Bearer)\s+[^\s,;]+", "[redacted-authorization]", value) + value = re.sub( + r"(?i)\b(authorization|password|client_secret|access_token|refresh_token|id_token)" + r"\s*[:=]\s*(?:\"[^\"]*\"|'[^']*'|[^\s,;]+)", + r"\1=[redacted]", value, + ) + value = re.sub(r"[A-Za-z0-9_+/=-]{40,}", "[redacted-value]", value) + value = "".join(character for character in value if character in "\n\r\t" or ord(character) >= 32) + return value if len(value) <= maximum else value[:maximum] + "[truncated]" + + +def failure_state(state, private_values): + health = state["State"].get("Health", {}) + configuration = state.get("Config", {}).get("Healthcheck", {}) + return { + "status": state["State"]["Status"], "exitCode": state["State"]["ExitCode"], + "oomKilled": state["State"]["OOMKilled"], "restarts": state["RestartCount"], + "startedAt": state["State"].get("StartedAt"), + "health": health.get("Status"), "failingStreak": health.get("FailingStreak"), + "healthcheck": {key: configuration.get(key) for key in ("StartPeriod", "Interval", "Timeout", "Retries")}, + "healthLog": [{ + "start": attempt.get("Start"), "end": attempt.get("End"), + "exitCode": attempt.get("ExitCode"), + "output": sanitized_diagnostic(attempt.get("Output", ""), private_values, 1024), + } for attempt in health.get("Log", [])[-5:]], + } + + +def startup_diagnostics(logs, private_values): + lines = [sanitized_diagnostic(line, private_values) for line in logs.splitlines()] + startup = [line for line in lines if any(marker in line for marker in ( + "Starting BusyStackApplication", "Started BusyStackApplication", "HikariPool-", + "Tomcat initialized", "Tomcat started", "Root WebApplicationContext", + ))] + migrations = [line for line in lines if any(marker in line for marker in ( + "Validated", "Successfully validated", "Current version of schema", "Migrating schema", + "Successfully applied", "Schema ", + ))] + errors = [line for line in lines if any(marker in line for marker in ( + " ERROR ", "Caused by:", "APPLICATION FAILED", "Description:", "Reason:", "Action:", + ))] + return {"startupMilestones": startup[-8:], "lastMigrations": migrations[-8:], "errors": errors[-8:]} + + +def execute(command, environment, *, content=None, expected=0): + result = subprocess.run( + command, + cwd=ROOT, + env=environment, + input=content, + text=True, + capture_output=True, + timeout=300, + ) + if result.returncode != expected: + raise RuntimeError( + f"Command {command[0:3]} returned {result.returncode}; " + f"expected {expected}.\n{result.stdout}\n{result.stderr}" + ) + return result.stdout.strip() + + +def development_environment(image): + config = json.loads(execute(['docker', 'image', 'inspect', image], os.environ.copy()))[0] + labels = config['Config']['Labels'] + return {'BUSYSTACK_APPLICATION_IMAGE': image, 'BUSYSTACK_BUILD_CONTEXT': str(ROOT / 'target/docker-context'), + 'BUSYSTACK_BUILD_VERSION': labels['org.opencontainers.image.version'], + 'BUSYSTACK_SOURCE_REVISION': labels['org.opencontainers.image.revision']} + + +def compose(environment, *arguments, content=None): + return execute(["docker", "compose", *arguments], environment, content=content) + + +def sql(environment, statement): + return compose( + environment, + "exec", + "-T", + "database", + "psql", + "--username", + "postgres", + "--dbname", + "busystack", + "--no-psqlrc", + "--tuples-only", + "--no-align", + "--set", + "ON_ERROR_STOP=1", + content=statement, + ) + + +class NoRedirect(urllib.request.HTTPRedirectHandler): + def redirect_request(self, req, fp, code, msg, headers, newurl): + return None + + +def request(base, path): + opener = urllib.request.build_opener(NoRedirect) + try: + response = opener.open(base + path, timeout=10) + except urllib.error.HTTPError as error: + response = error + with response: + return response.status, response.headers, response.read() + + +def checks(environment): + address = compose(environment, "port", "application", "8080").splitlines()[0] + base = "http://" + address + for probe in ("readiness", "liveness"): + status, _, body = request(base, "/actuator/health/" + probe) + assert status == 200 and json.loads(body)["status"] == "UP" + status, headers, _ = request(base, "/.well-known/caldav") + assert status == 308 + assert headers["Location"] == "https://calendar.example.test/dav/" + status, _, body = request(base, "/.well-known/oauth-authorization-server") + assert status == 200 + assert json.loads(body)["issuer"] == "https://calendar.example.test" + status, headers, _ = request(base, "/dav/") + assert status == 401 and "Basic" in headers.get("WWW-Authenticate", "") + application = compose(environment, "ps", "--quiet", "application") + config = json.loads( + execute(["docker", "inspect", application], environment) + )[0] + assert config["Config"]["User"] == "10001:10001" + assert config["HostConfig"]["ReadonlyRootfs"] + assert "ALL" in config["HostConfig"]["CapDrop"] + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--image", required=True) + parser.add_argument("--baseline-image", help="Existing qualified internal baseline for actual replacement; not an older public release") + args = parser.parse_args() + if not args.image or any(character.isspace() for character in args.image): + parser.error("--image must be a single Docker image reference") + if args.baseline_image and any(character.isspace() for character in args.baseline_image): + parser.error("--baseline-image must be a single Docker image reference") + execute(["docker", "info"], os.environ.copy()) + args.image = execute(["docker", "image", "inspect", "--format", "{{.Id}}", args.image], os.environ.copy()) + if args.baseline_image: + args.baseline_image = execute(["docker", "image", "inspect", "--format", "{{.Id}}", args.baseline_image], os.environ.copy()) + print("OPERATIONS_APPLICATION_IMAGE=" + args.image, flush=True) + + for initial_version in (None, "9"): + verify(args, initial_version) + + +def verify(args, initial_version): + print("OPERATIONS_PROFILE=" + ("same-version" if initial_version is None else "restore-V9-then-upgrade"), flush=True) + projects = [] + with tempfile.TemporaryDirectory(prefix="busystack-operations-") as directory: + private = Path(directory) + private.chmod(0o700) + for name in ("database-password", "admin-password", "bootstrap-password", "recovery-password"): + secret_file = private / name + secret_file.write_text(secrets.token_urlsafe(32) + "\n") + secret_file.chmod(0o600 if name in ("bootstrap-password", "recovery-password") else 0o444) + baseline_image = getattr(args, "baseline_image", None) if initial_version is None else None + initial_image = baseline_image or args.image + override = private / "compose.override.yaml" + override.write_text( + "services:\n application:\n" + f" image: {json.dumps(initial_image)}\n" + " ports: !override\n - '127.0.0.1::8080'\n" + ) + environment = os.environ.copy() + environment.update(development_environment(initial_image)) + environment.update( + COMPOSE_FILE=f"{ROOT / 'compose.yaml'}:{override}", + COMPOSE_PROJECT_NAME="busystack-operations-" + secrets.token_hex(6), + BUSYSTACK_DATABASE_PASSWORD_FILE=str(private / "database-password"), + BUSYSTACK_POSTGRES_ADMIN_PASSWORD_FILE=str(private / "admin-password"), + BUSYSTACK_PUBLIC_BASE_URL="https://calendar.example.test", + BUSYSTACK_ISSUER="https://calendar.example.test", + BUSYSTACK_ALLOW_INSECURE_LOCAL="false", + BUSYSTACK_HTTP_PORT="8080", + COMPOSE_PATH_SEPARATOR=":", + ) + try: + projects.append(environment.copy()) + compose(environment, "up", "-d", "--wait", "--wait-timeout", "120", "database") + compose( + environment, + "run", + "--rm", + "--no-deps", + "--user", + f"{os.getuid()}:{os.getgid()}", + "--volume", + f"{private / 'bootstrap-password'}:/bootstrap-password:ro", + "application", + "--spring.main.web-application-type=none", + "--busystack.bootstrap=true", + "--busystack.bootstrap-username=opsadmin", + "--busystack.bootstrap-password-file=/bootstrap-password", + *([] if initial_version is None else ["--spring.flyway.target=" + initial_version]), + ) + print("OPERATIONS_SCHEMA_VERSIONS=" + sql(environment, "SELECT string_agg(version,',' ORDER BY installed_rank) FROM flyway_schema_history WHERE success AND version IS NOT NULL;"), flush=True) + content = ( + "BEGIN:VCALENDAR\r\nVERSION:2.0\r\n" + "PRODID:-//BusyStack Operations Fixture//EN\r\n" + "BEGIN:VEVENT\r\nUID:operations-recovery-fixture\r\n" + "DTSTAMP:20261027T000000Z\r\nDTSTART:20261028T100000Z\r\n" + "SUMMARY:Recovery verification\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n" + ).encode() + checksum = hashlib.sha256(content).hexdigest() + # These are database-recovery fixtures, not issued protocol credentials. + sql( + environment, + f"""BEGIN; +INSERT INTO accounts(id,username,display_name) VALUES ('22222222-2222-4222-8222-222222222222','opsreader','Operations Reader'); +INSERT INTO collections(id,owner_id,name,component,revision) SELECT '11111111-1111-4111-8111-111111111111',id,'Operations fixture','VEVENT',1 FROM accounts WHERE username='opsadmin'; +INSERT INTO collection_members(collection_id,account_id,permission) VALUES ('11111111-1111-4111-8111-111111111111','22222222-2222-4222-8222-222222222222','READ'); +INSERT INTO calendar_resources(collection_id,name,uid,content,etag,revision) VALUES ('11111111-1111-4111-8111-111111111111','fixture.ics','operations-recovery-fixture',decode('{content.hex()}','hex'),'"{checksum}"',1); +INSERT INTO collection_changes(collection_id,revision,name,deleted) VALUES ('11111111-1111-4111-8111-111111111111',1,'fixture.ics',false); +INSERT INTO dav_credentials(id,account_id,label,verifier,expires_at) SELECT '33333333-3333-4333-8333-333333333333',id,'Operations fixture',password_hash,now()+interval '1 day' FROM accounts WHERE username='opsadmin'; +INSERT INTO oauth2_registered_client(id,client_id,client_name,client_authentication_methods,authorization_grant_types,redirect_uris,scopes,client_settings,token_settings) VALUES ('operations-client','operations-client','Operations database fixture','none','authorization_code,refresh_token','http://127.0.0.1:59621/callback','dav','{{}}','{{}}'); +INSERT INTO oauth_refresh_families(authorization_id,account_id,client_id,recovery_epoch,expires_at) SELECT 'operations-family',a.id,'operations-client',s.recovery_epoch,now()+interval '1 day' FROM accounts a CROSS JOIN system_state s WHERE username='opsadmin'; +INSERT INTO oauth_refresh_tokens(token_hash,authorization_id,expires_at) VALUES (repeat('a',64),'operations-family',now()+interval '1 day'); +INSERT INTO oauth2_authorization(id,registered_client_id,principal_name,authorization_grant_type) SELECT 'operations-grant','operations-client',id::text,'authorization_code' FROM accounts WHERE username='opsadmin'; +INSERT INTO oauth2_authorization_consent(registered_client_id,principal_name,authorities) SELECT 'operations-client',id::text,'SCOPE_dav' FROM accounts WHERE username='opsadmin'; +INSERT INTO user_entities(id,account_id,name,display_name) SELECT 'b3BlcmF0aW9ucw',id,'opsadmin','Operations Administrator' FROM accounts WHERE username='opsadmin'; +INSERT INTO user_credentials(credential_id,user_entity_user_id,public_key,signature_count,uv_initialized,backup_eligible,public_key_credential_type,backup_state,label) VALUES ('b3BlcmF0aW9ucy1jcmVkZW50aWFs','b3BlcmF0aW9ucw',decode('00','hex'),0,true,false,'public-key',false,'Operations database fixture'); +DO $fixture$ BEGIN + IF to_regclass('public.external_identities') IS NOT NULL THEN + INSERT INTO external_identities(issuer,subject,account_id,source,last_verified_at) VALUES ('https://identity.example.test/realms/operations','operations-subject','22222222-2222-4222-8222-222222222222','LINKED',now()); + END IF; +END; $fixture$; +DO $fixture$ BEGIN + IF to_regclass('public.spring_session') IS NOT NULL THEN + INSERT INTO spring_session(primary_id,session_id,creation_time,last_access_time,max_inactive_interval,expiry_time,principal_name) VALUES ('44444444-4444-4444-8444-444444444444','55555555-5555-4555-8555-555555555555',(extract(epoch FROM now())*1000)::bigint,(extract(epoch FROM now())*1000)::bigint,1800,(extract(epoch FROM now()+interval '30 minutes')*1000)::bigint,'opsadmin'); + INSERT INTO spring_session_attributes(session_primary_id,attribute_name,attribute_bytes) VALUES ('44444444-4444-4444-8444-444444444444','operations-database-fixture',decode('00','hex')); + INSERT INTO account_recovery_codes(id,account_id,token_hash,recovery_epoch,expires_at) SELECT '66666666-6666-4666-8666-666666666666',a.id,repeat('b',64),s.recovery_epoch,now()+interval '1 day' FROM accounts a CROSS JOIN system_state s WHERE username='opsadmin'; + INSERT INTO webauthn_ceremonies(id,session_id,purpose,account_id,security_epoch,recovery_epoch,challenge_hash,expires_at) SELECT '77777777-7777-4777-8777-777777777777','55555555-5555-4555-8555-555555555555','AUTHENTICATE',a.id,a.security_epoch,s.recovery_epoch,repeat('c',64),now()+interval '1 hour' FROM accounts a CROSS JOIN system_state s WHERE username='opsadmin'; + INSERT INTO login_throttle(bucket_key,window_started,attempts,expires_at) VALUES (repeat('d',64),now(),1,now()+interval '1 hour'); + END IF; +END; $fixture$; +DO $fixture$ BEGIN + IF to_regclass('public.dav_bindings') IS NOT NULL THEN + INSERT INTO dav_bindings(home_id,path,display_name) SELECT id,'recovery-folder','Recovery folder ü' FROM accounts WHERE username='opsadmin'; + INSERT INTO dav_binding_properties(home_id,path,namespace,name,xml) SELECT id,'recovery-folder','urn:busystack:operations','preserved','folder' FROM accounts WHERE username='opsadmin'; + INSERT INTO dav_collection_properties(collection_id,namespace,name,xml) VALUES ('11111111-1111-4111-8111-111111111111','urn:busystack:operations','preserved','collection'); + INSERT INTO dav_resource_properties(collection_id,resource_name,namespace,name,xml) VALUES ('11111111-1111-4111-8111-111111111111','fixture.ics','urn:busystack:operations','preserved','resource'); + END IF; +END; $fixture$; +DO $fixture$ BEGIN + IF to_regclass('public.sharing_groups') IS NOT NULL THEN + INSERT INTO sharing_groups(id,name,created_by) SELECT '88888888-8888-4888-8888-888888888888','Operations group',id FROM accounts WHERE username='opsadmin'; + INSERT INTO sharing_group_members(group_id,account_id) VALUES ('88888888-8888-4888-8888-888888888888','22222222-2222-4222-8222-222222222222'); + INSERT INTO collection_group_members(collection_id,group_id,permission) VALUES ('11111111-1111-4111-8111-111111111111','88888888-8888-4888-8888-888888888888','WRITE'); + INSERT INTO dav_principals(id,kind) VALUES ('99999999-9999-4999-8999-999999999999','GROUP'); + END IF; +END; $fixture$; +DO $fixture$ BEGIN + IF to_regclass('public.sync_continuations') IS NOT NULL THEN + INSERT INTO sync_continuations(id,collection_id,account_id,generation,recovery_epoch,lower_revision,upper_revision,retention_floor,initial_sync,last_name) +SELECT 'aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa',c.id,c.owner_id,c.generation,s.recovery_epoch,0,1,0,true,'' FROM collections c CROSS JOIN system_state s WHERE c.id='11111111-1111-4111-8111-111111111111'; + END IF; +END; $fixture$; +COMMIT; +""", + ) + epoch = sql(environment, "SELECT recovery_epoch FROM system_state WHERE id=1;") + initial_verifier = sql(environment, "SELECT password_hash FROM accounts WHERE username='opsadmin';") + has_federation = sql(environment, "SELECT to_regclass('public.external_identities') IS NOT NULL;") == "t" + has_groups = sql(environment, "SELECT to_regclass('public.sharing_groups') IS NOT NULL;") == "t" + has_bindings = sql(environment, "SELECT to_regclass('public.dav_bindings') IS NOT NULL;") == "t" + has_sessions = sql(environment, "SELECT to_regclass('public.spring_session') IS NOT NULL;") == "t" + account_generation = sql(environment, "SELECT security_epoch FROM accounts WHERE username='opsadmin';") if has_sessions else None + archive = private / "recovery.dump" + if initial_version is not None: + assert sql(environment, "SELECT max(version::integer) FROM flyway_schema_history WHERE version IS NOT NULL;") == "9" + assert sql(environment, "SELECT to_regclass('public.sync_continuations') IS NULL;") == "t" + execute(["./scripts/backup.sh", str(archive)], environment) + print("OPERATIONS_V9_ARCHIVE_CREATED_BEFORE_CURRENT_STARTUP", flush=True) + if baseline_image: + compose(environment, "up", "-d", "--no-build", "--wait", "--wait-timeout", "240", "application") + checks(environment) + baseline_data = sql(environment, "SELECT encode(content,'hex') || etag FROM calendar_resources ORDER BY collection_id,name;") + baseline_history = sql(environment, "SELECT json_agg(row_to_json(h) ORDER BY installed_rank) FROM flyway_schema_history h;") + compose(environment, "stop", "application") + override.write_text(override.read_text().replace(json.dumps(initial_image), json.dumps(args.image))) + print("OPERATIONS_REPLACING_INTERNAL_BASELINE=" + baseline_image + "; corrected=" + args.image, flush=True) + compose(environment, "up", "-d", "--no-build", "--wait", "--wait-timeout", "240", "application") + checks(environment) + if baseline_image: + actual = execute(["docker", "inspect", "--format", "{{.Image}}", compose(environment, "ps", "--quiet", "application")], environment) + assert actual == args.image + assert sql(environment, "SELECT encode(content,'hex') || etag FROM calendar_resources ORDER BY collection_id,name;") == baseline_data + assert sql(environment, "SELECT json_agg(row_to_json(h) ORDER BY installed_rank) FROM flyway_schema_history h;") == baseline_history + print("OPERATIONS_BASELINE_REPLACEMENT_PRESERVED_DATA_ETAGS_AND_MIGRATION_HISTORY", flush=True) + compose(environment, "restart", "application") + compose(environment, "up", "-d", "--no-build", "--wait", "--wait-timeout", "240", "application") + checks(environment) + if initial_version is None: + execute(["./scripts/backup.sh", str(archive)], environment) + execute(["./scripts/backup.sh", str(archive)], environment, expected=2) + compose(environment, "stop", "application") + execute(["./scripts/restore.sh", str(archive)], environment, expected=2) + + restored = environment.copy() + restored["COMPOSE_PROJECT_NAME"] += "-restore" + projects.append(restored) + compose(restored, "up", "-d", "--wait", "--wait-timeout", "120", "database") + execute(["./scripts/restore.sh", str(archive)], restored) + if initial_version is not None: + assert sql(restored, "SELECT max(version::integer) FROM flyway_schema_history WHERE version IS NOT NULL;") == "9" + assert sql(restored, "SELECT count(*) FROM accounts WHERE enabled;") == "0" + assert sql(restored, "SELECT (SELECT count(*) FROM dav_credentials)+(SELECT count(*) FROM spring_session)+(SELECT count(*) FROM user_credentials)+(SELECT count(*) FROM oauth2_authorization);") == "0" + # Migrate only after V9 recovery invalidation. All users stay disabled during startup. + compose(restored, "up", "-d", "--no-build", "--wait", "--wait-timeout", "240", "application") + checks(restored) + assert sql(restored, "SELECT count(*) FROM accounts WHERE enabled;") == "0" + assert sql(restored, "SELECT string_agg(version,',' ORDER BY installed_rank) FROM flyway_schema_history WHERE success AND version IS NOT NULL;") == "1,2,3,4,5,6,7,8,9,10,11,12,13" + compose(restored, "stop", "application") + print("OPERATIONS_V9_INVALIDATION_THEN_STARTUP_UPGRADE_TO_V13_PASSED", flush=True) + result = json.loads( + sql( + restored, + """SELECT json_build_object( +'accounts',(SELECT count(*) FROM accounts), +'enabled_accounts',(SELECT count(*) FROM accounts WHERE enabled), +'owner',(SELECT a.username FROM accounts a JOIN collections c ON c.owner_id=a.id), +'permission',(SELECT permission FROM collection_members), +'content_sha256',(SELECT encode(sha256(content),'hex') FROM calendar_resources), +'epoch',(SELECT recovery_epoch::text FROM system_state), +'credentials',(SELECT count(*) FROM dav_credentials), +'continuations',(SELECT count(*) FROM sync_continuations), +'versions',(SELECT count(*) FROM calendar_resource_versions), +'history_properties',(SELECT count(*) FROM calendar_resource_version_properties), +'registered_clients',(SELECT count(*) FROM oauth2_registered_client), +'grants',(SELECT count(*) FROM oauth2_authorization), +'consents',(SELECT count(*) FROM oauth2_authorization_consent), +'passkeys',(SELECT count(*) FROM user_credentials), +'active_families',(SELECT count(*) FROM oauth_refresh_families WHERE revoked_at IS NULL), +'history',(SELECT count(*) FROM collection_changes), +'tables_not_owned_by_app',(SELECT count(*) FROM pg_tables WHERE schemaname='public' AND tableowner <> 'busystack'));""", + ) + ) + assert result["accounts"] == 2 and result["owner"] == "opsadmin" + assert result["registered_clients"] == 1 + assert result["permission"] == "READ" and result["content_sha256"] == checksum + assert result["epoch"] != epoch and result["history"] == 1 + assert result["continuations"] == 0 and result["versions"] == 1 + assert result["history_properties"] == 1 + for key in ("enabled_accounts", "credentials", "grants", "consents", "passkeys", "active_families", "tables_not_owned_by_app"): + assert result[key] == 0, key + if has_federation: + mapping = sql(restored, "SELECT issuer || '|' || subject || '|' || account_id::text || '|' || (last_verified_at < now() - interval '24 hours')::text FROM external_identities;") + assert mapping == "https://identity.example.test/realms/operations|operations-subject|22222222-2222-4222-8222-222222222222|true" + if has_groups: + groups = json.loads(sql(restored, """SELECT json_build_object( +'name',(SELECT name FROM sharing_groups WHERE id='88888888-8888-4888-8888-888888888888'), +'creator',(SELECT a.username FROM sharing_groups g JOIN accounts a ON a.id=g.created_by WHERE g.id='88888888-8888-4888-8888-888888888888'), +'member',(SELECT a.username FROM sharing_group_members m JOIN accounts a ON a.id=m.account_id WHERE m.group_id='88888888-8888-4888-8888-888888888888'), +'permission',(SELECT permission FROM collection_group_members), +'effective_permission',(SELECT permission FROM collection_effective_members WHERE account_id='22222222-2222-4222-8222-222222222222'), +'retired_principal',(SELECT kind FROM dav_principals WHERE id='99999999-9999-4999-8999-999999999999'));""")) + assert groups == {'name': 'Operations group', 'creator': 'opsadmin', 'member': 'opsreader', 'permission': 'WRITE', 'effective_permission': 'WRITE', 'retired_principal': 'GROUP'} + if has_bindings: + binding_data = json.loads(sql(restored, """SELECT json_build_object( +'bindings',(SELECT count(*) FROM dav_bindings), +'folder',(SELECT display_name FROM dav_bindings WHERE path='recovery-folder'), +'owner_path',(SELECT path FROM dav_bindings b JOIN accounts a ON a.id=b.home_id WHERE a.username='opsadmin' AND b.collection_id='11111111-1111-4111-8111-111111111111'), +'folder_xml',(SELECT xml FROM dav_binding_properties WHERE namespace='urn:busystack:operations'), +'collection_xml',(SELECT xml FROM dav_collection_properties WHERE namespace='urn:busystack:operations'), +'resource_xml',(SELECT xml FROM dav_resource_properties WHERE namespace='urn:busystack:operations'));""")) + assert binding_data['bindings'] == 3 and binding_data['folder'] == 'Recovery folder ü' + assert binding_data['owner_path'] == '11111111-1111-4111-8111-111111111111' + for key, value in [('folder_xml', 'folder'), ('collection_xml', 'collection'), ('resource_xml', 'resource')]: + assert binding_data[key] == '' + value + '' + if has_sessions: + assert sql(restored, "SELECT (SELECT count(*) FROM spring_session)+(SELECT count(*) FROM spring_session_attributes)+(SELECT count(*) FROM account_recovery_codes)+(SELECT count(*) FROM webauthn_ceremonies)+(SELECT count(*) FROM login_throttle);") == "0" + assert sql(restored, "SELECT security_epoch FROM accounts WHERE username='opsadmin';") != account_generation + compose( + restored, + "run", "--rm", "--no-deps", "--user", f"{os.getuid()}:{os.getgid()}", + "--volume", f"{private / 'recovery-password'}:/recovery-password:ro", "application", + "--spring.main.web-application-type=none", "--busystack.recover-account=true", + "--busystack.recovery-username=opsadmin", "--busystack.recovery-password-file=/recovery-password", + ) + assert sql(restored, "SELECT count(*) FROM accounts WHERE enabled;") == "1" + assert sql(restored, "SELECT enabled FROM accounts WHERE username='opsadmin';") == "t" + assert sql(restored, "SELECT password_hash FROM accounts WHERE username='opsadmin';") != initial_verifier + if has_federation: + external_arguments = ["run", "--rm", "--no-deps", "application", + "--spring.main.web-application-type=none", "--busystack.recover-external-account=true", + "--busystack.recovery-username=opsreader", "--busystack.recovery-issuer=https://identity.example.test/realms/operations"] + execute(["docker", "compose", *external_arguments, "--busystack.recovery-subject=wrong-subject"], restored, expected=1) + assert sql(restored, "SELECT enabled FROM accounts WHERE username='opsreader';") == "f" + compose(restored, *external_arguments, "--busystack.recovery-subject=operations-subject") + assert sql(restored, "SELECT enabled AND password_hash IS NULL FROM accounts WHERE username='opsreader';") == "t" + assert sql(restored, "SELECT count(*) FROM accounts WHERE enabled;") == "2" + compose(restored, "up", "-d", "--no-build", "--wait", "--wait-timeout", "240", "application") + checks(restored) + if initial_version is None: + compose(environment, "stop", "application") + negative_restores(environment, private, projects) + print("PASS: packaged startup, non-root/read-only runtime, probes, configured discovery/issuer, DAV challenge, restart, database backup/restore, data/ownership/share preservation, restored credential/grant invalidation, controlled local/external account recovery (including rejected external subject mismatch).") + print("Not qualified here: interactive authentication, HTTP calendar/task content operations, HTTPS/proxy paths or independent clients.") + except Exception: + for created in reversed(projects): + try: + private_values = [ + file.read_text().strip() for file in private.iterdir() + if file.is_file() and "password" in file.name + ] + application = compose(created, "ps", "--all", "--quiet", "application") + if application: + state = json.loads(execute(["docker", "inspect", application], created))[0] + print("OPERATIONS_FAILURE_STATE=" + json.dumps(failure_state(state, private_values)), flush=True) + logs = compose(created, "logs", "--no-log-prefix", "--tail", "120", "application") + print("OPERATIONS_APPLICATION_DIAGNOSTICS=" + json.dumps(startup_diagnostics(logs, private_values)), flush=True) + except (RuntimeError, ValueError, KeyError, subprocess.TimeoutExpired): + print("OPERATIONS_FAILURE_DIAGNOSTICS_UNAVAILABLE", flush=True) + raise + finally: + for created in reversed(projects): + compose(created, "down", "--volumes", "--remove-orphans", "--timeout", "10") + + +def negative_restores(source, private, projects): + """Malformed histories are restored only into new owned DBs, never started as applications.""" + assert "application" not in compose(source, "ps", "--status", "running", "--services").splitlines() + original = sql(source, "SELECT json_agg(row_to_json(h) ORDER BY installed_rank) FROM flyway_schema_history h;") + escaped = original.replace("'", "''") + restore_history = "DELETE FROM flyway_schema_history; INSERT INTO flyway_schema_history SELECT * FROM json_populate_recordset(NULL::flyway_schema_history,'" + escaped + "'::json);" + appended = "INSERT INTO flyway_schema_history(installed_rank,version,description,type,script,checksum,installed_by,execution_time,success) SELECT max(installed_rank)+1,{version},'Deliberate negative fixture','SQL','negative.sql',0,'busystack',0,true FROM flyway_schema_history;" + cases = [ + ("future", appended.format(version="'14'"), "supports schema versions 1 through 13"), + ("failed", "UPDATE flyway_schema_history SET success=false WHERE version='13';", "Failed migration history"), + ("gap", "DELETE FROM flyway_schema_history WHERE version='6';", "Gap in migration history"), + ("duplicate", appended.format(version="'13'"), "Duplicate migration versions"), + ("missing-v1", "DELETE FROM flyway_schema_history WHERE version='1';", "Missing V1 migration history"), + ] + for label, mutation, message in cases: + archive = private / ("invalid-" + label + ".dump") + try: + sql(source, mutation) + execute(["./scripts/backup.sh", str(archive)], source) + finally: + sql(source, restore_history) + target = source.copy() + target["COMPOSE_PROJECT_NAME"] += "-negative-" + label + projects.append(target) + compose(target, "up", "-d", "--wait", "--wait-timeout", "120", "database") + result = subprocess.run(["./scripts/restore.sh", str(archive)], cwd=ROOT, env=target, + text=True, capture_output=True, timeout=300) + assert result.returncode != 0, label + " history incorrectly restored successfully" + assert message in result.stderr, (label, result.stderr[-1000:]) + assert "Restored database, disabled all accounts" not in result.stdout + assert "application" not in compose(target, "ps", "--status", "running", "--services").splitlines() + print("OPERATIONS_UNSUPPORTED_HISTORY_REJECTED=" + label + "; application remains stopped; no success message", flush=True) + compose(target, "down", "--volumes", "--remove-orphans", "--timeout", "10") + projects.remove(target) + assert sql(source, "SELECT json_agg(row_to_json(h) ORDER BY installed_rank) FROM flyway_schema_history h;") == original + + +if __name__ == "__main__": + main() diff --git a/src/main/java/org/busystack/caldav/BusyStackApplication.java b/src/main/java/org/busystack/caldav/BusyStackApplication.java new file mode 100644 index 0000000..da9bc80 --- /dev/null +++ b/src/main/java/org/busystack/caldav/BusyStackApplication.java @@ -0,0 +1,37 @@ +package org.busystack.caldav; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.properties.ConfigurationPropertiesScan; + +@SpringBootApplication +@ConfigurationPropertiesScan +public class BusyStackApplication { + public static void main(String[] args) { + var application = new SpringApplication(BusyStackApplication.class); + application.addListeners( + (org.springframework.context.ApplicationListener< + org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent>) + event -> { + var environment = event.getEnvironment(); + long operations = + java.util.stream.Stream.of( + "busystack.bootstrap", + "busystack.recover-account", + "busystack.recover-external-account") + .filter(name -> environment.getProperty(name, Boolean.class, false)) + .count(); + if (operations > 1 + || (operations == 1 + && !"none" + .equalsIgnoreCase( + environment.getProperty("spring.main.web-application-type")))) + throw new IllegalArgumentException( + "Choose one offline account operation and set spring.main.web-application-type=none"); + }); + var context = application.run(args); + if ("none" + .equalsIgnoreCase(context.getEnvironment().getProperty("spring.main.web-application-type"))) + context.close(); + } +} diff --git a/src/main/java/org/busystack/caldav/account/AccountService.java b/src/main/java/org/busystack/caldav/account/AccountService.java new file mode 100644 index 0000000..9e421d5 --- /dev/null +++ b/src/main/java/org/busystack/caldav/account/AccountService.java @@ -0,0 +1,263 @@ +package org.busystack.caldav.account; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.time.Instant; +import java.util.UUID; +import org.busystack.caldav.xml.XmlCharacters; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +public class AccountService { + public record Account( + UUID id, + String username, + String displayName, + String passwordHash, + boolean administrator, + boolean enabled, + UUID securityEpoch) { + @Override + public String toString() { + return "Account[id=" + id + ", username=" + username + "]"; + } + } + + public record IssuedCredential(UUID id, String username, String secret, Instant expiresAt) { + @Override + public String toString() { + return "IssuedCredential[id=" + id + ", secret=]"; + } + } + + private final JdbcTemplate jdbc; + private final PasswordEncoder passwords; + private final org.busystack.caldav.security.DavSecretVerifier davVerifiers; + private final org.busystack.caldav.security.passkey.AccountPasskeyCredentials passkeys; + private final org.busystack.caldav.security.oidc.ExternalAccessPolicy externalAccess; + private final org.busystack.caldav.security.session.ClientAccessRevocation clientAccess; + private final org.springframework.security.crypto.keygen.StringKeyGenerator davSecrets = + new org.springframework.security.crypto.keygen.Base64StringKeyGenerator( + java.util.Base64.getUrlEncoder().withoutPadding(), 32); + + public AccountService( + JdbcTemplate jdbc, + PasswordEncoder passwords, + org.busystack.caldav.security.passkey.AccountPasskeyCredentials passkeys, + org.busystack.caldav.security.oidc.ExternalAccessPolicy externalAccess, + org.busystack.caldav.security.session.ClientAccessRevocation clientAccess, + org.busystack.caldav.security.DavSecretVerifier davVerifiers) { + this.jdbc = jdbc; + this.passwords = passwords; + this.davVerifiers = davVerifiers; + this.passkeys = passkeys; + this.externalAccess = externalAccess; + this.clientAccess = clientAccess; + } + + public boolean isClientAccessCurrent(UUID accountId) { + return externalAccess.isCurrent(accountId); + } + + public boolean isExternallyManaged(UUID accountId) { + return externalAccess.isExternal(accountId); + } + + @Transactional + public UUID provision( + String username, String displayName, String password, boolean administrator) { + validatePassword(password); + if (username == null + || !username.matches("[a-zA-Z0-9][a-zA-Z0-9._-]{0,63}") + || displayName == null + || displayName.isBlank() + || displayName.codePointCount(0, displayName.length()) > 200 + || !XmlCharacters.valid(displayName) + || displayName.codePoints().anyMatch(Character::isISOControl)) + throw new IllegalArgumentException("Invalid account name or display name"); + UUID id = UUID.randomUUID(); + jdbc.update( + "INSERT INTO accounts(id,username,display_name,password_hash,administrator) VALUES (?,?,?,?,?)", + id, + username, + displayName, + passwords.encode(password), + administrator); + return id; + } + + private static void validatePassword(String password) { + org.busystack.caldav.security.password.PasswordPolicy.validate(password); + } + + /** Operator-only recovery entry point. No controller exposes this operation. */ + @Transactional + public void recoverOffline(String username, String replacementPassword) { + validatePassword(replacementPassword); + var ids = + jdbc.queryForList( + "SELECT id FROM accounts WHERE username=? FOR UPDATE", UUID.class, username); + if (ids.size() != 1) throw new IllegalArgumentException("Unknown recovery account"); + UUID id = ids.getFirst(); + if (isExternallyManaged(id)) + throw new IllegalArgumentException( + "Externally managed accounts require upstream identity recovery"); + jdbc.update( + "UPDATE accounts SET password_hash=?,enabled=true WHERE id=?", + passwords.encode(replacementPassword), + id); + clearClientAccess(id, username); + } + + /** Restores only an existing verified mapping; it never provisions a password fallback. */ + @Transactional + public void recoverExternalOffline(String username, String issuer, String subject) { + var ids = + jdbc.queryForList( + "SELECT id FROM accounts WHERE username=? FOR UPDATE", UUID.class, username); + if (ids.size() != 1) throw new IllegalArgumentException("Unknown recovery account"); + UUID id = ids.getFirst(); + if (!Boolean.TRUE.equals( + jdbc.queryForObject( + "SELECT EXISTS(SELECT 1 FROM external_identities WHERE account_id=? AND issuer=? AND subject=?)", + Boolean.class, + id, + issuer, + subject))) + throw new IllegalArgumentException( + "External recovery must match the existing issuer and subject"); + clearClientAccess(id, username); + jdbc.update( + "UPDATE external_identities SET last_verified_at='epoch'::timestamptz WHERE account_id=?", + id); + jdbc.update("UPDATE accounts SET password_hash=NULL,enabled=true WHERE id=?", id); + } + + private void clearClientAccess(UUID id, String username) { + clientAccess.revoke(id, username); + jdbc.update("DELETE FROM account_recovery_codes WHERE account_id=?", id); + jdbc.update("DELETE FROM dav_credentials WHERE account_id=?", id); + jdbc.update("DELETE FROM oauth_refresh_families WHERE account_id=?", id); + jdbc.update( + "DELETE FROM oauth2_authorization WHERE principal_name IN (?,?) OR attributes::jsonb ->> 'busystack.account_id'=?", + username, + id.toString(), + id.toString()); + jdbc.update( + "DELETE FROM oauth2_authorization_consent WHERE principal_name IN (?,?)", + username, + id.toString()); + var keys = + jdbc.queryForList( + "SELECT c.credential_id FROM user_credentials c JOIN user_entities u ON u.id=c.user_entity_user_id WHERE u.account_id=?", + String.class, + id); + keys.forEach( + key -> + passkeys.delete(org.springframework.security.web.webauthn.api.Bytes.fromBase64(key))); + jdbc.update("DELETE FROM user_entities WHERE account_id=?", id); + } + + public Account find(String username) { + return jdbc + .query("SELECT * FROM accounts WHERE username=?", AccountService::account, username) + .stream() + .findFirst() + .orElse(null); + } + + public Account find(UUID id) { + return jdbc.query("SELECT * FROM accounts WHERE id=?", AccountService::account, id).stream() + .findFirst() + .orElse(null); + } + + /** Internal operation; browser issuance must supply recent MFA before calling this service. */ + @Transactional + public IssuedCredential issueDavCredential(UUID accountId, String label, Instant expiresAt) { + Account account = find(accountId); + if (account == null || !account.enabled() || !isClientAccessCurrent(accountId)) + throw new IllegalArgumentException("Account unavailable"); + if (!expiresAt.isAfter(Instant.now()) + || expiresAt.isAfter(Instant.now().plusSeconds(366L * 86400))) + throw new IllegalArgumentException("Credential expiry must be within one year"); + UUID id = UUID.randomUUID(); + String value = davSecrets.generateKey(); + jdbc.update( + "INSERT INTO dav_credentials(id,account_id,label,verifier,expires_at) VALUES (?,?,?,?,?)", + id, + accountId, + label, + davVerifiers.encode(id, value), + java.sql.Timestamp.from(expiresAt)); + return new IssuedCredential(id, account.username(), id + "." + value, expiresAt); + } + + public boolean verifyDav(String username, String credential) { + if (credential == null || credential.length() != 80 || credential.charAt(36) != '.') + return false; + UUID id; + try { + id = UUID.fromString(credential.substring(0, 36)); + } catch (IllegalArgumentException ex) { + return false; + } + var verifiers = + jdbc.query( + "SELECT c.verifier,a.id FROM dav_credentials c JOIN accounts a ON a.id=c.account_id WHERE c.id=? AND a.username=? AND a.enabled AND c.revoked_at IS NULL AND c.expires_at > now()", + (rs, row) -> new DavVerifier(rs.getString(1), rs.getObject(2, UUID.class)), + id, + username); + if (verifiers.size() != 1) return false; + var stored = verifiers.getFirst(); + String secret = credential.substring(37); + if (!isClientAccessCurrent(stored.accountId()) + || !davVerifiers.matches(id, secret, stored.verifier())) return false; + if (davVerifiers.current(stored.verifier())) return true; + // Upgrade only a still-current credential; recovery/deletion/revocation can never revive it. + return jdbc.update( + "UPDATE dav_credentials c SET verifier=? WHERE c.id=? AND c.verifier=? AND c.revoked_at IS NULL AND c.expires_at>now() AND EXISTS(SELECT 1 FROM accounts a WHERE a.id=c.account_id AND a.enabled)", + davVerifiers.encode(id, secret), + id, + stored.verifier()) + == 1; + } + + private record DavVerifier(String verifier, UUID accountId) {} + + @Transactional + public void revokeDav(UUID actor, UUID credentialId) { + jdbc.update( + "UPDATE dav_credentials SET revoked_at=now() WHERE id=? AND account_id=?", + credentialId, + actor); + } + + @Transactional + public void disable(UUID id) { + jdbc.update("UPDATE accounts SET enabled=false WHERE id=?", id); + jdbc.update( + "UPDATE dav_credentials SET revoked_at=now() WHERE account_id=? AND revoked_at IS NULL", + id); + jdbc.update( + "UPDATE oauth_refresh_families SET revoked_at=now() WHERE account_id=? AND revoked_at IS NULL", + id); + var usernames = jdbc.queryForList("SELECT username FROM accounts WHERE id=?", String.class, id); + if (!usernames.isEmpty()) clientAccess.revoke(id, usernames.getFirst()); + jdbc.update("DELETE FROM account_recovery_codes WHERE account_id=?", id); + } + + private static Account account(ResultSet rs, int row) throws SQLException { + return new Account( + rs.getObject("id", UUID.class), + rs.getString("username"), + rs.getString("display_name"), + rs.getString("password_hash"), + rs.getBoolean("administrator"), + rs.getBoolean("enabled"), + rs.getObject("security_epoch", UUID.class)); + } +} diff --git a/src/main/java/org/busystack/caldav/account/BootstrapAdministrator.java b/src/main/java/org/busystack/caldav/account/BootstrapAdministrator.java new file mode 100644 index 0000000..82fff07 --- /dev/null +++ b/src/main/java/org/busystack/caldav/account/BootstrapAdministrator.java @@ -0,0 +1,48 @@ +package org.busystack.caldav.account; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.attribute.PosixFilePermission; +import java.util.Set; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.core.env.Environment; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; + +/** Offline operator-only command: no network setup endpoint and no generated default password. */ +@Component +@ConditionalOnProperty(name = "busystack.bootstrap", havingValue = "true") +public class BootstrapAdministrator implements ApplicationRunner { + private final AccountService accounts; + private final JdbcTemplate jdbc; + private final Environment environment; + + public BootstrapAdministrator( + AccountService accounts, JdbcTemplate jdbc, Environment environment) { + this.accounts = accounts; + this.jdbc = jdbc; + this.environment = environment; + } + + @Override + @Transactional + public void run(ApplicationArguments args) throws Exception { + if (!"none".equals(environment.getProperty("spring.main.web-application-type"))) + throw new IllegalArgumentException( + "Bootstrap requires --spring.main.web-application-type=none"); + jdbc.execute("LOCK TABLE accounts IN EXCLUSIVE MODE"); + if (jdbc.queryForObject("SELECT count(*) FROM accounts", Long.class) != 0) + throw new IllegalStateException("Bootstrap is permitted only on an empty account database"); + String username = environment.getRequiredProperty("busystack.bootstrap-username"); + Path file = Path.of(environment.getRequiredProperty("busystack.bootstrap-password-file")); + Set permissions = Files.getPosixFilePermissions(file); + if (permissions.stream() + .anyMatch(p -> p.name().startsWith("GROUP_") || p.name().startsWith("OTHERS_"))) + throw new IllegalArgumentException("Password file must be accessible only to its owner"); + String password = Files.readString(file).stripTrailing(); + accounts.provision(username, username, password, true); + } +} diff --git a/src/main/java/org/busystack/caldav/account/OfflineAccountRecovery.java b/src/main/java/org/busystack/caldav/account/OfflineAccountRecovery.java new file mode 100644 index 0000000..e263857 --- /dev/null +++ b/src/main/java/org/busystack/caldav/account/OfflineAccountRecovery.java @@ -0,0 +1,41 @@ +package org.busystack.caldav.account; + +import java.nio.file.Files; +import java.nio.file.LinkOption; +import java.nio.file.Path; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; + +/** Trusted local operator recovery; stop every application instance before invoking. */ +@Component +@ConditionalOnProperty(name = "busystack.recover-account", havingValue = "true") +public class OfflineAccountRecovery implements ApplicationRunner { + private final AccountService accounts; + private final Environment environment; + + public OfflineAccountRecovery(AccountService accounts, Environment environment) { + this.accounts = accounts; + this.environment = environment; + } + + @Override + public void run(ApplicationArguments args) throws Exception { + if (!"none".equals(environment.getProperty("spring.main.web-application-type")) + || environment.getProperty("busystack.bootstrap", Boolean.class, false)) + throw new IllegalArgumentException( + "Recovery requires a separate --spring.main.web-application-type=none operation"); + Path file = Path.of(environment.getRequiredProperty("busystack.recovery-password-file")); + if (!Files.isRegularFile(file, LinkOption.NOFOLLOW_LINKS) + || Files.size(file) > 1024 + || Files.getPosixFilePermissions(file).stream() + .anyMatch(p -> p.name().startsWith("GROUP_") || p.name().startsWith("OTHERS_"))) + throw new IllegalArgumentException( + "Recovery password file must be a small regular owner-only file"); + accounts.recoverOffline( + environment.getRequiredProperty("busystack.recovery-username"), + Files.readString(file).stripTrailing()); + } +} diff --git a/src/main/java/org/busystack/caldav/account/OfflineExternalRecovery.java b/src/main/java/org/busystack/caldav/account/OfflineExternalRecovery.java new file mode 100644 index 0000000..8c465cd --- /dev/null +++ b/src/main/java/org/busystack/caldav/account/OfflineExternalRecovery.java @@ -0,0 +1,32 @@ +package org.busystack.caldav.account; + +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; + +@Component +@ConditionalOnProperty(name = "busystack.recover-external-account", havingValue = "true") +public class OfflineExternalRecovery implements ApplicationRunner { + private final AccountService accounts; + private final Environment environment; + + public OfflineExternalRecovery(AccountService accounts, Environment environment) { + this.accounts = accounts; + this.environment = environment; + } + + @Override + public void run(ApplicationArguments args) { + if (!"none".equals(environment.getProperty("spring.main.web-application-type")) + || environment.getProperty("busystack.bootstrap", Boolean.class, false) + || environment.getProperty("busystack.recover-account", Boolean.class, false)) + throw new IllegalArgumentException( + "External recovery requires a separate --spring.main.web-application-type=none operation"); + accounts.recoverExternalOffline( + environment.getRequiredProperty("busystack.recovery-username"), + environment.getRequiredProperty("busystack.recovery-issuer"), + environment.getRequiredProperty("busystack.recovery-subject")); + } +} diff --git a/src/main/java/org/busystack/caldav/calendar/CalendarAdmission.java b/src/main/java/org/busystack/caldav/calendar/CalendarAdmission.java new file mode 100644 index 0000000..534c0ff --- /dev/null +++ b/src/main/java/org/busystack/caldav/calendar/CalendarAdmission.java @@ -0,0 +1,38 @@ +package org.busystack.caldav.calendar; + +import org.busystack.caldav.config.DavCompatibilityProperties; +import org.springframework.stereotype.Component; + +/** The sole input transformation policy; read/query parsing always remains strict. */ +@Component +public final class CalendarAdmission { + public record Result(byte[] content, CalendarContent.Parsed parsed, int repairedRules) { + public Result { + content = content.clone(); + } + + @Override + public byte[] content() { + return content.clone(); + } + + public boolean transformed() { + return repairedRules != 0; + } + } + + private final DavCompatibilityProperties compatibility; + + public CalendarAdmission(DavCompatibilityProperties compatibility) { + this.compatibility = compatibility; + } + + public Result admit(byte[] content, String component) { + var adapted = + compatibility.repairLocalObservanceUntil() + ? ObservanceUntilRepair.apply(content) + : new ObservanceUntilRepair.Result(content, 0); + return new Result( + adapted.content(), CalendarContent.parse(adapted.content(), component), adapted.rules()); + } +} diff --git a/src/main/java/org/busystack/caldav/calendar/CalendarConstraints.java b/src/main/java/org/busystack/caldav/calendar/CalendarConstraints.java new file mode 100644 index 0000000..8c98c64 --- /dev/null +++ b/src/main/java/org/busystack/caldav/calendar/CalendarConstraints.java @@ -0,0 +1,199 @@ +package org.busystack.caldav.calendar; + +import java.time.*; +import java.time.temporal.Temporal; +import java.util.*; +import net.fortuna.ical4j.model.*; +import net.fortuna.ical4j.model.property.*; +import net.fortuna.ical4j.transform.recurrence.Frequency; +import org.busystack.caldav.calendar.recurrence.TemporalSupport; + +/** + * Application write-boundary checks for RFC 5545 cross-property constraints. Parsing, typed values + * and their ordinary validators remain iCal4j responsibilities; no recurrence is expanded here. + */ +public final class CalendarConstraints { + private CalendarConstraints() {} + + /** Apply the same observance constraints to collection properties and request timezones. */ + public static void validateTimezone(net.fortuna.ical4j.model.component.VTimeZone timezone) { + validate(timezone, null, Map.of()); + } + + static void validate(net.fortuna.ical4j.model.Calendar calendar, Map lexical) { + for (Component component : calendar.getComponents()) validate(component, null, lexical); + } + + private static void validate( + Component component, Component parent, Map lexical) { + Temporal start = TemporalSupport.date(component, Property.DTSTART); + boolean event = component.getName().equals(Component.VEVENT); + boolean task = component.getName().equals(Component.VTODO); + if (event && start == null) invalid(); // METHOD is prohibited for stored calendar objects. + if (event || task) { + Temporal end = TemporalSupport.date(component, event ? Property.DTEND : Property.DUE); + Property duration = component.getProperty(Property.DURATION).orElse(null); + if (duration != null && (end != null || start == null)) invalid(); + if (end != null && start != null) { + sameType(start, end); + if (compare(start, end) >= 0) invalid(); + } + if (duration != null) { + String value = lexical.getOrDefault(duration, duration.getValue()); + positiveDuration(value); + if (start instanceof LocalDate && value.toUpperCase(Locale.ROOT).contains("T")) invalid(); + } + Temporal id = TemporalSupport.date(component, Property.RECURRENCE_ID); + if (id != null && start != null) sameType(start, id); + for (String name : List.of(Property.RDATE, Property.EXDATE)) + for (var property : component.getProperties(name)) { + if (property instanceof RDate date && date.getPeriods().isPresent()) { + if (start instanceof LocalDate) invalid(); + for (var period : date.getPeriods().get()) { + if (period.getEnd() != null && compare(period.getStart(), period.getEnd()) >= 0) + invalid(); + } + } else if (property instanceof DateListProperty dates && start != null) + for (Temporal date : dates.getDates()) sameType(start, date); + } + } + for (var property : component.getProperties()) { + String name = property.getName(); + if (Set.of(Property.COMPLETED, Property.PERCENT_COMPLETE).contains(name) && !task) invalid(); + if (Set.of(Property.ACTION, Property.TRIGGER, Property.REPEAT).contains(name) + && !component.getName().equals(Component.VALARM)) invalid(); + if (property instanceof Geo geo + && (geo.getLatitude().abs().compareTo(java.math.BigDecimal.valueOf(90)) > 0 + || geo.getLongitude().abs().compareTo(java.math.BigDecimal.valueOf(180)) > 0)) + invalid(); + if (property instanceof RRule rule) rule(rule, start, component.getName()); + if (property instanceof DateProperty date) { + Temporal value = date.getDate(); + if (Set.of(Property.DTSTAMP, Property.CREATED, Property.LAST_MODIFIED, Property.COMPLETED) + .contains(property.getName()) + && !utc(value)) invalid(); + if (property.getParameter(Parameter.TZID).isPresent() + && (value instanceof LocalDate || utc(value))) invalid(); + } + } + if (component.getName().equals(Component.VALARM)) alarm(component, parent, lexical); + if (component instanceof ComponentContainer children) + for (Component child : children.getComponents()) validate(child, component, lexical); + } + + private static void rule(RRule property, Temporal start, String component) { + var rule = property.getRecur(); + Temporal until = rule.getUntil(); + if (start != null && until != null) { + sameType(start, until); + if (component.equals("STANDARD") || component.equals("DAYLIGHT")) { + if (!utc(until)) invalid(); + } else if (start instanceof LocalDateTime) { + if (!(until instanceof LocalDateTime)) invalid(); + } else if (!(start instanceof LocalDate) && !utc(until)) invalid(); + } + Frequency frequency = rule.getFrequency(); + for (var day : rule.getDayList()) { + int ordinal = day.getOffset(); + if (Math.abs(ordinal) > 53 + || ordinal != 0 && frequency != Frequency.MONTHLY && frequency != Frequency.YEARLY + || ordinal != 0 && frequency == Frequency.YEARLY && !rule.getWeekNoList().isEmpty()) + invalid(); + } + if (!rule.getMonthDayList().isEmpty() && frequency == Frequency.WEEKLY + || !rule.getYearDayList().isEmpty() + && Set.of(Frequency.DAILY, Frequency.WEEKLY, Frequency.MONTHLY).contains(frequency) + || !rule.getWeekNoList().isEmpty() && frequency != Frequency.YEARLY) invalid(); + for (var values : + List.of( + rule.getMonthDayList(), + rule.getYearDayList(), + rule.getWeekNoList(), + rule.getSetPosList())) if (values.contains(0)) invalid(); + if (!rule.getSetPosList().isEmpty() + && rule.getDayList().isEmpty() + && rule.getMonthDayList().isEmpty() + && rule.getYearDayList().isEmpty() + && rule.getWeekNoList().isEmpty() + && rule.getMonthList().isEmpty() + && rule.getHourList().isEmpty() + && rule.getMinuteList().isEmpty() + && rule.getSecondList().isEmpty()) invalid(); + // DATE BYHOUR/BYMINUTE/BYSECOND are intentionally not rejected: §3.3.10 requires ignoring them. + } + + private static void alarm(Component alarm, Component parent, Map lexical) { + if (alarm.getProperties(Property.ACTION).size() != 1 + || alarm.getProperties(Property.TRIGGER).size() != 1) invalid(); + String action = alarm.getRequiredProperty(Property.ACTION).getValue().toUpperCase(Locale.ROOT); + if (!action.matches("[A-Z0-9-]+")) invalid(); + if (action.equals("DISPLAY") || action.equals("EMAIL")) + if (alarm.getProperties(Property.DESCRIPTION).size() != 1) invalid(); + if (action.equals("EMAIL") + && (alarm.getProperties(Property.SUMMARY).size() != 1 + || alarm.getProperties(Property.ATTENDEE).isEmpty())) invalid(); + if (action.equals("AUDIO") && alarm.getProperties(Property.ATTACH).size() > 1) invalid(); + Property duration = alarm.getProperty(Property.DURATION).orElse(null); + Property repeat = alarm.getProperty(Property.REPEAT).orElse(null); + if (alarm.getProperties(Property.DURATION).size() > 1 + || alarm.getProperties(Property.REPEAT).size() > 1) invalid(); + if ((duration == null) != (repeat == null)) invalid(); + if (duration != null) positiveDuration(lexical.getOrDefault(duration, duration.getValue())); + var trigger = alarm.getProperty(Property.TRIGGER).orElse(null); + if (!(trigger instanceof Trigger value)) + return; // Ordinary mandatory-property validation runs first. + if (value.getDate() != null) { + if (!utc(value.getDate()) || trigger.getParameter(Parameter.RELATED).isPresent()) invalid(); + } else if (parent != null) { + boolean end = + trigger + .getParameter(Parameter.RELATED) + .map(p -> p.getValue().equalsIgnoreCase("END")) + .orElse(false); + boolean start = parent.getProperty(Property.DTSTART).isPresent(); + if (!end && !start) invalid(); + if (end) { + boolean explicit = + parent + .getProperty( + parent.getName().equals(Component.VTODO) ? Property.DUE : Property.DTEND) + .isPresent(); + if (!explicit && !(start && parent.getProperty(Property.DURATION).isPresent())) invalid(); + } + } + } + + private static void positiveDuration(String value) { + var duration = TemporalSupport.duration(value); + if (duration.days() < 0 + || duration.clock().isNegative() + || duration.days() == 0 && duration.clock().isZero()) invalid(); + } + + private static void sameType(Temporal first, Temporal second) { + if ((first instanceof LocalDate) != (second instanceof LocalDate)) invalid(); + } + + /** Floating and absolute DATE-TIMEs cannot be ordered without collection timezone context. */ + private static int compare(Temporal first, Temporal second) { + if (first instanceof LocalDate a && second instanceof LocalDate b) return a.compareTo(b); + if (first instanceof LocalDateTime a && second instanceof LocalDateTime b) + return a.compareTo(b); + if (!(first instanceof LocalDateTime) && !(second instanceof LocalDateTime)) + return TemporalSupport.instant(first, ZoneOffset.UTC) + .compareTo(TemporalSupport.instant(second, ZoneOffset.UTC)); + return -1; + } + + private static boolean utc(Temporal value) { + return value instanceof Instant + || value instanceof OffsetDateTime offset && offset.getOffset().equals(ZoneOffset.UTC) + || value instanceof ZonedDateTime zoned + && zoned.getOffset().equals(ZoneOffset.UTC) + && zoned.getZone() instanceof ZoneOffset; + } + + private static void invalid() { + throw new CalendarValidationException(CalendarValidationException.Reason.INVALID_DATA); + } +} diff --git a/src/main/java/org/busystack/caldav/calendar/CalendarContent.java b/src/main/java/org/busystack/caldav/calendar/CalendarContent.java new file mode 100644 index 0000000..5001abe --- /dev/null +++ b/src/main/java/org/busystack/caldav/calendar/CalendarContent.java @@ -0,0 +1,147 @@ +package org.busystack.caldav.calendar; + +import java.io.IOException; +import java.util.HashSet; +import java.util.Set; +import java.util.concurrent.atomic.AtomicReference; +import net.fortuna.ical4j.data.ParserException; +import net.fortuna.ical4j.model.Calendar; +import net.fortuna.ical4j.model.Component; +import net.fortuna.ical4j.model.Property; +import org.busystack.caldav.calendar.query.BoundedTimeZoneRegistry; + +/** Strict model validation/indexing; admission supplies the authoritative representation octets. */ +public final class CalendarContent { + public record Parsed( + String uid, + Calendar calendar, + java.util.Map lexicalValues, + java.util.Map zoneCoverage) { + public Parsed { + lexicalValues = + java.util.Collections.unmodifiableMap(new java.util.IdentityHashMap<>(lexicalValues)); + zoneCoverage = java.util.Map.copyOf(zoneCoverage); + } + + public Parsed(String uid, Calendar calendar) { + this(uid, calendar, java.util.Map.of(), java.util.Map.of()); + } + + public Parsed(String uid, Calendar calendar, java.util.Map lexicalValues) { + this(uid, calendar, lexicalValues, java.util.Map.of()); + } + + public String lexicalValue(Property property) { + return lexicalValues.getOrDefault(property, property.getValue()); + } + } + + public static final int MAX_BYTES = 1048576; + + private CalendarContent() {} + + public static Parsed parse(byte[] bytes, String supportedComponent) { + if (bytes.length == 0 || bytes.length > MAX_BYTES) + throw new CalendarValidationException(CalendarValidationException.Reason.INVALID_DATA); + try { + String text = CalendarParsingInput.decode(bytes); + CalendarTextProfile.validate(text); + var calendar = new AtomicReference(); + var registry = new BoundedTimeZoneRegistry(); + var handler = + new org.busystack.caldav.calendar.parser.StrictContentHandler(calendar::set, registry); + var lexical = + new LexicalContentHandler( + new org.busystack.caldav.calendar.parser.ParsingLimitsHandler(handler)); + org.busystack.caldav.calendar.parser.ParameterInput.parse( + CalendarParsingInput.reader(text), lexical); + Calendar model = calendar.get(); + if (model == null) + throw new CalendarValidationException(CalendarValidationException.Reason.INVALID_DATA); + Set componentTypes = new HashSet<>(); + for (var component : model.getComponents()) + if (!Component.VTIMEZONE.equals(component.getName())) + componentTypes.add(component.getName()); + if (model.getProperty(Property.METHOD).isPresent() || componentTypes.size() != 1) + throw new CalendarValidationException(CalendarValidationException.Reason.INVALID_OBJECT); + validateTimezones(model); + if (model.validate().hasErrors()) + throw new CalendarValidationException(CalendarValidationException.Reason.INVALID_DATA); + CalendarConstraints.validate(model, lexical.values(model)); + String uid = null; + int masters = 0; + Set recurrenceIds = new HashSet<>(); + for (var component : model.getComponents()) { + if (Component.VTIMEZONE.equals(component.getName())) continue; + if (!component.getName().equals(supportedComponent)) + throw new CalendarValidationException( + CalendarValidationException.Reason.UNSUPPORTED_COMPONENT); + String current = + component + .getProperty(Property.UID) + .orElseThrow( + () -> + new CalendarValidationException( + CalendarValidationException.Reason.INVALID_DATA)) + .getValue(); + if (current.isBlank() || current.length() > 1024 || (uid != null && !uid.equals(current))) + throw new CalendarValidationException(CalendarValidationException.Reason.INVALID_OBJECT); + uid = current; + var recurrence = component.getProperty(Property.RECURRENCE_ID); + if (recurrence.isEmpty()) masters++; + else if (!recurrenceIds.add( + recurrenceKey((net.fortuna.ical4j.model.property.DateProperty) recurrence.get()))) + throw new CalendarValidationException(CalendarValidationException.Reason.INVALID_OBJECT); + } + if (uid == null || masters > 1) + throw new CalendarValidationException(CalendarValidationException.Reason.INVALID_OBJECT); + return new Parsed(uid, model, lexical.values(model), registry.coverage()); + } catch (CalendarValidationException ex) { + throw ex; + } catch (IllegalArgumentException ex) { + // Library and lexical validation messages may contain submitted values. + throw new CalendarValidationException(CalendarValidationException.Reason.INVALID_DATA); + } catch (IOException | ParserException ex) { + Throwable nested = ex; + for (int i = 0; i < 8 && nested != null; i++, nested = nested.getCause()) { + if (nested instanceof CalendarValidationException invalid) throw invalid; + if (nested instanceof org.busystack.caldav.calendar.query.QueryException query) throw query; + } + // Library exception messages can contain submitted data; never return or log them. + throw new CalendarValidationException(CalendarValidationException.Reason.INVALID_DATA); + } + } + + private static void validateTimezones(Calendar model) { + Set defined = new HashSet<>(); + for (var component : model.getComponents(Component.VTIMEZONE)) { + String id = component.getRequiredProperty(Property.TZID).getValue(); + if (!defined.add(id)) + throw new CalendarValidationException(CalendarValidationException.Reason.INVALID_OBJECT); + } + validateTimezoneProperties(model, defined); + for (var component : model.getComponents()) validateTimezoneTree(component, defined); + } + + private static void validateTimezoneTree(Component component, Set defined) { + validateTimezoneProperties(component, defined); + if (component instanceof net.fortuna.ical4j.model.ComponentContainer nested) + for (var child : nested.getComponents()) validateTimezoneTree(child, defined); + } + + private static void validateTimezoneProperties( + net.fortuna.ical4j.model.PropertyContainer owner, Set defined) { + for (var property : owner.getProperties()) + for (var parameter : property.getParameters(net.fortuna.ical4j.model.Parameter.TZID)) + if (!defined.contains(parameter.getValue())) + throw new CalendarValidationException(CalendarValidationException.Reason.INVALID_OBJECT); + } + + private static java.time.temporal.Temporal recurrenceKey( + net.fortuna.ical4j.model.property.DateProperty property) { + var value = property.getDate(); + if (value instanceof java.time.ZonedDateTime date) return date.toInstant(); + if (value instanceof java.time.OffsetDateTime date) return date.toInstant(); + return value; + } +} diff --git a/src/main/java/org/busystack/caldav/calendar/CalendarParsingInput.java b/src/main/java/org/busystack/caldav/calendar/CalendarParsingInput.java new file mode 100644 index 0000000..6e84b09 --- /dev/null +++ b/src/main/java/org/busystack/caldav/calendar/CalendarParsingInput.java @@ -0,0 +1,121 @@ +package org.busystack.caldav.calendar; + +import java.io.IOException; +import java.io.Reader; +import java.io.StringReader; +import java.nio.ByteBuffer; +import java.nio.charset.CharacterCodingException; +import java.nio.charset.CodingErrorAction; +import java.nio.charset.StandardCharsets; +import java.util.Locale; +import net.fortuna.ical4j.data.UnfoldingReader; + +/** + * Adapts only structural content-line identifiers to the parser's canonical spelling. RFC 5545 + * names are case insensitive; property values, parameters and authoritative octets are not changed. + * This is an input adapter, not a replacement calendar parser. + */ +public final class CalendarParsingInput { + private CalendarParsingInput() {} + + /** + * XML calendar-data needs characters rather than the authoritative octets. Keep ordinary source + * folding intact; recover a split UTF-8 code point by unfolding only when raw decoding fails. + */ + public static String reportText(byte[] original) { + try { + return strictUtf8(original, original.length); + } catch (CharacterCodingException foldedOrMalformed) { + try { + return decode(original); + } catch (CharacterCodingException malformed) { + throw new CalendarValidationException(CalendarValidationException.Reason.INVALID_DATA); + } + } + } + + /** Restore folded octets before decoding, including a fold inside a UTF-8 code point (§3.1). */ + static String decode(byte[] original) throws CharacterCodingException { + byte[] unfolded = new byte[original.length]; + int length = 0; + for (int index = 0; index < original.length; index++) { + if (original[index] == '\r' + && index + 2 < original.length + && original[index + 1] == '\n' + && (original[index + 2] == ' ' || original[index + 2] == '\t')) { + index += 2; + } else unfolded[length++] = original[index]; + } + return strictUtf8(unfolded, length); + } + + private static String strictUtf8(byte[] original, int length) throws CharacterCodingException { + return StandardCharsets.UTF_8 + .newDecoder() + .onMalformedInput(CodingErrorAction.REPORT) + .onUnmappableCharacter(CodingErrorAction.REPORT) + .decode(ByteBuffer.wrap(original, 0, length)) + .toString(); + } + + public static Reader reader(String text) throws IOException { + var unfolded = new StringBuilder(text.length()); + try (var input = new UnfoldingReader(new StringReader(text))) { + int character; + while ((character = input.read()) != -1) unfolded.append((char) character); + } + String value = unfolded.toString(); + // The parser's historical BOM helper accepts any token containing BEGIN. Check the + // actual boundary here and remove only a permitted leading BOM from the derived reader. + if (value.startsWith("\uFEFF")) value = value.substring(1); + int firstNewline = value.indexOf('\n'); + int firstEnd = firstNewline < 0 ? value.length() : firstNewline; + if (firstEnd > 0 && value.charAt(firstEnd - 1) == '\r') firstEnd--; + if (!asciiEqualsIgnoreCase(value.substring(0, firstEnd), "BEGIN:VCALENDAR")) + throw new CalendarValidationException(CalendarValidationException.Reason.INVALID_DATA); + StringBuilder normalized = new StringBuilder(value.length()); + int from = 0; + while (from < value.length()) { + int newline = value.indexOf('\n', from); + int end = newline < 0 ? value.length() : newline + 1; + int contentEnd = end; + if (contentEnd > from && value.charAt(contentEnd - 1) == '\n') contentEnd--; + if (contentEnd > from && value.charAt(contentEnd - 1) == '\r') contentEnd--; + String line = value.substring(from, contentEnd); + int colon = line.indexOf(':'); + if (colon > 0 + && (asciiEqualsIgnoreCase(line.substring(0, colon), "BEGIN") + || asciiEqualsIgnoreCase(line.substring(0, colon), "END")) + && identifier(line.substring(colon + 1))) { + normalized.append(line.toUpperCase(Locale.ROOT)); + } else normalized.append(line); + normalized.append(value, contentEnd, end); + from = end; + } + return new StringReader(normalized.toString()); + } + + private static boolean asciiEqualsIgnoreCase(String value, String expected) { + if (value.length() != expected.length()) return false; + for (int index = 0; index < value.length(); index++) { + char actual = value.charAt(index), required = expected.charAt(index); + if (actual > 0x7f || required > 0x7f) return false; + if (actual >= 'a' && actual <= 'z') actual -= 'a' - 'A'; + if (required >= 'a' && required <= 'z') required -= 'a' - 'A'; + if (actual != required) return false; + } + return true; + } + + private static boolean identifier(String value) { + return !value.isEmpty() + && value + .chars() + .allMatch( + c -> + c >= 'A' && c <= 'Z' + || c >= 'a' && c <= 'z' + || c >= '0' && c <= '9' + || c == '-'); + } +} diff --git a/src/main/java/org/busystack/caldav/calendar/CalendarTextProfile.java b/src/main/java/org/busystack/caldav/calendar/CalendarTextProfile.java new file mode 100644 index 0000000..f3f958e --- /dev/null +++ b/src/main/java/org/busystack/caldav/calendar/CalendarTextProfile.java @@ -0,0 +1,20 @@ +package org.busystack.caldav.calendar; + +/** Accepted calendar text must also be representable in a CalDAV XML calendar-data response. */ +public final class CalendarTextProfile { + private CalendarTextProfile() {} + + public static void validate(String text) { + text.codePoints() + .forEach( + value -> { + if (value < 0x20 && value != '\t' && value != '\r' && value != '\n' + || value == 0x7f + || value == 0xfffe + || value == 0xffff + || value >= 0xd800 && value <= 0xdfff) + throw new IllegalArgumentException( + "Calendar contains an unsupported control or XML character"); + }); + } +} diff --git a/src/main/java/org/busystack/caldav/calendar/CalendarTextValues.java b/src/main/java/org/busystack/caldav/calendar/CalendarTextValues.java new file mode 100644 index 0000000..5e41009 --- /dev/null +++ b/src/main/java/org/busystack/caldav/calendar/CalendarTextValues.java @@ -0,0 +1,50 @@ +package org.busystack.caldav.calendar; + +import java.util.ArrayList; +import java.util.List; + +/** Decodes a known list-TEXT property's original value once, retaining escaped list delimiters. */ +public final class CalendarTextValues { + private CalendarTextValues() {} + + /** Validate a known scalar TEXT value without decoding it or changing the authoritative bytes. */ + public static void scalar(String lexical) { + for (int index = 0; index < lexical.length(); index++) { + char value = lexical.charAt(index); + if (value == ',' || value == ';') invalid(); + if (value == '\\') { + if (++index == lexical.length()) invalid(); + char escaped = lexical.charAt(index); + if (escaped != 'n' && escaped != 'N' && escaped != '\\' && escaped != ',' && escaped != ';') + invalid(); + } + } + } + + public static List list(String lexical) { + List values = new ArrayList<>(); + StringBuilder current = new StringBuilder(); + for (int index = 0; index < lexical.length(); index++) { + char value = lexical.charAt(index); + if (value == ',') { + values.add(current.toString()); + current.setLength(0); + } else if (value == '\\') { + if (++index == lexical.length()) invalid(); + char escaped = lexical.charAt(index); + if (escaped == 'n' || escaped == 'N') current.append('\n'); + else if (escaped == '\\' || escaped == ',' || escaped == ';') current.append(escaped); + else invalid(); + } else { + if (value == ';') invalid(); + current.append(value); + } + } + values.add(current.toString()); + return List.copyOf(values); + } + + private static void invalid() { + throw new CalendarValidationException(CalendarValidationException.Reason.INVALID_DATA); + } +} diff --git a/src/main/java/org/busystack/caldav/calendar/CalendarValidationException.java b/src/main/java/org/busystack/caldav/calendar/CalendarValidationException.java new file mode 100644 index 0000000..df75340 --- /dev/null +++ b/src/main/java/org/busystack/caldav/calendar/CalendarValidationException.java @@ -0,0 +1,28 @@ +package org.busystack.caldav.calendar; + +/** CalDAV distinguishes malformed calendar data, resource shape and collection capabilities. */ +public final class CalendarValidationException extends IllegalArgumentException { + public enum Reason { + INVALID_DATA, + INVALID_OBJECT, + UNSUPPORTED_COMPONENT, + UNSUPPORTED_RSCALE + } + + private final Reason reason; + + public CalendarValidationException(Reason reason) { + super( + switch (java.util.Objects.requireNonNull(reason)) { + case INVALID_DATA -> "Calendar data is invalid"; + case INVALID_OBJECT -> "Calendar object resource restrictions were violated"; + case UNSUPPORTED_COMPONENT -> "Calendar component is unsupported by this collection"; + case UNSUPPORTED_RSCALE -> "Recurrence scale is unsupported by this server"; + }); + this.reason = reason; + } + + public Reason reason() { + return reason; + } +} diff --git a/src/main/java/org/busystack/caldav/calendar/DurationSyntax.java b/src/main/java/org/busystack/caldav/calendar/DurationSyntax.java new file mode 100644 index 0000000..e68bd64 --- /dev/null +++ b/src/main/java/org/busystack/caldav/calendar/DurationSyntax.java @@ -0,0 +1,32 @@ +package org.busystack.caldav.calendar; + +import java.util.regex.Pattern; + +/** RFC 5545 §3.3.6 lexical alternatives, checked before temporal model normalization. */ +public final class DurationSyntax { + private static final String TIME = + "T(?:[0-9]+H(?:[0-9]+M(?:[0-9]+S)?)?|[0-9]+M(?:[0-9]+S)?|[0-9]+S)"; + private static final Pattern DURATION = + Pattern.compile( + "[+-]?P(?:[0-9]+W|[0-9]+D(?:" + TIME + ")?|" + TIME + ")", Pattern.CASE_INSENSITIVE); + + private DurationSyntax() {} + + public static void validate(String value) { + if (!DURATION.matcher(value).matches()) invalid(); + } + + /** PERIOD may end with a date-time or a duration; date-time parsing remains delegated. */ + public static void validatePeriodDurations(String value) { + for (String period : value.split(",", -1)) { + int slash = period.indexOf('/'); + if (slash < 1 || slash != period.lastIndexOf('/') || slash == period.length() - 1) invalid(); + String end = period.substring(slash + 1); + if (end.charAt(0) < '0' || end.charAt(0) > '9') validate(end); + } + } + + private static void invalid() { + throw new CalendarValidationException(CalendarValidationException.Reason.INVALID_DATA); + } +} diff --git a/src/main/java/org/busystack/caldav/calendar/LexicalContentHandler.java b/src/main/java/org/busystack/caldav/calendar/LexicalContentHandler.java new file mode 100644 index 0000000..f4531e1 --- /dev/null +++ b/src/main/java/org/busystack/caldav/calendar/LexicalContentHandler.java @@ -0,0 +1,119 @@ +package org.busystack.caldav.calendar; + +import java.io.IOException; +import java.util.*; +import net.fortuna.ical4j.data.ContentHandler; +import net.fortuna.ical4j.model.*; + +/** Retains typed-value spelling that the library's temporal model normalizes away. */ +final class LexicalContentHandler + implements org.busystack.caldav.calendar.parser.ParameterListHandler { + private static final Set RETAIN = + Set.of( + Property.DURATION, + Property.RDATE, + Property.TRIGGER, + Property.CATEGORIES, + Property.RESOURCES); + private final ContentHandler delegate; + private final List frames = new ArrayList<>(); + private final Deque stack = new ArrayDeque<>(); + private String currentName; + + private static final class Frame { + int nextProperty; + final Map values = new HashMap<>(); + } + + LexicalContentHandler(ContentHandler delegate) { + this.delegate = delegate; + } + + private void enter() { + Frame frame = new Frame(); + frames.add(frame); + stack.push(frame); + } + + @Override + public void startCalendar() { + enter(); + delegate.startCalendar(); + } + + @Override + public void endCalendar() throws IOException { + delegate.endCalendar(); + stack.pop(); + } + + @Override + public void startComponent(String name) { + enter(); + delegate.startComponent(name); + } + + @Override + public void endComponent(String name) { + delegate.endComponent(name); + stack.pop(); + } + + @Override + public void startProperty(String name) { + currentName = name.toUpperCase(java.util.Locale.ROOT); + delegate.startProperty(name); + } + + @Override + public void endProperty(String name) { + delegate.endProperty(name); + stack.peek().nextProperty++; + } + + @Override + public void propertyValue(String value) { + if (currentName.equals(Property.CATEGORIES) || currentName.equals(Property.RESOURCES)) + CalendarTextValues.list(value); + if (RETAIN.contains(currentName)) stack.peek().values.put(stack.peek().nextProperty, value); + delegate.propertyValue(value); + } + + @Override + public void parameter(String name, String value) { + delegate.parameter(name, value); + } + + @Override + public void parameterValues(String name, List values) { + if (delegate instanceof org.busystack.caldav.calendar.parser.ParameterListHandler structured) + structured.parameterValues(name, values); + else delegate.parameter(name, String.join(",", values)); + } + + Map values(net.fortuna.ical4j.model.Calendar calendar) { + var result = new IdentityHashMap(); + int[] next = {0}; + collect(calendar, calendar.getComponents(), next, result); + if (next[0] != frames.size()) + throw new IllegalStateException("Calendar property alignment failed"); + return Collections.unmodifiableMap(result); + } + + private void collect( + PropertyContainer container, + List children, + int[] next, + IdentityHashMap result) { + Frame frame = frames.get(next[0]++); + var properties = container.getProperties(); + if (properties.size() != frame.nextProperty) + throw new IllegalStateException("Calendar property alignment failed"); + frame.values.forEach((index, value) -> result.put(properties.get(index), value)); + for (var child : children) { + List nested = + child instanceof ComponentContainer cc ? cc.getComponents() : List.of(); + collect(child, nested, next, result); + } + } +} diff --git a/src/main/java/org/busystack/caldav/calendar/ObservanceUntilRepair.java b/src/main/java/org/busystack/caldav/calendar/ObservanceUntilRepair.java new file mode 100644 index 0000000..26ac752 --- /dev/null +++ b/src/main/java/org/busystack/caldav/calendar/ObservanceUntilRepair.java @@ -0,0 +1,202 @@ +package org.busystack.caldav.calendar; + +import java.io.ByteArrayOutputStream; +import java.nio.charset.StandardCharsets; +import java.time.DateTimeException; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeFormatterBuilder; +import java.time.format.ResolverStyle; +import java.util.*; + +/** + * Lossless, bounded content-line adapter for local observance UNTILs. This is not a calendar + * validator: the complete resulting representation must still pass CalendarContent.parse(). + */ +final class ObservanceUntilRepair { + record Result(byte[] content, int rules) {} + + private record Line(int start, int end, String text, String ending) {} + + private static final DateTimeFormatter DATE = + new DateTimeFormatterBuilder() + .parseCaseInsensitive() + .appendPattern("uuuuMMdd'T'HHmmss") + .toFormatter(Locale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); + + private static final class Frame { + final String name; + final boolean observance; + final Map> properties = new HashMap<>(); + + Frame(String name, boolean observance) { + this.name = name; + this.observance = observance; + } + } + + private ObservanceUntilRepair() {} + + static Result apply(byte[] input) { + if (input.length == 0 || input.length > CalendarContent.MAX_BYTES) invalid(); + List lines = lines(input); + Deque stack = new ArrayDeque<>(); + Map replacements = new HashMap<>(); + int components = 0, properties = 0; + for (Line line : lines) { + String text = line.text(); + if (line.start() == 0 && text.startsWith("\u00ef\u00bb\u00bf")) text = text.substring(3); + int colon = colon(text); + if (colon < 1) invalid(); + String head = text.substring(0, colon); + String name = head.split(";", 2)[0].toUpperCase(Locale.ROOT); + String value = text.substring(colon + 1); + if (name.equals("BEGIN")) { + if (!head.equalsIgnoreCase("BEGIN") || !value.matches("[A-Za-z0-9-]+")) invalid(); + String type = value.toUpperCase(Locale.ROOT); + if (stack.isEmpty() && (!type.equals("VCALENDAR") || line.start() != 0)) invalid(); + boolean observance = + Set.of("STANDARD", "DAYLIGHT").contains(type) + && stack.size() == 2 + && stack.peek().name.equals("VTIMEZONE") + && stack.getLast().name.equals("VCALENDAR"); + if (stack.size() >= 9 || ++components > 2001) invalid(); + stack.push(new Frame(type, observance)); + } else if (name.equals("END")) { + if (!head.equalsIgnoreCase("END") + || stack.isEmpty() + || !stack.peek().name.equalsIgnoreCase(value)) invalid(); + Frame frame = stack.pop(); + if (frame.observance) repair(frame, replacements); + } else { + if (stack.isEmpty() || ++properties > 20000) invalid(); + if (stack.peek().observance) + stack.peek().properties.computeIfAbsent(name, ignored -> new ArrayList<>()).add(line); + } + } + if (!stack.isEmpty()) invalid(); + if (replacements.isEmpty()) return new Result(input, 0); + var output = new ByteArrayOutputStream(input.length); + for (Line line : lines) { + String replacement = replacements.get(line); + if (replacement == null) output.write(input, line.start(), line.end() - line.start()); + else { + // RRULE content is ASCII; ISO-8859-1 also preserves opaque parameter octets exactly. + byte[] bytes = replacement.getBytes(StandardCharsets.ISO_8859_1); + int from = 0, width = 75; + while (bytes.length - from > width) { + output.write(bytes, from, width); + output.writeBytes(new byte[] {'\r', '\n', ' '}); + from += width; + width = 74; + } + output.write(bytes, from, bytes.length - from); + output.writeBytes(line.ending().getBytes(StandardCharsets.US_ASCII)); + } + if (output.size() > CalendarContent.MAX_BYTES) invalid(); + } + return new Result(output.toByteArray(), replacements.size()); + } + + private static void repair(Frame frame, Map replacements) { + for (Line line : frame.properties.getOrDefault("RRULE", List.of())) { + int colon = colon(line.text()); + String rule = line.text().substring(colon + 1); + if (!rule.chars().allMatch(c -> c < 128)) invalid(); + RecurrenceRuleSyntax.validate(rule); // Before any typed model can erase duplicate parts. + int position = 0; + for (String part : rule.split(";", -1)) { + int equals = part.indexOf('='); + String value = part.substring(equals + 1); + if (part.substring(0, equals).equalsIgnoreCase("UNTIL") + && !value.toUpperCase(Locale.ROOT).endsWith("Z")) { + // Required singleton properties must be unambiguous even before strict validation. + for (String required : List.of("DTSTART", "TZOFFSETFROM", "TZOFFSETTO")) + if (frame.properties.getOrDefault(required, List.of()).size() != 1) invalid(); + String start = value(frame.properties.get("DTSTART").getFirst()); + local(start); + long seconds = offset(value(frame.properties.get("TZOFFSETFROM").getFirst())); + offset(value(frame.properties.get("TZOFFSETTO").getFirst())); + LocalDateTime utc; + try { + utc = local(value).minusSeconds(seconds); + } catch (DateTimeException | ArithmeticException ex) { + throw invalid(); + } + if (utc.getYear() < 1 || utc.getYear() > 9999) invalid(); + int begin = colon + 1 + position + equals + 1; + replacements.put( + line, + line.text().substring(0, begin) + + DATE.format(utc) + + "Z" + + line.text().substring(begin + value.length())); + } + position += part.length() + 1; + } + } + } + + private static String value(Line line) { + return line.text().substring(colon(line.text()) + 1); + } + + private static LocalDateTime local(String value) { + if (!value.matches("[0-9]{8}[Tt][0-9]{6}")) throw invalid(); + try { + LocalDateTime result = LocalDateTime.parse(value, DATE); + if (result.getYear() == 0) throw invalid(); + return result; + } catch (DateTimeException ex) { + throw invalid(); + } + } + + private static long offset(String value) { + if (!value.matches("[+-][0-9]{4}(?:[0-9]{2})?")) throw invalid(); + int hours = Integer.parseInt(value.substring(1, 3)); + int minutes = Integer.parseInt(value.substring(3, 5)); + int seconds = value.length() == 7 ? Integer.parseInt(value.substring(5, 7)) : 0; + if (hours > 23 || minutes > 59 || seconds > 59 || value.matches("-0{4}(?:00)?")) + throw invalid(); + long total = hours * 3600L + minutes * 60L + seconds; + return value.charAt(0) == '-' ? -total : total; + } + + private static int colon(String text) { + boolean quoted = false; + for (int i = 0; i < text.length(); i++) { + if (text.charAt(i) == '"') quoted = !quoted; + else if (text.charAt(i) == ':' && !quoted) return i; + } + return -1; + } + + private static List lines(byte[] input) { + List result = new ArrayList<>(); + for (int start = 0; start < input.length; ) { + int end = start; + var text = new ByteArrayOutputStream(); + String ending; + do { + int from = end == start ? end : end + 1; // Drop exactly one folding whitespace octet. + end = from; + while (end < input.length && input[end] != '\n') end++; + int contentEnd = end; + if (contentEnd > from && input[contentEnd - 1] == '\r') contentEnd--; + text.write(input, from, contentEnd - from); + ending = end == input.length ? "" : contentEnd == end ? "\n" : "\r\n"; + if (end < input.length) end++; + } while (end < input.length && (input[end] == ' ' || input[end] == '\t')); + result.add(new Line(start, end, text.toString(StandardCharsets.ISO_8859_1), ending)); + if (result.size() > 24002) invalid(); + start = end; + } + return result; + } + + private static CalendarValidationException invalid() { + throw new CalendarValidationException(CalendarValidationException.Reason.INVALID_DATA); + } +} diff --git a/src/main/java/org/busystack/caldav/calendar/RecurrenceRuleSyntax.java b/src/main/java/org/busystack/caldav/calendar/RecurrenceRuleSyntax.java new file mode 100644 index 0000000..41493c2 --- /dev/null +++ b/src/main/java/org/busystack/caldav/calendar/RecurrenceRuleSyntax.java @@ -0,0 +1,47 @@ +package org.busystack.caldav.calendar; + +import java.util.HashSet; +import java.util.Locale; +import java.util.regex.Pattern; + +/** Checks RECUR rule-part structure before the library's typed model replaces duplicate keys. */ +public final class RecurrenceRuleSyntax { + private static final Pattern WEEKDAY = + Pattern.compile("([+-]?[0-9]{1,2})?(?:SU|MO|TU|WE|TH|FR|SA)", Pattern.CASE_INSENSITIVE); + + private RecurrenceRuleSyntax() {} + + public static void validate(String value) { + var parts = new HashSet(); + for (String item : value.split(";", -1)) { + int equals = item.indexOf('='); + if (equals < 1 || equals == item.length() - 1 || item.indexOf('=', equals + 1) >= 0) + invalid(); + String key = item.substring(0, equals).toUpperCase(Locale.ROOT); + if (!parts.add(key)) invalid(); + if (key.equals("RSCALE")) + throw new CalendarValidationException( + CalendarValidationException.Reason.UNSUPPORTED_RSCALE); + if (key.equals("COUNT") || key.equals("INTERVAL")) { + String number = item.substring(equals + 1); + if (!number.chars().allMatch(c -> c >= '0' && c <= '9') + || number.chars().allMatch(c -> c == '0')) invalid(); + } + if (key.equals("BYDAY")) + for (String day : item.substring(equals + 1).split(",", -1)) { + var weekday = WEEKDAY.matcher(day); + if (!weekday.matches()) invalid(); + String ordinal = weekday.group(1); + if (ordinal != null) { + int offset = Integer.parseInt(ordinal); + if (offset == 0 || Math.abs(offset) > 53) invalid(); + } + } + } + if (!parts.contains("FREQ") || parts.contains("COUNT") && parts.contains("UNTIL")) invalid(); + } + + private static void invalid() { + throw new CalendarValidationException(CalendarValidationException.Reason.INVALID_DATA); + } +} diff --git a/src/main/java/org/busystack/caldav/calendar/parser/ClassificationFactory.java b/src/main/java/org/busystack/caldav/calendar/parser/ClassificationFactory.java new file mode 100644 index 0000000..fb6b40e --- /dev/null +++ b/src/main/java/org/busystack/caldav/calendar/parser/ClassificationFactory.java @@ -0,0 +1,33 @@ +package org.busystack.caldav.calendar.parser; + +import net.fortuna.ical4j.model.ParameterList; +import net.fortuna.ical4j.model.PropertyFactory; +import net.fortuna.ical4j.model.property.Clazz; +import net.fortuna.ical4j.validate.ValidationResult; + +/** Application property-factory extension for RFC 5545 §3.8.1.3's open token vocabulary. */ +final class ClassificationFactory extends Clazz.Factory { + @Override + public Clazz createProperty(ParameterList parameters, String value) { + return new Classification(parameters, value); + } + + private static final class Classification extends Clazz { + Classification(ParameterList parameters, String value) { + super(parameters, value); + } + + @Override + public ValidationResult validate() { + // The grammar includes iana-token and x-name, rather than only the three familiar values. + if (getValue() == null || !getValue().matches("[A-Za-z0-9-]+")) + throw new IllegalArgumentException("Invalid CLASS token"); + return ValidationResult.EMPTY; + } + + @Override + protected PropertyFactory newFactory() { + return new ClassificationFactory(); + } + } +} diff --git a/src/main/java/org/busystack/caldav/calendar/parser/ParameterInput.java b/src/main/java/org/busystack/caldav/calendar/parser/ParameterInput.java new file mode 100644 index 0000000..09782d0 --- /dev/null +++ b/src/main/java/org/busystack/caldav/calendar/parser/ParameterInput.java @@ -0,0 +1,139 @@ +package org.busystack.caldav.calendar.parser; + +import java.io.IOException; +import java.io.Reader; +import java.io.StringReader; +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.List; +import net.fortuna.ical4j.data.CalendarParserImpl; +import net.fortuna.ical4j.data.ContentHandler; +import net.fortuna.ical4j.data.ParserException; + +/** + * Bridges RFC 5545 parameter lists to the official iCal4j 4.4.0 scalar callback. Only parameter + * headers are adapted; iCal4j parses components, properties and values. This application-owned + * adapter never changes the authoritative stored representation. + */ +public final class ParameterInput implements ContentHandler { + private record Parameter(String name, List values) {} + + private final ContentHandler delegate; + private final ArrayDeque parameters = new ArrayDeque<>(); + + private ParameterInput(ContentHandler delegate) { + this.delegate = delegate; + } + + public static void parse(Reader input, ContentHandler handler) + throws IOException, ParserException { + var adapter = new ParameterInput(handler); + var writer = new java.io.StringWriter(); + input.transferTo(writer); + String source = writer.toString(); + var derived = new StringBuilder(source.length()); + for (String line : source.toString().split("(?<=\\n)", -1)) { + adapter.header(line, derived); + } + new CalendarParserImpl().parse(new StringReader(derived.toString()), adapter); + if (!adapter.parameters.isEmpty()) throw invalid(); + } + + private void header(String line, StringBuilder output) { + int cursor = 0; + while (cursor < line.length() && line.charAt(cursor) != ';' && line.charAt(cursor) != ':') { + output.append(line.charAt(cursor++)); + } + while (cursor < line.length() && line.charAt(cursor) == ';') { + int start = ++cursor; + while (cursor < line.length() && token(line.charAt(cursor))) cursor++; + if (cursor == start || cursor >= line.length() || line.charAt(cursor) != '=') throw invalid(); + String name = line.substring(start, cursor++); + var values = new ArrayList(); + do { + start = cursor; + if (cursor < line.length() && line.charAt(cursor) == '"') { + cursor++; + while (cursor < line.length() && line.charAt(cursor) != '"') { + if (line.charAt(cursor) == '\r' || line.charAt(cursor) == '\n') throw invalid(); + cursor++; + } + if (cursor >= line.length()) throw invalid(); + cursor++; + } else { + while (cursor < line.length() && ",;:".indexOf(line.charAt(cursor)) < 0) { + if (line.charAt(cursor) == '"' + || line.charAt(cursor) == '\r' + || line.charAt(cursor) == '\n') throw invalid(); + cursor++; + } + } + values.add(line.substring(start, cursor)); + if (values.size() > 1000 || cursor >= line.length()) throw invalid(); + char delimiter = line.charAt(cursor); + if (delimiter == ';' || delimiter == ':') break; + if (delimiter != ',') throw invalid(); + cursor++; + } while (true); + parameters.add(new Parameter(name, List.copyOf(values))); + output.append(';').append(name).append("=BUSYSTACK"); + } + output.append(line, cursor, line.length()); + } + + private static boolean token(char value) { + return value >= 'a' && value <= 'z' + || value >= 'A' && value <= 'Z' + || value >= '0' && value <= '9' + || value == '-'; + } + + private static IllegalArgumentException invalid() { + return new IllegalArgumentException("Malformed calendar parameter header"); + } + + @Override + public void parameter(String name, String value) { + var original = parameters.poll(); + if (original == null || !original.name().equals(name) || !value.equals("BUSYSTACK")) + throw invalid(); + if (delegate instanceof ParameterListHandler lists) + lists.parameterValues(name, original.values()); + else delegate.parameter(name, String.join(",", original.values())); + } + + @Override + public void startCalendar() { + delegate.startCalendar(); + } + + @Override + public void endCalendar() throws IOException { + delegate.endCalendar(); + } + + @Override + public void startComponent(String name) { + delegate.startComponent(name); + } + + @Override + public void endComponent(String name) { + delegate.endComponent(name); + } + + @Override + public void startProperty(String name) { + delegate.startProperty(name); + } + + @Override + public void endProperty(String name) { + delegate.endProperty(name); + } + + @Override + public void propertyValue(String value) { + delegate.propertyValue(value); + } +} diff --git a/src/main/java/org/busystack/caldav/calendar/parser/ParameterListHandler.java b/src/main/java/org/busystack/caldav/calendar/parser/ParameterListHandler.java new file mode 100644 index 0000000..eb10c55 --- /dev/null +++ b/src/main/java/org/busystack/caldav/calendar/parser/ParameterListHandler.java @@ -0,0 +1,9 @@ +package org.busystack.caldav.calendar.parser; + +import java.util.List; +import net.fortuna.ical4j.data.ContentHandler; + +/** Application callback retaining parameter-list boundaries around the official parser. */ +public interface ParameterListHandler extends ContentHandler { + void parameterValues(String name, List rawValues); +} diff --git a/src/main/java/org/busystack/caldav/calendar/parser/ParsingLimitsHandler.java b/src/main/java/org/busystack/caldav/calendar/parser/ParsingLimitsHandler.java new file mode 100644 index 0000000..c53c3da --- /dev/null +++ b/src/main/java/org/busystack/caldav/calendar/parser/ParsingLimitsHandler.java @@ -0,0 +1,72 @@ +package org.busystack.caldav.calendar.parser; + +import java.io.IOException; +import java.util.List; +import net.fortuna.ical4j.data.ContentHandler; + +/** Bounds the established parser's callbacks, before model or timezone construction. */ +public final class ParsingLimitsHandler implements ParameterListHandler { + private final ContentHandler delegate; + private int calendars, depth, components, properties, parameters; + + public ParsingLimitsHandler(ContentHandler delegate) { + this.delegate = delegate; + } + + @Override + public void startCalendar() { + if (++calendars > 1) fail(); + delegate.startCalendar(); + } + + @Override + public void endCalendar() throws IOException { + delegate.endCalendar(); + } + + @Override + public void startComponent(String name) { + if (++depth > 8 || ++components > 2000) fail(); + delegate.startComponent(name); + } + + @Override + public void endComponent(String name) { + depth--; + delegate.endComponent(name); + } + + @Override + public void startProperty(String name) { + if (++properties > 20000) fail(); + delegate.startProperty(name); + } + + @Override + public void endProperty(String name) { + delegate.endProperty(name); + } + + @Override + public void propertyValue(String value) { + delegate.propertyValue(value); + } + + @Override + public void parameter(String name, String value) { + parameterValues(name, List.of(value)); + } + + @Override + public void parameterValues(String name, List values) { + parameters += values.size(); + if (parameters > 40000) fail(); + if (delegate instanceof ParameterListHandler structured) + structured.parameterValues(name, values); + else delegate.parameter(name, String.join(",", values)); + } + + private static void fail() { + throw new IllegalArgumentException("Calendar complexity exceeds supported limits"); + } +} diff --git a/src/main/java/org/busystack/caldav/calendar/parser/Rfc6868.java b/src/main/java/org/busystack/caldav/calendar/parser/Rfc6868.java new file mode 100644 index 0000000..14f961c --- /dev/null +++ b/src/main/java/org/busystack/caldav/calendar/parser/Rfc6868.java @@ -0,0 +1,27 @@ +package org.busystack.caldav.calendar.parser; + +/** Sequential parameter escape decoding; content-line tokenization remains in iCal4j. */ +public final class Rfc6868 { + private Rfc6868() {} + + public static String decode(String value) { + var decoded = new StringBuilder(value.length()); + for (int index = 0; index < value.length(); index++) { + char c = value.charAt(index); + if (c == '^' && index + 1 < value.length()) { + char next = value.charAt(index + 1); + if (next == '^' || next == 'n' || next == '\'') { + decoded.append(next == 'n' ? '\n' : next == '\'' ? '"' : '^'); + index++; + continue; + } + } + decoded.append(c); + } + return decoded.toString(); + } + + public static String value(String raw) { + return decode(net.fortuna.ical4j.util.Strings.unquote(raw)); + } +} diff --git a/src/main/java/org/busystack/caldav/calendar/parser/StrictContentHandler.java b/src/main/java/org/busystack/caldav/calendar/parser/StrictContentHandler.java new file mode 100644 index 0000000..4035055 --- /dev/null +++ b/src/main/java/org/busystack/caldav/calendar/parser/StrictContentHandler.java @@ -0,0 +1,245 @@ +package org.busystack.caldav.calendar.parser; + +import java.net.URI; +import java.util.*; +import java.util.function.Consumer; +import net.fortuna.ical4j.data.*; +import net.fortuna.ical4j.model.*; +import net.fortuna.ical4j.model.parameter.*; +import org.busystack.caldav.calendar.query.QueryException; + +/** Uses iCal4j factories while retaining the parsed boundaries and RFC6868 literal characters. */ +public final class StrictContentHandler extends DefaultContentHandler + implements ParameterListHandler { + private String currentProperty; + private final Map currentParameters = new HashMap<>(); + private static final Set DATE_PROPERTIES = + Set.of( + Property.DTSTART, + Property.DTEND, + Property.DUE, + Property.RECURRENCE_ID, + Property.DTSTAMP, + Property.CREATED, + Property.LAST_MODIFIED, + Property.COMPLETED, + Property.RDATE, + Property.EXDATE); + private static final Set UTC_PROPERTIES = + Set.of(Property.DTSTAMP, Property.CREATED, Property.LAST_MODIFIED, Property.COMPLETED); + private static final Set SINGLE = + Set.of( + "ALTREP", + "CN", + "CUTYPE", + "DIR", + "ENCODING", + "FMTTYPE", + "FBTYPE", + "LANGUAGE", + "PARTSTAT", + "RANGE", + "RELATED", + "RELTYPE", + "ROLE", + "RSVP", + "SENT-BY", + "TZID", + "VALUE"); + + public StrictContentHandler( + Consumer consumer, TimeZoneRegistry registry) { + super(consumer, registry, context()); + } + + private static ContentHandlerContext context() { + return new ContentHandlerContext() + .withPropertyFactorySupplier( + () -> { + List> factories = + new ArrayList<>(new DefaultPropertyFactorySupplier().get()); + factories.removeIf(factory -> factory.supports(Property.CLASS)); + factories.addFirst(new ClassificationFactory()); + return factories; + }); + } + + @Override + public void startProperty(String name) { + token(name); + currentProperty = name.toUpperCase(Locale.ROOT); + currentParameters.clear(); + super.startProperty(name.toUpperCase(Locale.ROOT)); + } + + @Override + public void propertyValue(String value) { + String declared = currentParameters.getOrDefault(Parameter.VALUE, "").toUpperCase(Locale.ROOT); + if (Property.RRULE.equals(currentProperty) + || Property.EXRULE.equals(currentProperty) + || declared.equals("RECUR")) + org.busystack.caldav.calendar.RecurrenceRuleSyntax.validate(value); + if (Set.of(Property.DURATION, "REFRESH-INTERVAL").contains(currentProperty) + || Property.TRIGGER.equals(currentProperty) && !declared.equals("DATE-TIME")) + org.busystack.caldav.calendar.DurationSyntax.validate(value); + else if (declared.equals("DURATION")) + for (String duration : value.split(",", -1)) + org.busystack.caldav.calendar.DurationSyntax.validate(duration); + if (declared.equals("PERIOD") || Property.FREEBUSY.equals(currentProperty)) + org.busystack.caldav.calendar.DurationSyntax.validatePeriodDurations(value); + if (DATE_PROPERTIES.contains(currentProperty)) validateDateShape(value); + if ((!Property.RELATED_TO.equals(currentProperty) + || !"URI".equalsIgnoreCase(currentParameters.get(Parameter.VALUE))) + && Set.of( + Property.SUMMARY, + Property.DESCRIPTION, + Property.COMMENT, + Property.LOCATION, + Property.CONTACT, + Property.UID, + Property.PRODID, + Property.TZID, + Property.TZNAME, + Property.RELATED_TO) + .contains(currentProperty)) + org.busystack.caldav.calendar.CalendarTextValues.scalar(value); + // A declared primitive type is meaningful even on an otherwise opaque extension property. + switch (declared) { + case "BOOLEAN" -> { + if (!value.equalsIgnoreCase("TRUE") && !value.equalsIgnoreCase("FALSE")) + throw new IllegalArgumentException("Invalid BOOLEAN value"); + } + case "INTEGER" -> { + for (String item : value.split(",", -1)) { + if (!item.matches("[+-]?[0-9]+")) throw new IllegalArgumentException("Invalid INTEGER"); + Integer.parseInt(item); + } + } + case "FLOAT" -> { + for (String item : value.split(",", -1)) + if (!item.matches("[+-]?[0-9]+(?:\\.[0-9]+)?")) + throw new IllegalArgumentException("Invalid FLOAT value"); + } + default -> { + /* Unknown extension types remain opaque. */ + } + } + if (Property.ATTACH.equals(currentProperty)) { + boolean binary = "BINARY".equalsIgnoreCase(currentParameters.get(Parameter.VALUE)); + boolean encoded = "BASE64".equalsIgnoreCase(currentParameters.get(Parameter.ENCODING)); + if (binary != encoded) + throw new IllegalArgumentException("Invalid attachment representation"); + if (binary) { + if (value.length() % 4 != 0) throw new IllegalArgumentException("Invalid binary length"); + Base64.getDecoder().decode(value); + } + } + super.propertyValue(value); + } + + private void validateDateShape(String value) { + String type = currentParameters.getOrDefault(Parameter.VALUE, "DATE-TIME"); + if (currentProperty.equals(Property.RDATE) && type.equalsIgnoreCase("PERIOD")) return; + boolean date = type.equalsIgnoreCase("DATE"); + if ((!date && !type.equalsIgnoreCase("DATE-TIME")) + || date && UTC_PROPERTIES.contains(currentProperty)) + throw new IllegalArgumentException("Invalid temporal value type"); + if ((date || value.toUpperCase(Locale.ROOT).endsWith("Z")) + && currentParameters.containsKey(Parameter.TZID)) + throw new IllegalArgumentException("TZID is not permitted for DATE or UTC values"); + String[] values = + (currentProperty.equals(Property.RDATE) || currentProperty.equals(Property.EXDATE)) + ? value.split(",", -1) + : new String[] {value}; + for (String item : values) { + if (!item.matches(date ? "[0-9]{8}" : "(?i)[0-9]{8}T[0-9]{6}Z?")) + throw new IllegalArgumentException("Temporal value does not match its declared type"); + if (UTC_PROPERTIES.contains(currentProperty) && !item.toUpperCase(Locale.ROOT).endsWith("Z")) + throw new IllegalArgumentException("UTC timestamp required"); + } + } + + @Override + public void startComponent(String name) { + token(name); + super.startComponent(name.toUpperCase(Locale.ROOT)); + } + + @Override + public void endComponent(String name) { + super.endComponent(name.toUpperCase(Locale.ROOT)); + } + + @Override + public void parameter(String name, String value) { + parameterValues(name, List.of(value)); + } + + @Override + public void parameterValues(String supplied, List rawValues) { + token(supplied); + String name = supplied.toUpperCase(Locale.ROOT); + if (propertyBuilder == null) return; + if (SINGLE.contains(name) && currentParameters.containsKey(name)) + throw new QueryException(QueryException.Kind.INVALID_DATA, "Duplicate calendar parameter"); + currentParameters.put(name, String.join(",", rawValues)); + if (name.equals(Parameter.TZID) + && getComponentBuilder() != null + && (getComponentBuilder().hasName(net.fortuna.ical4j.model.component.Observance.STANDARD) + || getComponentBuilder() + .hasName(net.fortuna.ical4j.model.component.Observance.DAYLIGHT))) + throw new QueryException( + QueryException.Kind.INVALID_DATA, + "TZID is not permitted on timezone observance properties"); + var values = rawValues.stream().map(Rfc6868::value).toList(); + Parameter parameter; + if (name.equals(Parameter.MEMBER) + || name.equals(Parameter.DELEGATED_FROM) + || name.equals(Parameter.DELEGATED_TO)) { + List addresses = new ArrayList<>(); + try { + for (String value : values) addresses.add(new URI(value)); + } catch (java.net.URISyntaxException ex) { + throw new QueryException( + QueryException.Kind.INVALID_DATA, "Invalid calendar address parameter"); + } + parameter = + name.equals(Parameter.MEMBER) + ? new Member(addresses) + : name.equals(Parameter.DELEGATED_FROM) + ? new DelegatedFrom(addresses) + : new DelegatedTo(addresses); + } else { + if (rawValues.size() > 1 && SINGLE.contains(name)) + throw new QueryException( + QueryException.Kind.INVALID_DATA, "Calendar parameter is not multi-valued"); + var factory = + new DefaultParameterFactorySupplier() + .get().stream().filter(f -> f.supports(name)).findFirst(); + if (factory.isEmpty()) + parameter = + values.size() > 1 + ? new StructuredParameter(name, values) + : new XParameter(name, '"' + values.getFirst() + '"'); + else { + String value = String.join(",", values); + parameter = factory.get().createParameter(value); + // These model constructors strip one outer quote pair; retain literal encoded quotes. + if (parameter instanceof Encodable && value.startsWith("\"") && value.endsWith("\"")) + parameter = factory.get().createParameter('"' + value + '"'); + } + } + propertyBuilder.parameter(parameter); + } + + private static void token(String name) { + if (name.isEmpty() || name.length() > 200) + throw new QueryException(QueryException.Kind.INVALID_DATA, "Invalid calendar parameter name"); + for (int index = 0; index < name.length(); index++) { + char c = name.charAt(index); + if (!(c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z' || c >= '0' && c <= '9' || c == '-')) + throw new QueryException( + QueryException.Kind.INVALID_DATA, "Invalid calendar parameter name"); + } + } +} diff --git a/src/main/java/org/busystack/caldav/calendar/parser/StructuredParameter.java b/src/main/java/org/busystack/caldav/calendar/parser/StructuredParameter.java new file mode 100644 index 0000000..fd00822 --- /dev/null +++ b/src/main/java/org/busystack/caldav/calendar/parser/StructuredParameter.java @@ -0,0 +1,35 @@ +package org.busystack.caldav.calendar.parser; + +import java.util.List; +import net.fortuna.ical4j.model.Parameter; +import net.fortuna.ical4j.model.ParameterCodec; + +/** + * An unknown multi-valued parameter keeps its value boundaries when report copies are serialized. + */ +public final class StructuredParameter extends Parameter { + private final List values; + + public StructuredParameter(String name, List values) { + super(name); + this.values = List.copyOf(values); + } + + public List values() { + return values; + } + + @Override + public String getValue() { + return values.stream() + .map( + value -> { + try { + return ParameterCodec.INSTANCE.encode(value); + } catch (org.apache.commons.codec.EncoderException ex) { + throw new IllegalStateException("Parameter encoding failed", ex); + } + }) + .collect(java.util.stream.Collectors.joining(",")); + } +} diff --git a/src/main/java/org/busystack/caldav/calendar/query/AnnualTimezoneRules.java b/src/main/java/org/busystack/caldav/calendar/query/AnnualTimezoneRules.java new file mode 100644 index 0000000..7ddd685 --- /dev/null +++ b/src/main/java/org/busystack/caldav/calendar/query/AnnualTimezoneRules.java @@ -0,0 +1,55 @@ +package org.busystack.caldav.calendar.query; + +import java.time.*; +import net.fortuna.ical4j.model.Property; +import net.fortuna.ical4j.model.component.Observance; +import net.fortuna.ical4j.model.property.DtStart; +import net.fortuna.ical4j.model.property.RRule; +import net.fortuna.ical4j.transform.recurrence.Frequency; + +/** Conservative shapes that Java's one-transition-per-year lastRules can represent exactly. */ +final class AnnualTimezoneRules { + private AnnualTimezoneRules() {} + + static boolean eligible(Observance observance) { + for (var property : observance.getProperties(Property.RRULE)) { + var rule = ((RRule) property).getRecur(); + if (rule.getUntil() != null || rule.getCount() > 0) continue; + if (rule.getFrequency() != Frequency.YEARLY + || Math.max(1, rule.getInterval()) != 1 + || rule.getMonthList().size() > 1 + || rule.getDayList().size() > 1 + || rule.getMonthDayList().size() > 1 + || !rule.getSetPosList().isEmpty() + || !rule.getYearDayList().isEmpty() + || !rule.getWeekNoList().isEmpty()) return false; + var onset = ((DtStart) observance.getRequiredProperty(Property.DTSTART)).getDate(); + if (!(onset instanceof LocalDateTime date)) return false; + if (!same(rule.getHourList(), date.getHour()) + || !same(rule.getMinuteList(), date.getMinute()) + || !same(rule.getSecondList(), date.getSecond())) return false; + Month month = + rule.getMonthList().isEmpty() + ? date.getMonth() + : Month.of(rule.getMonthList().getFirst().getMonthOfYear()); + if (!rule.getDayList().isEmpty()) { + int ordinal = rule.getDayList().getFirst().getOffset(); + if (ordinal == 0 || Math.abs(ordinal) > 4 || !rule.getMonthDayList().isEmpty()) + return false; + if (rule.getMonthList().isEmpty() + && month != (ordinal > 0 ? Month.JANUARY : Month.DECEMBER)) return false; + } else { + int day = + rule.getMonthDayList().isEmpty() + ? date.getDayOfMonth() + : rule.getMonthDayList().getFirst(); + if (day > month.minLength() || day < -month.minLength()) return false; + } + } + return true; + } + + private static boolean same(java.util.List values, int expected) { + return values.isEmpty() || values.size() == 1 && values.getFirst() == expected; + } +} diff --git a/src/main/java/org/busystack/caldav/calendar/query/BoundedTimeZoneRegistry.java b/src/main/java/org/busystack/caldav/calendar/query/BoundedTimeZoneRegistry.java new file mode 100644 index 0000000..beebcef --- /dev/null +++ b/src/main/java/org/busystack/caldav/calendar/query/BoundedTimeZoneRegistry.java @@ -0,0 +1,192 @@ +package org.busystack.caldav.calendar.query; + +import java.time.*; +import java.time.temporal.*; +import java.time.temporal.Temporal; +import java.time.zone.ZoneRules; +import java.util.*; +import java.util.Map; +import net.fortuna.ical4j.model.Property; +import net.fortuna.ical4j.model.TimeZone; +import net.fortuna.ical4j.model.TimeZoneRegistry; +import net.fortuna.ical4j.model.ZoneRulesProviderImpl; +import net.fortuna.ical4j.model.component.Observance; +import net.fortuna.ical4j.model.property.DtStart; +import net.fortuna.ical4j.model.property.RRule; +import org.busystack.caldav.calendar.recurrence.TemporalSupport; +import org.busystack.caldav.calendar.recurrence.WorkBudget; + +/** Checks observance work before the library eagerly constructs timezone transition rules. */ +public final class BoundedTimeZoneRegistry implements TimeZoneRegistry { + private final Map timezones = new HashMap<>(); + private final Map rules = new HashMap<>(); + private final Map identifiers = new HashMap<>(); + private final WorkBudget budget = new WorkBudget(QueryLimits.defaults()); + private int zones; + private final Map coverage = new HashMap<>(); + + public Map coverage() { + return Map.copyOf(coverage); + } + + @Override + public void register(TimeZone timezone) { + register(timezone, false); + } + + @Override + public void register(TimeZone timezone, boolean update) { + if (update) throw new IllegalArgumentException("External timezone updates are disabled"); + if (++zones > 32 || timezone.getVTimeZone().getComponents().size() > 128) + throw QueryException.limit(); + boolean bounded = + timezone.getVTimeZone().getComponents().stream() + .anyMatch(o -> !AnnualTimezoneRules.eligible(o)); + LocalDateTime horizon = + LocalDateTime.now(ZoneOffset.UTC).plusDays(budget.limits().maximumRangeDays()); + LocalDateTime explicitEnd = LocalDateTime.now(ZoneOffset.UTC); + for (var observance : timezone.getVTimeZone().getComponents()) { + var onset = ((DtStart) observance.getRequiredProperty(Property.DTSTART)).getDate(); + if (!(onset instanceof LocalDateTime start)) + throw new QueryException(QueryException.Kind.INVALID_DATA, "Timezone onset must be local"); + if (start.isAfter(horizon)) horizon = start.plusYears(5); + for (var property : observance.getProperties(Property.RDATE)) { + var dates = (net.fortuna.ical4j.model.property.RDate) property; + if (dates.getPeriods().isPresent()) + throw new QueryException( + QueryException.Kind.INVALID_DATA, "Timezone onsets must be local date-times"); + for (var value : dates.getDates()) { + if (!(value instanceof LocalDateTime date)) + throw new QueryException( + QueryException.Kind.INVALID_DATA, "Timezone onsets must be local date-times"); + if (date.isAfter(explicitEnd)) explicitEnd = date; + } + } + } + var ends = new IdentityHashMap(); + for (var observance : timezone.getVTimeZone().getComponents()) { + DtStart property = observance.getRequiredProperty(Property.DTSTART); + Temporal start = property.getDate(); + if (!(start instanceof LocalDateTime local)) + throw new QueryException(QueryException.Kind.INVALID_DATA, "Timezone onset must be local"); + LocalDateTime end = LocalDateTime.now(); + if (end.isBefore(local)) end = local.plusYears(5); + if (bounded) end = horizon; + else if (explicitEnd.isAfter(end)) end = explicitEnd.plusNanos(1); + for (var propertyRule : observance.getProperties(Property.RRULE)) { + var rule = ((RRule) propertyRule).getRecur(); + var offset = + ((net.fortuna.ical4j.model.property.TzOffsetFrom) + observance.getRequiredProperty(Property.TZOFFSETFROM)) + .getOffset(); + if (rule.getUntil() != null) { + var until = + TemporalSupport.instant(rule.getUntil(), ZoneOffset.UTC) + .atOffset(offset) + .toLocalDateTime(); + end = until.plusNanos(1); + } else if (rule.getCount() > 0) { + var counted = countedEnd(rule, local, offset); + end = counted; + } + budget.rule(rule, local, local, end); + } + ends.put(observance, end); + for (var dates : observance.getProperties(Property.RDATE)) + budget.charge(dates.getValue().length()); + } + var corrected = + new ObservanceZoneRules(budget).build(timezone.getVTimeZone(), ends, horizon, !bounded); + // TimeZoneRegistry is the library's extension point. Allocate a registry-local ZoneId without + // invoking its eager, wall-clock-selected ZoneRulesBuilder first. Weak registry references in + // the provider's bounded id pool retain the ordinary lifecycle and prevent a global-id leak. + String id = + identifiers.computeIfAbsent( + timezone.getID(), + ignored -> + ZoneRulesProviderImpl.getInstance() + .orElseThrow(QueryException::limit) + .getZoneIdPool() + .allocate(this)); + timezones.put(timezone.getID(), timezone); + rules.put(id, corrected.rules()); + if (corrected.coverageEnd() != null) { + coverage.put(id, corrected.coverageEnd()); + coverage.put(timezone.getID(), corrected.coverageEnd()); + } + } + + private LocalDateTime countedEnd( + net.fortuna.ical4j.model.Recur rule, LocalDateTime start, ZoneOffset offset) { + int count = rule.getCount(); + if (count > budget.limits().maximumInstances()) throw QueryException.limit(); + var unit = + switch (rule.getFrequency()) { + case SECONDLY -> ChronoUnit.SECONDS; + case MINUTELY -> ChronoUnit.MINUTES; + case HOURLY -> ChronoUnit.HOURS; + case DAILY -> ChronoUnit.DAYS; + case WEEKLY -> ChronoUnit.WEEKS; + case MONTHLY -> ChronoUnit.MONTHS; + case YEARLY -> ChronoUnit.YEARS; + }; + int maximum = budget.limits().maximumRuleIncrements() - 5; + int increments = Math.min(maximum, Math.max(2, Math.min(32, count + 1))); + @SuppressWarnings("unchecked") + var typed = + (net.fortuna.ical4j.model.Recur) (net.fortuna.ical4j.model.Recur) rule; + while (true) { + LocalDateTime end; + try { + end = + start + .plus(Math.multiplyExact((long) Math.max(1, rule.getInterval()), increments), unit) + .plusNanos(1); + } catch (DateTimeException | ArithmeticException ex) { + throw QueryException.limit(); + } + budget.rule(rule, start, start, end); + List dates; + try (var stream = + typed.getDatesAsStream( + start.atOffset(offset), start.atOffset(offset), end.atOffset(offset), count)) { + dates = stream.toList(); + } + if (dates.size() >= count) return dates.getLast().toLocalDateTime().plusNanos(1); + if (increments == maximum) throw QueryException.limit(); + increments = Math.min(maximum, increments * 2); + } + } + + @Override + public void clear() { + timezones.clear(); + coverage.clear(); + } + + @Override + public TimeZone getTimeZone(String id) { + return timezones.get(id); + } + + @Override + public Map getZoneRules() { + return rules; + } + + @Override + public java.time.ZoneId getZoneId(String id) { + String allocated = identifiers.get(id); + if (allocated == null) throw new DateTimeException("Unknown timezone identifier"); + return ZoneId.of(allocated); + } + + @Override + public String getTzId(String id) { + return identifiers.entrySet().stream() + .filter(entry -> entry.getValue().equals(id)) + .map(Map.Entry::getKey) + .findFirst() + .orElse(null); + } +} diff --git a/src/main/java/org/busystack/caldav/calendar/query/CalendarDataProjection.java b/src/main/java/org/busystack/caldav/calendar/query/CalendarDataProjection.java new file mode 100644 index 0000000..0b4f5cc --- /dev/null +++ b/src/main/java/org/busystack/caldav/calendar/query/CalendarDataProjection.java @@ -0,0 +1,232 @@ +package org.busystack.caldav.calendar.query; + +import java.time.Duration; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Locale; +import java.util.Objects; +import net.fortuna.ical4j.model.Calendar; +import net.fortuna.ical4j.model.Component; +import net.fortuna.ical4j.model.ComponentContainer; +import net.fortuna.ical4j.model.ComponentList; +import net.fortuna.ical4j.model.Property; +import net.fortuna.ical4j.model.PropertyContainer; +import net.fortuna.ical4j.model.PropertyList; +import net.fortuna.ical4j.model.property.FreeBusy; +import net.fortuna.ical4j.model.property.XProperty; +import org.busystack.caldav.calendar.CalendarContent; +import org.busystack.caldav.calendar.recurrence.CalendarRecurrence; + +/** RFC 4791 section 9.6 report projection. Stored representations are never mutated. */ +public final class CalendarDataProjection { + public record Selection( + ComponentSelection component, + TimeRange expand, + TimeRange limitRecurrence, + TimeRange limitFreeBusy) { + public Selection { + if (expand != null && limitRecurrence != null) + throw QueryException.invalid("Expansion and recurrence limiting are mutually exclusive"); + for (TimeRange range : new TimeRange[] {expand, limitRecurrence, limitFreeBusy}) { + if (range != null && (range.start() == null || range.end() == null)) + throw QueryException.invalid("Calendar-data time selections require both bounds"); + } + } + } + + public record ComponentSelection( + String name, + boolean allProperties, + boolean allComponents, + List properties, + List components) { + public ComponentSelection { + name = checkedName(name); + properties = List.copyOf(properties); + components = List.copyOf(components); + if (allProperties && !properties.isEmpty() || allComponents && !components.isEmpty()) + throw QueryException.invalid("Explicit and all-item selectors cannot be combined"); + } + } + + public record PropertySelection(String name, boolean noValue) { + public PropertySelection { + name = checkedName(name); + } + } + + private final QueryLimits limits; + private final CalendarRecurrence recurrence; + private long candidateWork; + private int returnedComponents; + private int returnedPeriods; + private long reservedReportBytes; + + /** One instance belongs to one REPORT, including every calendar resource in its response. */ + public CalendarDataProjection(QueryLimits limits) { + this.limits = Objects.requireNonNull(limits); + this.recurrence = new CalendarRecurrence(limits); + } + + public Calendar project( + CalendarContent.Parsed parsed, Selection selection, FloatingTimezone floating) { + Objects.requireNonNull(parsed); + long sourceBytes = CalendarReportSize.estimate(parsed); + reserveBytes(sourceBytes); // Input retained for this report. + reserveBytes(sourceBytes); // Baseline report copy, before copying or temporal expansion. + spend(parsed.calendar().getProperties().size()); + chargeInput(parsed.calendar()); + if (selection == null) { + Calendar result = CalendarReports.copy(parsed); + chargeOutput(result); + return result; + } + if (selection.component() != null) { + if (!Calendar.VCALENDAR.equals(selection.component().name())) + throw QueryException.invalid("Calendar-data selection must start at VCALENDAR"); + validateComplexity(selection.component(), new int[] {0}, 0); + } + Calendar result = + selection.expand() != null + ? recurrence.expand(parsed, selection.expand(), floating) + : selection.limitRecurrence() != null + ? recurrence.limitRecurrence(parsed, selection.limitRecurrence(), floating) + : CalendarReports.copy(parsed); + if (selection.limitFreeBusy() != null) limitFreeBusy(result, selection.limitFreeBusy()); + if (selection.component() != null) { + projectProperties(result, selection.component()); + projectChildren(result, selection.component()); + } + chargeOutput(result); + return result; + } + + private void reserveBytes(long amount) { + if (amount > CalendarReportSize.MAX_REPORT_BYTES - reservedReportBytes) + throw QueryException.limit(); + reservedReportBytes += amount; + } + + private void spend(long amount) { + if (amount > limits.maximumCandidateWork() - candidateWork) throw QueryException.limit(); + candidateWork += amount; + } + + private void chargeInput(ComponentContainer owner) { + for (Component component : owner.getComponents()) { + spend(1L + component.getProperties().size()); + if (component instanceof ComponentContainer nested) chargeInput(nested); + } + } + + private void chargeOutput(ComponentContainer owner) { + for (Component component : owner.getComponents()) { + if (++returnedComponents > limits.maximumInstances()) throw QueryException.limit(); + if (component instanceof ComponentContainer nested) chargeOutput(nested); + } + } + + private void limitFreeBusy(Calendar calendar, TimeRange range) { + if (Duration.between(range.start(), range.end()) + .compareTo(Duration.ofDays(limits.maximumRangeDays())) + > 0) throw QueryException.limit(); + for (var component : calendar.getComponents()) { + if (!Component.VFREEBUSY.equals(component.getName())) continue; + var filtered = new ArrayList(); + for (Property property : component.getProperties()) { + if (!(property instanceof FreeBusy freeBusy)) { + filtered.add(property); + continue; + } + var retained = new ArrayList(); + for (var interval : freeBusy.getIntervals()) { + spend(1); + if (range.overlaps(interval.getStart(), interval.getEnd())) { + if (++returnedPeriods > limits.maximumInstances()) throw QueryException.limit(); + retained.add(interval); + } + } + // Retain intersecting complete periods (including their FBTYPE), without clipping them. + if (!retained.isEmpty()) filtered.add(new FreeBusy(property.getParameterList(), retained)); + } + component.setPropertyList(new PropertyList(filtered)); + } + } + + private void validateComplexity(ComponentSelection selection, int[] work, int depth) { + if (depth > 8 || ++work[0] + selection.properties().size() > limits.maximumFilterNodes()) + throw QueryException.limit(); + work[0] += selection.properties().size(); + for (ComponentSelection child : selection.components()) + validateComplexity(child, work, depth + 1); + } + + private static void projectProperties(PropertyContainer target, ComponentSelection selection) { + if (selection.allProperties()) return; + var requested = new LinkedHashMap(); + for (PropertySelection property : selection.properties()) + requested.merge(property.name(), property.noValue(), (first, second) -> first && second); + var properties = new ArrayList(); + for (Property property : target.getProperties()) { + Boolean noValue = requested.get(property.getName().toUpperCase(Locale.ROOT)); + if (noValue == null) continue; + // A report may omit required values. A generic library property represents that projection + // without forcing a date/URI parser to accept an empty value. Parameters remain on the copy. + if (noValue) { + var empty = new XProperty(property.getName(), property.getParameterList(), ""); + empty.setPrefix(property.getPrefix()); + properties.add(empty); + } else properties.add(property); + } + target.setPropertyList(new PropertyList(properties)); + } + + private static void projectChildren( + ComponentContainer owner, ComponentSelection selection) { + if (selection.allComponents()) return; + var selected = new ArrayList(); + for (T component : owner.getComponents()) { + List matching = + selection.components().stream() + .filter(value -> value.name().equalsIgnoreCase(component.getName())) + .toList(); + if (matching.isEmpty()) continue; + ComponentSelection combined = combine(matching); + projectProperties(component, combined); + if (component instanceof ComponentContainer nested) projectChildren(nested, combined); + selected.add(component); + } + owner.setComponentList(new ComponentList<>(selected)); + } + + private static ComponentSelection combine(List selections) { + boolean allProperties = selections.stream().anyMatch(ComponentSelection::allProperties); + boolean allComponents = selections.stream().anyMatch(ComponentSelection::allComponents); + return new ComponentSelection( + selections.getFirst().name(), + allProperties, + allComponents, + allProperties + ? List.of() + : selections.stream().flatMap(value -> value.properties().stream()).toList(), + allComponents + ? List.of() + : selections.stream().flatMap(value -> value.components().stream()).toList()); + } + + private static String checkedName(String name) { + if (name == null + || name.isEmpty() + || name.length() > 255 + || !name.chars() + .allMatch( + value -> + value >= 'A' && value <= 'Z' + || value >= 'a' && value <= 'z' + || value >= '0' && value <= '9' + || value == '-')) + throw QueryException.invalid("Invalid calendar selector name"); + return name.toUpperCase(Locale.ROOT); + } +} diff --git a/src/main/java/org/busystack/caldav/calendar/query/CalendarFilter.java b/src/main/java/org/busystack/caldav/calendar/query/CalendarFilter.java new file mode 100644 index 0000000..3d18072 --- /dev/null +++ b/src/main/java/org/busystack/caldav/calendar/query/CalendarFilter.java @@ -0,0 +1,98 @@ +package org.busystack.caldav.calendar.query; + +import java.util.List; +import java.util.Locale; +import java.util.Objects; + +/** Typed RFC 4791 section 9.7 filter. XML parsing belongs to the protocol module. */ +public final class CalendarFilter { + private CalendarFilter() {} + + public record ComponentFilter( + String name, + boolean notDefined, + TimeRange timeRange, + List properties, + List components) { + public ComponentFilter { + name = CalendarFilter.name(name); + properties = List.copyOf(properties == null ? List.of() : properties); + components = List.copyOf(components == null ? List.of() : components); + if (notDefined && (timeRange != null || !properties.isEmpty() || !components.isEmpty())) + throw QueryException.invalid("is-not-defined must be the only component condition"); + } + } + + public record PropertyFilter( + String name, + boolean notDefined, + TimeRange timeRange, + TextMatch textMatch, + List parameters) { + public PropertyFilter { + name = CalendarFilter.name(name); + parameters = List.copyOf(parameters == null ? List.of() : parameters); + if (timeRange != null && textMatch != null + || notDefined && (timeRange != null || textMatch != null || !parameters.isEmpty())) + throw QueryException.invalid("Conflicting property filter conditions"); + } + } + + public record ParameterFilter(String name, boolean notDefined, TextMatch textMatch) { + public ParameterFilter { + name = CalendarFilter.name(name); + if (notDefined && textMatch != null) + throw QueryException.invalid("Conflicting parameter filter conditions"); + } + } + + public record TextMatch(String text, String collation, boolean negate) { + public TextMatch { + Objects.requireNonNull(text, "text"); + collation = collation == null || collation.equals("default") ? "i;ascii-casemap" : collation; + if (!collation.equals("i;ascii-casemap") && !collation.equals("i;octet")) + throw new QueryException( + QueryException.Kind.UNSUPPORTED_COLLATION, "Unsupported text collation"); + } + + public boolean matches(String value) { + return negate != contains(value); + } + + public boolean matchesAny(Iterable values) { + boolean found = false; + for (String value : values) + if (contains(value)) { + found = true; + break; + } + return negate != found; + } + + private boolean contains(String value) { + return collation.equals("i;octet") + ? value.contains(text) + : ascii(value).contains(ascii(text)); + } + } + + private static String name(String value) { + if (value == null || value.isEmpty() || value.length() > 200) + throw QueryException.invalid("Invalid filter name"); + for (int index = 0; index < value.length(); index++) { + char c = value.charAt(index); + if (!(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == '-')) + throw QueryException.invalid("Invalid filter name"); + } + return value.toUpperCase(Locale.ROOT); + } + + private static String ascii(String value) { + var result = new StringBuilder(value.length()); + for (int i = 0; i < value.length(); i++) { + char c = value.charAt(i); + result.append(c >= 'a' && c <= 'z' ? (char) (c - 32) : c); + } + return result.toString(); + } +} diff --git a/src/main/java/org/busystack/caldav/calendar/query/CalendarFreeBusy.java b/src/main/java/org/busystack/caldav/calendar/query/CalendarFreeBusy.java new file mode 100644 index 0000000..dc93fb0 --- /dev/null +++ b/src/main/java/org/busystack/caldav/calendar/query/CalendarFreeBusy.java @@ -0,0 +1,154 @@ +package org.busystack.caldav.calendar.query; + +import static net.fortuna.ical4j.model.Property.*; + +import java.time.*; +import java.util.*; +import net.fortuna.ical4j.model.Component; +import net.fortuna.ical4j.model.Parameter; +import net.fortuna.ical4j.model.property.FreeBusy; +import org.busystack.caldav.calendar.CalendarContent; +import org.busystack.caldav.calendar.recurrence.*; + +/** + * Computes busy intervals from authorized input resources; does not deliver invitations or alarms. + */ +public final class CalendarFreeBusy { + public enum BusyType { + BUSY("BUSY"), + BUSY_TENTATIVE("BUSY-TENTATIVE"), + BUSY_UNAVAILABLE("BUSY-UNAVAILABLE"); + private final String value; + + BusyType(String value) { + this.value = value; + } + + public String value() { + return value; + } + } + + public record BusyPeriod(Instant start, Instant end, BusyType type) {} + + public record Source(CalendarContent.Parsed calendar, FloatingTimezone timezone) { + public Source { + Objects.requireNonNull(calendar); + Objects.requireNonNull(timezone); + } + } + + private final WorkBudget budget; + private final RecurrenceExpander recurrence; + + public CalendarFreeBusy(QueryLimits limits) { + budget = new WorkBudget(limits); + recurrence = new RecurrenceExpander(budget); + } + + public List query( + List calendars, TimeRange range, ZoneId zone) { + return query(calendars, range, FloatingTimezone.of(zone)); + } + + public List query( + List calendars, TimeRange range, FloatingTimezone timezone) { + return querySources( + calendars.stream().map(calendar -> new Source(calendar, timezone)).toList(), range); + } + + public List querySources(List sources, TimeRange range) { + return querySources((Iterable) sources, range); + } + + /** Consume one parsed resource at a time; only the bounded busy-period result is retained. */ + public List querySources(Iterable sources, TimeRange range) { + if (range.start() == null || range.end() == null) + throw QueryException.invalid("Free/busy requires a finite time range"); + var periods = new ArrayList(); + for (var source : sources) { + var parsed = source.calendar(); + var timezone = source.timezone(); + budget.charge(1); + if (parsed.calendar().getComponents(Component.VEVENT).isEmpty() + && parsed.calendar().getComponents(Component.VFREEBUSY).isEmpty()) continue; + for (var occurrence : recurrence.occurrences(parsed, range, timezone)) { + var c = occurrence.component(); + budget.charge(1); + if (Component.VEVENT.equals(c.getName())) { + if (c.getProperty(TRANSP) + .map(p -> p.getValue().equalsIgnoreCase("TRANSPARENT")) + .orElse(false)) continue; + String status = + c.getProperty(STATUS) + .map(p -> p.getValue()) + .orElse("CONFIRMED") + .toUpperCase(Locale.ROOT); + if (status.equals("CANCELLED")) continue; + var start = occurrence.instant(DTSTART, timezone.zoneId()); + var end = occurrence.instant(DTEND, timezone.zoneId()); + if (start != null && end != null) + add( + periods, + range, + start, + end, + status.equals("TENTATIVE") ? BusyType.BUSY_TENTATIVE : BusyType.BUSY); + } else if (Component.VFREEBUSY.equals(c.getName())) { + for (var p : c.getProperties(FREEBUSY)) { + String type = + p.getParameter(Parameter.FBTYPE) + .map(value -> value.getValue()) + .orElse("BUSY") + .toUpperCase(Locale.ROOT); + if (type.equals("FREE")) continue; + BusyType busy = + type.equals("BUSY-TENTATIVE") + ? BusyType.BUSY_TENTATIVE + : type.equals("BUSY-UNAVAILABLE") ? BusyType.BUSY_UNAVAILABLE : BusyType.BUSY; + for (var interval : ((FreeBusy) p).getIntervals()) + add(periods, range, interval.getStart(), interval.getEnd(), busy); + } + } + } + } + var result = new ArrayList(); + for (BusyType type : BusyType.values()) { + var sorted = + periods.stream() + .filter(p -> p.type() == type) + .sorted(Comparator.comparing(BusyPeriod::start).thenComparing(BusyPeriod::end)) + .toList(); + BusyPeriod current = null; + for (var next : sorted) { + if (current == null) current = next; + else if (!next.start().isAfter(current.end())) + current = + new BusyPeriod( + current.start(), + current.end().isAfter(next.end()) ? current.end() : next.end(), + type); + else { + result.add(current); + current = next; + } + } + if (current != null) result.add(current); + } + result.sort(Comparator.comparing(BusyPeriod::start).thenComparing(p -> p.type().value())); + java.lang.ref.Reference.reachabilityFence(sources); + return List.copyOf(result); + } + + private void add( + List result, TimeRange range, Instant start, Instant end, BusyType type) { + budget.charge(1); + if (!start.isBefore(end) || !range.overlaps(start, end)) return; + budget.instance(); + result.add( + new BusyPeriod( + start.isBefore(range.start()) ? range.start() : start, + end.isAfter(range.end()) ? range.end() : end, + type)); + } +} diff --git a/src/main/java/org/busystack/caldav/calendar/query/CalendarQuery.java b/src/main/java/org/busystack/caldav/calendar/query/CalendarQuery.java new file mode 100644 index 0000000..5d0eb6c --- /dev/null +++ b/src/main/java/org/busystack/caldav/calendar/query/CalendarQuery.java @@ -0,0 +1,254 @@ +package org.busystack.caldav.calendar.query; + +import static net.fortuna.ical4j.model.Property.*; + +import java.time.*; +import java.util.*; +import net.fortuna.ical4j.model.*; +import net.fortuna.ical4j.model.property.*; +import org.busystack.caldav.calendar.CalendarContent; +import org.busystack.caldav.calendar.query.CalendarFilter.*; +import org.busystack.caldav.calendar.recurrence.*; + +/** + * One instance per REPORT: cumulative work budget covers all resources evaluated by that report. + */ +public final class CalendarQuery { + private final QueryLimits limits; + private final WorkBudget budget; + private final RecurrenceExpander recurrence; + private int matchedResources; + + public CalendarQuery(QueryLimits limits) { + this.limits = limits; + budget = new WorkBudget(limits); + recurrence = new RecurrenceExpander(budget); + } + + public boolean matches(CalendarContent.Parsed parsed, ComponentFilter filter, ZoneId zone) { + return matches(parsed, filter, FloatingTimezone.of(zone)); + } + + public boolean matches( + CalendarContent.Parsed parsed, ComponentFilter filter, FloatingTimezone timezone) { + validate(filter); + var temporalRanges = new ArrayList(); + collectAllRanges(filter, temporalRanges); + for (var range : temporalRanges) CalendarTemporalCoverage.check(parsed, range, timezone); + if (!filter.name().equals("VCALENDAR")) + throw QueryException.invalid("The root component filter must target VCALENDAR"); + if (filter.notDefined()) return false; + if (filter.timeRange() != null) throw unsupported(); + if (!properties(parsed, parsed.calendar(), null, filter.properties(), timezone.zoneId())) + return false; + for (ComponentFilter child : filter.components()) + if (!components(parsed, parsed.calendar().getComponents(), child, null, timezone)) + return false; + if (++matchedResources > limits.maximumInstances()) throw QueryException.limit(); + return true; + } + + private boolean components( + CalendarContent.Parsed parsed, + List available, + ComponentFilter filter, + Occurrence owner, + FloatingTimezone timezone) { + var selected = available.stream().filter(c -> c.getName().equals(filter.name())).toList(); + if (filter.notDefined()) return selected.isEmpty(); + if (selected.isEmpty()) return false; + boolean primary = + filter.name().equals(Component.VEVENT) || filter.name().equals(Component.VTODO); + TimeRange window = window(filter); + List occurrences; + if (primary && window != null) + occurrences = recurrence.occurrences(parsed, window, timezone, hasAlarmRange(filter)); + else { + occurrences = new ArrayList<>(); + for (Component c : selected) + occurrences.add(RecurrenceExpander.single(parsed, c, null, null, timezone.zoneId())); + } + boolean unknown = false; + for (Occurrence occurrence : occurrences) { + if (RecurrenceExpander.unknownFuture(occurrence)) { + unknown = true; + continue; + } + if (!occurrence.component().getName().equals(filter.name())) continue; + budget.charge(1); + if (filter.timeRange() != null) { + boolean overlaps; + if (filter.name().equals(Component.VALARM)) { + if (owner == null) throw unsupported(); + overlaps = + AlarmCalculator.triggers( + parsed, occurrence.component(), owner, timezone.zoneId(), budget) + .stream() + .anyMatch(filter.timeRange()::contains); + } else + overlaps = TimeRangeMatcher.matches(occurrence, filter.timeRange(), timezone.zoneId()); + if (!overlaps) continue; + } + if (!properties( + parsed, occurrence.component(), occurrence, filter.properties(), timezone.zoneId())) + continue; + boolean children = true; + for (ComponentFilter child : filter.components()) { + List nested = + occurrence.component() instanceof ComponentContainer container + ? container.getComponents() + : List.of(); + if (!components(parsed, nested, child, primary ? occurrence : owner, timezone)) { + children = false; + break; + } + } + if (children) return true; + } + if (unknown) throw QueryException.limit(); + return false; + } + + private boolean properties( + CalendarContent.Parsed parsed, + PropertyContainer container, + Occurrence occurrence, + List filters, + ZoneId zone) { + for (PropertyFilter filter : filters) { + var found = container.getProperties(filter.name()); + if (filter.notDefined()) { + if (!found.isEmpty()) return false; + continue; + } + boolean match = false; + for (var property : found) + if (property(parsed, property, occurrence, filter, zone)) { + match = true; + break; + } + if (!match + && found.isEmpty() + && filter.timeRange() != null + && occurrence != null + && (filter.name().equals(DTEND) || filter.name().equals(DUE))) { + var effective = occurrence.dates().get(filter.name()); + if (effective != null && filter.parameters().isEmpty()) + match = filter.timeRange().contains(TemporalSupport.instant(effective, zone)); + } + if (!match) return false; + } + return true; + } + + private boolean property( + CalendarContent.Parsed parsed, + Property property, + Occurrence occurrence, + PropertyFilter filter, + ZoneId zone) { + budget.charge(1); + if (filter.timeRange() != null) { + if (!Set.of(COMPLETED, CREATED, DTEND, DTSTAMP, DTSTART, DUE, LAST_MODIFIED) + .contains(property.getName())) throw unsupported(); + var temporal = + occurrence == null + ? (property instanceof DateProperty d ? d.getDate() : null) + : occurrence.dates().get(property.getName()); + if (temporal == null || !filter.timeRange().contains(TemporalSupport.instant(temporal, zone))) + return false; + } + if (filter.textMatch() != null + && !filter.textMatch().matchesAny(semanticText(parsed, property))) return false; + for (ParameterFilter parameter : filter.parameters()) { + var found = property.getParameters(parameter.name()); + if (parameter.notDefined()) { + if (!found.isEmpty()) return false; + continue; + } + if (found.isEmpty() + || parameter.textMatch() != null + && found.stream() + .flatMap(p -> parameterValues(p).stream()) + .noneMatch(parameter.textMatch()::matches)) return false; + } + return true; + } + + private static Iterable semanticText(CalendarContent.Parsed parsed, Property property) { + // A single typed-boundary decode avoids searching escaped serialization or unescaping twice. + if (property instanceof Categories || property instanceof Resources) + return org.busystack.caldav.calendar.CalendarTextValues.list(parsed.lexicalValue(property)); + return List.of(property.getValue()); + } + + private static List parameterValues(Parameter parameter) { + if (parameter instanceof org.busystack.caldav.calendar.parser.StructuredParameter structured) + return structured.values(); + if (parameter instanceof net.fortuna.ical4j.model.parameter.Member member) + return member.getGroups().stream().map(java.net.URI::toString).toList(); + if (parameter instanceof net.fortuna.ical4j.model.parameter.DelegatedFrom from) + return from.getDelegators().stream().map(java.net.URI::toString).toList(); + if (parameter instanceof net.fortuna.ical4j.model.parameter.DelegatedTo to) + return to.getDelegatees().stream().map(java.net.URI::toString).toList(); + return List.of(parameter.getValue()); + } + + private TimeRange window(ComponentFilter filter) { + List ranges = new ArrayList<>(); + collectRanges(filter, ranges); + if (ranges.isEmpty()) return null; + Instant first = null, last = null; + boolean openFirst = false, openLast = false; + for (TimeRange range : ranges) { + if (range.start() == null) openFirst = true; + else if (first == null || range.start().isBefore(first)) first = range.start(); + if (range.end() == null) openLast = true; + else if (last == null || range.end().isAfter(last)) last = range.end(); + } + if (openFirst && openLast) throw QueryException.limit(); + return new TimeRange(openFirst ? null : first, openLast ? null : last); + } + + private void collectAllRanges(ComponentFilter filter, List ranges) { + if (filter.timeRange() != null) ranges.add(filter.timeRange()); + for (var property : filter.properties()) + if (property.timeRange() != null) ranges.add(property.timeRange()); + for (var child : filter.components()) collectAllRanges(child, ranges); + } + + private void collectRanges(ComponentFilter f, List ranges) { + if (f.timeRange() != null) ranges.add(f.timeRange()); + for (var p : f.properties()) + if (p.timeRange() != null && Set.of(DTSTART, DTEND, DUE).contains(p.name())) + ranges.add(p.timeRange()); + for (var c : f.components()) collectRanges(c, ranges); + } + + private boolean hasAlarmRange(ComponentFilter filter) { + if (filter.name().equals(Component.VALARM) && filter.timeRange() != null) return true; + return filter.components().stream().anyMatch(this::hasAlarmRange); + } + + private void validate(ComponentFilter filter) { + record Node(ComponentFilter filter, int depth) {} + var nodes = new ArrayDeque(); + nodes.push(new Node(filter, 0)); + int count = 0; + while (!nodes.isEmpty()) { + var node = nodes.pop(); + if (++count > limits.maximumFilterNodes() || node.depth() > 16) throw QueryException.limit(); + for (var p : node.filter().properties()) { + count += 1 + p.parameters().size(); + if (count > limits.maximumFilterNodes()) throw QueryException.limit(); + } + for (var c : node.filter().components()) nodes.push(new Node(c, node.depth() + 1)); + } + } + + private static QueryException unsupported() { + return new QueryException( + QueryException.Kind.UNSUPPORTED_FILTER, + "Time range semantics are undefined for this filter target"); + } +} diff --git a/src/main/java/org/busystack/caldav/calendar/query/CalendarReportSize.java b/src/main/java/org/busystack/caldav/calendar/query/CalendarReportSize.java new file mode 100644 index 0000000..1e07fce --- /dev/null +++ b/src/main/java/org/busystack/caldav/calendar/query/CalendarReportSize.java @@ -0,0 +1,70 @@ +package org.busystack.caldav.calendar.query; + +import java.util.function.Function; +import net.fortuna.ical4j.model.Calendar; +import net.fortuna.ical4j.model.Component; +import net.fortuna.ical4j.model.ComponentContainer; +import net.fortuna.ical4j.model.Property; +import net.fortuna.ical4j.model.PropertyContainer; +import org.busystack.caldav.calendar.CalendarContent; + +/** Conservative UTF-8/XML allocation estimate without serializing an entire calendar. */ +public final class CalendarReportSize { + public static final long MAX_REPORT_BYTES = 16L * 1024 * 1024; + + private CalendarReportSize() {} + + public static long estimate(CalendarContent.Parsed parsed) { + return calendar(parsed.calendar(), parsed::lexicalValue); + } + + public static long estimate(CalendarContent.Parsed parsed, Component component) { + return component(component, parsed::lexicalValue); + } + + public static long estimate(Calendar calendar) { + return calendar(calendar, Property::getValue); + } + + public static long estimate(Component component) { + return component(component, Property::getValue); + } + + private static long calendar(Calendar calendar, Function values) { + long size = add(64, properties(calendar, values)); + for (Component component : calendar.getComponents()) + size = add(size, component(component, values)); + return size; + } + + private static long component(Component component, Function values) { + long size = add(add(64, text(component.getName()) * 2), properties(component, values)); + if (component instanceof ComponentContainer nested) + for (Component child : nested.getComponents()) size = add(size, component(child, values)); + return size; + } + + private static long properties(PropertyContainer owner, Function values) { + long size = 0; + for (Property property : owner.getProperties()) { + size = add(size, add(32, add(text(property.getName()), text(values.apply(property))))); + size = add(size, text(property.getPrefix())); + for (var parameter : property.getParameterList().getAll()) + size = add(size, add(32, add(text(parameter.getName()), text(parameter.getValue())))); + } + return size; + } + + private static long text(String value) { + // Six bytes per UTF-16 code unit bounds UTF-8, XML escaping, and iCalendar escaping/folding. + return value == null ? 0 : 6L * value.length(); + } + + private static long add(long first, long second) { + try { + return Math.addExact(first, second); + } catch (ArithmeticException error) { + throw QueryException.limit(); + } + } +} diff --git a/src/main/java/org/busystack/caldav/calendar/query/CalendarReports.java b/src/main/java/org/busystack/caldav/calendar/query/CalendarReports.java new file mode 100644 index 0000000..29c4062 --- /dev/null +++ b/src/main/java/org/busystack/caldav/calendar/query/CalendarReports.java @@ -0,0 +1,116 @@ +package org.busystack.caldav.calendar.query; + +import java.util.*; +import net.fortuna.ical4j.model.*; +import net.fortuna.ical4j.validate.*; +import org.busystack.caldav.calendar.CalendarContent; + +/** + * Creates report models without losing typed-value spellings such as exact versus nominal duration. + */ +public final class CalendarReports { + private CalendarReports() {} + + public static net.fortuna.ical4j.model.Calendar copy(CalendarContent.Parsed parsed) { + var copy = parsed.calendar().copy(); + restore(parsed, parsed.calendar(), copy); + var originals = parsed.calendar().getComponents(); + for (int index = 0; index < originals.size(); index++) + restoreTree(parsed, originals.get(index), copy.getComponents().get(index)); + return copy; + } + + public static Component componentCopy(CalendarContent.Parsed parsed, Component original) { + var copy = original.copy(); + restoreTree(parsed, original, copy); + return copy; + } + + private static void restoreTree( + CalendarContent.Parsed parsed, Component original, Component copy) { + restore(parsed, original, copy); + if (original instanceof ComponentContainer a && copy instanceof ComponentContainer b) + for (int index = 0; index < a.getComponents().size(); index++) + restoreTree(parsed, a.getComponents().get(index), b.getComponents().get(index)); + } + + private static void restore( + CalendarContent.Parsed parsed, PropertyContainer original, PropertyContainer copy) { + var values = new ArrayList(copy.getProperties()); + List properties = original.getProperties(); + if (values.size() != properties.size()) + throw new IllegalStateException("Report property alignment failed"); + for (int index = 0; index < properties.size(); index++) { + Property property = properties.get(index); + String lexical = parsed.lexicalValues().get(property); + if (lexical != null) + values.set( + index, + new PreservedValue( + property, + lexical, + new ParameterList(values.get(index).getParameterList().getAll()), + lexical)); + } + copy.setPropertyList(new PropertyList(values)); + } + + private static final class PreservedValue extends Property { + private final Property original; + private final String accepted; + private String value; + + PreservedValue(Property original, String accepted, ParameterList parameters, String value) { + super(original.getName(), parameters); + this.original = original; + this.accepted = accepted; + this.value = value; + } + + @Override + public String getValue() { + return value; + } + + @Override + public void setValue(String value) { + if (!value.isEmpty() && !value.equals(accepted)) + throw new IllegalArgumentException("Report value must be original or omitted"); + this.value = value; + } + + @Override + public ValidationResult validate() { + if (value.equals(accepted)) return original.validate(); + var result = new ValidationResult(); + result + .getEntries() + .add( + new ValidationEntry( + "A projected omitted value is not a complete storage property", + ValidationEntry.Severity.ERROR, + getName())); + return result; + } + + @Override + protected PropertyFactory newFactory() { + return new PropertyFactory() { + @Override + public PreservedValue createProperty() { + return createProperty(new ParameterList(), ""); + } + + @Override + public PreservedValue createProperty(ParameterList parameters, String value) { + return new PreservedValue(original, accepted, parameters, value); + } + + @Override + public boolean supports(String name) { + return original.getName().equalsIgnoreCase(name); + } + }; + } + } +} diff --git a/src/main/java/org/busystack/caldav/calendar/query/CalendarTemporalCoverage.java b/src/main/java/org/busystack/caldav/calendar/query/CalendarTemporalCoverage.java new file mode 100644 index 0000000..94f1fea --- /dev/null +++ b/src/main/java/org/busystack/caldav/calendar/query/CalendarTemporalCoverage.java @@ -0,0 +1,78 @@ +package org.busystack.caldav.calendar.query; + +import java.time.*; +import net.fortuna.ical4j.model.*; +import net.fortuna.ical4j.model.property.*; +import org.busystack.caldav.calendar.CalendarContent; + +/** + * Coverage is recomputed from authoritative VTIMEZONE data on every parse, never trusted from a + * cache. + */ +public final class CalendarTemporalCoverage { + private CalendarTemporalCoverage() {} + + public static void check( + CalendarContent.Parsed parsed, TimeRange range, FloatingTimezone floating) { + checkEnd(parsed, range.end(), floating); + for (var component : parsed.calendar().getComponents()) checkComponent(parsed, component); + } + + public static void checkEnd( + CalendarContent.Parsed parsed, Instant end, FloatingTimezone floating) { + checkEnd(floating.coverageEnd(), end); + for (Instant bound : parsed.zoneCoverage().values()) checkEnd(bound, end); + } + + private static void checkEnd(Instant bound, Instant end) { + if (bound != null && (end == null || end.isAfter(bound))) throw QueryException.limit(); + } + + private static void checkComponent(CalendarContent.Parsed parsed, Component component) { + if (component.getName().equals(Component.VTIMEZONE)) return; + for (Property property : component.getProperties()) { + if (property instanceof DateProperty date) checkValue(parsed, date.getDate()); + else if (property instanceof RDate rdate && rdate.getPeriods().isPresent()) { + Instant bound = + property + .getParameter(Parameter.TZID) + .map(p -> parsed.zoneCoverage().get(p.getValue())) + .orElse(null); + for (var period : rdate.getPeriods().get()) { + checkPeriodValue(parsed, bound, period.getStart()); + if (period.getEnd() != null) checkPeriodValue(parsed, bound, period.getEnd()); + } + } else if (property instanceof DateListProperty dates) { + for (var value : dates.getDates()) checkValue(parsed, value); + } + } + if (component instanceof ComponentContainer nested) + for (var child : nested.getComponents()) checkComponent(parsed, child); + } + + private static void checkPeriodValue( + CalendarContent.Parsed parsed, Instant bound, java.time.temporal.Temporal value) { + checkValue(parsed, value); + if (bound != null + && value instanceof LocalDateTime local + && !local.toInstant(ZoneOffset.MIN).isBefore(bound)) throw QueryException.limit(); + } + + private static void checkValue(CalendarContent.Parsed parsed, java.time.temporal.Temporal value) { + checkTemporal( + value, + value instanceof ZonedDateTime date + ? parsed.zoneCoverage().get(date.getZone().getId()) + : null); + } + + public static void checkTemporal(java.time.temporal.Temporal value, Instant bound) { + if (value instanceof ZonedDateTime date) { + // Use the largest legal offset so an unresolved value beyond the horizon cannot + // acquire a misleading instant from ZoneRules' final fixed-offset tail. + if (bound != null + && (!date.toLocalDateTime().toInstant(ZoneOffset.MAX).isBefore(bound) + || !date.toInstant().isBefore(bound))) throw QueryException.limit(); + } + } +} diff --git a/src/main/java/org/busystack/caldav/calendar/query/FloatingTimezone.java b/src/main/java/org/busystack/caldav/calendar/query/FloatingTimezone.java new file mode 100644 index 0000000..e9bf944 --- /dev/null +++ b/src/main/java/org/busystack/caldav/calendar/query/FloatingTimezone.java @@ -0,0 +1,82 @@ +package org.busystack.caldav.calendar.query; + +import java.time.ZoneId; +import java.util.Objects; +import net.fortuna.ical4j.model.component.VTimeZone; + +/** Keeps a calendar-specific ical4j timezone registry alive for a report's whole evaluation. */ +public final class FloatingTimezone { + private final ZoneId zoneId; + private final BoundedTimeZoneRegistry registry; + + private FloatingTimezone(ZoneId zoneId, BoundedTimeZoneRegistry registry) { + this.zoneId = Objects.requireNonNull(zoneId); + this.registry = registry; + } + + public static FloatingTimezone of(ZoneId zone) { + return new FloatingTimezone(zone, null); + } + + public static FloatingTimezone from(VTimeZone component) { + validate(component); + var registry = new BoundedTimeZoneRegistry(); + var timezone = new net.fortuna.ical4j.model.TimeZone(component); + registry.register(timezone, false); + return new FloatingTimezone(registry.getZoneId(timezone.getID()), registry); + } + + public static FloatingTimezone parse(String content) { + if (content == null || content.getBytes(java.nio.charset.StandardCharsets.UTF_8).length > 65536) + throw QueryException.limit(); + org.busystack.caldav.calendar.CalendarTextProfile.validate(content); + try { + var registry = new BoundedTimeZoneRegistry(); + var result = + new java.util.concurrent.atomic.AtomicReference(); + var handler = + new org.busystack.caldav.calendar.parser.ParsingLimitsHandler( + new org.busystack.caldav.calendar.parser.StrictContentHandler(result::set, registry)); + org.busystack.caldav.calendar.parser.ParameterInput.parse( + org.busystack.caldav.calendar.CalendarParsingInput.reader(content), handler); + var calendar = result.get(); + if (calendar.getComponents().size() != 1 + || !(calendar.getComponents().getFirst() instanceof VTimeZone zone) + || calendar.validate().hasErrors()) + throw QueryException.invalid("A calendar timezone must contain exactly one VTIMEZONE"); + validate(zone); + var timezone = new net.fortuna.ical4j.model.TimeZone(zone); + return new FloatingTimezone(registry.getZoneId(timezone.getID()), registry); + } catch (org.busystack.caldav.calendar.CalendarValidationException invalid) { + throw invalidTimezone(); + } catch (java.io.IOException | net.fortuna.ical4j.data.ParserException ex) { + Throwable nested = ex; + for (int index = 0; index < 8 && nested != null; index++, nested = nested.getCause()) + if (nested instanceof QueryException query) throw query; + else if (nested instanceof org.busystack.caldav.calendar.CalendarValidationException) + throw invalidTimezone(); + throw QueryException.invalid("Malformed calendar timezone"); + } + } + + private static QueryException invalidTimezone() { + return new QueryException(QueryException.Kind.INVALID_DATA, "Calendar timezone is invalid"); + } + + private static void validate(VTimeZone component) { + try { + org.busystack.caldav.calendar.CalendarConstraints.validateTimezone(component); + } catch (org.busystack.caldav.calendar.CalendarValidationException invalid) { + throw new QueryException(QueryException.Kind.INVALID_DATA, "Calendar timezone is invalid"); + } + } + + public java.time.Instant coverageEnd() { + return registry == null ? null : registry.coverage().get(zoneId.getId()); + } + + public ZoneId zoneId() { + java.lang.ref.Reference.reachabilityFence(registry); + return zoneId; + } +} diff --git a/src/main/java/org/busystack/caldav/calendar/query/ObservanceZoneRules.java b/src/main/java/org/busystack/caldav/calendar/query/ObservanceZoneRules.java new file mode 100644 index 0000000..012a0ac --- /dev/null +++ b/src/main/java/org/busystack/caldav/calendar/query/ObservanceZoneRules.java @@ -0,0 +1,251 @@ +package org.busystack.caldav.calendar.query; + +import java.time.*; +import java.time.temporal.Temporal; +import java.time.zone.*; +import java.util.*; +import net.fortuna.ical4j.model.Property; +import net.fortuna.ical4j.model.Recur; +import net.fortuna.ical4j.model.component.Observance; +import net.fortuna.ical4j.model.component.VTimeZone; +import net.fortuna.ical4j.model.property.*; +import org.busystack.caldav.calendar.recurrence.WorkBudget; + +/** + * Application adaptation from chronological iCalendar observance onsets to Java zone rules. Only + * recurrence generation is delegated to iCal4j. A perpetual tail is proven against the complete + * Gregorian cycle after all finite eras and explicit dates, never selected by today's observance. + */ +final class ObservanceZoneRules { + record Result(ZoneRules rules, Instant coverageEnd) {} + + private record Onset(Instant instant, ZoneOffset from, ZoneOffset to, boolean standard) {} + + private record Tail(ZoneOffsetTransitionRule rule, boolean standard) {} + + private record History( + ZoneOffset base, + ZoneOffset standard, + ZoneOffset wall, + List standards, + List walls) {} + + private final WorkBudget budget; + + ObservanceZoneRules(WorkBudget budget) { + this.budget = budget; + } + + Result build( + VTimeZone zone, + Map finiteEnds, + LocalDateTime boundedEnd, + boolean annual) { + int finalEra = 1; + for (Observance observance : zone.getComponents()) { + finalEra = Math.max(finalEra, start(observance).getYear()); + for (var date : observance.getProperties(Property.RDATE)) + for (Temporal value : ((RDate) date).getDates()) + finalEra = Math.max(finalEra, ((LocalDateTime) value).getYear()); + for (var property : observance.getProperties(Property.RRULE)) { + var rule = ((RRule) property).getRecur(); + if (rule.getUntil() != null || rule.getCount() > 0) + finalEra = Math.max(finalEra, finiteEnds.get(observance).getYear()); + } + } + // The complete history ends after two whole years in the final recurrence regime. This is + // an era boundary, not a fixed extension of the old materialization window. + LocalDateTime tailStart = LocalDate.of(finalEra + 3, 1, 1).atStartOfDay(); + if (annual) { + History history = history(onsets(zone, finiteEnds, tailStart)); + List candidates = tails(zone, history.standard()); + List proven = + prove(candidates, history.wall(), tailStart.getYear()); + if (proven != null) + return new Result( + ZoneRules.of( + history.base(), history.base(), history.standards(), history.walls(), proven), + null); + } + History history = history(onsets(zone, finiteEnds, boundedEnd)); + return new Result( + ZoneRules.of( + history.base(), history.base(), history.standards(), history.walls(), List.of()), + boundedEnd.toInstant(ZoneOffset.MAX)); + } + + private List onsets( + VTimeZone zone, Map finiteEnds, LocalDateTime infiniteEnd) { + List result = new ArrayList<>(); + for (Observance observance : zone.getComponents()) { + LocalDateTime start = start(observance); + ZoneOffset from = + ((TzOffsetFrom) observance.getRequiredProperty(Property.TZOFFSETFROM)).getOffset(); + ZoneOffset to = + ((TzOffsetTo) observance.getRequiredProperty(Property.TZOFFSETTO)).getOffset(); + Set dates = new TreeSet<>(); + dates.add(start); + for (var property : observance.getProperties(Property.RDATE)) + for (Temporal date : ((RDate) property).getDates()) dates.add((LocalDateTime) date); + for (var property : observance.getProperties(Property.RRULE)) { + var recur = ((RRule) property).getRecur(); + LocalDateTime end = + recur.getUntil() != null || recur.getCount() > 0 + ? finiteEnds.get(observance) + : infiniteEnd; + budget.rule(recur, start, start, end); + @SuppressWarnings("unchecked") + var rule = (Recur) (Recur) recur; + try (var generated = + rule.getDatesAsStream( + start.atOffset(from), + start.atOffset(from), + end.atOffset(from), + budget.limits().maximumInstances() + 1)) { + generated + .filter(date -> date.toLocalDateTime().isBefore(end)) + .forEach( + date -> { + budget.instance(); + dates.add(date.toLocalDateTime()); + }); + } + } + for (LocalDateTime date : dates) { + budget.charge(1); + result.add( + new Onset(date.toInstant(from), from, to, observance.getName().equals("STANDARD"))); + } + } + result.sort(Comparator.comparing(Onset::instant)); + return result; + } + + private static History history(List onsets) { + if (onsets.isEmpty()) throw invalid("Timezone has no observance"); + ZoneOffset base = onsets.getFirst().from(), wall = base, standard = base; + List walls = new ArrayList<>(), standards = new ArrayList<>(); + Onset previous = null; + for (Onset onset : onsets) { + if (previous != null + && onset.instant().equals(previous.instant()) + && !onset.to().equals(previous.to())) throw invalid("Conflicting timezone onsets"); + if (onset.standard() && !standard.equals(onset.to())) { + standards.add( + ZoneOffsetTransition.of( + LocalDateTime.ofInstant(onset.instant(), standard), standard, onset.to())); + standard = onset.to(); + } + if (!wall.equals(onset.to())) { + walls.add( + ZoneOffsetTransition.of( + LocalDateTime.ofInstant(onset.instant(), wall), wall, onset.to())); + wall = onset.to(); + } + previous = onset; + } + return new History(base, standard, wall, List.copyOf(standards), List.copyOf(walls)); + } + + private static List tails(VTimeZone zone, ZoneOffset standard) { + List result = new ArrayList<>(); + for (Observance observance : zone.getComponents()) { + LocalDateTime start = start(observance); + ZoneOffset from = + ((TzOffsetFrom) observance.getRequiredProperty(Property.TZOFFSETFROM)).getOffset(); + ZoneOffset to = + ((TzOffsetTo) observance.getRequiredProperty(Property.TZOFFSETTO)).getOffset(); + for (var property : observance.getProperties(Property.RRULE)) { + var rule = ((RRule) property).getRecur(); + if (rule.getUntil() != null || rule.getCount() > 0 || from.equals(to)) continue; + Month month = + rule.getMonthList().isEmpty() + ? start.getMonth() + : Month.of(rule.getMonthList().getFirst().getMonthOfYear()); + int day = + rule.getMonthDayList().isEmpty() + ? start.getDayOfMonth() + : rule.getMonthDayList().getFirst(); + DayOfWeek weekday = null; + if (!rule.getDayList().isEmpty()) { + var byday = rule.getDayList().getFirst(); + int ordinal = byday.getOffset(); + day = ordinal > 0 ? 1 + 7 * (ordinal - 1) : -1 - 7 * (-ordinal - 1); + weekday = + switch (byday.getDay()) { + case MO -> DayOfWeek.MONDAY; + case TU -> DayOfWeek.TUESDAY; + case WE -> DayOfWeek.WEDNESDAY; + case TH -> DayOfWeek.THURSDAY; + case FR -> DayOfWeek.FRIDAY; + case SA -> DayOfWeek.SATURDAY; + case SU -> DayOfWeek.SUNDAY; + }; + } + var transition = + ZoneOffsetTransitionRule.of( + month, + day, + weekday, + start.toLocalTime(), + false, + ZoneOffsetTransitionRule.TimeDefinition.WALL, + standard, + from, + to); + result.add(new Tail(transition, observance.getName().equals("STANDARD"))); + } + } + return result; + } + + /** A Java lastRules array needs a stable chronological rule order and continuous offsets. */ + private List prove( + List candidates, ZoneOffset initial, int firstYear) { + if (candidates.size() > 16) return null; + List expected = null; + ZoneOffset wall = initial; + for (int year = firstYear; year <= firstYear + 400; year++) { + int currentYear = year; + List indexes = new ArrayList<>(); + for (int index = 0; index < candidates.size(); index++) indexes.add(index); + indexes.sort( + Comparator.comparing( + index -> candidates.get(index).rule().createTransition(currentYear).getInstant())); + List effective = new ArrayList<>(); + Instant previous = null; + ZoneOffset previousTo = null; + for (int index : indexes) { + budget.charge(1); + Tail candidate = candidates.get(index); + if (candidate.standard() + && !candidate.rule().getStandardOffset().equals(candidate.rule().getOffsetAfter())) + return null; + var transition = candidate.rule().createTransition(year); + if (previous != null + && previous.equals(transition.getInstant()) + && !previousTo.equals(transition.getOffsetAfter())) return null; + previous = transition.getInstant(); + previousTo = transition.getOffsetAfter(); + if (wall.equals(transition.getOffsetAfter())) continue; + if (!wall.equals(transition.getOffsetBefore())) return null; + wall = transition.getOffsetAfter(); + effective.add(index); + } + if (expected == null) expected = effective; + else if (!expected.equals(effective)) return null; + } + if (expected == null) return List.of(); + return expected.stream().map(index -> candidates.get(index).rule()).toList(); + } + + private static LocalDateTime start(Observance observance) { + var value = ((DtStart) observance.getRequiredProperty(Property.DTSTART)).getDate(); + if (!(value instanceof LocalDateTime date)) throw invalid("Timezone onset must be local"); + return date; + } + + private static QueryException invalid(String message) { + return new QueryException(QueryException.Kind.INVALID_DATA, message); + } +} diff --git a/src/main/java/org/busystack/caldav/calendar/query/QueryException.java b/src/main/java/org/busystack/caldav/calendar/query/QueryException.java new file mode 100644 index 0000000..8da5d3b --- /dev/null +++ b/src/main/java/org/busystack/caldav/calendar/query/QueryException.java @@ -0,0 +1,32 @@ +package org.busystack.caldav.calendar.query; + +/** Semantic failure; the protocol adapter maps the kind to the corresponding DAV error. */ +public final class QueryException extends RuntimeException { + public enum Kind { + INVALID_FILTER, + UNSUPPORTED_FILTER, + UNSUPPORTED_COLLATION, + LIMIT_EXCEEDED, + INVALID_DATA + } + + private final Kind kind; + + public QueryException(Kind kind, String message) { + super(message); + this.kind = kind; + } + + public Kind kind() { + return kind; + } + + public static QueryException limit() { + return new QueryException( + Kind.LIMIT_EXCEEDED, "Calendar query exceeds the supported work limit"); + } + + public static QueryException invalid(String message) { + return new QueryException(Kind.INVALID_FILTER, message); + } +} diff --git a/src/main/java/org/busystack/caldav/calendar/query/QueryLimits.java b/src/main/java/org/busystack/caldav/calendar/query/QueryLimits.java new file mode 100644 index 0000000..26e9e2a --- /dev/null +++ b/src/main/java/org/busystack/caldav/calendar/query/QueryLimits.java @@ -0,0 +1,22 @@ +package org.busystack.caldav.calendar.query; + +public record QueryLimits( + int maximumInstances, + int maximumRuleIncrements, + long maximumCandidateWork, + int maximumRangeDays, + int maximumFilterNodes) { + public QueryLimits { + if (maximumInstances < 1 + || maximumRuleIncrements < 1 + || maximumRuleIncrements > 900 + || maximumCandidateWork < 1 + || maximumRangeDays < 1 + || maximumFilterNodes < 1) + throw new IllegalArgumentException("Invalid calendar query limits"); + } + + public static QueryLimits defaults() { + return new QueryLimits(10000, 900, 500000, 3660, 256); + } +} diff --git a/src/main/java/org/busystack/caldav/calendar/query/TimeRange.java b/src/main/java/org/busystack/caldav/calendar/query/TimeRange.java new file mode 100644 index 0000000..f447bac --- /dev/null +++ b/src/main/java/org/busystack/caldav/calendar/query/TimeRange.java @@ -0,0 +1,35 @@ +package org.busystack.caldav.calendar.query; + +import java.time.Instant; + +/** UTC range; one absent endpoint denotes the corresponding infinity. */ +public record TimeRange(Instant start, Instant end) { + public TimeRange { + if (start == null && end == null || start != null && end != null && !start.isBefore(end)) + throw QueryException.invalid("A time range requires an ordered start or end"); + } + + public boolean startsBefore(Instant value) { + return start == null || start.isBefore(value); + } + + public boolean startsAtOrBefore(Instant value) { + return start == null || !start.isAfter(value); + } + + public boolean endsAfter(Instant value) { + return end == null || end.isAfter(value); + } + + public boolean endsAtOrAfter(Instant value) { + return end == null || !end.isBefore(value); + } + + public boolean contains(Instant point) { + return startsAtOrBefore(point) && endsAfter(point); + } + + public boolean overlaps(Instant first, Instant last) { + return first.equals(last) ? contains(first) : startsBefore(last) && endsAfter(first); + } +} diff --git a/src/main/java/org/busystack/caldav/calendar/query/TimeRangeMatcher.java b/src/main/java/org/busystack/caldav/calendar/query/TimeRangeMatcher.java new file mode 100644 index 0000000..9e59503 --- /dev/null +++ b/src/main/java/org/busystack/caldav/calendar/query/TimeRangeMatcher.java @@ -0,0 +1,38 @@ +package org.busystack.caldav.calendar.query; + +import static net.fortuna.ical4j.model.Property.*; + +import java.time.*; +import net.fortuna.ical4j.model.Component; +import org.busystack.caldav.calendar.recurrence.Occurrence; + +/** RFC 4791 section 9.9 component tables, including their intentionally different boundaries. */ +public final class TimeRangeMatcher { + private TimeRangeMatcher() {} + + public static boolean matches(Occurrence occurrence, TimeRange range, ZoneId zone) { + Instant start = occurrence.instant(DTSTART, zone), end = occurrence.instant(DTEND, zone); + if (Component.VEVENT.equals(occurrence.component().getName())) + return start != null && range.overlaps(start, end == null ? start : end); + if (!Component.VTODO.equals(occurrence.component().getName())) + throw new QueryException( + QueryException.Kind.UNSUPPORTED_FILTER, "Time range is undefined for this component"); + Instant due = occurrence.instant(DUE, zone), + completed = occurrence.instant(COMPLETED, zone), + created = occurrence.instant(CREATED, zone); + if (start != null && occurrence.hasDuration()) + return range.startsAtOrBefore(due) && (range.endsAfter(start) || range.endsAtOrAfter(due)); + if (start != null && due != null) + return (range.startsBefore(due) || range.startsAtOrBefore(start)) + && (range.endsAfter(start) || range.endsAtOrAfter(due)); + if (start != null) return range.contains(start); + if (due != null) return range.startsBefore(due) && range.endsAtOrAfter(due); + if (completed != null && created != null) + return (range.startsAtOrBefore(created) || range.startsAtOrBefore(completed)) + && (range.endsAtOrAfter(created) || range.endsAtOrAfter(completed)); + if (completed != null) + return range.startsAtOrBefore(completed) && range.endsAtOrAfter(completed); + if (created != null) return range.endsAfter(created); + return true; + } +} diff --git a/src/main/java/org/busystack/caldav/calendar/recurrence/AlarmCalculator.java b/src/main/java/org/busystack/caldav/calendar/recurrence/AlarmCalculator.java new file mode 100644 index 0000000..376904e --- /dev/null +++ b/src/main/java/org/busystack/caldav/calendar/recurrence/AlarmCalculator.java @@ -0,0 +1,64 @@ +package org.busystack.caldav.calendar.recurrence; + +import static net.fortuna.ical4j.model.Property.*; + +import java.time.*; +import java.time.temporal.Temporal; +import java.util.*; +import net.fortuna.ical4j.model.*; +import net.fortuna.ical4j.model.property.Repeat; +import net.fortuna.ical4j.model.property.Trigger; +import org.busystack.caldav.calendar.CalendarContent; +import org.busystack.caldav.calendar.query.*; + +/** Calculates stored alarm trigger times only; no notifications or reminder execution. */ +public final class AlarmCalculator { + private AlarmCalculator() {} + + public static List triggers( + CalendarContent.Parsed parsed, + Component alarm, + Occurrence owner, + ZoneId zone, + WorkBudget budget) { + Trigger trigger = alarm.getRequiredProperty(TRIGGER); + Temporal first; + if (trigger.isAbsolute()) first = trigger.getDate(); + else { + boolean endRelated = + trigger + .getParameter(Parameter.RELATED) + .map(p -> p.getValue().equalsIgnoreCase("END")) + .orElse(false); + first = + owner + .dates() + .get( + endRelated + ? (Component.VTODO.equals(owner.component().getName()) ? DUE : DTEND) + : DTSTART); + if (first == null) + throw new QueryException( + QueryException.Kind.INVALID_DATA, "Relative alarm has no effective anchor"); + first = TemporalSupport.duration(parsed, trigger).addTo(first, zone); + } + int repeat = alarm.getProperty(REPEAT).map(p -> ((Repeat) p).getCount()).orElse(0); + if (repeat < 0 || repeat > budget.limits().maximumInstances()) throw QueryException.limit(); + var result = new ArrayList(); + for (int index = 0; index <= repeat; index++) { + budget.charge(1); + result.add(TemporalSupport.instant(first, zone)); + if (index < repeat) { + var duration = + alarm + .getProperty(DURATION) + .orElseThrow( + () -> + new QueryException( + QueryException.Kind.INVALID_DATA, "Repeated alarm lacks DURATION")); + first = TemporalSupport.duration(parsed, duration).addTo(first, zone); + } + } + return result; + } +} diff --git a/src/main/java/org/busystack/caldav/calendar/recurrence/CalendarRecurrence.java b/src/main/java/org/busystack/caldav/calendar/recurrence/CalendarRecurrence.java new file mode 100644 index 0000000..2c07a06 --- /dev/null +++ b/src/main/java/org/busystack/caldav/calendar/recurrence/CalendarRecurrence.java @@ -0,0 +1,239 @@ +package org.busystack.caldav.calendar.recurrence; + +import static net.fortuna.ical4j.model.Property.*; + +import java.time.*; +import java.time.temporal.Temporal; +import java.util.*; +import net.fortuna.ical4j.model.*; +import net.fortuna.ical4j.model.property.*; +import org.busystack.caldav.calendar.CalendarContent; +import org.busystack.caldav.calendar.query.*; + +/** Report-only copies; cumulative work belongs to one projection/REPORT instance. */ +public final class CalendarRecurrence { + private final WorkBudget budget; + private final RecurrenceExpander recurrence; + private static final long MAX_REPORT_BYTES = CalendarReportSize.MAX_REPORT_BYTES; + private long reportBytes; + private final Map outputEstimates = new IdentityHashMap<>(); + + public CalendarRecurrence(QueryLimits limits) { + budget = new WorkBudget(limits); + recurrence = new RecurrenceExpander(budget); + } + + public net.fortuna.ical4j.model.Calendar expand( + CalendarContent.Parsed parsed, TimeRange range, FloatingTimezone timezone) { + finite(range); + var result = CalendarReports.copy(parsed); + var components = new ArrayList(); + var zones = new HashMap(); + for (var occurrence : recurrence.occurrences(parsed, range, timezone)) { + if (!TimeRangeMatcher.matches(occurrence, range, timezone.zoneId())) continue; + reserveOutput(parsed, occurrence.component()); + var copy = CalendarReports.componentCopy(parsed, occurrence.component()); + copy.removeAll(RRULE, RDATE, EXDATE, EXRULE, RECURRENCE_ID, DURATION); + for (var field : occurrence.dates().entrySet()) { + if (field.getKey().equals(RECURRENCE_ID)) continue; + Temporal value = reportValue(field.getValue()); + Property property = + switch (field.getKey()) { + case DTSTART -> new DtStart<>(parameters(occurrence.component(), DTSTART), value); + case DTEND -> new DtEnd<>(parameters(occurrence.component(), DTEND), value); + case DUE -> new Due<>(parameters(occurrence.component(), DUE), value); + default -> null; + }; + if (property != null) copy.replace(property); + } + if (occurrence.recurrenceId() != null) + copy.add( + new RecurrenceId<>( + parameters(occurrence.component(), RECURRENCE_ID), + reportValue(occurrence.recurrenceId()))); + expandAlarms(parsed, occurrence, copy, timezone); + normalizeTimezones(parsed, copy, zones); + components.add((net.fortuna.ical4j.model.component.CalendarComponent) copy); + } + result.setComponentList(new ComponentList<>(components)); + java.lang.ref.Reference.reachabilityFence(timezone); + return result; + } + + public net.fortuna.ical4j.model.Calendar limitRecurrence( + CalendarContent.Parsed parsed, TimeRange range, FloatingTimezone timezone) { + finite(range); + Component master = + parsed.calendar().getComponents().stream() + .filter( + c -> + !c.getName().equals(Component.VTIMEZONE) + && c.getProperty(RECURRENCE_ID).isEmpty()) + .findFirst() + .orElse(null); + Set keep = Collections.newSetFromMap(new IdentityHashMap<>()); + for (var occurrence : recurrence.occurrences(parsed, range, timezone)) { + var c = occurrence.component(); + if (c.getProperty(RECURRENCE_ID).isEmpty()) continue; + boolean current = TimeRangeMatcher.matches(occurrence, range, timezone.zoneId()); + boolean original = false; + if (master != null && occurrence.recurrenceId() != null) + original = + TimeRangeMatcher.matches( + RecurrenceExpander.single( + parsed, + master, + occurrence.recurrenceId(), + occurrence.recurrenceId(), + timezone.zoneId()), + range, + timezone.zoneId()); + if (current || original) keep.add(c); + } + var result = CalendarReports.copy(parsed); + var retained = new ArrayList(); + var originals = parsed.calendar().getComponents(); + for (int i = 0; i < originals.size(); i++) { + var original = originals.get(i); + if (original.getName().equals(Component.VTIMEZONE) + || original.getProperty(RECURRENCE_ID).isEmpty() + || keep.contains(original)) retained.add(result.getComponents().get(i)); + } + result.setComponentList(new ComponentList<>(retained)); + java.lang.ref.Reference.reachabilityFence(timezone); + return result; + } + + private void reserveOutput(CalendarContent.Parsed parsed, Component component) { + long estimate = + outputEstimates.computeIfAbsent( + component, value -> Math.addExact(CalendarReportSize.estimate(parsed, value), 1024L)); + if (estimate > MAX_REPORT_BYTES - reportBytes) throw QueryException.limit(); + reportBytes += estimate; + } + + private void normalizeTimezones( + CalendarContent.Parsed parsed, Component component, Map zones) { + var properties = new ArrayList(); + for (Property property : component.getProperties()) { + var id = property.getParameter(Parameter.TZID); + if (id.isEmpty()) { + properties.add(property); + continue; + } + budget.charge(1); + var zone = + zones.computeIfAbsent( + id.get().getValue(), + key -> { + for (var definition : parsed.calendar().getComponents(Component.VTIMEZONE)) + if (definition.getRequiredProperty(TZID).getValue().equals(key)) + return FloatingTimezone.from( + (net.fortuna.ical4j.model.component.VTimeZone) definition); + return FloatingTimezone.of( + net.fortuna.ical4j.model.TimeZoneRegistry.getGlobalZoneId(key)); + }); + String type = + property + .getParameter(Parameter.VALUE) + .map(p -> p.getValue().toUpperCase(Locale.ROOT)) + .orElse(property instanceof DateProperty ? "DATE-TIME" : "TEXT"); + var values = new ArrayList(); + if (type.equals("DATE-TIME")) { + for (String value : property.getValue().split(",", -1)) values.add(utcValue(value, zone)); + } else if (type.equals("PERIOD")) { + for (String value : parsed.lexicalValue(property).split(",", -1)) { + var period = net.fortuna.ical4j.model.Period.parse(value); + Temporal start = period.getStart(); + if (start instanceof LocalDateTime local) start = local.atZone(zone.zoneId()); + String tail = value.substring(value.indexOf('/') + 1); + Temporal end = + tail.startsWith("P") || tail.startsWith("+P") + ? TemporalSupport.duration(tail).addTo(start, zone.zoneId()) + : period.getEnd(); + if (end instanceof LocalDateTime local) end = local.atZone(zone.zoneId()); + CalendarTemporalCoverage.checkTemporal(start, zone.coverageEnd()); + CalendarTemporalCoverage.checkTemporal(end, zone.coverageEnd()); + values.add( + new TemporalAdapter<>(TemporalSupport.instant(start, zone.zoneId())) + + "/" + + new TemporalAdapter<>(TemporalSupport.instant(end, zone.zoneId()))); + } + } else + throw new QueryException( + QueryException.Kind.UNSUPPORTED_FILTER, + "Cannot expand an untyped timezone-qualified extension value"); + var parameters = + new ParameterList( + property.getParameterList().getAll().stream() + .filter(p -> !p.getName().equals(Parameter.TZID)) + .toList()); + var converted = new XProperty(property.getName(), parameters, String.join(",", values)); + converted.setPrefix(property.getPrefix()); + properties.add(converted); + } + component.setPropertyList(new PropertyList(properties)); + if (component instanceof ComponentContainer nested) + for (var child : nested.getComponents()) normalizeTimezones(parsed, child, zones); + } + + private static String utcValue(String value, FloatingTimezone zone) { + var date = TemporalAdapter.parse(value, zone.zoneId()).getTemporal(); + CalendarTemporalCoverage.checkTemporal(date, zone.coverageEnd()); + return new TemporalAdapter<>(date.toInstant()).toString(); + } + + private static ParameterList parameters(Component component, String name) { + var property = component.getProperty(name); + return new ParameterList( + property.stream() + .flatMap(p -> p.getParameterList().getAll().stream()) + .filter( + p -> + !Set.of(Parameter.TZID, Parameter.VALUE, Parameter.RANGE).contains(p.getName())) + .toList()); + } + + @SuppressWarnings("unchecked") + private void expandAlarms( + CalendarContent.Parsed parsed, + Occurrence occurrence, + Component copy, + FloatingTimezone timezone) { + if (!(occurrence.component() instanceof ComponentContainer original) + || !(copy instanceof ComponentContainer destination)) return; + var expanded = new ArrayList(); + for (Component child : original.getComponents()) { + if (!child.getName().equals(Component.VALARM)) { + expanded.add(CalendarReports.componentCopy(parsed, child)); + continue; + } + // Parent DTSTART becomes UTC in an expanded report. Absolute alarm instances retain + // the original nominal-day/DST trigger instants after that representation change. + for (Instant instant : + AlarmCalculator.triggers(parsed, child, occurrence, timezone.zoneId(), budget)) { + budget.instance(); + reserveOutput(parsed, child); + var alarm = CalendarReports.componentCopy(parsed, child); + alarm.removeAll(TRIGGER, REPEAT, DURATION); + var trigger = new Trigger(instant); + for (var parameter : parameters(child, TRIGGER).getAll()) + if (!parameter.getName().equals(Parameter.RELATED)) trigger.add(parameter); + alarm.add(trigger); + expanded.add(alarm); + } + } + ((ComponentContainer) destination).setComponentList(new ComponentList<>(expanded)); + } + + private static Temporal reportValue(Temporal value) { + return value instanceof ZonedDateTime z + ? z.toInstant() + : value instanceof OffsetDateTime o ? o.toInstant() : value; + } + + private static void finite(TimeRange range) { + if (range.start() == null || range.end() == null) + throw QueryException.invalid("Calendar-data selectors require both time boundaries"); + } +} diff --git a/src/main/java/org/busystack/caldav/calendar/recurrence/Occurrence.java b/src/main/java/org/busystack/caldav/calendar/recurrence/Occurrence.java new file mode 100644 index 0000000..05e3d40 --- /dev/null +++ b/src/main/java/org/busystack/caldav/calendar/recurrence/Occurrence.java @@ -0,0 +1,25 @@ +package org.busystack.caldav.calendar.recurrence; + +import java.time.Instant; +import java.time.ZoneId; +import java.time.temporal.Temporal; +import java.util.Map; +import net.fortuna.ical4j.model.Component; + +/** Read-only view of one effective recurrence; the authoritative component is never modified. */ +public record Occurrence( + Component component, + Temporal recurrenceId, + Map dates, + boolean hasDuration, + Temporal originalStart, + Temporal originalEnd) { + public Occurrence { + dates = Map.copyOf(dates); + } + + public Instant instant(String name, ZoneId floating) { + Temporal value = dates.get(name); + return value == null ? null : TemporalSupport.instant(value, floating); + } +} diff --git a/src/main/java/org/busystack/caldav/calendar/recurrence/RecurrenceExpander.java b/src/main/java/org/busystack/caldav/calendar/recurrence/RecurrenceExpander.java new file mode 100644 index 0000000..dd7e84a --- /dev/null +++ b/src/main/java/org/busystack/caldav/calendar/recurrence/RecurrenceExpander.java @@ -0,0 +1,488 @@ +package org.busystack.caldav.calendar.recurrence; + +import static net.fortuna.ical4j.model.Property.*; + +import java.time.*; +import java.time.Duration; +import java.time.temporal.*; +import java.util.*; +import net.fortuna.ical4j.model.*; +import net.fortuna.ical4j.model.property.*; +import org.busystack.caldav.calendar.CalendarContent; +import org.busystack.caldav.calendar.query.*; + +/** + * iCal4j generates RRULE dates; this layer applies exclusions, detached revisions and report + * bounds. + */ +public final class RecurrenceExpander { + private final WorkBudget budget; + + public RecurrenceExpander(WorkBudget budget) { + this.budget = budget; + } + + public List occurrences( + CalendarContent.Parsed parsed, TimeRange requested, FloatingTimezone timezone) { + return occurrences(parsed, requested, timezone, false); + } + + public List occurrences( + CalendarContent.Parsed parsed, + TimeRange requested, + FloatingTimezone timezone, + boolean includeAlarms) { + try { + CalendarTemporalCoverage.check(parsed, requested, timezone); + return expand(parsed, requested, timezone.zoneId(), includeAlarms, timezone.coverageEnd()); + } catch (java.time.DateTimeException | ArithmeticException ex) { + throw QueryException.limit(); + } finally { + java.lang.ref.Reference.reachabilityFence(timezone); + } + } + + private List expand( + CalendarContent.Parsed parsed, + TimeRange requested, + ZoneId zone, + boolean includeAlarms, + Instant floatingEnd) { + List components = new ArrayList<>(); + for (var c : parsed.calendar().getComponents()) + if (!Component.VTIMEZONE.equals(c.getName())) components.add(c); + Component master = + components.stream() + .filter(c -> c.getProperty(RECURRENCE_ID).isEmpty()) + .findFirst() + .orElse(null); + if (master == null) { + var result = new ArrayList(); + for (Component c : components) { + budget.charge(1); + result.add(single(parsed, c, null, null, zone)); + } + return result; + } + Temporal seed = TemporalSupport.date(master, DTSTART); + boolean recurring = + !master.getProperties(RRULE).isEmpty() || !master.getProperties(RDATE).isEmpty(); + if (!recurring) { + var result = new ArrayList(); + // Existing components are consumed one resource at a time. They spend shared candidate + // work; generated recurrence dates and retained report results have separate instance bounds. + budget.charge(1); + result.add(single(parsed, master, null, null, zone)); + for (Component c : components) + if (c != master) { + budget.charge(1); + result.add(single(parsed, c, null, null, zone)); + } + return result; + } + if (seed == null) + throw new QueryException( + QueryException.Kind.INVALID_DATA, "Recurrence requires an explicit DTSTART anchor"); + Map overrides = new HashMap<>(); + List ranges = new ArrayList<>(); + for (Component c : components) + if (c != master) { + Temporal id = TemporalSupport.date(c, RECURRENCE_ID); + if (id == null || overrides.put(key(id, seed, zone), c) != null) + throw new QueryException( + QueryException.Kind.INVALID_DATA, "Ambiguous recurrence override"); + var rangeParameter = c.getRequiredProperty(RECURRENCE_ID).getParameter(Parameter.RANGE); + if (rangeParameter.isPresent()) { + if (!rangeParameter.get().getValue().equalsIgnoreCase("THISANDFUTURE")) + throw new QueryException( + QueryException.Kind.INVALID_DATA, "Unsupported recurrence range value"); + ranges.add(c); + } + } + ranges.sort( + Comparator.comparing( + c -> TemporalSupport.instant(TemporalSupport.date(c, RECURRENCE_ID), zone))); + Duration padding = padding(parsed, components, zone, includeAlarms); + Instant first = + requested.start() == null + ? TemporalSupport.instant(seed, zone) + : requested.start().minus(padding); + Instant last = + requested.end() == null + ? first.plus(Duration.ofDays(budget.limits().maximumRangeDays())) + : requested.end().plus(padding); + // Explicit RDATEs and detached instances may precede DTSTART; only skip the rule search. + boolean searchRules = first.isBefore(last); + if (Duration.between(first, last) + .compareTo( + Duration.ofDays(budget.limits().maximumRangeDays() + 2L) + .plus(padding.multipliedBy(2))) + > 0) throw QueryException.limit(); + if (floatingEnd != null && last.isAfter(floatingEnd)) throw QueryException.limit(); + for (var bound : parsed.zoneCoverage().values()) + if (last.isAfter(bound)) throw QueryException.limit(); + Temporal[] bounds = ruleBounds(first, last, seed, zone); + Temporal nativeFirst = bounds[0]; + Temporal nativeLast = bounds[1]; + var dates = new LinkedHashMap(); + dates.put(key(seed, seed, zone), seed); + var periodEnds = new HashMap(); + for (var property : master.getProperties(RDATE)) { + RDate rdate = (RDate) property; + if (rdate.getPeriods().isPresent()) { + // PERIOD has only comma-separated start/end-or-duration values. The established parser + // validates each value; retain its lexical duration to distinguish P1D from PT24H. + for (String rawPeriod : parsed.lexicalValue(property).split(",", -1)) { + var period = net.fortuna.ical4j.model.Period.parse(rawPeriod); + budget.charge(1); + Temporal value = period.getStart(); + if (value instanceof LocalDateTime && property.getParameter(Parameter.TZID).isPresent()) + value = ((LocalDateTime) value).atZone(zoneFor(parsed, property, zone)); + Object id = key(value, seed, zone); + dates.put(id, value); + String endValue = rawPeriod.substring(rawPeriod.indexOf('/') + 1); + Temporal end = + endValue.startsWith("P") || endValue.startsWith("+P") + ? TemporalSupport.duration(endValue).addTo(value, zone) + : period.getEnd(); + if (end instanceof LocalDateTime && property.getParameter(Parameter.TZID).isPresent()) + end = ((LocalDateTime) end).atZone(zoneFor(parsed, property, zone)); + periodEnds.put(id, end); + } + } else + for (Temporal value : rdate.getDates()) { + budget.charge(1); + dates.put(key(value, seed, zone), value); + } + } + if (master.getProperties(RRULE).size() > 1) + throw new QueryException( + QueryException.Kind.INVALID_DATA, "Multiple RRULE recurrence sets are undefined"); + for (var property : searchRules ? master.getProperties(RRULE) : List.of()) { + Recur original = ((RRule) property).getRecur(); + Temporal localSeed = nativeRange(TemporalSupport.instant(seed, zone), seed, zone); + if (seed instanceof LocalDateTime || seed instanceof LocalDate) localSeed = seed; + Temporal lower = original.getCount() > 0 ? localSeed : nativeFirst; + @SuppressWarnings("unchecked") + Recur copied = new Recur<>((String) property.getValue()); + Temporal until = original.getUntil(); + if (until != null) { + if (localSeed instanceof LocalDate) { + if (!(until instanceof LocalDate)) + throw new QueryException( + QueryException.Kind.INVALID_DATA, "Recurrence UNTIL type differs from DTSTART"); + } else if (!(until instanceof LocalDateTime) + || seed instanceof ZonedDateTime + || seed instanceof OffsetDateTime + || seed instanceof Instant) { + until = nativeRange(TemporalSupport.instant(until, zone), seed, zone); + } + } + if (until != null + && net.fortuna.ical4j.model.TemporalComparator.INSTANCE.compare(until, lower) < 0) + continue; + Recur rule = new Recur.Builder<>(copied).count(null).until(until).build(); + int maximum = budget.limits().maximumRuleIncrements() - 5; + int increments = + original.getCount() > 0 + ? Math.min(maximum, (int) Math.min(maximum, 2L * original.getCount() + 8)) + : maximum; + while (true) { + Temporal upper = nativeLast; + boolean capped = false; + if (original.getCount() > 0) { + Temporal cap = + localSeed + .plus( + Math.multiplyExact((long) Math.max(1, original.getInterval()), increments), + frequencyUnit(original)) + .plus(1, localSeed instanceof LocalDate ? ChronoUnit.DAYS : ChronoUnit.NANOS); + if (net.fortuna.ical4j.model.TemporalComparator.INSTANCE.compare(cap, nativeLast) < 0) { + upper = cap; + capped = true; + } + } + budget.rule(original, localSeed, lower, upper); + List generated; + try (var stream = + rule.getDatesAsStream( + localSeed, lower, upper, budget.limits().maximumInstances() + 1)) { + // iCal4j's spliterator may advance without emitting pre-window candidates. + // Keep its stream pipeline rather than adapting it to an Iterator. + generated = + stream + .peek(candidate -> budget.instance()) + .map(candidate -> resolveCandidate(candidate, seed, zone)) + .limit(original.getCount() > 0 ? original.getCount() : Long.MAX_VALUE) + .toList(); + } + if (!capped || generated.size() >= original.getCount()) { + for (var resolved : generated) dates.putIfAbsent(key(resolved, seed, zone), resolved); + break; + } + if (increments == maximum) throw QueryException.limit(); + increments = Math.min(maximum, increments * 2); + } + } + Set excluded = new HashSet<>(); + for (var property : master.getProperties(EXDATE)) + for (Temporal value : ((DateListProperty) property).getDates()) + excluded.add(key(value, seed, zone)); + dates.keySet().removeAll(excluded); + var result = new ArrayList(); + for (var entry : dates.entrySet()) { + if (overrides.containsKey(entry.getKey())) continue; + Temporal original = entry.getValue(); + Component range = null; + for (Component candidate : ranges) { + Temporal rid = TemporalSupport.date(candidate, RECURRENCE_ID); + if (TemporalSupport.instant(rid, zone).isAfter(TemporalSupport.instant(original, zone))) + break; + range = candidate; + } + Temporal shifted = + range == null + ? original + : shifted( + original, + TemporalSupport.date(range, RECURRENCE_ID), + TemporalSupport.date(range, DTSTART), + zone); + Occurrence occurrence = + single(parsed, range == null ? master : range, shifted, original, zone); + if (range == null && periodEnds.containsKey(entry.getKey())) { + var effective = new HashMap<>(occurrence.dates()); + effective.put( + Component.VTODO.equals(master.getName()) ? DUE : DTEND, periodEnds.get(entry.getKey())); + occurrence = + new Occurrence( + master, original, effective, true, original, periodEnds.get(entry.getKey())); + } + result.add(occurrence); + if (result.size() > budget.limits().maximumInstances()) throw QueryException.limit(); + } + for (Component override : overrides.values()) { + budget.instance(); + result.add( + single(parsed, override, null, TemporalSupport.date(override, RECURRENCE_ID), zone)); + } + if (requested.end() == null + && master.getProperties(RRULE).stream() + .map(p -> ((RRule) p).getRecur()) + .anyMatch(r -> r.getUntil() == null && r.getCount() <= 0)) { + // The caller can return true after a match. A negative answer beyond this horizon is not + // proven. + result.add(new Occurrence(new UnknownFuture(), null, Map.of(), false, null, null)); + } + return result; + } + + public static boolean unknownFuture(Occurrence occurrence) { + return occurrence.component() instanceof UnknownFuture; + } + + private static final class UnknownFuture extends net.fortuna.ical4j.model.component.XComponent { + UnknownFuture() { + super("X-BUSYSTACK-QUERY-LIMIT"); + } + } + + public static Occurrence single( + CalendarContent.Parsed parsed, + Component component, + Temporal occurrenceStart, + Temporal recurrenceId, + ZoneId zone) { + var fields = new HashMap(); + for (var p : component.getProperties()) + if (p instanceof DateProperty d && d.getDate() != null) + fields.put(p.getName(), d.getDate()); + Temporal start = TemporalSupport.date(component, DTSTART); + Temporal anchor = occurrenceStart == null ? start : occurrenceStart; + boolean duration = component.getProperty(DURATION).isPresent(); + if (anchor != null) { + fields.put(DTSTART, anchor); + for (String name : List.of(DTEND, DUE)) { + Temporal end = TemporalSupport.date(component, name); + if (end != null) fields.put(name, TemporalSupport.exactEnd(start, end, anchor, zone)); + } + if (duration) { + var p = component.getRequiredProperty(DURATION); + fields.put( + Component.VTODO.equals(component.getName()) ? DUE : DTEND, + TemporalSupport.duration(parsed, p).addTo(anchor, zone)); + } else if (Component.VEVENT.equals(component.getName()) && !fields.containsKey(DTEND)) + fields.put(DTEND, anchor instanceof LocalDate d ? d.plusDays(1) : anchor); + } + Temporal originalStart = recurrenceId == null ? start : recurrenceId; + Temporal originalEnd = fields.get(Component.VTODO.equals(component.getName()) ? DUE : DTEND); + return new Occurrence(component, recurrenceId, fields, duration, originalStart, originalEnd); + } + + private static ChronoUnit frequencyUnit(Recur rule) { + return switch (rule.getFrequency()) { + case SECONDLY -> ChronoUnit.SECONDS; + case MINUTELY -> ChronoUnit.MINUTES; + case HOURLY -> ChronoUnit.HOURS; + case DAILY -> ChronoUnit.DAYS; + case WEEKLY -> ChronoUnit.WEEKS; + case MONTHLY -> ChronoUnit.MONTHS; + case YEARLY -> ChronoUnit.YEARS; + }; + } + + private Temporal[] ruleBounds(Instant first, Instant last, Temporal seed, ZoneId floating) { + ZoneId zone = + seed instanceof ZonedDateTime z + ? z.getZone() + : seed instanceof OffsetDateTime o + ? o.getOffset() + : seed instanceof Instant ? ZoneOffset.UTC : floating; + var rules = zone.getRules(); + int smallest = rules.getOffset(first).getTotalSeconds(), largest = smallest; + var transition = rules.nextTransition(first.minusNanos(1)); + while (transition != null && !transition.getInstant().isAfter(last)) { + budget.charge(1); + smallest = Math.min(smallest, transition.getOffsetAfter().getTotalSeconds()); + largest = Math.max(largest, transition.getOffsetAfter().getTotalSeconds()); + transition = rules.nextTransition(transition.getInstant()); + } + // Across an overlap, local start/end can run backwards. Search a conservative local + // interval using every offset in this request, then apply exact instant predicates. + var lower = LocalDateTime.ofInstant(first, ZoneOffset.ofTotalSeconds(smallest)); + var upper = LocalDateTime.ofInstant(last, ZoneOffset.ofTotalSeconds(largest)); + return seed instanceof LocalDate + ? new Temporal[] {lower.toLocalDate(), upper.toLocalDate()} + : new Temporal[] {lower, upper}; + } + + private static Temporal nativeRange(Instant value, Temporal seed, ZoneId zone) { + if (seed instanceof LocalDate) return value.atZone(zone).toLocalDate(); + ZoneId actual = + seed instanceof ZonedDateTime z + ? z.getZone() + : seed instanceof OffsetDateTime o + ? o.getOffset() + : seed instanceof Instant ? ZoneOffset.UTC : zone; + return value.atZone(actual).toLocalDateTime(); + } + + private static Temporal resolveCandidate(Temporal candidate, Temporal seed, ZoneId zone) { + if (seed instanceof LocalDate || seed instanceof LocalDateTime) return candidate; + LocalDateTime local = (LocalDateTime) candidate; + if (seed instanceof ZonedDateTime z) { + // RFC5545 verified erratum4271 applies the pre-gap offset to nonexistent + // local times. Java atZone shifts the gap by the same amount, preserving + // that instant; the recurrence still counts toward COUNT. + return local.atZone(z.getZone()); + } + if (seed instanceof OffsetDateTime o) return local.atOffset(o.getOffset()); + return local.toInstant(ZoneOffset.UTC); + } + + private static Object key(Temporal value, Temporal seed, ZoneId zone) { + if (seed instanceof LocalDate) { + if (!(value instanceof LocalDate)) + throw new QueryException(QueryException.Kind.INVALID_DATA, "Recurrence DATE types differ"); + return value; + } + if (seed instanceof LocalDateTime) + return value instanceof LocalDateTime + ? value + : TemporalSupport.zoned(value, zone).toLocalDateTime(); + return TemporalSupport.instant(value, zone); + } + + private static Temporal shifted(Temporal value, Temporal rid, Temporal replacement, ZoneId zone) { + if (replacement == null) return value; + if (value instanceof LocalDate d + && rid instanceof LocalDate r + && replacement instanceof LocalDate s) return d.plusDays(ChronoUnit.DAYS.between(r, s)); + LocalDateTime originalLocal = TemporalSupport.zoned(rid, zone).toLocalDateTime(); + LocalDateTime replacementLocal = TemporalSupport.zoned(replacement, zone).toLocalDateTime(); + Duration difference = Duration.between(originalLocal, replacementLocal); + if (value instanceof LocalDateTime l) return l.plus(difference); + ZonedDateTime original = TemporalSupport.zoned(value, zone); + ZonedDateTime result = original.toLocalDateTime().plus(difference).atZone(original.getZone()); + if (value instanceof OffsetDateTime) return result.toOffsetDateTime(); + if (value instanceof Instant) return result.toInstant(); + return result; + } + + private static Duration padding( + CalendarContent.Parsed parsed, + List components, + ZoneId zone, + boolean includeAlarms) { + Duration max = Duration.ZERO; + for (Component c : components) { + Temporal start = TemporalSupport.date(c, DTSTART); + if (start == null) continue; + for (String name : List.of(DTEND, DUE, RECURRENCE_ID)) { + Temporal end = TemporalSupport.date(c, name); + if (end != null) { + Duration reach = + start instanceof LocalDate a && end instanceof LocalDate b + ? durationBound( + new TemporalSupport.CalendarDuration( + ChronoUnit.DAYS.between(a, b), Duration.ZERO)) + : Duration.between( + TemporalSupport.instant(start, zone), TemporalSupport.instant(end, zone)) + .abs(); + if (reach.compareTo(max) > 0) max = reach; + } + } + if (start instanceof LocalDate && c.getName().equals(Component.VEVENT)) + max = max.compareTo(Duration.ofHours(60)) < 0 ? Duration.ofHours(60) : max; + if (c.getProperty(DURATION).isPresent()) { + Duration d = + durationBound(TemporalSupport.duration(parsed, c.getRequiredProperty(DURATION))); + if (d.compareTo(max) > 0) max = d; + } + } + if (includeAlarms) { + Duration alarmMax = Duration.ZERO; + for (Component c : components) + if (c instanceof ComponentContainer nested) + for (Component alarm : nested.getComponents()) { + if (!alarm.getName().equals(Component.VALARM)) continue; + Trigger trigger = alarm.getRequiredProperty(TRIGGER); + if (trigger.isAbsolute()) continue; + Duration reach = durationBound(TemporalSupport.duration(parsed, trigger)); + int repeat = alarm.getProperty(REPEAT).map(p -> ((Repeat) p).getCount()).orElse(0); + if (repeat < 0) + throw new QueryException( + QueryException.Kind.INVALID_DATA, "Negative alarm repeat count"); + if (repeat > 0) + reach = + reach.plus( + durationBound( + TemporalSupport.duration(parsed, alarm.getRequiredProperty(DURATION))) + .multipliedBy(repeat)); + if (reach.compareTo(alarmMax) > 0) alarmMax = reach; + } + max = max.plus(alarmMax); + } + return max; + } + + private static Duration durationBound(TemporalSupport.CalendarDuration duration) { + try { + // ZoneOffset permits -18h..+18h. One nominal addition can differ from elapsed days + // by at most their 36h span, including custom VTIMEZONE definitions. + return Duration.ofDays(Math.abs(duration.days())) + .plus(duration.clock().abs()) + .plus(duration.days() == 0 ? Duration.ZERO : Duration.ofHours(36)); + } catch (ArithmeticException ex) { + throw QueryException.limit(); + } + } + + private static ZoneId zoneFor(CalendarContent.Parsed parsed, Property property, ZoneId fallback) { + String id = property.getParameter(Parameter.TZID).orElseThrow().getValue(); + for (var c : parsed.calendar().getComponents(Component.VTIMEZONE)) + if (c.getRequiredProperty(TZID).getValue().equals(id)) + return FloatingTimezone.from((net.fortuna.ical4j.model.component.VTimeZone) c).zoneId(); + return TimeZoneRegistry.getGlobalZoneId(id); + } +} diff --git a/src/main/java/org/busystack/caldav/calendar/recurrence/TemporalSupport.java b/src/main/java/org/busystack/caldav/calendar/recurrence/TemporalSupport.java new file mode 100644 index 0000000..796e4ea --- /dev/null +++ b/src/main/java/org/busystack/caldav/calendar/recurrence/TemporalSupport.java @@ -0,0 +1,91 @@ +package org.busystack.caldav.calendar.recurrence; + +import java.time.*; +import java.time.temporal.*; +import net.fortuna.ical4j.model.Component; +import net.fortuna.ical4j.model.Property; +import net.fortuna.ical4j.model.property.DateProperty; +import org.busystack.caldav.calendar.CalendarContent; +import org.busystack.caldav.calendar.query.QueryException; + +/** Calendar arithmetic around typed iCal4j values; never parses content-line syntax. */ +public final class TemporalSupport { + private TemporalSupport() {} + + public static Temporal date(Component component, String name) { + var property = component.getProperty(name).orElse(null); + return property instanceof DateProperty date ? date.getDate() : null; + } + + public static Instant instant(Temporal value, ZoneId floating) { + if (value instanceof Instant i) return i; + if (value instanceof OffsetDateTime o) return o.toInstant(); + if (value instanceof ZonedDateTime z) return z.toInstant(); + if (value instanceof LocalDateTime l) return l.atZone(floating).toInstant(); + if (value instanceof LocalDate l) return l.atStartOfDay(floating).toInstant(); + throw new QueryException( + QueryException.Kind.INVALID_DATA, "Unsupported calendar date representation"); + } + + public static ZonedDateTime zoned(Temporal value, ZoneId floating) { + if (value instanceof ZonedDateTime z) return z; + if (value instanceof OffsetDateTime o) return o.toZonedDateTime(); + if (value instanceof Instant i) return i.atZone(ZoneOffset.UTC); + if (value instanceof LocalDateTime l) return l.atZone(floating); + if (value instanceof LocalDate l) return l.atStartOfDay(floating); + throw new QueryException( + QueryException.Kind.INVALID_DATA, "Unsupported calendar date representation"); + } + + public static Temporal nativeBound(Instant instant, Temporal seed, ZoneId floating) { + if (seed instanceof LocalDate) return instant.atZone(floating).toLocalDate(); + if (seed instanceof LocalDateTime) return instant.atZone(floating).toLocalDateTime(); + if (seed instanceof ZonedDateTime z) return instant.atZone(z.getZone()); + if (seed instanceof OffsetDateTime o) return instant.atOffset(o.getOffset()); + return instant; + } + + public record CalendarDuration(long days, Duration clock) { + public Temporal addTo(Temporal value, ZoneId floating) { + try { + if (value instanceof LocalDate d && clock.isZero()) return d.plusDays(days); + ZonedDateTime result = zoned(value, floating).plusDays(days).plus(clock); + if (value instanceof LocalDateTime) return result.toLocalDateTime(); + if (value instanceof OffsetDateTime) return result.toOffsetDateTime(); + if (value instanceof Instant) return result.toInstant(); + return result; + } catch (DateTimeException | ArithmeticException ex) { + throw QueryException.limit(); + } + } + } + + @SuppressWarnings("deprecation") + public static CalendarDuration duration(CalendarContent.Parsed parsed, Property property) { + return duration(parsed.lexicalValue(property)); + } + + @SuppressWarnings("deprecation") + public static CalendarDuration duration(String rawValue) { + var value = new net.fortuna.ical4j.model.Dur(rawValue); + long days = Math.addExact(Math.multiplyExact((long) value.getWeeks(), 7), value.getDays()); + Duration clock = + Duration.ofHours(value.getHours()) + .plusMinutes(value.getMinutes()) + .plusSeconds(value.getSeconds()); + return new CalendarDuration( + value.isNegative() ? -days : days, value.isNegative() ? clock.negated() : clock); + } + + public static Temporal exactEnd(Temporal seed, Temporal end, Temporal occurrence, ZoneId zone) { + if (seed instanceof LocalDate a + && end instanceof LocalDate b + && occurrence instanceof LocalDate c) return c.plusDays(ChronoUnit.DAYS.between(a, b)); + Duration elapsed = Duration.between(instant(seed, zone), instant(end, zone)); + ZonedDateTime result = zoned(occurrence, zone).plus(elapsed); + if (occurrence instanceof LocalDateTime) return result.toLocalDateTime(); + if (occurrence instanceof OffsetDateTime) return result.toOffsetDateTime(); + if (occurrence instanceof Instant) return result.toInstant(); + return result; + } +} diff --git a/src/main/java/org/busystack/caldav/calendar/recurrence/WorkBudget.java b/src/main/java/org/busystack/caldav/calendar/recurrence/WorkBudget.java new file mode 100644 index 0000000..2e3ba03 --- /dev/null +++ b/src/main/java/org/busystack/caldav/calendar/recurrence/WorkBudget.java @@ -0,0 +1,90 @@ +package org.busystack.caldav.calendar.recurrence; + +import java.time.*; +import java.time.temporal.*; +import net.fortuna.ical4j.model.Recur; +import net.fortuna.ical4j.util.Configurator; +import org.busystack.caldav.calendar.query.QueryException; +import org.busystack.caldav.calendar.query.QueryLimits; + +/** Conservative bounds before entering iCal4j's recurrence candidate generation. */ +public final class WorkBudget { + private final QueryLimits limits; + private long work; + private int instances; + + public WorkBudget(QueryLimits limits) { + this.limits = limits; + } + + public QueryLimits limits() { + return limits; + } + + public void instance() { + if (++instances > limits.maximumInstances()) throw QueryException.limit(); + } + + public void charge(long amount) { + if (amount < 0 || amount > limits.maximumCandidateWork() - work) throw QueryException.limit(); + work += amount; + } + + public void rule(Recur recur, Temporal seed, Temporal from, Temporal end) { + ChronoUnit unit = + switch (recur.getFrequency()) { + case SECONDLY -> ChronoUnit.SECONDS; + case MINUTELY -> ChronoUnit.MINUTES; + case HOURLY -> ChronoUnit.HOURS; + case DAILY -> ChronoUnit.DAYS; + case WEEKLY -> ChronoUnit.WEEKS; + case MONTHLY -> ChronoUnit.MONTHS; + case YEARLY -> ChronoUnit.YEARS; + }; + int interval = Math.max(1, recur.getInterval()); + Temporal beginning = recur.getCount() > 0 ? seed : from; + long increments; + try { + increments = Math.max(0, unit.between(comparable(beginning), comparable(end))) / interval + 4; + // Recur's skip-search multiplies int interval * int multiplier. Reject before overflow. + long distance = Math.abs(unit.between(comparable(seed), comparable(end))); + if (distance > Integer.MAX_VALUE / 4L) throw QueryException.limit(); + } catch (DateTimeException | ArithmeticException ex) { + throw QueryException.limit(); + } + int internalStop = Configurator.getIntProperty(Recur.KEY_MAX_INCREMENT_COUNT).orElse(1000); + long permitted = + internalStop > 0 + ? Math.min(limits.maximumRuleIncrements(), internalStop - 2L) + : limits.maximumRuleIncrements(); + if (increments > permitted) throw QueryException.limit(); + long candidates = + switch (recur.getFrequency()) { + case YEARLY -> 366; + case MONTHLY -> 31; + case WEEKLY -> 7; + default -> 1; + }; + int[] sizes = { + recur.getMonthList().size(), + recur.getWeekNoList().size(), + recur.getYearDayList().size(), + recur.getMonthDayList().size(), + recur.getDayList().size(), + recur.getHourList().size(), + recur.getMinuteList().size(), + recur.getSecondList().size(), + recur.getSetPosList().size() + }; + try { + for (int size : sizes) candidates = Math.multiplyExact(candidates, Math.max(1, size)); + charge(Math.multiplyExact(candidates, increments)); + } catch (ArithmeticException ex) { + throw QueryException.limit(); + } + } + + private static Temporal comparable(Temporal value) { + return value instanceof Instant instant ? instant.atOffset(ZoneOffset.UTC) : value; + } +} diff --git a/src/main/java/org/busystack/caldav/config/CanonicalOriginFilter.java b/src/main/java/org/busystack/caldav/config/CanonicalOriginFilter.java new file mode 100644 index 0000000..b5f6d95 --- /dev/null +++ b/src/main/java/org/busystack/caldav/config/CanonicalOriginFilter.java @@ -0,0 +1,128 @@ +package org.busystack.caldav.config; + +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletRequestWrapper; +import jakarta.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpServletResponseWrapper; +import java.io.IOException; +import java.net.URI; +import java.util.Collections; +import java.util.Locale; +import java.util.Set; +import org.springframework.web.filter.OncePerRequestFilter; + +/** All application URL builders, including security saved requests, use operator configuration. */ +final class CanonicalOriginFilter extends OncePerRequestFilter { + private static final Set FORWARDING_HEADERS = + Set.of( + "forwarded", + "x-forwarded-for", + "x-forwarded-host", + "x-forwarded-proto", + "x-forwarded-port", + "x-forwarded-prefix", + "x-forwarded-server", + "x-forwarded-ssl", + "x-real-ip"); + private final URI base; + + CanonicalOriginFilter(URI base) { + this.base = base; + } + + @Override + protected void doFilterInternal( + HttpServletRequest request, HttpServletResponse response, FilterChain chain) + throws ServletException, IOException { + chain.doFilter( + new HttpServletRequestWrapper(request) { + @Override + public String getScheme() { + return base.getScheme(); + } + + @Override + public boolean isSecure() { + return "https".equals(base.getScheme()); + } + + @Override + public String getServerName() { + return base.getHost(); + } + + @Override + public int getServerPort() { + return base.getPort() >= 0 ? base.getPort() : (isSecure() ? 443 : 80); + } + + @Override + public StringBuffer getRequestURL() { + return new StringBuffer( + base.getScheme() + "://" + base.getRawAuthority() + getRequestURI()); + } + + @Override + public String getHeader(String name) { + if ("host".equalsIgnoreCase(name)) return base.getRawAuthority(); + return hidden(name) ? null : super.getHeader(name); + } + + @Override + public java.util.Enumeration getHeaders(String name) { + if ("host".equalsIgnoreCase(name)) + return Collections.enumeration(java.util.List.of(base.getRawAuthority())); + return hidden(name) ? Collections.emptyEnumeration() : super.getHeaders(name); + } + + @Override + public java.util.Enumeration getHeaderNames() { + return Collections.enumeration( + Collections.list(super.getHeaderNames()).stream() + .filter(name -> !hidden(name)) + .toList()); + } + }, + new HttpServletResponseWrapper(response) { + private String redirect(String location) { + URI reference = URI.create(location); + if (reference.getRawAuthority() != null && !reference.isAbsolute()) + throw new IllegalArgumentException("Network-path redirects are not supported"); + if (reference.isAbsolute()) return location; + URI current = + URI.create( + base.getScheme() + "://" + base.getRawAuthority() + request.getRequestURI()); + return current.resolve(reference).toASCIIString(); + } + + @Override + public void sendRedirect(String location) throws IOException { + super.sendRedirect(redirect(location)); + } + + @Override + public void sendRedirect(String location, int status, boolean clearBuffer) + throws IOException { + super.sendRedirect(redirect(location), status, clearBuffer); + } + + @Override + public void setHeader(String name, String value) { + super.setHeader( + name, "Location".equalsIgnoreCase(name) && value != null ? redirect(value) : value); + } + + @Override + public void addHeader(String name, String value) { + super.addHeader( + name, "Location".equalsIgnoreCase(name) && value != null ? redirect(value) : value); + } + }); + } + + private static boolean hidden(String name) { + return name != null && FORWARDING_HEADERS.contains(name.toLowerCase(Locale.ROOT)); + } +} diff --git a/src/main/java/org/busystack/caldav/config/DavCompatibilityProperties.java b/src/main/java/org/busystack/caldav/config/DavCompatibilityProperties.java new file mode 100644 index 0000000..c6d40ab --- /dev/null +++ b/src/main/java/org/busystack/caldav/config/DavCompatibilityProperties.java @@ -0,0 +1,10 @@ +package org.busystack.caldav.config; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.bind.DefaultValue; + +/** Independently switchable, narrow DAV input interoperability accommodations. */ +@ConfigurationProperties("busystack.dav") +public record DavCompatibilityProperties( + @DefaultValue("true") boolean allowSyncDepthOne, + @DefaultValue("true") boolean repairLocalObservanceUntil) {} diff --git a/src/main/java/org/busystack/caldav/config/DeploymentProperties.java b/src/main/java/org/busystack/caldav/config/DeploymentProperties.java new file mode 100644 index 0000000..9deb143 --- /dev/null +++ b/src/main/java/org/busystack/caldav/config/DeploymentProperties.java @@ -0,0 +1,50 @@ +package org.busystack.caldav.config; + +import java.net.URI; +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** URLs are configured by the operator; Host and forwarding headers are never an authority. */ +@ConfigurationProperties("busystack") +public record DeploymentProperties(URI publicBaseUrl, URI issuer, boolean allowInsecureLocal) { + public DeploymentProperties { + validate(publicBaseUrl, "busystack.public-base-url", allowInsecureLocal); + validate(issuer, "busystack.issuer", allowInsecureLocal); + if (!publicBaseUrl.equals(issuer)) { + throw new IllegalArgumentException("busystack.issuer must equal busystack.public-base-url"); + } + } + + private static void validate(URI uri, String name, boolean allowLocal) { + if (uri == null + || uri.getHost() == null + || uri.getPort() == 0 + || uri.getPort() > 65535 + || uri.getUserInfo() != null + || uri.getQuery() != null + || uri.getFragment() != null + || !uri.toString().chars().allMatch(character -> character >= 0x21 && character <= 0x7e) + || uri.getRawPath().contains(";") + || uri.getRawPath().contains("%") + || uri.getPath().contains("//") + || uri.getPath().contains("..") + || (!uri.getPath().isEmpty() && uri.getPath().endsWith("/"))) { + throw new IllegalArgumentException( + name + + " must be an absolute canonical ASCII URL with a valid port and without trailing slash, credentials, query, fragment, semicolon or encoded path"); + } + boolean local = + "localhost".equals(uri.getHost()) + || "127.0.0.1".equals(uri.getHost()) + || "[::1]".equals(uri.getHost()); + if (!"https".equals(uri.getScheme()) + && !(allowLocal && local && "http".equals(uri.getScheme()))) { + throw new IllegalArgumentException( + name + + " must use HTTPS (HTTP is permitted only for explicitly enabled loopback development)"); + } + } + + public String davRoot() { + return publicBaseUrl + "/dav/"; + } +} diff --git a/src/main/java/org/busystack/caldav/config/RequestDiagnosticsFilter.java b/src/main/java/org/busystack/caldav/config/RequestDiagnosticsFilter.java new file mode 100644 index 0000000..f53e7d5 --- /dev/null +++ b/src/main/java/org/busystack/caldav/config/RequestDiagnosticsFilter.java @@ -0,0 +1,71 @@ +package org.busystack.caldav.config; + +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.UUID; +import java.util.concurrent.TimeUnit; +import org.slf4j.LoggerFactory; +import org.slf4j.MDC; +import org.springframework.web.filter.OncePerRequestFilter; + +/** Useful correlation/latency without logging resource paths, identities, headers or bodies. */ +final class RequestDiagnosticsFilter extends OncePerRequestFilter { + private static final org.slf4j.Logger LOG = + LoggerFactory.getLogger(RequestDiagnosticsFilter.class); + + @Override + protected void doFilterInternal( + HttpServletRequest request, HttpServletResponse response, FilterChain chain) + throws ServletException, IOException { + String id = UUID.randomUUID().toString(), previous = MDC.get("request_id"); + response.setHeader("X-Request-ID", id); + MDC.put("request_id", id); + long start = System.nanoTime(); + boolean failed = false; + try { + chain.doFilter(request, response); + } catch (IOException | ServletException | RuntimeException ex) { + failed = true; + throw ex; + } finally { + String relative = request.getRequestURI().substring(request.getContextPath().length()); + String surface = + relative.startsWith("/dav") + ? "dav" + : relative.startsWith("/oauth") + ? "oauth" + : relative.startsWith("/actuator") ? "health" : "browser"; + String method = + java.util.Set.of( + "GET", + "HEAD", + "POST", + "PUT", + "DELETE", + "OPTIONS", + "PROPFIND", + "PROPPATCH", + "REPORT", + "MKCALENDAR", + "MKCOL", + "COPY", + "MOVE", + "ACL") + .contains(request.getMethod()) + ? request.getMethod() + : "other"; + LOG.info( + "event=http_request request_id={} surface={} method={} status={} duration_ms={}", + id, + surface, + method, + failed ? 500 : response.getStatus(), + TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start)); + if (previous == null) MDC.remove("request_id"); + else MDC.put("request_id", previous); + } + } +} diff --git a/src/main/java/org/busystack/caldav/config/ServletDeploymentConfiguration.java b/src/main/java/org/busystack/caldav/config/ServletDeploymentConfiguration.java new file mode 100644 index 0000000..c7bd122 --- /dev/null +++ b/src/main/java/org/busystack/caldav/config/ServletDeploymentConfiguration.java @@ -0,0 +1,44 @@ +package org.busystack.caldav.config; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; +import org.springframework.boot.web.server.WebServerFactoryCustomizer; +import org.springframework.boot.web.server.servlet.ConfigurableServletWebServerFactory; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.Environment; + +/** Context, cookies and generated browser links all use the configured external path. */ +@Configuration +@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) +public class ServletDeploymentConfiguration { + @Bean + org.springframework.boot.web.servlet.FilterRegistrationBean + requestDiagnostics() { + var registration = + new org.springframework.boot.web.servlet.FilterRegistrationBean<>( + new RequestDiagnosticsFilter()); + registration.setOrder(org.springframework.core.Ordered.HIGHEST_PRECEDENCE + 1); + return registration; + } + + @Bean + org.springframework.boot.web.servlet.FilterRegistrationBean + canonicalOrigin(DeploymentProperties deployment) { + var registration = + new org.springframework.boot.web.servlet.FilterRegistrationBean<>( + new CanonicalOriginFilter(deployment.publicBaseUrl())); + registration.setOrder(org.springframework.core.Ordered.HIGHEST_PRECEDENCE); + return registration; + } + + @Bean + WebServerFactoryCustomizer deploymentContext( + DeploymentProperties deployment, Environment environment) { + String path = deployment.publicBaseUrl().getPath(); + String explicit = environment.getProperty("server.servlet.context-path"); + if (explicit != null && !explicit.equals(path)) + throw new IllegalArgumentException( + "server.servlet.context-path must match the path of busystack.public-base-url"); + return factory -> factory.setContextPath(path); + } +} diff --git a/src/main/java/org/busystack/caldav/management/DavConnectionAdvice.java b/src/main/java/org/busystack/caldav/management/DavConnectionAdvice.java new file mode 100644 index 0000000..759017c --- /dev/null +++ b/src/main/java/org/busystack/caldav/management/DavConnectionAdvice.java @@ -0,0 +1,35 @@ +package org.busystack.caldav.management; + +import org.busystack.caldav.config.DavCompatibilityProperties; +import org.busystack.caldav.config.DeploymentProperties; +import org.springframework.security.core.Authentication; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ModelAttribute; + +@ControllerAdvice(assignableTypes = ManagementController.class) +public final class DavConnectionAdvice { + public record Connection(String url, String username, String home, boolean compatibility) {} + + private final ManagementService management; + private final DeploymentProperties deployment; + private final DavCompatibilityProperties compatibility; + + public DavConnectionAdvice( + ManagementService management, + DeploymentProperties deployment, + DavCompatibilityProperties compatibility) { + this.management = management; + this.deployment = deployment; + this.compatibility = compatibility; + } + + @ModelAttribute("davConnection") + Connection connection(Authentication authentication) { + var account = management.current(authentication); + return new Connection( + deployment.davRoot(), + account.username(), + deployment.davRoot() + "calendars/" + account.id() + "/", + compatibility.allowSyncDepthOne()); + } +} diff --git a/src/main/java/org/busystack/caldav/management/GroupManagementController.java b/src/main/java/org/busystack/caldav/management/GroupManagementController.java new file mode 100644 index 0000000..4d1fc28 --- /dev/null +++ b/src/main/java/org/busystack/caldav/management/GroupManagementController.java @@ -0,0 +1,132 @@ +package org.busystack.caldav.management; + +import jakarta.servlet.http.HttpServletResponse; +import java.util.LinkedHashSet; +import java.util.Set; +import java.util.UUID; +import java.util.stream.Collectors; +import org.busystack.caldav.sharing.GroupService; +import org.busystack.caldav.storage.StoreException; +import org.springframework.security.core.Authentication; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; + +/** Group administration and owner-controlled sharing use separate service authorization rules. */ +@Controller +public class GroupManagementController { + public record GroupForm(UUID id, String name, String usernames, int memberCount) {} + + private final GroupService groups; + + public GroupManagementController(GroupService groups) { + this.groups = groups; + } + + @GetMapping("/admin/groups") + String list(Authentication authentication, Model model) { + model.addAttribute( + "groups", + groups.list(authentication).stream() + .map( + group -> + new GroupForm( + group.id(), + group.name(), + group.members().stream() + .map(GroupService.Member::username) + .collect(Collectors.joining("\n")), + group.members().size())) + .toList()); + return "groups"; + } + + @PostMapping("/admin/groups") + String create( + Authentication authentication, + @RequestParam String name, + @RequestParam(defaultValue = "") String usernames) { + groups.create(authentication, name, members(usernames)); + return "redirect:/admin/groups"; + } + + @PostMapping("/admin/groups/{id}") + String update( + Authentication authentication, + @PathVariable UUID id, + @RequestParam String name, + @RequestParam(defaultValue = "") String usernames) { + groups.update(authentication, id, name, members(usernames)); + return "redirect:/admin/groups"; + } + + @PostMapping("/admin/groups/{id}/delete") + String delete(Authentication authentication, @PathVariable UUID id) { + groups.delete(authentication, id); + return "redirect:/admin/groups"; + } + + @PostMapping("/group-shares") + String share( + Authentication authentication, + @RequestParam UUID collection, + @RequestParam UUID group, + @RequestParam String permission) { + groups.grant(authentication, collection, group, permission); + return "redirect:/"; + } + + @ExceptionHandler(IllegalArgumentException.class) + String invalid(HttpServletResponse response, Model model) { + response.setStatus(400); + model.addAttribute( + "message", + "Check the group name and account usernames. Use a name of 1–200 characters and at most" + + " 1,000 active account usernames, one per line. Sharing requires a valid owned" + + " collection, group and access choice."); + return "group-error"; + } + + @ExceptionHandler(StoreException.class) + String storeFailure(StoreException error, HttpServletResponse response, Model model) { + int status = + switch (error.kind()) { + case NOT_FOUND -> 404; + case FORBIDDEN -> 403; + case LIMIT -> 507; + default -> 409; + }; + response.setStatus(status); + model.addAttribute( + "message", + switch (error.kind()) { + case NOT_FOUND -> "The requested collection no longer exists."; + case FORBIDDEN -> "Only the collection owner can change its group access."; + case LIMIT -> "This request exceeds the supported group or sharing limits."; + default -> + "The request conflicts with the current collection or group state. Refresh the page" + + " before trying again."; + }); + return "group-error"; + } + + private static Set members(String usernames) { + if (usernames.length() > 65536) throw new IllegalArgumentException("Member list too large"); + var result = new LinkedHashSet(); + usernames + .lines() + .map(String::strip) + .filter(value -> !value.isEmpty()) + .forEach( + value -> { + if (result.size() >= 1000 && !result.contains(value)) + throw new IllegalArgumentException("Too many group members"); + result.add(value); + }); + return Set.copyOf(result); + } +} diff --git a/src/main/java/org/busystack/caldav/management/GroupSharingAdvice.java b/src/main/java/org/busystack/caldav/management/GroupSharingAdvice.java new file mode 100644 index 0000000..287baee --- /dev/null +++ b/src/main/java/org/busystack/caldav/management/GroupSharingAdvice.java @@ -0,0 +1,37 @@ +package org.busystack.caldav.management; + +import jakarta.servlet.http.HttpServletRequest; +import org.busystack.caldav.sharing.GroupService; +import org.springframework.security.core.Authentication; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ModelAttribute; + +/** The primary-authenticated dashboard needs only group names and the user's owned grants. */ +@ControllerAdvice(assignableTypes = ManagementController.class) +public class GroupSharingAdvice { + private final GroupService groups; + + public GroupSharingAdvice(GroupService groups) { + this.groups = groups; + } + + @ModelAttribute + void sharing(Authentication authentication, HttpServletRequest request, Model model) { + if ("GET".equals(request.getMethod()) && "/".equals(request.getServletPath())) { + model.addAttribute("groupChoices", groups.choices(authentication)); + int page = 0; + String supplied = request.getParameter("group-shares-page"); + if (supplied != null) { + try { + page = Integer.parseInt(supplied); + } catch (NumberFormatException invalid) { + throw new IllegalArgumentException("Invalid group sharing page"); + } + } + var grants = groups.ownedGrants(authentication, page); + model.addAttribute("groupGrants", grants.rows()); + model.addAttribute("groupGrantPage", grants); + } + } +} diff --git a/src/main/java/org/busystack/caldav/management/IndividualSharingAdvice.java b/src/main/java/org/busystack/caldav/management/IndividualSharingAdvice.java new file mode 100644 index 0000000..3e9fa95 --- /dev/null +++ b/src/main/java/org/busystack/caldav/management/IndividualSharingAdvice.java @@ -0,0 +1,52 @@ +package org.busystack.caldav.management; + +import org.springframework.http.HttpStatus; +import org.springframework.security.core.Authentication; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ControllerAdvice(assignableTypes = ManagementController.class) +public final class IndividualSharingAdvice { + private final ManagementService management; + + public IndividualSharingAdvice(ManagementService management) { + this.management = management; + } + + @ModelAttribute("individualShares") + ManagementService.SharePage shares( + Authentication authentication, + @RequestParam(name = "shares-page", defaultValue = "0") int page) { + return management.individualShares(authentication, page); + } + + @ExceptionHandler(org.springframework.dao.DataIntegrityViolationException.class) + @ResponseStatus(HttpStatus.CONFLICT) + String conflict() { + return "account-error"; + } + + @ExceptionHandler(org.busystack.caldav.storage.StoreException.class) + String storeFailure( + org.busystack.caldav.storage.StoreException failure, + jakarta.servlet.http.HttpServletResponse response) { + response.setStatus( + switch (failure.kind()) { + case NOT_FOUND -> 404; + case FORBIDDEN -> 403; + case LIMIT -> 507; + case PRECONDITION -> 412; + default -> 409; + }); + return "account-error"; + } + + @ExceptionHandler(IllegalArgumentException.class) + @ResponseStatus(HttpStatus.BAD_REQUEST) + String invalidInput() { + return "account-error"; + } +} diff --git a/src/main/java/org/busystack/caldav/management/ManagementController.java b/src/main/java/org/busystack/caldav/management/ManagementController.java new file mode 100644 index 0000000..baea06c --- /dev/null +++ b/src/main/java/org/busystack/caldav/management/ManagementController.java @@ -0,0 +1,82 @@ +package org.busystack.caldav.management; + +import java.util.UUID; +import org.springframework.security.core.Authentication; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.*; + +@Controller +public class ManagementController { + private final ManagementService management; + + public ManagementController(ManagementService management) { + this.management = management; + } + + @GetMapping("/") + String dashboard(Authentication auth, Model model) { + model.addAttribute("account", management.current(auth)); + model.addAttribute("externallyManaged", management.externallyManaged(auth)); + model.addAttribute("collections", management.collections(auth)); + model.addAttribute("credentials", management.credentials(auth)); + return "dashboard"; + } + + @PostMapping("/collections") + String createCollection( + Authentication auth, @RequestParam String name, @RequestParam String component) { + management.createCollection(auth, name, component); + return "redirect:/"; + } + + @PostMapping("/shares") + String share( + Authentication auth, + @RequestParam UUID collection, + @RequestParam String username, + @RequestParam String permission) { + management.share(auth, collection, username, permission); + return "redirect:/"; + } + + @PostMapping("/credentials") + String issue( + Authentication auth, + @RequestParam String label, + @RequestParam int days, + Model model, + jakarta.servlet.http.HttpServletResponse response) { + model.addAttribute("credential", management.issue(auth, label, days)); + response.setHeader("Cache-Control", "no-store"); + return "issued-credential"; + } + + @PostMapping("/credentials/{id}/revoke") + String revoke(Authentication auth, @PathVariable UUID id) { + management.revoke(auth, id); + return "redirect:/"; + } + + @GetMapping("/admin") + String admin(Authentication auth, Model model) { + model.addAttribute("accounts", management.users(auth)); + return "administration"; + } + + @PostMapping("/admin/accounts") + String provision( + Authentication auth, + @RequestParam String username, + @RequestParam String displayName, + @RequestParam String password) { + management.provision(auth, username, displayName, password); + return "redirect:/admin"; + } + + @PostMapping("/admin/accounts/{id}/disable") + String disable(Authentication auth, @PathVariable UUID id) { + management.disable(auth, id); + return "redirect:/admin"; + } +} diff --git a/src/main/java/org/busystack/caldav/management/ManagementService.java b/src/main/java/org/busystack/caldav/management/ManagementService.java new file mode 100644 index 0000000..40de729 --- /dev/null +++ b/src/main/java/org/busystack/caldav/management/ManagementService.java @@ -0,0 +1,187 @@ +package org.busystack.caldav.management; + +import java.time.Instant; +import java.util.List; +import java.util.UUID; +import org.busystack.caldav.account.AccountService; +import org.busystack.caldav.security.RecentAuthentication; +import org.busystack.caldav.storage.CalendarStore; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.security.access.AccessDeniedException; +import org.springframework.security.core.Authentication; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +/** + * Browser operations enforce current account state and sensitive-operation factors at the service. + */ +@Service +public class ManagementService { + public record Credential( + UUID id, String label, Instant created, Instant expires, boolean revoked) {} + + public record UserView( + UUID id, String username, String displayName, boolean administrator, boolean enabled) {} + + public record IndividualShare( + UUID collection, + String collectionName, + String username, + String permission, + boolean enabled) {} + + public record SharePage(List rows, int page, boolean hasNext) { + public SharePage { + rows = List.copyOf(rows); + } + } + + private final AccountService accounts; + private final CalendarStore calendars; + private final JdbcTemplate jdbc; + private final org.busystack.caldav.security.session.SessionSecurity sessions; + + public ManagementService( + AccountService accounts, + CalendarStore calendars, + JdbcTemplate jdbc, + org.busystack.caldav.security.session.SessionSecurity sessions) { + this.accounts = accounts; + this.calendars = calendars; + this.jdbc = jdbc; + this.sessions = sessions; + } + + public UserView current(Authentication auth) { + return view(requireAccount(auth)); + } + + public boolean externallyManaged(Authentication auth) { + return accounts.isExternallyManaged(requireAccount(auth).id()); + } + + public List collections(Authentication auth) { + return calendars.collections(requireAccount(auth).id()); + } + + public SharePage individualShares(Authentication auth, int page) { + if (page < 0 || page > Integer.MAX_VALUE / 50) + throw new IllegalArgumentException("Invalid sharing page"); + UUID actor = requireAccount(auth).id(); + var rows = + jdbc.query( + "SELECT c.id,c.name,a.username,m.permission,a.enabled FROM collection_members m JOIN collections c ON c.id=m.collection_id JOIN accounts a ON a.id=m.account_id WHERE c.owner_id=? ORDER BY c.id,a.id LIMIT 51 OFFSET ?", + (rs, row) -> + new IndividualShare( + rs.getObject(1, UUID.class), + rs.getString(2), + rs.getString(3), + rs.getString(4), + rs.getBoolean(5)), + actor, + page * 50); + return new SharePage(rows.size() > 50 ? rows.subList(0, 50) : rows, page, rows.size() > 50); + } + + public List credentials(Authentication auth) { + UUID actor = requireAccount(auth).id(); + return jdbc.query( + "SELECT id,label,created_at,expires_at,revoked_at FROM dav_credentials WHERE account_id=? ORDER BY created_at DESC", + (rs, row) -> + new Credential( + rs.getObject(1, UUID.class), + rs.getString(2), + rs.getTimestamp(3).toInstant(), + rs.getTimestamp(4).toInstant(), + rs.getTimestamp(5) != null), + actor); + } + + public List users(Authentication auth) { + requireAdministrator(auth); + return jdbc.query( + "SELECT id,username,display_name,administrator,enabled FROM accounts ORDER BY username", + (rs, row) -> + new UserView( + rs.getObject(1, UUID.class), + rs.getString(2), + rs.getString(3), + rs.getBoolean(4), + rs.getBoolean(5))); + } + + @Transactional + public void provision(Authentication auth, String username, String displayName, String password) { + requireAdministrator(auth); + accounts.provision(username, displayName, password, false); + } + + @Transactional + public void disable(Authentication auth, UUID id) { + requireAdministrator(auth); + accounts.disable(id); + } + + @Transactional + public AccountService.IssuedCredential issue(Authentication auth, String label, int days) { + RecentAuthentication.requireMfa(auth); + if (days < 1 || days > 365) + throw new IllegalArgumentException("Expiry must be between 1 and 365 days"); + return accounts.issueDavCredential( + requireAccount(auth).id(), label, Instant.now().plusSeconds(days * 86400L)); + } + + @Transactional + public void revoke(Authentication auth, UUID credential) { + RecentAuthentication.requireMfa(auth); + accounts.revokeDav(requireAccount(auth).id(), credential); + } + + @Transactional + public void createCollection(Authentication auth, String name, String component) { + org.busystack.caldav.storage.DavMutationLock.shared(jdbc); + RecentAuthentication.requireMfa(auth); + calendars.createCollection(requireAccount(auth).id(), name, component); + } + + @Transactional + public void share(Authentication auth, UUID collection, String username, String permission) { + org.busystack.caldav.storage.DavMutationLock.shared(jdbc); + RecentAuthentication.requireMfa(auth); + if (!java.util.Set.of("READ", "WRITE", "REVOKE").contains(permission)) + throw new IllegalArgumentException("Invalid sharing permission"); + var member = accounts.find(username); + if (member == null || (!member.enabled() && !"REVOKE".equals(permission))) + throw new IllegalArgumentException("Unknown active account"); + calendars.share( + requireAccount(auth).id(), + collection, + member.id(), + "REVOKE".equals(permission) ? null : permission); + } + + private AccountService.Account requireAccount(Authentication auth) { + if (auth == null || !auth.isAuthenticated()) + throw new AccessDeniedException("Authentication required"); + sessions.assertCurrent(auth); + var account = accounts.find(auth.getName()); + if (account == null || !account.enabled() || !accounts.isClientAccessCurrent(account.id())) + throw new AccessDeniedException("Account unavailable"); + return account; + } + + private void requireAdministrator(Authentication auth) { + RecentAuthentication.requireMfa(auth); + if (!requireAccount(auth).administrator()) + throw new AccessDeniedException("Administrator required"); + } + + private static UserView view(AccountService.Account account) { + return new UserView( + account.id(), + account.username(), + account.displayName(), + account.administrator(), + account.enabled()); + } +} diff --git a/src/main/java/org/busystack/caldav/management/OperationalDiagnostics.java b/src/main/java/org/busystack/caldav/management/OperationalDiagnostics.java new file mode 100644 index 0000000..11db950 --- /dev/null +++ b/src/main/java/org/busystack/caldav/management/OperationalDiagnostics.java @@ -0,0 +1,73 @@ +package org.busystack.caldav.management; + +import io.micrometer.core.instrument.MeterRegistry; +import java.util.concurrent.TimeUnit; +import org.busystack.caldav.config.DeploymentProperties; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.security.access.AccessDeniedException; +import org.springframework.security.core.Authentication; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; + +/** Administrative aggregate diagnostics never load or reveal calendar content. */ +@Controller +public final class OperationalDiagnostics { + private final ManagementService management; + private final JdbcTemplate jdbc; + private final MeterRegistry meters; + private final DeploymentProperties deployment; + + public OperationalDiagnostics( + ManagementService management, + JdbcTemplate jdbc, + MeterRegistry meters, + DeploymentProperties deployment) { + this.management = management; + this.jdbc = jdbc; + this.meters = meters; + this.deployment = deployment; + } + + @GetMapping("/admin/diagnostics") + String diagnostics( + Authentication authentication, + Model model, + jakarta.servlet.http.HttpServletResponse response) { + if (!management.current(authentication).administrator()) + throw new AccessDeniedException("Administrator privilege required"); + response.setHeader("Cache-Control", "no-store"); + model.addAttribute("baseUrl", deployment.publicBaseUrl().toString()); + model.addAttribute("issuer", deployment.issuer().toString()); + model.addAttribute("javaVersion", Runtime.version().toString()); + model.addAttribute( + "databaseVersion", + jdbc.queryForObject("SELECT current_setting('server_version')", String.class)); + model.addAttribute( + "migration", + jdbc.queryForObject( + "SELECT version FROM flyway_schema_history WHERE success AND version IS NOT NULL ORDER BY installed_rank DESC LIMIT 1", + String.class)); + model.addAttribute( + "enabledAccounts", + jdbc.queryForObject("SELECT count(*) FROM accounts WHERE enabled", Long.class)); + model.addAttribute( + "collections", jdbc.queryForObject("SELECT count(*) FROM collections", Long.class)); + model.addAttribute( + "resources", jdbc.queryForObject("SELECT count(*) FROM calendar_resources", Long.class)); + long requests = 0, serverErrors = 0; + double total = 0, max = 0; + for (var timer : meters.find("http.server.requests").timers()) { + requests += timer.count(); + total += timer.totalTime(TimeUnit.MILLISECONDS); + max = Math.max(max, timer.max(TimeUnit.MILLISECONDS)); + String status = timer.getId().getTag("status"); + if (status != null && status.startsWith("5")) serverErrors += timer.count(); + } + model.addAttribute("requests", requests); + model.addAttribute("serverErrors", serverErrors); + model.addAttribute("meanMillis", requests == 0 ? 0 : Math.round(total / requests)); + model.addAttribute("maxMillis", Math.round(max)); + return "diagnostics"; + } +} diff --git a/src/main/java/org/busystack/caldav/protocol/CalendarQueryXml.java b/src/main/java/org/busystack/caldav/protocol/CalendarQueryXml.java new file mode 100644 index 0000000..5d4b89f --- /dev/null +++ b/src/main/java/org/busystack/caldav/protocol/CalendarQueryXml.java @@ -0,0 +1,158 @@ +package org.busystack.caldav.protocol; + +import static org.busystack.caldav.protocol.DavXml.*; + +import java.time.Instant; +import java.time.ZoneOffset; +import java.time.format.DateTimeFormatter; +import java.time.format.ResolverStyle; +import java.util.ArrayList; +import java.util.List; +import org.busystack.caldav.calendar.query.CalendarDataProjection; +import org.busystack.caldav.calendar.query.CalendarFilter; +import org.busystack.caldav.calendar.query.TimeRange; +import org.w3c.dom.Element; + +/** XML grammar belongs to the protocol; calendaring filters are immutable semantic values. */ +final class CalendarQueryXml { + private static final DateTimeFormatter UTC = + DateTimeFormatter.ofPattern("uuuuMMdd'T'HHmmss'Z'") + .withResolverStyle(ResolverStyle.STRICT) + .withZone(ZoneOffset.UTC); + + private CalendarQueryXml() {} + + static CalendarFilter.ComponentFilter filter(Element root) { + Element filter = required(root, CAL, "filter"); + List children = children(filter, CAL, "comp-filter"); + if (children.size() != 1 || !is(children.getFirst(), CAL, "comp-filter")) + throw new DavException(403, c("valid-filter")); + var result = component(children.getFirst()); + if (!"VCALENDAR".equals(result.name())) throw new DavException(403, c("valid-filter")); + return result; + } + + private static CalendarFilter.ComponentFilter component(Element node) { + List properties = new ArrayList<>(); + List components = new ArrayList<>(); + for (Element child : + children(node, CAL, "prop-filter", "comp-filter", "is-not-defined", "time-range")) { + if (is(child, CAL, "prop-filter")) properties.add(property(child)); + else if (is(child, CAL, "comp-filter")) components.add(component(child)); + } + boolean absent = child(node, CAL, "is-not-defined") != null; + if (absent + && children(node, CAL, "prop-filter", "comp-filter", "is-not-defined", "time-range").size() + != 1) throw new DavException(403, c("valid-filter")); + Element range = child(node, CAL, "time-range"); + return new CalendarFilter.ComponentFilter( + nameAttribute(node), absent, range == null ? null : range(range), properties, components); + } + + private static CalendarFilter.PropertyFilter property(Element node) { + List parameters = new ArrayList<>(); + for (Element child : + children(node, CAL, "param-filter", "is-not-defined", "time-range", "text-match")) { + if (is(child, CAL, "param-filter")) parameters.add(parameter(child)); + } + boolean absent = child(node, CAL, "is-not-defined") != null; + Element range = child(node, CAL, "time-range"), text = child(node, CAL, "text-match"); + if (absent + && children(node, CAL, "param-filter", "is-not-defined", "time-range", "text-match") + .size() + != 1 + || range != null && text != null) throw new DavException(403, c("valid-filter")); + return new CalendarFilter.PropertyFilter( + nameAttribute(node), + absent, + range == null ? null : range(range), + text == null ? null : text(text), + parameters); + } + + private static CalendarFilter.ParameterFilter parameter(Element node) { + boolean absent = child(node, CAL, "is-not-defined") != null; + Element text = child(node, CAL, "text-match"); + if (absent && children(node, CAL, "is-not-defined", "text-match").size() != 1) + throw new DavException(403, c("valid-filter")); + return new CalendarFilter.ParameterFilter( + nameAttribute(node), absent, text == null ? null : text(text)); + } + + private static CalendarFilter.TextMatch text(Element node) { + String negate = node.getAttribute("negate-condition"); + if (!negate.isEmpty() && !negate.equals("yes") && !negate.equals("no")) + throw new DavException(403, c("valid-filter")); + if (scalarText(node).length() > 8192) throw new DavException(403, c("valid-filter")); + return new CalendarFilter.TextMatch( + scalarText(node), + node.hasAttribute("collation") ? node.getAttribute("collation") : null, + negate.equals("yes")); + } + + static TimeRange range(Element node) { + return new TimeRange( + node.hasAttribute("start") ? instant(node.getAttribute("start")) : null, + node.hasAttribute("end") ? instant(node.getAttribute("end")) : null); + } + + private static Instant instant(String value) { + try { + return Instant.from(UTC.parse(value)); + } catch (java.time.DateTimeException ex) { + throw new DavException(403, c("valid-filter")); + } + } + + static CalendarDataProjection.Selection selection(Element node) { + if (node.hasAttribute("content-type") + && !node.getAttribute("content-type").equals("text/calendar") + || node.hasAttribute("version") && !node.getAttribute("version").equals("2.0")) + throw new DavException(403, c("supported-calendar-data")); + Element component = child(node, CAL, "comp"), + expand = child(node, CAL, "expand"), + limit = child(node, CAL, "limit-recurrence-set"), + busy = child(node, CAL, "limit-freebusy-set"); + if (expand != null && limit != null) throw new DavException(400, null); + return new CalendarDataProjection.Selection( + component == null ? null : selectionComponent(component), + expand == null ? null : range(expand), + limit == null ? null : range(limit), + busy == null ? null : range(busy)); + } + + private static CalendarDataProjection.ComponentSelection selectionComponent(Element node) { + List properties = new ArrayList<>(); + List components = new ArrayList<>(); + boolean allProps = child(node, CAL, "allprop") != null, + allComps = child(node, CAL, "allcomp") != null; + for (Element child : children(node, CAL, "prop", "comp", "allprop", "allcomp")) { + if (is(child, CAL, "prop")) { + String novalue = child.getAttribute("novalue"); + if (!novalue.isEmpty() && !novalue.equals("yes") && !novalue.equals("no")) + throw new DavException(400, null); + properties.add( + new CalendarDataProjection.PropertySelection( + nameAttribute(child), novalue.equals("yes"))); + } else if (is(child, CAL, "comp")) components.add(selectionComponent(child)); + } + if (allProps && !properties.isEmpty() || allComps && !components.isEmpty()) + throw new DavException(400, null); + return new CalendarDataProjection.ComponentSelection( + nameAttribute(node), allProps, allComps, properties, components); + } + + private static String nameAttribute(Element node) { + String name = node.getAttribute("name"); + if (name.isBlank() + || name.length() > 255 + || !name.codePoints() + .allMatch( + c -> + c >= 'A' && c <= 'Z' + || c >= 'a' && c <= 'z' + || c >= '0' && c <= '9' + || c == '-')) throw new DavException(400, null); + return name.toUpperCase(java.util.Locale.ROOT); + } +} diff --git a/src/main/java/org/busystack/caldav/protocol/DavController.java b/src/main/java/org/busystack/caldav/protocol/DavController.java new file mode 100644 index 0000000..518a586 --- /dev/null +++ b/src/main/java/org/busystack/caldav/protocol/DavController.java @@ -0,0 +1,999 @@ +package org.busystack.caldav.protocol; + +import static org.busystack.caldav.protocol.DavXml.*; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.time.ZoneOffset; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.UUID; +import java.util.function.Supplier; +import javax.xml.namespace.QName; +import org.busystack.caldav.calendar.CalendarContent; +import org.busystack.caldav.calendar.query.CalendarFreeBusy; +import org.busystack.caldav.calendar.query.CalendarQuery; +import org.busystack.caldav.calendar.query.FloatingTimezone; +import org.busystack.caldav.calendar.query.QueryException; +import org.busystack.caldav.calendar.query.QueryLimits; +import org.busystack.caldav.config.DeploymentProperties; +import org.busystack.caldav.storage.CalendarStore; +import org.busystack.caldav.storage.DavStore; +import org.busystack.caldav.storage.StoreException; +import org.springframework.http.ResponseEntity; +import org.springframework.security.core.Authentication; +import org.springframework.stereotype.Controller; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.TransactionDefinition; +import org.springframework.transaction.support.TransactionTemplate; +import org.springframework.web.bind.annotation.RequestMapping; +import org.w3c.dom.Element; + +/** DAV HTTP boundary. Browser sessions cannot authenticate this security chain. */ +@Controller +final class DavController { + private final java.util.concurrent.Semaphore workSlots = new java.util.concurrent.Semaphore(4); + private final DavStore store; + private final CalendarStore calendars; + private final DavResources resources; + private final DavProperties properties; + private final DavPaths paths; + private final TransactionTemplate snapshot; + private final TransactionTemplate mutation; + private final org.springframework.jdbc.core.JdbcTemplate jdbc; + private final org.busystack.caldav.config.DavCompatibilityProperties compatibility; + + DavController( + DavStore store, + CalendarStore calendars, + DavResources resources, + DavProperties properties, + DeploymentProperties deployment, + PlatformTransactionManager transactions, + org.springframework.jdbc.core.JdbcTemplate jdbc, + org.busystack.caldav.config.DavCompatibilityProperties compatibility) { + this.store = store; + this.calendars = calendars; + this.resources = resources; + this.properties = properties; + this.paths = new DavPaths(deployment); + snapshot = new TransactionTemplate(transactions); + snapshot.setReadOnly(true); + snapshot.setIsolationLevel(TransactionDefinition.ISOLATION_REPEATABLE_READ); + mutation = new TransactionTemplate(transactions); + this.jdbc = jdbc; + this.compatibility = compatibility; + } + + @RequestMapping("/dav/**") + void request( + HttpServletRequest request, HttpServletResponse response, Authentication authentication) + throws IOException { + if (!workSlots.tryAcquire()) { + response.setStatus(503); + response.setHeader("Retry-After", "1"); + return; + } + try { + write(response, request.getMethod(), dispatch(request, authentication)); + } finally { + workSlots.release(); + } + } + + // The protocol has already evaluated all validators under its snapshot/mutation locks. + // A ResponseEntity return would ask MVC to evaluate GET/HEAD date conditions a second + // time, using its different safe-method If-Match and lenient date parsing rules. + private static void write( + HttpServletResponse response, String method, ResponseEntity result) + throws IOException { + response.setStatus(result.getStatusCode().value()); + result + .getHeaders() + .forEach( + (name, values) -> { + if (!values.isEmpty()) { + response.setHeader(name, values.getFirst()); + for (int index = 1; index < values.size(); index++) + response.addHeader(name, values.get(index)); + } + }); + byte[] body = result.getBody(); + if (body != null && !method.equals("HEAD")) response.getOutputStream().write(body); + } + + private ResponseEntity dispatch(HttpServletRequest request, Authentication authentication) + throws IOException { + UUID actor = UUID.fromString(authentication.getName()); + try { + List parts = paths.parse(request.getRequestURI()); + String method = request.getMethod(); + byte[] body = body(request); + boolean mutating = + Set.of("PUT", "DELETE", "MKCALENDAR", "MKCOL", "PROPPATCH", "COPY", "MOVE", "ACL") + .contains(method); + boolean metadataCondition = + Set.of("PROPPATCH", "ACL", "MKCALENDAR", "MKCOL").contains(method) + && (request.getHeader("If-Match") != null + || request.getHeader("If-None-Match") != null); + Supplier> handler = + () -> { + if (mutating) { + if (request.getHeader("If") != null || metadataCondition) + org.busystack.caldav.storage.DavMutationLock.exclusive(jdbc); + else org.busystack.caldav.storage.DavMutationLock.shared(jdbc); + } + evaluateIf(actor, request); + if (metadataCondition) { + DavNode existing = null; + try { + existing = resources.resolve(actor, parts); + } catch (DavException ex) { + if (ex.status() != 404) throw ex; + } catch (StoreException ex) { + if (ex.kind() != StoreException.Kind.NOT_FOUND) throw ex; + } + HttpConditions.write(request, paths) + .check( + existing == null || existing.metadata() == null + ? null + : existing.metadata().etag(), + existing != null); + } + return switch (method) { + case "OPTIONS" -> options(actor, parts); + case "GET", "HEAD" -> get(actor, parts, request); + case "PROPFIND" -> propfind(actor, parts, request, body); + case "REPORT" -> report(actor, parts, request, parse(body)); + case "PUT" -> put(actor, parts, request, body); + case "DELETE" -> delete(actor, parts, request); + case "MKCALENDAR", "MKCOL" -> create(actor, parts, method, body); + case "PROPPATCH" -> patch(actor, parts, request, parse(body)); + case "COPY", "MOVE" -> transfer(actor, parts, request); + case "ACL" -> acl(actor, parts, parse(body)); + default -> methodNotAllowed(actor, parts, null); + }; + }; + return (mutating ? mutation : snapshot).execute(status -> handler.get()); + } catch (DavException ex) { + if (ex.status() == 405) + return methodNotAllowed(actor, paths.parse(request.getRequestURI()), ex.condition()); + return error(ex.status(), ex.condition(), ex.failingHref(), ex.requiredPrivilege()); + } catch (StoreException ex) { + return switch (ex.kind()) { + case NOT_FOUND -> error(404, null); + case FORBIDDEN -> { + if (ex.requiredAccess() == null) yield error(403, null); + String href = + paths.href( + paths.parse(request.getRequestURI()), request.getRequestURI().endsWith("/")); + if (ex.collection() != null) { + try { + var binding = store.bindingFor(actor, ex.collection()); + if (binding != null) { + List parts = new ArrayList<>(List.of("calendars", actor.toString())); + parts.addAll(List.of(binding.path().split("/"))); + href = paths.href(parts, true); + } + } catch (StoreException stateChanged) { + if (stateChanged.kind() != StoreException.Kind.FORBIDDEN) throw stateChanged; + } + } + QName privilege = + switch (ex.requiredAccess()) { + case READ -> d("read"); + case WRITE -> d("write"); + case OWNER -> d("write-acl"); + }; + yield error(403, d("need-privileges"), href, privilege); + } + case PRECONDITION -> error(412, null); + case UID_CONFLICT -> error(403, c("no-uid-conflict")); + case INVALID_TOKEN -> error(403, d("valid-sync-token")); + case LIMIT -> error(507, d("number-of-matches-within-limits")); + case EXISTS -> methodNotAllowed(actor, paths.parse(request.getRequestURI()), null); + case INVALID_PROPERTY -> error(409, d("valid-property-value")); + default -> error(409, null); + }; + } catch (QueryException ex) { + if (ex.kind() == QueryException.Kind.LIMIT_EXCEEDED + && Set.of("PUT", "MKCALENDAR").contains(request.getMethod())) + return error( + 413, new QName("urn:busystack:params:xml:ns:dav", "calendar-complexity-limit")); + return queryError(ex); + } catch (org.busystack.caldav.calendar.CalendarValidationException ex) { + return error( + 403, + c( + switch (ex.reason()) { + case INVALID_DATA -> "valid-calendar-data"; + case INVALID_OBJECT -> "valid-calendar-object-resource"; + case UNSUPPORTED_COMPONENT -> "supported-calendar-component"; + case UNSUPPORTED_RSCALE -> "supported-rscale"; + })); + } catch (org.springframework.dao.QueryTimeoutException + | org.springframework.transaction.TransactionTimedOutException timeout) { + return ResponseEntity.status(503).header("Retry-After", "1").body(new byte[0]); + } catch (IllegalArgumentException ex) { + return error(400, null); + } + } + + // An explicit OPTIONS mapping prevents MVC's synthetic HttpOptionsHandler from replacing DAV + // headers. + @RequestMapping( + value = "/dav/**", + method = org.springframework.web.bind.annotation.RequestMethod.OPTIONS) + void optionsRequest( + HttpServletRequest request, HttpServletResponse response, Authentication authentication) + throws IOException { + request(request, response, authentication); + } + + private void evaluateIf(UUID actor, HttpServletRequest request) { + var values = java.util.Collections.list(request.getHeaders("If")); + if (values.isEmpty()) return; + DavIf condition = DavIf.parse(String.join(" ", values), request.getRequestURI(), paths); + if (!condition.matchesStates( + parts -> { + try { + DavNode node = resources.resolve(actor, parts); + return new DavIf.ResourceState( + node.metadata() == null ? null : node.metadata().etag(), + node.kind() == DavNode.Kind.CALENDAR + ? calendars.currentSyncToken(actor, node.collectionId()) + : null); + } catch (DavException ex) { + if (ex.status() == 404) return DavIf.ResourceState.ABSENT; + throw ex; + } catch (StoreException ex) { + if (ex.kind() == StoreException.Kind.NOT_FOUND) return DavIf.ResourceState.ABSENT; + throw ex; + } + })) throw new DavException(412, null); + } + + private static String allow() { + return "OPTIONS, GET, HEAD, PROPFIND, PROPPATCH, REPORT, MKCALENDAR, MKCOL, PUT, DELETE, COPY, MOVE, ACL"; + } + + private static String allow(DavNode.Kind kind) { + String read = "OPTIONS, GET, HEAD, PROPFIND, REPORT"; + return switch (kind) { + case CALENDAR -> read + ", PROPPATCH, DELETE, COPY, MOVE, ACL"; + case FOLDER -> read + ", PROPPATCH, DELETE, COPY, MOVE"; + case RESOURCE -> read + ", PUT, DELETE, PROPPATCH, COPY, MOVE"; + default -> read; + }; + } + + private ResponseEntity options(UUID actor, List parts) { + DavNode node = resources.resolve(actor, parts); + return ResponseEntity.ok() + .header("Allow", allow(node.kind())) + .header("DAV", "1, access-control, calendar-access, sync-collection") + .body(new byte[0]); + } + + private ResponseEntity methodNotAllowed(UUID actor, List parts, QName condition) { + DavNode node = resources.resolve(actor, parts); + var response = error(405, condition); + var headers = new org.springframework.http.HttpHeaders(); + headers.putAll(response.getHeaders()); + headers.set("Allow", allow(node.kind())); + return new ResponseEntity<>(response.getBody(), headers, response.getStatusCode()); + } + + private static byte[] body(HttpServletRequest request) throws IOException { + String encoding = request.getHeader("Content-Encoding"); + if (encoding != null && !encoding.equalsIgnoreCase("identity")) + throw new DavException(415, null); + boolean calendarPut = request.getMethod().equals("PUT"); + int limit = calendarPut ? Math.min(MAX_REQUEST, CalendarContent.MAX_BYTES) : MAX_REQUEST; + QName condition = + calendarPut && limit == CalendarContent.MAX_BYTES ? c("max-resource-size") : null; + if (request.getContentLengthLong() > limit) throw new DavException(413, condition); + byte[] bytes = request.getInputStream().readNBytes(limit + 1); + if (bytes.length > limit) throw new DavException(413, condition); + return bytes; + } + + private ResponseEntity get(UUID actor, List parts, HttpServletRequest request) { + DavNode node = resources.resolve(actor, parts); + if (node.kind() == DavNode.Kind.RESOURCE) { + String etag = node.metadata().etag(); + java.time.Instant origin = java.time.Instant.now(); + java.time.Instant modifiedAt = + org.busystack.caldav.storage.ModificationDates.effective( + node.metadata().modifiedAt(), origin); + if (HttpConditions.notModified(request, etag, modifiedAt, paths)) + return ResponseEntity.status(304) + .eTag(etag) + .header("Date", HttpDateValues.format(origin)) + .header("Last-Modified", HttpDateValues.format(modifiedAt)) + .body(new byte[0]); + byte[] bytes = + request.getMethod().equals("HEAD") + ? new byte[0] + : resources.content(actor, node).resource().content(); + return ResponseEntity.ok() + .eTag(etag) + .header("Date", HttpDateValues.format(origin)) + .header("Last-Modified", HttpDateValues.format(modifiedAt)) + .header("Cache-Control", "private, no-cache") + .header("Content-Type", "text/calendar; charset=utf-8") + .contentLength(node.metadata().contentLength()) + .body(bytes); + } + if (!request.getRequestURI().endsWith("/")) + return ResponseEntity.status(308) + .header("Location", paths.href(parts, true)) + .body(new byte[0]); + if (HttpConditions.notModified(request, null, paths)) + return ResponseEntity.status(304).body(new byte[0]); + StringBuilder text = new StringBuilder(properties.displayName(actor, node)).append('\n'); + for (DavNode child : resources.children(actor, node)) + text.append(paths.href(child.segments(), child.trailingSlash())).append('\n'); + byte[] bytes = text.toString().getBytes(StandardCharsets.UTF_8); + return ResponseEntity.ok() + .header("Content-Type", "text/plain; charset=utf-8") + .header("Cache-Control", "private, no-store") + .contentLength(bytes.length) + .body(request.getMethod().equals("HEAD") ? new byte[0] : bytes); + } + + private ResponseEntity propfind( + UUID actor, List parts, HttpServletRequest request, byte[] body) { + Element root = body.length == 0 ? null : parse(body); + if (root != null && !is(root, DAV, "propfind")) throw new DavException(400, null); + int depth = depth(request, 20); + var requested = DavProperties.requested(root); + var document = document(); + var multistatus = add(document, d("multistatus")); + Map timezones = timezoneCache(); + for (DavNode node : resources.descendants(actor, resources.resolve(actor, parts), depth)) + properties.response( + multistatus, + actor, + node, + requested, + responseTimezone(actor, node, requested, null, timezones)); + return xml(207, document); + } + + private ResponseEntity put( + UUID actor, List parts, HttpServletRequest request, byte[] body) { + if (parts.size() < 4 || request.getRequestURI().endsWith("/")) + throw new DavException(409, null); + String type = request.getContentType(); + if (type != null && !type.split(";", 2)[0].trim().equalsIgnoreCase("text/calendar")) + throw new DavException(415, c("supported-calendar-data")); + DavNode parent = resources.resolve(actor, parts.subList(0, parts.size() - 1)); + if (parent.kind() != DavNode.Kind.CALENDAR) + throw new DavException(409, c("calendar-collection-location-ok")); + DavStore.PutResult result; + try { + result = + store.putResource( + actor, + parent.collectionId(), + parts.getLast(), + body, + HttpConditions.write(request, paths)); + } catch (org.busystack.caldav.calendar.CalendarValidationException ex) { + throw ex; + } catch (IllegalArgumentException ex) { + throw new DavException(403, c("valid-calendar-data")); + } + var response = + ResponseEntity.status(result.created() ? 201 : 204) + .header("Location", paths.href(parts, false)); + // RFC 9110 §9.3.4: validators describe the stored representation, not transformed PUT bytes. + if (!result.transformed()) response.eTag(result.resource().etag()); + return response.body(new byte[0]); + } + + private ResponseEntity delete( + UUID actor, List parts, HttpServletRequest request) { + DavNode node = resources.resolve(actor, parts); + if (node.kind() == DavNode.Kind.RESOURCE) + calendars.delete( + actor, node.collectionId(), node.metadata().name(), HttpConditions.write(request, paths)); + else if (node.kind() == DavNode.Kind.CALENDAR || node.kind() == DavNode.Kind.FOLDER) + store.deleteBinding( + actor, node.principal(), node.relativePath(), HttpConditions.write(request, paths)); + else return methodNotAllowed(actor, parts, null); + return ResponseEntity.noContent().build(); + } + + private ResponseEntity create( + UUID actor, List parts, String method, byte[] body) { + if (parts.size() < 3 || !parts.getFirst().equals("calendars")) + throw new DavException(403, null); + UUID home = DavResources.uuid(parts.get(1)); + String relative = String.join("/", parts.subList(2, parts.size())); + String display = parts.getLast(), component = method.equals("MKCALENDAR") ? "VEVENT" : null; + Map values = new LinkedHashMap<>(); + if (body.length > 0) { + if (method.equals("MKCOL")) throw new DavException(415, null); + Element root = parse(body); + if (!is(root, CAL, "mkcalendar")) throw new DavException(400, null); + Element set = required(root, DAV, "set"), prop = required(set, DAV, "prop"); + for (Element value : children(prop)) { + QName name = name(value); + if (name.equals(c("supported-calendar-component-set"))) { + var components = children(value, CAL, "comp"); + if (components.size() != 1 + || !is(components.getFirst(), CAL, "comp") + || !Set.of("VEVENT", "VTODO").contains(components.getFirst().getAttribute("name"))) + throw new DavException(403, c("supported-calendar-component")); + component = components.getFirst().getAttribute("name"); + } else if (name.equals(d("displayname"))) { + validateProperty(name, value); + display = value.getTextContent(); + values.put(name, new String(propertyBytes(value), StandardCharsets.UTF_8)); + } else if (DavProperties.protectedProperty(name)) + throw new DavException(403, d("cannot-modify-protected-property")); + else { + validateProperty(name, value); + values.put(name, new String(propertyBytes(value), StandardCharsets.UTF_8)); + } + } + } + if (!home.equals(actor)) + throw new DavException( + 403, d("need-privileges"), paths.href(parts.subList(0, 2), true), d("bind")); + try { + store.create(actor, home, relative, display, component, values); + } catch (StoreException failure) { + if (failure.kind() == StoreException.Kind.EXISTS && method.equals("MKCALENDAR")) + throw new DavException(405, d("resource-must-be-null")); + throw failure; + } + return ResponseEntity.status(201).header("Location", paths.href(parts, true)).body(new byte[0]); + } + + private ResponseEntity patch( + UUID actor, List parts, HttpServletRequest request, Element root) { + if (!is(root, DAV, "propertyupdate")) throw new DavException(400, null); + DavNode node = resources.resolve(actor, parts); + if (node.entry() == null) throw new DavException(403, d("cannot-modify-protected-property")); + Map set = new LinkedHashMap<>(); + Set remove = new LinkedHashSet<>(), all = new LinkedHashSet<>(); + Map bad = new LinkedHashMap<>(); + for (Element operation : children(root, DAV, "set", "remove")) { + for (Element value : children(required(operation, DAV, "prop"))) { + QName name = name(value); + all.add(name); + if (DavProperties.protectedProperty(name)) + bad.put(name, new DavException(403, d("cannot-modify-protected-property"))); + else if (name.equals(d("displayname")) + && node.kind() != DavNode.Kind.RESOURCE + && is(operation, DAV, "remove")) + bad.put(name, new DavException(409, d("valid-property-value"))); + else if (is(operation, DAV, "set")) { + try { + validateProperty(name, value); + set.put(name, new String(propertyBytes(value), StandardCharsets.UTF_8)); + remove.remove(name); + } catch (DavException ex) { + if (ex.status() != 403 && ex.status() != 409) throw ex; + bad.put(name, ex); + } catch (QueryException ex) { + bad.put( + name, + new DavException( + 409, + name.equals(c("calendar-timezone")) + ? c("valid-calendar-data") + : d("valid-property-value"))); + } + } else { + remove.add(name); + set.remove(name); + } + } + } + if (all.isEmpty()) throw new DavException(400, null); + if (bad.isEmpty()) { + List binding = + node.kind() == DavNode.Kind.RESOURCE + ? parts.subList(2, parts.size() - 1) + : parts.subList(2, parts.size()); + store.patch( + actor, + node.principal(), + String.join("/", binding), + node.metadata() == null ? null : node.metadata().name(), + set, + remove, + HttpConditions.write(request, paths)); + } + var document = document(); + Element multistatus = add(document, d("multistatus")), + response = add(multistatus, d("response")); + text(response, d("href"), paths.href(parts, node.collection())); + for (QName property : all) { + Element propstat = add(response, d("propstat")); + add(add(propstat, d("prop")), property); + int status = + bad.isEmpty() ? 200 : bad.containsKey(property) ? bad.get(property).status() : 424; + text(propstat, d("status"), DavProperties.status(status)); + DavException failure = bad.get(property); + if (failure != null) + DavErrors.append( + propstat, failure.condition(), failure.failingHref(), failure.requiredPrivilege()); + } + return xml(207, document); + } + + private static void validateProperty(QName name, Element value) { + if (bytes(value).length > 65536) throw new DavException(403, d("valid-property-value")); + if (name.equals(d("displayname")) + && (value.getTextContent().isBlank() + || value.getTextContent().length() > 200 + || !children(value).isEmpty())) throw new DavException(409, d("valid-property-value")); + if (name.equals(c("calendar-timezone"))) { + try { + FloatingTimezone.parse(value.getTextContent()); + } catch (QueryException failure) { + if (failure.kind() == QueryException.Kind.LIMIT_EXCEEDED) + throw new DavException( + 413, new QName("urn:busystack:params:xml:ns:dav", "calendar-complexity-limit")); + throw new DavException(409, c("valid-calendar-data")); + } catch (IllegalArgumentException failure) { + throw new DavException(409, c("valid-calendar-data")); + } + } + } + + private ResponseEntity transfer( + UUID actor, List parts, HttpServletRequest request) { + String destination = request.getHeader("Destination"); + if (destination == null) throw new DavException(400, null); + List target = paths.parse(destination); + if (target.equals(parts)) throw new DavException(403, null); + boolean move = request.getMethod().equals("MOVE"), overwrite = true; + String value = request.getHeader("Overwrite"); + if (value != null) { + if (!value.equals("T") && !value.equals("F")) throw new DavException(400, null); + overwrite = value.equals("T"); + } + String depth = request.getHeader("Depth"); + if (depth != null && !depth.equals("infinity") && !(depth.equals("0") && !move)) + throw new DavException(400, null); + DavNode source = resources.resolve(actor, parts); + boolean created; + if (source.kind() == DavNode.Kind.RESOURCE) { + if (target.size() < 4) throw new DavException(409, null); + DavNode parent = resources.resolve(actor, target.subList(0, target.size() - 1)); + if (parent.kind() != DavNode.Kind.CALENDAR) throw new DavException(409, null); + created = + store.transferResource( + actor, + source.collectionId(), + source.metadata().name(), + parent.collectionId(), + target.getLast(), + move, + overwrite, + HttpConditions.write(request, paths)); + } else if (source.kind() == DavNode.Kind.CALENDAR || source.kind() == DavNode.Kind.FOLDER) { + if (target.size() < 3 || !target.getFirst().equals("calendars")) + throw new DavException(409, null); + if (!DavResources.uuid(target.get(1)).equals(actor)) + throw new DavException( + 403, + d("need-privileges"), + paths.href(target.subList(0, target.size() - 1), true), + d("bind")); + created = + store.transfer( + actor, + source.principal(), + source.relativePath(), + DavResources.uuid(target.get(1)), + String.join("/", target.subList(2, target.size())), + move, + overwrite, + HttpConditions.write(request, paths), + !"0".equals(depth)); + } else throw new DavException(403, null); + return ResponseEntity.status(created ? 201 : 204) + .header("Location", paths.href(target, source.collection())) + .body(new byte[0]); + } + + private ResponseEntity acl(UUID actor, List parts, Element root) { + if (!is(root, DAV, "acl")) throw new DavException(400, null); + DavNode node = resources.resolve(actor, parts); + if (node.kind() != DavNode.Kind.CALENDAR) + throw new DavException(403, d("no-inherited-ace-conflict")); + if (!properties.privileges(actor, node).contains(d("write-acl"))) + throw new DavException(403, d("need-privileges"), paths.href(parts, true), d("write-acl")); + List members = new ArrayList<>(); + List aces = children(root, DAV, "ace"); + if (aces.size() > 1000) throw new DavException(403, d("limited-number-of-aces")); + Set principals = new LinkedHashSet<>(); + for (Element ace : aces) { + if (child(ace, DAV, "deny") != null) throw new DavException(403, d("grant-only")); + if (child(ace, DAV, "invert") != null) throw new DavException(403, d("no-invert")); + if (child(ace, DAV, "protected") != null || child(ace, DAV, "inherited") != null) + throw new DavException(403, d("no-protected-ace-conflict")); + Element principal = required(ace, DAV, "principal"); + var principalForms = + children( + principal, + DAV, + "href", + "all", + "authenticated", + "unauthenticated", + "self", + "property"); + if (principalForms.size() != 1) throw new DavException(400, null); + Element href = child(principal, DAV, "href"); + if (href == null) throw new DavException(403, d("allowed-principal")); + List who = paths.parse(scalarText(href)); + if (who.size() != 2 || !who.getFirst().equals("principals")) + throw new DavException(403, d("recognized-principal")); + UUID id = DavResources.uuid(who.get(1)); + if (!store.recognizedPrincipal(id)) throw new DavException(403, d("recognized-principal")); + if (id.equals(node.entry().collection().owner())) + throw new DavException(403, d("no-protected-ace-conflict")); + if (!principals.add(id)) throw new DavException(403, d("no-ace-conflict")); + Set grants = new LinkedHashSet<>(); + var privileges = children(required(ace, DAV, "grant"), DAV, "privilege"); + if (privileges.isEmpty()) throw new DavException(400, null); + for (Element privilege : privileges) { + // A privilege's ANY child is its requested QName, including unknown privileges. + // Siblings outside the known model are extensions unless no known QName exists. + List names = children(privilege); + List recognized = + names.stream() + .filter(value -> DavPrivileges.find(node.kind(), name(value)) != null) + .toList(); + if (recognized.size() > 1 || recognized.isEmpty() && names.size() != 1) + throw new DavException(400, null); + grants.add(name(recognized.isEmpty() ? names.getFirst() : recognized.getFirst())); + } + members.add( + new DavStore.Member( + id, DavPrivileges.permission(node.kind(), grants), store.isGroupPrincipal(id))); + } + store.replaceAcl(actor, node.collectionId(), members); + return ResponseEntity.ok().body(new byte[0]); + } + + private ResponseEntity report( + UUID actor, List parts, HttpServletRequest request, Element root) { + boolean freeBusyRequest = is(root, CAL, "free-busy-query"); + DavNode node; + try { + node = resources.resolve(actor, parts); + } catch (StoreException failure) { + if (freeBusyRequest && failure.kind() == StoreException.Kind.FORBIDDEN) + return error(404, null); + throw failure; + } catch (DavException failure) { + if (freeBusyRequest && failure.status() == 403) return error(404, null); + throw failure; + } + if (is(root, CAL, "calendar-query")) return query(actor, node, request, root); + if (is(root, CAL, "calendar-multiget")) return multiget(actor, node, root); + if (is(root, CAL, "free-busy-query")) return freeBusy(actor, node, request, root); + if (is(root, DAV, "sync-collection")) return sync(actor, node, request, root); + return principalReport(actor, node, request, root); + } + + private ResponseEntity query( + UUID actor, DavNode node, HttpServletRequest request, Element root) { + var filter = CalendarQueryXml.filter(root); + FloatingTimezone suppliedTimezone = queryTimezone(root); + var matcher = new CalendarQuery(QueryLimits.defaults()); + var requested = DavProperties.requested(root); + var document = document(); + Element multi = add(document, d("multistatus")); + Map timezones = timezoneCache(); + for (DavNode metadata : resources.candidates(actor, node, depth(request, 0), true, false)) { + DavNode candidate = resources.content(actor, metadata); + if (candidate.kind() == DavNode.Kind.RESOURCE) { + var timezone = + suppliedTimezone != null + ? suppliedTimezone + : timezones.computeIfAbsent( + candidate.collectionId(), id -> timezone(actor, candidate, null)); + var parsed = + CalendarContent.parse( + candidate.resource().content(), candidate.entry().collection().component()); + if (matcher.matches(parsed, filter, timezone)) + properties.response(multi, actor, candidate, requested, timezone); + } + } + return xml(207, document); + } + + private static FloatingTimezone queryTimezone(Element root) { + Element supplied = child(root, CAL, "timezone"); + if (supplied == null) return null; + try { + return FloatingTimezone.parse(scalarText(supplied)); + } catch (QueryException failure) { + if (failure.kind() == QueryException.Kind.LIMIT_EXCEEDED) throw failure; + throw new DavException(403, c("valid-calendar-data")); + } catch (IllegalArgumentException failure) { + throw new DavException(403, c("valid-calendar-data")); + } + } + + private ResponseEntity multiget(UUID actor, DavNode node, Element root) { + var document = document(); + Element multi = add(document, d("multistatus")); + var requested = DavProperties.requested(root); + var hrefs = children(root).stream().filter(value -> is(value, DAV, "href")).toList(); + if (hrefs.isEmpty() || node.kind() == DavNode.Kind.RESOURCE && hrefs.size() != 1) + throw new DavException(400, null); + int count = 0; + Map timezones = timezoneCache(); + for (Element href : hrefs) { + if (++count > 10000) throw new DavException(507, d("number-of-matches-within-limits")); + List parts = paths.parse(scalarText(href)); + boolean member = + node.kind() == DavNode.Kind.RESOURCE + ? parts.equals(node.segments()) + : parts.size() > node.segments().size() + && parts.subList(0, node.segments().size()).equals(node.segments()); + if (!member) throw new DavException(403, c("valid-calendar-object-resource")); + try { + DavNode target = resources.resolveMember(actor, parts); + if (target.kind() != DavNode.Kind.RESOURCE) throw new DavException(404, null); + properties.response( + multi, + actor, + target, + requested, + responseTimezone(actor, target, requested, root, timezones)); + } catch (DavException ex) { + if (ex.status() != 404 && ex.status() != 403) throw ex; + Element response = add(multi, d("response")); + text(response, d("href"), paths.href(parts, false)); + text(response, d("status"), DavProperties.status(ex.status())); + if (ex.condition() != null) + DavErrors.append(response, ex.condition(), ex.failingHref(), ex.requiredPrivilege()); + } + } + return xml(207, document); + } + + private ResponseEntity sync( + UUID actor, DavNode node, HttpServletRequest request, Element root) { + if (node.kind() != DavNode.Kind.CALENDAR) throw new DavException(403, d("supported-report")); + String level = scalarText(required(root, DAV, "sync-level")); + int depth = depth(request, 0); + if (depth != 0 && !(compatibility.allowSyncDepthOne() && depth == 1 && level.equals("1"))) + throw new DavException(400, null); + if (!Set.of("1", "infinite").contains(level)) + throw new DavException(403, d("sync-traversal-supported")); + required(root, DAV, "prop"); + String token = scalarText(required(root, DAV, "sync-token")); + int limit = 10000; + Element limitElement = child(root, DAV, "limit"); + if (limitElement != null) + try { + String digits = scalarText(required(limitElement, DAV, "nresults")); + if (digits.isEmpty() || !digits.chars().allMatch(value -> value >= '0' && value <= '9')) + throw new DavException(400, null); + int significant = 0; + while (significant < digits.length() - 1 && digits.charAt(significant) == '0') + significant++; + // nresults is a client ceiling, not a minimum or a server allocation request. + // Larger valid integers saturate at the server page cap without overflowing int. + limit = + digits.length() - significant > 5 + ? 10000 + : Math.min(10000, Integer.parseInt(digits.substring(significant))); + } catch (NumberFormatException ex) { + throw new DavException(400, null); + } + var requested = DavProperties.requested(root); + boolean includeContent = requested.properties().containsKey(c("calendar-data")); + FloatingTimezone timezone = + includeContent ? timezone(actor, node, null) : FloatingTimezone.of(ZoneOffset.UTC); + long pageBudget = MAX_RESPONSE - 65536L; + long[] remaining = {pageBudget}; + Map rendered = new LinkedHashMap<>(); + var page = + calendars.sync( + actor, + node.collectionId(), + token, + limit, + includeContent, + change -> { + var sizing = document(); + Element holder = add(sizing, d("multistatus")); + Element item = syncResponse(holder, actor, node, change, requested, timezone); + long cost = Math.max(bytes(sizing).length, copyWork(item)); + if (cost <= remaining[0]) { + rendered.put(change.name(), item); + remaining[0] -= cost; + } + return cost; + }, + pageBudget); + var document = document(); + Element multi = add(document, d("multistatus")); + for (var change : page.changes()) appendAdopt(multi, rendered.remove(change.name())); + if (page.truncated()) { + Element incomplete = add(multi, d("response")); + text(incomplete, d("href"), paths.href(node.segments(), true)); + text(incomplete, d("status"), DavProperties.status(507)); + add(add(incomplete, d("error")), d("number-of-matches-within-limits")); + } + text(multi, d("sync-token"), page.token()); + return xml(207, document); + } + + private Element syncResponse( + Element multi, + UUID actor, + DavNode collection, + CalendarStore.Change change, + DavProperties.Requested requested, + FloatingTimezone timezone) { + List parts = new ArrayList<>(collection.segments()); + parts.add(change.name()); + if (change.deleted()) { + Element response = add(multi, d("response")); + text(response, d("href"), paths.href(parts, false)); + text(response, d("status"), DavProperties.status(404)); + return response; + } else + return properties.response( + multi, + actor, + new DavNode( + parts, + DavNode.Kind.RESOURCE, + collection.principal(), + collection.entry(), + change.resource(), + change.metadata(), + true), + requested, + timezone); + } + + private ResponseEntity freeBusy( + UUID actor, DavNode node, HttpServletRequest request, Element root) { + if (!node.collection()) throw new DavException(403, d("supported-report")); + var range = CalendarQueryXml.range(required(root, CAL, "time-range")); + Map timezones = timezoneCache(); + Iterable parsed = + () -> + java.util.stream.StreamSupport.stream( + resources.candidates(actor, node, depth(request, 0), true, true).spliterator(), + false) + .filter(candidate -> candidate.entry().collection().component().equals("VEVENT")) + .map( + candidate -> + new CalendarFreeBusy.Source( + CalendarContent.parse( + resources.content(actor, candidate).resource().content(), "VEVENT"), + timezones.computeIfAbsent( + candidate.collectionId(), id -> timezone(actor, candidate, null)))) + .iterator(); + var periods = new CalendarFreeBusy(QueryLimits.defaults()).querySources(parsed, range); + var calendar = new net.fortuna.ical4j.model.Calendar(); + calendar.add(new net.fortuna.ical4j.model.property.ProdId("-//BusyStack//CalDAV Server//EN")); + calendar.add( + new net.fortuna.ical4j.model.property.Version( + new net.fortuna.ical4j.model.ParameterList(), "2.0")); + var busy = new net.fortuna.ical4j.model.component.VFreeBusy(range.start(), range.end()); + busy.add(new net.fortuna.ical4j.model.property.Uid(UUID.randomUUID().toString())); + for (var period : periods) { + var value = + new net.fortuna.ical4j.model.property.FreeBusy( + List.of(org.threeten.extra.Interval.of(period.start(), period.end()))); + value.add( + new net.fortuna.ical4j.model.parameter.FbType(period.type().name().replace('_', '-'))); + busy.add(value); + } + calendar.add(busy); + return ResponseEntity.ok() + .header("Content-Type", "text/calendar; charset=utf-8") + .body(calendar.toString().getBytes(StandardCharsets.UTF_8)); + } + + private static Map timezoneCache() { + return new java.util.LinkedHashMap<>(16, 0.75f, true) { + @Override + protected boolean removeEldestEntry(Map.Entry entry) { + return size() > 64; + } + }; + } + + private FloatingTimezone responseTimezone( + UUID actor, + DavNode node, + DavProperties.Requested requested, + Element report, + Map cache) { + if (requested.context() != DavProperties.Context.CALENDAR_REPORT + || node.collectionId() == null + || !requested.properties().containsKey(c("calendar-data"))) + return FloatingTimezone.of(ZoneOffset.UTC); + return cache.computeIfAbsent(node.collectionId(), id -> timezone(actor, node, report)); + } + + private FloatingTimezone timezone(UUID actor, DavNode node, Element report) { + Element timezone = report == null ? null : child(report, CAL, "timezone"); + if (timezone != null) return FloatingTimezone.parse(scalarText(timezone)); + if (node.collectionId() != null) { + String value = store.deadProperty(actor, node.collectionId(), null, c("calendar-timezone")); + if (value != null) + return FloatingTimezone.parse( + parse(value.getBytes(StandardCharsets.UTF_8)).getTextContent()); + } + return FloatingTimezone.of(ZoneOffset.UTC); + } + + private ResponseEntity principalReport( + UUID actor, DavNode node, HttpServletRequest request, Element root) { + return new DavPrincipalReports(store, resources, properties, paths) + .report(actor, node, request, root); + } + + private static int depth(HttpServletRequest request, int defaultDepth) { + if (java.util.Collections.list(request.getHeaders("Depth")).size() > 1) + throw new DavException(400, null); + String depth = request.getHeader("Depth"); + if (depth == null) return defaultDepth; + return switch (depth) { + case "0" -> 0; + case "1" -> 1; + case "infinity" -> 20; + default -> throw new DavException(400, null); + }; + } + + static ResponseEntity xml(int status, org.w3c.dom.Node document) { + return ResponseEntity.status(status) + .header("Date", HttpDateValues.format(java.time.Instant.now())) + .header("Content-Type", "application/xml; charset=utf-8") + .header("Cache-Control", "private, no-store") + .body(bytes(document)); + } + + static ResponseEntity error(int status, QName condition) { + return error(status, condition, null, null); + } + + static ResponseEntity error(int status, QName condition, String href, QName privilege) { + var document = document(); + DavErrors.append(document, condition, href, privilege); + var response = xml(status, document); + if (status != 405) return response; + return ResponseEntity.status(status) + .headers(response.getHeaders()) + .header("Allow", allow()) + .body(response.getBody()); + } + + private static ResponseEntity queryError(QueryException ex) { + return switch (ex.kind()) { + case INVALID_FILTER -> error(403, c("valid-filter")); + case UNSUPPORTED_FILTER -> error(403, c("supported-filter")); + case UNSUPPORTED_COLLATION -> error(403, c("supported-collation")); + case LIMIT_EXCEEDED -> error(507, d("number-of-matches-within-limits")); + case INVALID_DATA -> error(403, c("valid-calendar-data")); + }; + } +} diff --git a/src/main/java/org/busystack/caldav/protocol/DavErrors.java b/src/main/java/org/busystack/caldav/protocol/DavErrors.java new file mode 100644 index 0000000..e5cf6f8 --- /dev/null +++ b/src/main/java/org/busystack/caldav/protocol/DavErrors.java @@ -0,0 +1,22 @@ +package org.busystack.caldav.protocol; + +import javax.xml.namespace.QName; +import org.w3c.dom.Element; +import org.w3c.dom.Node; + +/** RFC 3744 privilege failures name the affected resource and at least one missing privilege. */ +final class DavErrors { + private DavErrors() {} + + static Element append(Node parent, QName condition, String href, QName privilege) { + Element error = DavXml.add(parent, DavXml.d("error")); + if (condition == null) return error; + if (condition.equals(DavXml.d("need-privileges"))) { + if (href == null || privilege == null) return error; + Element resource = DavXml.add(DavXml.add(error, condition), DavXml.d("resource")); + DavXml.text(resource, DavXml.d("href"), href); + DavXml.add(DavXml.add(resource, DavXml.d("privilege")), privilege); + } else DavXml.add(error, condition); + return error; + } +} diff --git a/src/main/java/org/busystack/caldav/protocol/DavException.java b/src/main/java/org/busystack/caldav/protocol/DavException.java new file mode 100644 index 0000000..a729c5f --- /dev/null +++ b/src/main/java/org/busystack/caldav/protocol/DavException.java @@ -0,0 +1,41 @@ +package org.busystack.caldav.protocol; + +import javax.xml.namespace.QName; + +/** An intentional protocol failure; messages never contain submitted calendar data. */ +public final class DavException extends RuntimeException { + private final int status; + private final QName condition; + private final String failingHref; + private final QName requiredPrivilege; + + public DavException(int status, QName condition) { + this(status, condition, null, null); + } + + public DavException(int status, QName condition, String failingHref, QName requiredPrivilege) { + super("DAV request rejected (" + status + ")"); + if ((failingHref == null) != (requiredPrivilege == null)) + throw new IllegalArgumentException("Privilege failures require a resource and privilege"); + this.status = status; + this.condition = condition; + this.failingHref = failingHref; + this.requiredPrivilege = requiredPrivilege; + } + + public int status() { + return status; + } + + public QName condition() { + return condition; + } + + public String failingHref() { + return failingHref; + } + + public QName requiredPrivilege() { + return requiredPrivilege; + } +} diff --git a/src/main/java/org/busystack/caldav/protocol/DavIf.java b/src/main/java/org/busystack/caldav/protocol/DavIf.java new file mode 100644 index 0000000..7076cac --- /dev/null +++ b/src/main/java/org/busystack/caldav/protocol/DavIf.java @@ -0,0 +1,155 @@ +package org.busystack.caldav.protocol; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.function.Function; + +/** DAV If lists are Boolean conditions, not a comma-separated HTTP entity-tag field. */ +final class DavIf { + record ResourceState(String etag, String syncToken) { + static final ResourceState ABSENT = new ResourceState(null, null); + } + + record Test(boolean negate, String etag, String stateToken) { + boolean matches(ResourceState existing) { + boolean value = + etag != null ? etag.equals(existing.etag()) : stateToken.equals(existing.syncToken()); + return negate ? !value : value; + } + } + + private final Map, List>> groups; + private final List request; + + private DavIf(Map, List>> groups, List request) { + Map, List>> copy = new LinkedHashMap<>(); + groups.forEach((key, value) -> copy.put(List.copyOf(key), List.copyOf(value))); + this.groups = java.util.Collections.unmodifiableMap(copy); + this.request = request; + } + + static DavIf parse(String input, String requestUri, DavPaths paths) { + if (input.length() > 8192) throw new DavException(400, null); + var parser = new Parser(input, requestUri, paths); + return parser.parse(); + } + + boolean matches(String existing) { + return !groups.containsKey(request) + || matches(groups.get(request), new ResourceState(existing, null)); + } + + boolean matchesAll(Function, String> etags) { + return matchesStates(parts -> new ResourceState(etags.apply(parts), null)); + } + + boolean matchesStates(Function, ResourceState> resources) { + return groups.entrySet().stream() + .allMatch(group -> matches(group.getValue(), resources.apply(group.getKey()))); + } + + private static boolean matches(List> lists, ResourceState existing) { + return lists.stream().anyMatch(list -> list.stream().allMatch(test -> test.matches(existing))); + } + + private static final class Parser { + final String input, request; + final DavPaths paths; + int position; + boolean tagged, untagged; + int tokens; + final Map, List>> groups = new LinkedHashMap<>(); + + Parser(String input, String request, DavPaths paths) { + this.input = input; + this.request = request; + this.paths = paths; + } + + DavIf parse() { + whitespace(); + List target = paths.parse(request); + while (position < input.length()) { + if (input.charAt(position) == '<') { + if (untagged) bad(); + tagged = true; + String href = angle(); + target = paths.parse(href); + whitespace(); + if (position == input.length() || input.charAt(position) != '(') bad(); + } else if (!tagged) untagged = true; + while (position < input.length() && input.charAt(position) == '(') { + groups.computeIfAbsent(target, ignored -> new ArrayList<>()).add(list()); + whitespace(); + } + if (position < input.length() && input.charAt(position) != '<') bad(); + } + if (groups.isEmpty()) bad(); + return new DavIf(groups, paths.parse(request)); + } + + List list() { + position++; + whitespace(); + List list = new ArrayList<>(); + while (position < input.length() && input.charAt(position) != ')') { + if (++tokens > 100) bad(); + boolean negate = false; + if (input.startsWith("Not", position)) { + position += 3; + if (position >= input.length() || !Character.isWhitespace(input.charAt(position))) bad(); + whitespace(); + negate = true; + } + String etag = null, stateToken = null; + if (position < input.length() && input.charAt(position) == '<') stateToken = angle(); + else if (position < input.length() && input.charAt(position) == '[') { + position++; + if (input.startsWith("W/", position)) position += 2; + if (position >= input.length() || input.charAt(position) != '"') bad(); + int start = position++; + while (position < input.length() && input.charAt(position) != '"') { + if (input.charAt(position) < 0x21 || input.charAt(position) == 0x7f) bad(); + position++; + } + if (position >= input.length()) bad(); + position++; + etag = input.substring(start, position); + if (position >= input.length() || input.charAt(position++) != ']') bad(); + } else bad(); + list.add(new Test(negate, etag, stateToken)); + whitespace(); + } + if (list.isEmpty() || position >= input.length() || input.charAt(position++) != ')') bad(); + return List.copyOf(list); + } + + String angle() { + if (input.charAt(position++) != '<') bad(); + int start = position; + while (position < input.length() && input.charAt(position) != '>') { + if (Character.isWhitespace(input.charAt(position))) bad(); + position++; + } + if (position >= input.length() || start == position) bad(); + String result = input.substring(start, position++); + try { + if (!java.net.URI.create(result).isAbsolute() && !result.startsWith("/")) bad(); + } catch (IllegalArgumentException ex) { + bad(); + } + return result; + } + + void whitespace() { + while (position < input.length() + && (input.charAt(position) == ' ' || input.charAt(position) == '\t')) position++; + } + + void bad() { + throw new DavException(400, null); + } + } +} diff --git a/src/main/java/org/busystack/caldav/protocol/DavNode.java b/src/main/java/org/busystack/caldav/protocol/DavNode.java new file mode 100644 index 0000000..9c029e2 --- /dev/null +++ b/src/main/java/org/busystack/caldav/protocol/DavNode.java @@ -0,0 +1,62 @@ +package org.busystack.caldav.protocol; + +import java.util.List; +import java.util.UUID; +import org.busystack.caldav.storage.CalendarStore; +import org.busystack.caldav.storage.DavStore; + +record DavNode( + List segments, + Kind kind, + UUID principal, + DavStore.Entry entry, + CalendarStore.Resource resource, + CalendarStore.Metadata metadata, + boolean historical) { + DavNode( + List segments, + Kind kind, + UUID principal, + DavStore.Entry entry, + CalendarStore.Resource resource) { + this( + segments, + kind, + principal, + entry, + resource, + resource == null ? null : resource.metadata(), + false); + } + + enum Kind { + ROOT, + PRINCIPALS, + PRINCIPAL, + CALENDARS, + HOME, + FOLDER, + CALENDAR, + RESOURCE + } + + DavNode { + segments = List.copyOf(segments); + } + + boolean collection() { + return kind != Kind.RESOURCE && kind != Kind.PRINCIPAL; + } + + boolean trailingSlash() { + return kind != Kind.RESOURCE; + } + + UUID collectionId() { + return entry == null ? null : entry.binding().collectionId(); + } + + String relativePath() { + return segments.size() > 2 ? String.join("/", segments.subList(2, segments.size())) : ""; + } +} diff --git a/src/main/java/org/busystack/caldav/protocol/DavPaths.java b/src/main/java/org/busystack/caldav/protocol/DavPaths.java new file mode 100644 index 0000000..e748289 --- /dev/null +++ b/src/main/java/org/busystack/caldav/protocol/DavPaths.java @@ -0,0 +1,134 @@ +package org.busystack.caldav.protocol; + +import java.io.ByteArrayOutputStream; +import java.net.URI; +import java.nio.ByteBuffer; +import java.nio.charset.CodingErrorAction; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; +import org.busystack.caldav.config.DeploymentProperties; + +/** URI segments use percent encoding, never application/x-www-form-urlencoded decoding. */ +public final class DavPaths { + private final DeploymentProperties deployment; + private final String rawPrefix; + + public DavPaths(DeploymentProperties deployment) { + this.deployment = deployment; + rawPrefix = deployment.publicBaseUrl().getRawPath() + "/dav/"; + } + + public List parse(String href) { + final URI uri; + try { + uri = URI.create(href); + } catch (IllegalArgumentException ex) { + throw new DavException(400, null); + } + if (uri.getRawQuery() != null || uri.getRawFragment() != null || uri.getRawUserInfo() != null) + throw new DavException(400, null); + if (uri.isAbsolute()) { + URI base = deployment.publicBaseUrl(); + if (!base.getScheme().equalsIgnoreCase(uri.getScheme()) + || !base.getHost().equalsIgnoreCase(uri.getHost() == null ? "" : uri.getHost()) + || effectivePort(base) != effectivePort(uri)) throw new DavException(403, null); + } else if (uri.getRawAuthority() != null) throw new DavException(400, null); + String path = uri.getRawPath(); + if (path.equals(rawPrefix.substring(0, rawPrefix.length() - 1))) return List.of(); + if (!path.startsWith(rawPrefix)) throw new DavException(403, null); + String relative = path.substring(rawPrefix.length()); + if (relative.length() > org.busystack.caldav.storage.DavNamespace.MAX_ENCODED_LENGTH + 1) + throw new DavException(414, null); + if (relative.endsWith("/")) relative = relative.substring(0, relative.length() - 1); + if (relative.isEmpty()) return List.of(); + List segments = new ArrayList<>(); + for (String segment : relative.split("/", -1)) segments.add(decode(segment)); + try { + org.busystack.caldav.storage.DavNamespace.parts(segments); + } catch (IllegalArgumentException invalid) { + throw new DavException(414, null); + } + return List.copyOf(segments); + } + + public String href(List parts, boolean collection) { + org.busystack.caldav.storage.DavNamespace.parts(parts); + return deployment.davRoot() + + String.join("/", parts.stream().map(DavPaths::encode).toList()) + + (!parts.isEmpty() && collection ? "/" : ""); + } + + public static String encode(String part) { + StringBuilder result = new StringBuilder(); + for (byte b : part.getBytes(StandardCharsets.UTF_8)) { + int value = b & 255; + if (value >= 'a' && value <= 'z' + || value >= 'A' && value <= 'Z' + || value >= '0' && value <= '9' + || value == '-' + || value == '.' + || value == '_' + || value == '~') result.append((char) value); + else + result + .append('%') + .append(Character.toUpperCase(Character.forDigit(value >>> 4, 16))) + .append(Character.toUpperCase(Character.forDigit(value & 15, 16))); + } + return result.toString(); + } + + private static String decode(String part) { + var output = new ByteArrayOutputStream(); + for (int i = 0; i < part.length(); ) { + char c = part.charAt(i); + if (c == '%') { + if (i + 2 >= part.length()) throw new DavException(400, null); + int high = Character.digit(part.charAt(i + 1), 16), + low = Character.digit(part.charAt(i + 2), 16); + if (high < 0 || low < 0) throw new DavException(400, null); + output.write((high << 4) | low); + i += 3; + } else { + int cp = part.codePointAt(i); + output.writeBytes(new String(Character.toChars(cp)).getBytes(StandardCharsets.UTF_8)); + i += Character.charCount(cp); + } + } + try { + String value = + StandardCharsets.UTF_8 + .newDecoder() + .onMalformedInput(CodingErrorAction.REPORT) + .onUnmappableCharacter(CodingErrorAction.REPORT) + .decode(ByteBuffer.wrap(output.toByteArray())) + .toString(); + if (value.isBlank() + || value.length() > 255 + || value.equals(".") + || value.equals("..") + || value + .codePoints() + .anyMatch( + c -> + c == '/' + || c == ';' + || c == 0x2028 + || c == 0x2029 + || c == 0xFFFE + || c == 0xFFFF + || c == '\\' + || Character.isISOControl(c))) throw new DavException(400, null); + return value; + } catch (java.nio.charset.CharacterCodingException ex) { + throw new DavException(400, null); + } + } + + private static int effectivePort(URI uri) { + return uri.getPort() != -1 + ? uri.getPort() + : "https".equalsIgnoreCase(uri.getScheme()) ? 443 : 80; + } +} diff --git a/src/main/java/org/busystack/caldav/protocol/DavPrincipalReports.java b/src/main/java/org/busystack/caldav/protocol/DavPrincipalReports.java new file mode 100644 index 0000000..d73a49c --- /dev/null +++ b/src/main/java/org/busystack/caldav/protocol/DavPrincipalReports.java @@ -0,0 +1,264 @@ +package org.busystack.caldav.protocol; + +import static org.busystack.caldav.protocol.DavXml.*; + +import jakarta.servlet.http.HttpServletRequest; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Set; +import java.util.UUID; +import javax.xml.namespace.QName; +import org.busystack.caldav.calendar.query.FloatingTimezone; +import org.busystack.caldav.storage.DavStore; +import org.springframework.http.ResponseEntity; +import org.w3c.dom.Element; + +/** Required DAV ACL/principal reports operate only on the caller's visible principal graph. */ +final class DavPrincipalReports { + private final DavStore store; + private final DavResources resources; + private final DavProperties properties; + private final DavPaths paths; + private int expansionWork; + + DavPrincipalReports( + DavStore store, DavResources resources, DavProperties properties, DavPaths paths) { + this.store = store; + this.resources = resources; + this.properties = properties; + this.paths = paths; + } + + ResponseEntity report( + UUID actor, DavNode node, HttpServletRequest request, Element root) { + if (is(root, DAV, "principal-search-property-set")) return searchable(request, root); + if (is(root, DAV, "expand-property")) return expand(actor, node, request, root); + List result = new ArrayList<>(); + if (is(root, DAV, "principal-property-search")) { + requireDepthZero(request); + List searches = + children(root).stream().filter(e -> is(e, DAV, "property-search")).toList(); + if (searches.isEmpty()) throw new DavException(400, null); + boolean collectionSet = child(root, DAV, "apply-to-principal-collection-set") != null; + if (!collectionSet && !node.collection()) throw new DavException(403, d("supported-report")); + for (var principal : store.principals(actor)) { + DavNode candidate = + resources.resolve(actor, List.of("principals", principal.id().toString())); + if (!collectionSet + && !(candidate.segments().size() > node.segments().size() + && candidate.segments().subList(0, node.segments().size()).equals(node.segments()))) + continue; + boolean matches = true; + for (Element search : searches) { + Element prop = required(search, DAV, "prop"), match = required(search, DAV, "match"); + if (children(prop).isEmpty() || scalarText(match).length() > 8192) + throw new DavException(400, null); + boolean propertyMatch = true; + for (Element p : children(prop)) { + propertyMatch &= + name(p).equals(d("displayname")) + && principal + .displayName() + .toLowerCase(Locale.ROOT) + .contains(scalarText(match).toLowerCase(Locale.ROOT)); + } + matches &= propertyMatch; + } + if (matches) result.add(candidate); + } + } else if (is(root, DAV, "acl-principal-prop-set")) { + requireDepthZero(request); + if (!properties.privileges(actor, node).contains(d("read-acl"))) + throw new DavException( + 403, + d("need-privileges"), + paths.href(node.segments(), node.trailingSlash()), + d("read-acl")); + Set principals = new LinkedHashSet<>(); + if (node.collectionId() != null) { + principals.add(node.entry().collection().owner()); + for (var member : store.members(actor, node.collectionId())) + principals.add(member.account()); + } else principals.add(properties.owner(actor, node)); + for (UUID principal : principals) + result.add(resources.resolve(actor, List.of("principals", principal.toString()))); + } else if (is(root, DAV, "principal-match")) { + requireDepthZero(request); + Element self = child(root, DAV, "self"), + principalProperty = child(root, DAV, "principal-property"); + if ((self == null) == (principalProperty == null)) throw new DavException(400, null); + boolean group = + node.kind() == DavNode.Kind.PRINCIPAL && store.principal(actor, node.principal()).group(); + if (!node.collection() && !(self != null && group)) + throw new DavException(403, d("supported-report")); + var candidates = node.collection() ? resources.descendants(actor, node, 20) : List.of(node); + if (self != null) { + for (DavNode candidate : candidates) + if ((!candidate.segments().equals(node.segments()) || group) + && candidate.kind() == DavNode.Kind.PRINCIPAL + && (candidate.principal().equals(actor) + || store.groupContains(actor, candidate.principal()))) result.add(candidate); + } else { + if (children(principalProperty).size() != 1) throw new DavException(400, null); + QName property = name(children(principalProperty).getFirst()); + for (DavNode candidate : candidates) { + if (candidate.segments().equals(node.segments())) continue; + var document = document(); + Element holder = add(document, d("prop")); + Element value = + properties.value( + holder, + actor, + candidate, + property, + null, + properties.dead(actor, candidate, Set.of(property)), + FloatingTimezone.of(java.time.ZoneOffset.UTC)); + if (value != null + && children(value).stream() + .anyMatch(e -> is(e, DAV, "href") && matchesPrincipal(actor, e.getTextContent()))) + result.add(candidate); + } + } + } else throw new DavException(403, d("supported-report")); + var document = document(); + Element multi = add(document, d("multistatus")); + var requested = DavProperties.requested(root); + for (DavNode candidate : result) + properties.response( + multi, actor, candidate, requested, FloatingTimezone.of(java.time.ZoneOffset.UTC)); + return DavController.xml(207, document); + } + + private ResponseEntity searchable(HttpServletRequest request, Element root) { + requireDepthZero(request); + var document = document(); + Element result = add(document, d("principal-search-property-set")), + property = add(result, d("principal-search-property")); + add(add(property, d("prop")), d("displayname")); + Element description = + text(property, d("description"), "Principal display name (case-insensitive substring)"); + description.setAttributeNS(javax.xml.XMLConstants.XML_NS_URI, "xml:lang", "en"); + return DavController.xml(200, document); + } + + private ResponseEntity expand( + UUID actor, DavNode node, HttpServletRequest request, Element root) { + for (Element property : children(root, DAV, "property")) validateExpansion(property); + var document = document(); + Element multi = add(document, d("multistatus")); + for (DavNode candidate : resources.descendants(actor, node, reportDepth(request))) + expandedResponse(multi, actor, candidate, children(root, DAV, "property"), 0); + return DavController.xml(207, document); + } + + private Element expandedResponse( + Element parent, UUID actor, DavNode node, List selectors, int depth) { + if (++expansionWork > 10000 || depth > 16) + throw new DavException(507, d("number-of-matches-within-limits")); + Map requested = new LinkedHashMap<>(); + for (Element selector : selectors) requested.put(expansionName(selector), null); + Element response = + properties.response( + parent, + actor, + node, + new DavProperties.Requested(false, false, requested), + FloatingTimezone.of(java.time.ZoneOffset.UTC)); + for (Element propstat : children(response)) + if (is(propstat, DAV, "propstat")) { + Element status = required(propstat, DAV, "status"); + if (!status.getTextContent().equals(DavProperties.status(200))) continue; + for (Element property : children(required(propstat, DAV, "prop"))) { + Element selector = + selectors.stream() + .filter(s -> expansionName(s).equals(name(property))) + .findFirst() + .orElse(null); + if (selector == null || children(selector, DAV, "property").isEmpty()) continue; + for (Element href : new ArrayList<>(children(property))) + if (is(href, DAV, "href")) { + String value = href.getTextContent(); + property.removeChild(href); + try { + expandedResponse( + property, + actor, + resources.resolveMember(actor, paths.parse(value)), + children(selector, DAV, "property"), + depth + 1); + } catch (DavException ex) { + if (ex.status() != 403 && ex.status() != 404) throw ex; + Element missing = add(property, d("response")); + text(missing, d("href"), value); + text(missing, d("status"), DavProperties.status(ex.status())); + if (ex.condition() != null) + DavErrors.append( + missing, ex.condition(), ex.failingHref(), ex.requiredPrivilege()); + } + } + } + } + return response; + } + + private static void validateExpansion(Element selector) { + if (!is(selector, DAV, "property") + || !selector.hasAttribute("name") + || selector.getAttribute("name").isBlank()) throw new DavException(400, null); + expansionName(selector); + for (Element child : children(selector, DAV, "property")) validateExpansion(child); + } + + private static QName expansionName(Element selector) { + String name = selector.getAttribute("name"), + namespace = selector.hasAttribute("namespace") ? selector.getAttribute("namespace") : DAV; + if (name.isEmpty() + || name.indexOf(':') >= 0 + || name.length() > 255 + || namespace.length() > 1024) throw new DavException(400, null); + try { + document() + .createElementNS( + namespace.isEmpty() ? null : namespace, namespace.isEmpty() ? name : "P:" + name); + } catch (org.w3c.dom.DOMException ex) { + throw new DavException(400, null); + } + return new QName(namespace, name); + } + + private boolean matchesPrincipal(UUID actor, String href) { + try { + var target = paths.parse(href); + return target.size() == 2 + && target.getFirst().equals("principals") + && (DavResources.uuid(target.get(1)).equals(actor) + || store.groupContains(actor, DavResources.uuid(target.get(1)))); + } catch (DavException unknown) { + return false; + } + } + + private static void requireDepthZero(HttpServletRequest request) { + String depth = singleDepth(request); + if (depth != null && !depth.equals("0")) throw new DavException(400, null); + } + + private static int reportDepth(HttpServletRequest request) { + String depth = singleDepth(request); + if (depth == null || depth.equals("0")) return 0; + if (depth.equals("1")) return 1; + if (depth.equals("infinity")) return 20; + throw new DavException(400, null); + } + + private static String singleDepth(HttpServletRequest request) { + if (java.util.Collections.list(request.getHeaders("Depth")).size() > 1) + throw new DavException(400, null); + return request.getHeader("Depth"); + } +} diff --git a/src/main/java/org/busystack/caldav/protocol/DavPrivileges.java b/src/main/java/org/busystack/caldav/protocol/DavPrivileges.java new file mode 100644 index 0000000..51835bb --- /dev/null +++ b/src/main/java/org/busystack/caldav/protocol/DavPrivileges.java @@ -0,0 +1,94 @@ +package org.busystack.caldav.protocol; + +import static org.busystack.caldav.protocol.DavXml.*; + +import java.util.ArrayList; +import java.util.List; +import java.util.Set; +import javax.xml.namespace.QName; + +/** The supported, effective and writable views of the application's coarse DAV privilege model. */ +final class DavPrivileges { + record Privilege( + QName name, String description, boolean abstractPrivilege, List children) { + Privilege { + children = List.copyOf(children); + } + } + + private DavPrivileges() {} + + static boolean calendarReports(DavNode.Kind kind) { + return Set.of( + DavNode.Kind.CALENDAR, DavNode.Kind.RESOURCE, DavNode.Kind.HOME, DavNode.Kind.FOLDER) + .contains(kind); + } + + private static Privilege leaf(QName name, String description, boolean abstractPrivilege) { + return new Privilege(name, description, abstractPrivilege, List.of()); + } + + static Privilege supported(DavNode.Kind kind) { + List privileges = new ArrayList<>(); + List reads = new ArrayList<>(); + reads.add(leaf(d("read-current-user-privilege-set"), "Read current privileges", true)); + if (calendarReports(kind)) + reads.add(leaf(c("read-free-busy"), "Read free/busy information", true)); + privileges.add( + new Privilege(d("read"), "Read calendar data and permitted properties", false, reads)); + if (calendarReports(kind)) { + List writes = new ArrayList<>(); + if (kind == DavNode.Kind.RESOURCE) + writes.add(leaf(d("write-content"), "Modify calendar resource data", true)); + if (kind != DavNode.Kind.HOME) + writes.add(leaf(d("write-properties"), "Modify writable properties", true)); + if (kind != DavNode.Kind.RESOURCE) { + writes.add(leaf(d("bind"), "Add collection members", true)); + writes.add(leaf(d("unbind"), "Remove collection members", true)); + } + privileges.add( + new Privilege( + d("write"), "Modify supported content or collection members", false, writes)); + } + privileges.add(leaf(d("read-acl"), "Read the permitted ACL", false)); + if (kind == DavNode.Kind.CALENDAR) + privileges.add(leaf(d("write-acl"), "Collection owner manages sharing", false)); + return new Privilege(d("all"), "All privileges applicable to this resource", false, privileges); + } + + static Privilege find(DavNode.Kind kind, QName name) { + return find(supported(kind), name); + } + + private static Privilege find(Privilege privilege, QName name) { + if (privilege.name().equals(name)) return privilege; + for (Privilege child : privilege.children()) { + Privilege found = find(child, name); + if (found != null) return found; + } + return null; + } + + static List effective(DavNode.Kind kind, String permission) { + boolean owner = permission.equals("OWNER"), writable = !permission.equals("READ"); + List result = new ArrayList<>(List.of(d("read"))); + if (writable && calendarReports(kind)) result.add(d("write")); + if (owner) { + result.add(d("read-acl")); + if (kind == DavNode.Kind.CALENDAR) result.add(d("write-acl")); + result.add(d("all")); + } + return List.copyOf(result); + } + + static String permission(DavNode.Kind kind, Set grants) { + for (QName name : grants) { + Privilege supported = find(kind, name); + if (supported == null) throw new DavException(403, d("not-supported-privilege")); + if (supported.abstractPrivilege()) throw new DavException(403, d("no-abstract")); + } + if (!grants.contains(d("read")) || !Set.of(d("read"), d("write")).containsAll(grants)) + throw new DavException(403, d("no-ace-conflict")); + return grants.contains(d("write")) ? "WRITE" : "READ"; + } +} diff --git a/src/main/java/org/busystack/caldav/protocol/DavProperties.java b/src/main/java/org/busystack/caldav/protocol/DavProperties.java new file mode 100644 index 0000000..6659d67 --- /dev/null +++ b/src/main/java/org/busystack/caldav/protocol/DavProperties.java @@ -0,0 +1,575 @@ +package org.busystack.caldav.protocol; + +import static org.busystack.caldav.protocol.DavXml.*; + +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.UUID; +import javax.xml.namespace.QName; +import org.busystack.caldav.calendar.CalendarContent; +import org.busystack.caldav.calendar.CalendarParsingInput; +import org.busystack.caldav.calendar.query.CalendarDataProjection; +import org.busystack.caldav.calendar.query.FloatingTimezone; +import org.busystack.caldav.calendar.query.QueryLimits; +import org.busystack.caldav.storage.CalendarStore; +import org.busystack.caldav.storage.DavStore; +import org.springframework.stereotype.Service; +import org.w3c.dom.Element; +import org.w3c.dom.Node; + +@Service +final class DavProperties { + enum Context { + PROPERTIES, + CALENDAR_REPORT + } + + record Requested( + boolean all, + boolean names, + Map properties, + CalendarDataProjection projection, + Context context) { + Requested(boolean all, boolean names, Map properties) { + this( + all, + names, + properties, + new CalendarDataProjection(QueryLimits.defaults()), + Context.PROPERTIES); + } + + Requested { + properties = java.util.Collections.unmodifiableMap(new LinkedHashMap<>(properties)); + } + } + + private static final List COMMON = + List.of( + "resourcetype", + "displayname", + "owner", + "group", + "current-user-principal", + "principal-collection-set", + "supported-report-set", + "current-user-privilege-set", + "supported-privilege-set", + "acl-restrictions", + "inherited-acl-set"); + private static final Set WRITABLE = + Set.of(d("displayname"), c("calendar-description"), c("calendar-timezone")); + private final DavStore store; + private final DavPaths paths; + private final CalendarStore calendars; + + DavProperties( + DavStore store, + CalendarStore calendars, + org.busystack.caldav.config.DeploymentProperties deployment) { + this.store = store; + this.calendars = calendars; + this.paths = new DavPaths(deployment); + } + + static Requested requested(Element root) { + if (root == null) return new Requested(true, false, Map.of()); + Element prop = child(root, DAV, "prop"), + all = child(root, DAV, "allprop"), + names = child(root, DAV, "propname"), + include = child(root, DAV, "include"); + if ((prop != null ? 1 : 0) + (all != null ? 1 : 0) + (names != null ? 1 : 0) > 1 + || include != null && all == null) throw new DavException(400, null); + Map selection = new LinkedHashMap<>(); + if (prop != null) + for (Element p : children(prop)) { + if (selection.put(name(p), p) != null) throw new DavException(400, null); + } + if (include != null) for (Element p : children(include)) selection.put(name(p), p); + Context context = + is(root, CAL, "calendar-query") + || is(root, CAL, "calendar-multiget") + || is(root, DAV, "sync-collection") + ? Context.CALENDAR_REPORT + : Context.PROPERTIES; + return new Requested( + prop == null && names == null, + names != null, + selection, + new CalendarDataProjection(QueryLimits.defaults()), + context); + } + + Element response( + Element multistatus, + UUID actor, + DavNode node, + Requested requested, + FloatingTimezone timezone) { + Element response = add(multistatus, d("response")); + text(response, d("href"), paths.href(node.segments(), node.trailingSlash())); + Map dead = + requested.names() + ? Map.of() + : dead(actor, node, requested.all() ? null : requested.properties().keySet()); + Set names = new LinkedHashSet<>(); + if (requested.all() || requested.names()) { + names.addAll(definedNames(actor, node)); + if (requested.all()) + names.removeAll( + Set.of( + d("acl"), + d("sync-token"), + d("owner"), + d("group"), + d("supported-privilege-set"), + d("current-user-privilege-set"), + d("acl-restrictions"), + d("inherited-acl-set"))); + names.addAll(requested.names() ? deadNames(actor, node) : dead.keySet()); + } + names.addAll(requested.properties().keySet()); + Map> statuses = new LinkedHashMap<>(); + Map failures = new LinkedHashMap<>(); + for (QName property : names) { + // Build on the response document's shared work budget and move the completed node, + // avoiding a second DOM copy for every property. + Element holder = multistatus.getOwnerDocument().createElementNS(DAV, "D:prop"); + int status = 200; + try { + Element value = + requested.names() + ? add(holder, property) + : value( + holder, + actor, + node, + property, + requested.properties().get(property), + dead, + timezone, + requested.projection(), + requested.context()); + if (value == null) { + status = 404; + add(holder, property); + } + } catch (DavException ex) { + if (ex.status() != 403) throw ex; + status = 403; + failures.putIfAbsent(status, ex); + while (holder.hasChildNodes()) holder.removeChild(holder.getFirstChild()); + add(holder, property); + } + statuses + .computeIfAbsent(status, ignored -> new ArrayList<>()) + .add((Element) holder.getFirstChild()); + } + // A successful changed member requires a propstat even for an explicit empty prop set. + if (statuses.isEmpty()) statuses.put(200, List.of()); + for (var entry : statuses.entrySet()) { + Element propstat = add(response, d("propstat")), prop = add(propstat, d("prop")); + for (Element value : entry.getValue()) prop.appendChild(value); + text(propstat, d("status"), status(entry.getKey())); + var failure = failures.get(entry.getKey()); + if (failure != null) + DavErrors.append( + propstat, failure.condition(), failure.failingHref(), failure.requiredPrivilege()); + } + return response; + } + + private Set definedNames(UUID actor, DavNode node) { + Set names = new LinkedHashSet<>(); + for (String local : COMMON) names.add(d(local)); + if (owner(actor, node).equals(actor)) names.add(d("acl")); + if (DavPrivileges.calendarReports(node.kind())) names.add(c("supported-collation-set")); + if (node.kind() == DavNode.Kind.RESOURCE) { + names.add(d("getetag")); + if (node.metadata().modifiedAt() != null) names.add(d("getlastmodified")); + names.add(d("getcontenttype")); + names.add(d("getcontentlength")); + } + if (node.kind() == DavNode.Kind.CALENDAR) { + for (String local : + List.of( + "supported-calendar-component-set", "supported-calendar-data", "max-resource-size")) + names.add(c(local)); + names.add(d("sync-token")); + } + if (node.kind() == DavNode.Kind.PRINCIPAL) { + names.add(d("principal-URL")); + names.add(d("alternate-URI-set")); + names.add(d("group-membership")); + if (store.principal(actor, node.principal()).group()) names.add(d("group-member-set")); + else names.add(c("calendar-home-set")); + } + return names; + } + + Map dead(UUID actor, DavNode node) { + return dead(actor, node, null); + } + + Map dead(UUID actor, DavNode node, Set selected) { + if (selected != null) { + selected = + selected.stream() + .filter(property -> !liveOnly(property)) + .collect(java.util.stream.Collectors.toCollection(LinkedHashSet::new)); + if (selected.isEmpty()) return Map.of(); + } + if (node.entry() == null) return Map.of(); + if (node.collectionId() != null) { + if (node.historical()) + return calendars.historicalProperties( + actor, + node.collectionId(), + node.metadata().name(), + node.metadata().revision(), + selected); + return store.deadProperties( + actor, + node.collectionId(), + node.metadata() == null ? null : node.metadata().name(), + selected); + } + return store.folderProperties(actor, node.principal(), node.relativePath(), selected); + } + + private Set deadNames(UUID actor, DavNode node) { + if (node.entry() == null) return Set.of(); + if (node.collectionId() != null) { + if (node.historical()) + return calendars.historicalPropertyNames( + actor, node.collectionId(), node.metadata().name(), node.metadata().revision()); + return store.deadPropertyNames( + actor, node.collectionId(), node.metadata() == null ? null : node.metadata().name()); + } + return store.folderPropertyNames(actor, node.principal(), node.relativePath()); + } + + Element value( + Element parent, + UUID actor, + DavNode node, + QName property, + Element selector, + Map dead, + FloatingTimezone timezone) { + return value( + parent, + actor, + node, + property, + selector, + dead, + timezone, + new CalendarDataProjection(QueryLimits.defaults()), + Context.PROPERTIES); + } + + private Element value( + Element parent, + UUID actor, + DavNode node, + QName property, + Element selector, + Map dead, + FloatingTimezone timezone, + CalendarDataProjection projection, + Context context) { + if (property.equals(c("calendar-data")) && context != Context.CALENDAR_REPORT) return null; + if (dead.containsKey(property)) { + Element stored = parse(dead.get(property).getBytes(StandardCharsets.UTF_8)); + return appendCopy(parent, stored); + } + if (property.equals(d("resourcetype"))) { + Element value = add(parent, property); + if (node.collection()) add(value, d("collection")); + if (node.kind() == DavNode.Kind.CALENDAR) add(value, c("calendar")); + if (node.kind() == DavNode.Kind.PRINCIPAL) add(value, d("principal")); + return value; + } + if (property.equals(d("displayname"))) return text(parent, property, displayName(actor, node)); + if (property.equals(d("current-user-principal"))) + return href(parent, property, principal(actor)); + if (property.equals(d("principal-collection-set"))) + return href(parent, property, paths.href(List.of("principals"), true)); + if (property.equals(d("owner"))) return href(parent, property, principal(owner(actor, node))); + if (property.equals(d("group"))) return add(parent, property); + if (property.equals(c("supported-collation-set")) + && DavPrivileges.calendarReports(node.kind())) { + Element value = add(parent, property); + text(value, c("supported-collation"), "i;ascii-casemap"); + text(value, c("supported-collation"), "i;octet"); + return value; + } + if (property.equals(d("supported-report-set"))) { + Element value = add(parent, property); + List reports = + new ArrayList<>( + List.of( + d("expand-property"), + d("principal-property-search"), + d("principal-search-property-set"), + d("acl-principal-prop-set"))); + if (node.collection() + || (node.kind() == DavNode.Kind.PRINCIPAL + && store.principal(actor, node.principal()).group())) + reports.add(d("principal-match")); + if (DavPrivileges.calendarReports(node.kind())) + reports.addAll(List.of(c("calendar-query"), c("calendar-multiget"))); + if (Set.of(DavNode.Kind.CALENDAR, DavNode.Kind.FOLDER, DavNode.Kind.HOME) + .contains(node.kind())) reports.add(c("free-busy-query")); + if (node.kind() == DavNode.Kind.CALENDAR) reports.add(d("sync-collection")); + for (QName report : reports) add(add(add(value, d("supported-report")), d("report")), report); + return value; + } + if (property.equals(d("current-user-privilege-set"))) { + Element value = add(parent, property); + for (QName privilege : privileges(actor, node)) add(add(value, d("privilege")), privilege); + return value; + } + if (property.equals(d("supported-privilege-set"))) + return supportedPrivileges(parent, property, node); + if (property.equals(d("acl-restrictions"))) { + Element value = add(parent, property); + add(value, d("grant-only")); + add(value, d("no-invert")); + add(add(add(value, d("required-principal")), d("property")), d("owner")); + return value; + } + if (property.equals(d("inherited-acl-set"))) { + Element value = add(parent, property); + if (node.kind() == DavNode.Kind.RESOURCE) { + var parts = node.segments().subList(0, node.segments().size() - 1); + text(value, d("href"), paths.href(parts, true)); + } + return value; + } + if (property.equals(d("acl"))) return acl(parent, actor, node, property); + if (node.kind() == DavNode.Kind.PRINCIPAL) { + if (property.equals(d("principal-URL"))) + return href(parent, property, principal(node.principal())); + if (property.equals(d("alternate-URI-set"))) return add(parent, property); + if (property.equals(d("group-member-set"))) { + if (!store.principal(actor, node.principal()).group()) return null; + Element value = add(parent, property); + for (var member : store.groupMembers(actor, node.principal())) + text(value, d("href"), principal(member.id())); + return value; + } + if (property.equals(d("group-membership"))) { + Element value = add(parent, property); + for (var group : store.principalGroups(actor, node.principal())) + text(value, d("href"), principal(group.id())); + return value; + } + if (property.equals(c("calendar-home-set"))) { + if (store.principal(actor, node.principal()).group()) return null; + return href( + parent, property, paths.href(List.of("calendars", node.principal().toString()), true)); + } + } + if (node.kind() == DavNode.Kind.RESOURCE) { + if (property.equals(d("getetag"))) return text(parent, property, node.metadata().etag()); + if (property.equals(d("getlastmodified")) && node.metadata().modifiedAt() != null) + return text( + parent, + property, + HttpDateValues.format( + org.busystack.caldav.storage.ModificationDates.effective( + node.metadata().modifiedAt(), java.time.Instant.now()))); + if (property.equals(d("getcontenttype"))) + return text(parent, property, "text/calendar; charset=utf-8"); + if (property.equals(d("getcontentlength"))) + return text(parent, property, Long.toString(node.metadata().contentLength())); + if (property.equals(c("calendar-data"))) { + CalendarStore.Resource resource = node.resource(); + if (resource == null) { + if (node.historical()) + throw new IllegalStateException( + "Historical calendar data was not selected by synchronization"); + resource = calendars.get(actor, node.collectionId(), node.metadata().name()); + if (resource == null) throw new DavException(404, null); + } + String content; + var selection = selector == null ? null : CalendarQueryXml.selection(selector); + if (selector == null + || children( + selector, CAL, "comp", "expand", "limit-recurrence-set", "limit-freebusy-set") + .isEmpty()) content = CalendarParsingInput.reportText(resource.content()); + else + content = + projection + .project( + CalendarContent.parse( + resource.content(), node.entry().collection().component()), + selection, + timezone) + .toString(); + return text(parent, property, content); + } + } + if (node.kind() == DavNode.Kind.CALENDAR) { + if (property.equals(d("sync-token"))) + return text(parent, property, calendars.currentSyncToken(actor, node.collectionId())); + if (property.equals(c("supported-calendar-component-set"))) { + Element value = add(parent, property); + add(value, c("comp")).setAttribute("name", node.entry().collection().component()); + return value; + } + if (property.equals(c("supported-calendar-data"))) { + Element value = add(parent, property), data = add(value, c("calendar-data")); + data.setAttribute("content-type", "text/calendar"); + data.setAttribute("version", "2.0"); + return value; + } + if (property.equals(c("max-resource-size"))) + return text(parent, property, Integer.toString(CalendarContent.MAX_BYTES)); + } + return null; + } + + String displayName(UUID actor, DavNode node) { + if (node.entry() != null) + return node.kind() == DavNode.Kind.RESOURCE + ? node.metadata().name() + : node.entry().binding().displayName(); + if (node.kind() == DavNode.Kind.PRINCIPAL) + return store.principal(actor, node.principal()).displayName(); + return switch (node.kind()) { + case ROOT -> "BusyStack CalDAV"; + case PRINCIPALS -> "Principals"; + case CALENDARS -> "Calendar homes"; + case HOME -> "Calendars and tasks"; + default -> ""; + }; + } + + List privileges(UUID actor, DavNode node) { + String permission = + node.collectionId() == null + ? (owner(actor, node).equals(actor) ? "OWNER" : "READ") + : store.permission(actor, node.collectionId()); + return DavPrivileges.effective(node.kind(), permission); + } + + private Element acl(Element parent, UUID actor, DavNode node, QName property) { + UUID owner = owner(actor, node); + if (!owner.equals(actor)) + throw new DavException( + 403, + d("need-privileges"), + paths.href(node.segments(), node.trailingSlash()), + d("read-acl")); + Element value = add(parent, property); + Element ownerAce = add(value, d("ace")); + add(add(add(ownerAce, d("principal")), d("property")), d("owner")); + add(add(add(ownerAce, d("grant")), d("privilege")), d("all")); + add(ownerAce, d("protected")); + if (node.collectionId() != null) + for (var member : store.members(actor, node.collectionId())) { + Element ace = add(value, d("ace")); + text(add(ace, d("principal")), d("href"), principal(member.account())); + Element grant = add(ace, d("grant")); + for (QName privilege : List.of(d("read"))) add(add(grant, d("privilege")), privilege); + if (member.permission().equals("WRITE")) add(add(grant, d("privilege")), d("write")); + if (node.kind() == DavNode.Kind.RESOURCE) + text( + add(ace, d("inherited")), + d("href"), + paths.href(node.segments().subList(0, node.segments().size() - 1), true)); + } + return value; + } + + private Element supportedPrivileges(Element parent, QName property, DavNode node) { + Element value = add(parent, property); + supported(value, DavPrivileges.supported(node.kind())); + return value; + } + + private void supported(Element parent, DavPrivileges.Privilege privilege) { + Element value = add(parent, d("supported-privilege")); + add(add(value, d("privilege")), privilege.name()); + if (privilege.abstractPrivilege()) add(value, d("abstract")); + Element description = text(value, d("description"), privilege.description()); + description.setAttributeNS(javax.xml.XMLConstants.XML_NS_URI, "xml:lang", "en"); + for (var child : privilege.children()) supported(value, child); + } + + UUID owner(UUID actor, DavNode node) { + if (node.kind() == DavNode.Kind.PRINCIPAL) + return store.principal(actor, node.principal()).owner(); + return node.entry() != null && node.entry().collection() != null + ? node.entry().collection().owner() + : node.principal() == null ? actor : node.principal(); + } + + private String principal(UUID id) { + return paths.href(List.of("principals", id.toString()), true); + } + + private static Element href(Node parent, QName property, String href) { + Element value = add(parent, property); + text(value, d("href"), href); + return value; + } + + static String status(int code) { + return "HTTP/1.1 " + + code + + " " + + switch (code) { + case 200 -> "OK"; + case 403 -> "Forbidden"; + case 404 -> "Not Found"; + case 409 -> "Conflict"; + case 424 -> "Failed Dependency"; + case 507 -> "Insufficient Storage"; + default -> "Error"; + }; + } + + private static boolean liveOnly(QName property) { + if (WRITABLE.contains(property)) return false; + if (DAV.equals(property.getNamespaceURI())) + return COMMON.contains(property.getLocalPart()) + || Set.of( + "acl", + "principal-URL", + "alternate-URI-set", + "group-member-set", + "group-membership", + "getetag", + "getlastmodified", + "getcontenttype", + "getcontentlength", + "sync-token") + .contains(property.getLocalPart()); + return CAL.equals(property.getNamespaceURI()) + && Set.of( + "calendar-data", + "calendar-home-set", + "supported-calendar-component-set", + "supported-calendar-data", + "max-resource-size", + "supported-collation-set") + .contains(property.getLocalPart()); + } + + static boolean protectedProperty(QName property) { + return !WRITABLE.contains(property) + && (DAV.equals(property.getNamespaceURI()) + || CAL.equals(property.getNamespaceURI()) + && !Set.of("calendar-description", "calendar-timezone") + .contains(property.getLocalPart())); + } +} diff --git a/src/main/java/org/busystack/caldav/protocol/DavResources.java b/src/main/java/org/busystack/caldav/protocol/DavResources.java new file mode 100644 index 0000000..b7a76ba --- /dev/null +++ b/src/main/java/org/busystack/caldav/protocol/DavResources.java @@ -0,0 +1,264 @@ +package org.busystack.caldav.protocol; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; +import org.busystack.caldav.storage.CalendarStore; +import org.busystack.caldav.storage.DavStore; +import org.busystack.caldav.storage.StoreException; +import org.springframework.stereotype.Service; + +@Service +final class DavResources { + private final DavStore store; + private final CalendarStore calendars; + private final DavPaths paths; + + DavResources( + DavStore store, + CalendarStore calendars, + org.busystack.caldav.config.DeploymentProperties deployment) { + this.store = store; + this.calendars = calendars; + this.paths = new DavPaths(deployment); + } + + DavNode resolve(UUID actor, List parts) { + if (parts.isEmpty()) return new DavNode(parts, DavNode.Kind.ROOT, actor, null, null); + if (parts.getFirst().equals("principals")) { + if (parts.size() == 1) return new DavNode(parts, DavNode.Kind.PRINCIPALS, actor, null, null); + if (parts.size() != 2) throw new DavException(404, null); + UUID principal = uuid(parts.get(1)); + try { + store.principal(actor, principal); + } catch (StoreException missing) { + if (missing.kind() == StoreException.Kind.NOT_FOUND) throw new DavException(404, null); + throw missing; + } + return new DavNode(parts, DavNode.Kind.PRINCIPAL, principal, null, null); + } + if (!parts.getFirst().equals("calendars")) throw new DavException(404, null); + if (parts.size() == 1) return new DavNode(parts, DavNode.Kind.CALENDARS, actor, null, null); + UUID home = uuid(parts.get(1)); + if (!home.equals(actor)) + throw new DavException( + 403, DavXml.d("need-privileges"), paths.href(parts, parts.size() == 2), DavXml.d("read")); + if (parts.size() == 2) return new DavNode(parts, DavNode.Kind.HOME, home, null, null); + String relative = String.join("/", parts.subList(2, parts.size())); + try { + var entry = store.resolve(actor, home, relative); + return new DavNode( + parts, + entry.binding().calendar() ? DavNode.Kind.CALENDAR : DavNode.Kind.FOLDER, + home, + entry, + null); + } catch (StoreException ex) { + if (ex.kind() != StoreException.Kind.NOT_FOUND) throw ex; + } + if (parts.size() < 4) throw new DavException(404, null); + String parent = String.join("/", parts.subList(2, parts.size() - 1)); + var entry = store.resolve(actor, home, parent); + if (!entry.binding().calendar()) throw new DavException(404, null); + var resource = calendars.metadata(actor, entry.binding().collectionId(), parts.getLast()); + if (resource == null) throw new DavException(404, null); + return new DavNode(parts, DavNode.Kind.RESOURCE, home, entry, null, resource, false); + } + + /** Only expected member lookup failures become per-resource report statuses. */ + DavNode resolveMember(UUID actor, List parts) { + try { + return resolve(actor, parts); + } catch (StoreException unavailable) { + if (unavailable.kind() == StoreException.Kind.NOT_FOUND) throw new DavException(404, null); + if (unavailable.kind() == StoreException.Kind.FORBIDDEN) + throw new DavException( + 403, DavXml.d("need-privileges"), paths.href(parts, false), DavXml.d("read")); + throw unavailable; + } + } + + DavNode content(UUID actor, DavNode node) { + if (node.kind() != DavNode.Kind.RESOURCE || node.resource() != null) return node; + var body = calendars.get(actor, node.collectionId(), node.metadata().name()); + if (body == null) throw new DavException(404, null); + return new DavNode(node.segments(), node.kind(), node.principal(), node.entry(), body); + } + + List children(UUID actor, DavNode parent) { + List result = new ArrayList<>(); + switch (parent.kind()) { + case ROOT -> { + result.add(resolve(actor, List.of("principals"))); + result.add(resolve(actor, List.of("calendars"))); + } + case PRINCIPALS -> { + for (var principal : store.principals(actor)) + result.add( + new DavNode( + List.of("principals", principal.id().toString()), + DavNode.Kind.PRINCIPAL, + principal.id(), + null, + null)); + } + case CALENDARS -> result.add(resolve(actor, List.of("calendars", actor.toString()))); + case HOME, FOLDER -> { + for (var entry : store.children(actor, parent.principal(), parent.relativePath())) { + List segments = + new ArrayList<>(List.of("calendars", parent.principal().toString())); + segments.addAll(List.of(entry.binding().path().split("/"))); + result.add( + new DavNode( + segments, + entry.binding().calendar() ? DavNode.Kind.CALENDAR : DavNode.Kind.FOLDER, + parent.principal(), + entry, + null)); + } + } + case CALENDAR -> { + var metadata = calendars.metadataBatch(actor, parent.collectionId(), "", 10001); + if (metadata.size() > 10000) + throw new DavException(507, DavXml.d("number-of-matches-within-limits")); + for (var resource : metadata) { + List segments = new ArrayList<>(parent.segments()); + segments.add(resource.name()); + result.add( + new DavNode( + segments, + DavNode.Kind.RESOURCE, + parent.principal(), + parent.entry(), + null, + resource, + false)); + } + } + default -> {} + } + return List.copyOf(result); + } + + List descendants(UUID actor, DavNode node, int depth) { + List result = new ArrayList<>(); + walk(actor, node, depth, result); + return List.copyOf(result); + } + + private void walk(UUID actor, DavNode node, int depth, List result) { + if (result.size() >= 10000) + throw new DavException(507, DavXml.d("number-of-matches-within-limits")); + result.add(node); + if (depth > 0 && node.collection()) + for (var child : children(actor, node)) walk(actor, child, depth - 1, result); + } + + /** Queries scan bounded metadata batches; PROPFIND retains its complete-response row limit. */ + Iterable candidates(UUID actor, DavNode root, int depth) { + return candidates(actor, root, depth, false, false); + } + + Iterable candidates( + UUID actor, DavNode root, int depth, boolean content, boolean eventsOnly) { + return () -> + new java.util.Iterator<>() { + record Frame(java.util.Iterator nodes, int depth) {} + + final java.util.Deque frames = + new java.util.ArrayDeque<>(List.of(new Frame(List.of(root).iterator(), depth))); + DavNode ready; + int examined; + + public boolean hasNext() { + while (ready == null && !frames.isEmpty()) { + Frame frame = frames.peek(); + if (!frame.nodes().hasNext()) { + frames.pop(); + continue; + } + DavNode node = frame.nodes().next(); + if (++examined > 100000) + throw new DavException(507, DavXml.d("number-of-matches-within-limits")); + if (node.kind() == DavNode.Kind.RESOURCE) { + ready = node; + } else if (node.collection() && frame.depth() > 0) { + if (eventsOnly + && node.kind() == DavNode.Kind.CALENDAR + && !node.entry().collection().component().equals("VEVENT")) continue; + var iterator = + node.kind() == DavNode.Kind.CALENDAR + ? calendarChildren(actor, node, content) + : children(actor, node).iterator(); + frames.push(new Frame(iterator, frame.depth() - 1)); + } + } + return ready != null; + } + + public DavNode next() { + if (!hasNext()) throw new java.util.NoSuchElementException(); + DavNode result = ready; + ready = null; + return result; + } + }; + } + + private java.util.Iterator calendarChildren( + UUID actor, DavNode calendar, boolean content) { + return new java.util.Iterator<>() { + List batch = List.of(); + int position; + String after = ""; + boolean complete; + + public boolean hasNext() { + if (position == batch.size() && !complete) { + batch = + content + ? calendars.resourceBatch(actor, calendar.collectionId(), after).stream() + .map(item -> candidate(calendar, item.metadata(), item)) + .toList() + : calendars.metadataBatch(actor, calendar.collectionId(), after, 128).stream() + .map(item -> candidate(calendar, item, null)) + .toList(); + position = 0; + complete = batch.isEmpty(); + } + return position < batch.size(); + } + + public DavNode next() { + if (!hasNext()) throw new java.util.NoSuchElementException(); + var item = batch.get(position++); + after = item.metadata().name(); + return item; + } + }; + } + + private static DavNode candidate( + DavNode collection, CalendarStore.Metadata metadata, CalendarStore.Resource resource) { + var parts = new ArrayList<>(collection.segments()); + parts.add(metadata.name()); + return new DavNode( + parts, + DavNode.Kind.RESOURCE, + collection.principal(), + collection.entry(), + resource, + metadata, + false); + } + + static UUID uuid(String text) { + try { + var id = UUID.fromString(text); + if (!id.toString().equals(text)) throw new DavException(404, null); + return id; + } catch (IllegalArgumentException ex) { + throw new DavException(404, null); + } + } +} diff --git a/src/main/java/org/busystack/caldav/protocol/DavXml.java b/src/main/java/org/busystack/caldav/protocol/DavXml.java new file mode 100644 index 0000000..b9c59a0 --- /dev/null +++ b/src/main/java/org/busystack/caldav/protocol/DavXml.java @@ -0,0 +1,335 @@ +package org.busystack.caldav.protocol; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.util.ArrayList; +import java.util.List; +import javax.xml.XMLConstants; +import javax.xml.namespace.QName; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; +import org.busystack.caldav.xml.XmlCharacters; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.DefaultHandler; + +/** + * Namespace-aware XML with no DTDs, entity expansion, external access or recursive unbounded input. + */ +public final class DavXml { + public static final String DAV = "DAV:"; + public static final String CAL = "urn:ietf:params:xml:ns:caldav"; + public static final int MAX_REQUEST = 1048576; + public static final int MAX_RESPONSE = 16777216; + public static final int MAX_RESPONSE_WORK = MAX_RESPONSE; + + private static final String BUDGET_KEY = DavXml.class.getName() + ".budget"; + private static final int MAX_RESPONSE_NODES = 50000; + + private static final class Budget { + long work; + int nodes; + + void reserve(long additional, int addedNodes) { + if (additional > MAX_RESPONSE - work || addedNodes > MAX_RESPONSE_NODES - nodes) + throw new DavException(507, d("number-of-matches-within-limits")); + work += additional; + nodes += addedNodes; + } + } + + private static Budget budget(Node parent) { + Document document = parent instanceof Document d ? d : parent.getOwnerDocument(); + Budget budget = (Budget) document.getUserData(BUDGET_KEY); + if (budget == null) { + budget = new Budget(); + document.setUserData(BUDGET_KEY, budget, null); + } + return budget; + } + + /** Conservative DOM and serialized-text work: UTF-16 storage plus escaped UTF-8 output. */ + private static long textWork(String value) { + long result = 2L * value.length(); + for (int i = 0; i < value.length(); ) { + int cp = value.codePointAt(i); + i += Character.charCount(cp); + result += + switch (cp) { + case '&' -> 5; + case '<', '>' -> 4; + case '\"', '\'', '\n', '\r', '\t' -> 6; + default -> cp < 128 ? 1 : cp < 2048 ? 2 : cp < 65536 ? 3 : 4; + }; + } + return result; + } + + private static void reserveCopy(Budget budget, Node source) { + budget.reserve(256 + textWork(source.getNodeName()), 1); + if (source.getNodeValue() != null) budget.reserve(textWork(source.getNodeValue()), 0); + var attributes = source.getAttributes(); + if (attributes != null) + for (int i = 0; i < attributes.getLength(); i++) { + Node attribute = attributes.item(i); + budget.reserve( + 128 + textWork(attribute.getNodeName()) + textWork(attribute.getNodeValue()), 1); + } + for (Node child = source.getFirstChild(); child != null; child = child.getNextSibling()) + reserveCopy(budget, child); + } + + public static long constructionWork(Node node) { + Budget used = budget(node); + return Math.max( + used.work, + used.nodes * ((MAX_RESPONSE_WORK + MAX_RESPONSE_NODES - 1L) / MAX_RESPONSE_NODES)); + } + + public static long copyWork(Node node) { + Budget estimate = new Budget(); + reserveCopy(estimate, node); + return Math.max( + estimate.work, + estimate.nodes * ((MAX_RESPONSE_WORK + MAX_RESPONSE_NODES - 1L) / MAX_RESPONSE_NODES)); + } + + /** Validate the complete subtree before allocating its imported DOM copy. */ + public static Element appendCopy(Node parent, Element source) { + reserveCopy(budget(parent), source); + Document document = parent instanceof Document d ? d : parent.getOwnerDocument(); + Element result = (Element) document.importNode(source, true); + parent.appendChild(result); + return result; + } + + /** Transfer an already bounded response subtree without allocating a duplicate DOM. */ + public static Element appendAdopt(Node parent, Element source) { + reserveCopy(budget(parent), source); + Document document = parent instanceof Document d ? d : parent.getOwnerDocument(); + Node result = document.adoptNode(source); + if (!(result instanceof Element element)) + throw new IllegalStateException("XML DOM adoption unavailable"); + parent.appendChild(element); + return element; + } + + /** A detached dead property retains its effective xml:lang, including an explicit empty value. */ + public static byte[] propertyBytes(Element property) { + Element copy = (Element) property.cloneNode(true); + java.util.Set prefixes = new java.util.HashSet<>(); + for (Node context = property; + context instanceof Element element; + context = context.getParentNode()) { + var attributes = element.getAttributes(); + for (int i = 0; i < attributes.getLength(); i++) { + Node attribute = attributes.item(i); + if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(attribute.getNamespaceURI()) + && prefixes.add(attribute.getLocalName())) + copy.setAttributeNS( + XMLConstants.XMLNS_ATTRIBUTE_NS_URI, + attribute.getNodeName(), + attribute.getNodeValue()); + } + } + for (Node context = property; + context instanceof Element element; + context = context.getParentNode()) + if (element.hasAttributeNS(XMLConstants.XML_NS_URI, "lang")) { + copy.setAttributeNS( + XMLConstants.XML_NS_URI, + "xml:lang", + element.getAttributeNS(XMLConstants.XML_NS_URI, "lang")); + break; + } + return bytes(copy); + } + + private DavXml() {} + + private static DocumentBuilderFactory factory() + throws javax.xml.parsers.ParserConfigurationException { + var factory = DocumentBuilderFactory.newInstance(); + factory.setNamespaceAware(true); + factory.setXIncludeAware(false); + factory.setExpandEntityReferences(false); + factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); + factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); + factory.setFeature("http://xml.org/sax/features/external-general-entities", false); + factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); + factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, ""); + factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, ""); + factory.setAttribute("jdk.xml.maxElementDepth", "32"); + factory.setAttribute("jdk.xml.elementAttributeLimit", "100"); + factory.setAttribute("jdk.xml.maxXMLNameLimit", "256"); + return factory; + } + + public static Document document() { + try { + return factory().newDocumentBuilder().newDocument(); + } catch (javax.xml.parsers.ParserConfigurationException ex) { + throw new IllegalStateException("XML parser unavailable", ex); + } + } + + public static Element parse(byte[] bytes) { + if (bytes.length == 0 || bytes.length > MAX_REQUEST) + throw new DavException(bytes.length == 0 ? 400 : 413, null); + try { + var builder = factory().newDocumentBuilder(); + builder.setErrorHandler( + new DefaultHandler() { + @Override + public void error(org.xml.sax.SAXParseException ex) throws SAXException { + throw ex; + } + + @Override + public void fatalError(org.xml.sax.SAXParseException ex) throws SAXException { + throw ex; + } + }); + Element root = builder.parse(new ByteArrayInputStream(bytes)).getDocumentElement(); + if (count(root) > 20000) throw new DavException(413, null); + return root; + } catch (org.xml.sax.SAXException | java.io.IOException ex) { + throw new DavException(400, null); + } catch (javax.xml.parsers.ParserConfigurationException ex) { + throw new IllegalStateException("XML parser unavailable", ex); + } + } + + private static int count(Element root) { + int total = 1; + for (Element child : children(root)) total += count(child); + return total; + } + + public static List children(Element parent) { + List result = new ArrayList<>(); + for (Node n = parent.getFirstChild(); n != null; n = n.getNextSibling()) + if (n instanceof Element element) result.add(element); + return result; + } + + public static QName name(Element element) { + return new QName( + element.getNamespaceURI() == null ? "" : element.getNamespaceURI(), + element.getLocalName() == null ? element.getTagName() : element.getLocalName()); + } + + public static QName d(String local) { + return new QName(DAV, local); + } + + public static QName c(String local) { + return new QName(CAL, local); + } + + public static boolean is(Element element, String namespace, String local) { + return name(element).equals(new QName(namespace, local)); + } + + public static Element child(Element parent, String namespace, String local) { + List found = children(parent).stream().filter(e -> is(e, namespace, local)).toList(); + if (found.size() > 1) throw new DavException(400, null); + return found.isEmpty() ? null : found.getFirst(); + } + + public static Element required(Element parent, String namespace, String local) { + Element child = child(parent, namespace, local); + if (child == null) throw new DavException(400, null); + return child; + } + + /** Recognized immediate command children; an ignored extension's whole subtree stays ignored. */ + public static List children(Element parent, String namespace, String... locals) { + var accepted = java.util.Set.of(locals); + return children(parent).stream() + .filter( + e -> + namespace.equals(name(e).getNamespaceURI()) + && accepted.contains(name(e).getLocalPart())) + .toList(); + } + + /** Character data of a protocol scalar, without text hidden in ignored extension subtrees. */ + public static String scalarText(Element element) { + StringBuilder value = new StringBuilder(); + for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) + if (child.getNodeType() == Node.TEXT_NODE || child.getNodeType() == Node.CDATA_SECTION_NODE) + value.append(child.getNodeValue()); + return value.toString(); + } + + public static Element add(Node parent, QName name) { + Document document = parent instanceof Document d ? d : parent.getOwnerDocument(); + String prefix = + DAV.equals(name.getNamespaceURI()) ? "D" : CAL.equals(name.getNamespaceURI()) ? "C" : "P"; + budget(parent) + .reserve(256 + textWork(name.getNamespaceURI()) * 2 + textWork(name.getLocalPart()) * 2, 1); + Element result = + document.createElementNS( + name.getNamespaceURI().isEmpty() ? null : name.getNamespaceURI(), + name.getNamespaceURI().isEmpty() + ? name.getLocalPart() + : prefix + ":" + name.getLocalPart()); + parent.appendChild(result); + return result; + } + + public static Element text(Node parent, QName name, String value) { + // Invalid labels retained by an older version must not produce a malformed success response. + if (!XmlCharacters.valid(value)) throw new DavException(500, null); + budget(parent).reserve(textWork(value), 1); + Element result = add(parent, name); + result.setTextContent(value); + return result; + } + + public static byte[] bytes(Node node) { + try { + var factory = TransformerFactory.newInstance(); + factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); + factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, ""); + factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, ""); + var transformer = factory.newTransformer(); + transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); + transformer.setOutputProperty( + OutputKeys.OMIT_XML_DECLARATION, node instanceof Document ? "no" : "yes"); + var output = + new ByteArrayOutputStream() { + private void bound(int additional) { + if (additional > MAX_RESPONSE - count) + throw new DavException(507, d("number-of-matches-within-limits")); + } + + @Override + public synchronized void write(int value) { + bound(1); + super.write(value); + } + + @Override + public synchronized void write(byte[] value, int offset, int length) { + bound(length); + super.write(value, offset, length); + } + }; + transformer.transform(new DOMSource(node), new StreamResult(output)); + if (output.size() > MAX_RESPONSE) + throw new DavException(507, d("number-of-matches-within-limits")); + return output.toByteArray(); + } catch (javax.xml.transform.TransformerException ex) { + for (Throwable cause = ex; cause != null; cause = cause.getCause()) + if (cause instanceof DavException failure) throw failure; + throw new IllegalStateException("XML serialization failed", ex); + } + } +} diff --git a/src/main/java/org/busystack/caldav/protocol/DiscoveryController.java b/src/main/java/org/busystack/caldav/protocol/DiscoveryController.java new file mode 100644 index 0000000..be84175 --- /dev/null +++ b/src/main/java/org/busystack/caldav/protocol/DiscoveryController.java @@ -0,0 +1,20 @@ +package org.busystack.caldav.protocol; + +import org.busystack.caldav.config.DeploymentProperties; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +public class DiscoveryController { + private final DeploymentProperties deployment; + + public DiscoveryController(DeploymentProperties deployment) { + this.deployment = deployment; + } + + @RequestMapping("/.well-known/caldav") + ResponseEntity discover() { + return ResponseEntity.status(308).location(java.net.URI.create(deployment.davRoot())).build(); + } +} diff --git a/src/main/java/org/busystack/caldav/protocol/HttpConditions.java b/src/main/java/org/busystack/caldav/protocol/HttpConditions.java new file mode 100644 index 0000000..ca87059 --- /dev/null +++ b/src/main/java/org/busystack/caldav/protocol/HttpConditions.java @@ -0,0 +1,88 @@ +package org.busystack.caldav.protocol; + +import jakarta.servlet.http.HttpServletRequest; +import java.time.Instant; +import java.util.LinkedHashSet; +import java.util.Set; +import org.busystack.caldav.storage.WriteCondition; + +/** RFC 9110 validators; mutation conditions are evaluated again under the storage lock. */ +final class HttpConditions { + private HttpConditions() {} + + static WriteCondition write(HttpServletRequest request, DavPaths paths) { + String match = header(request, "If-Match", ","), none = header(request, "If-None-Match", ","); + boolean create = none != null && none.trim().equals("*"), + exists = match != null && match.trim().equals("*"); + Set + matches = new LinkedHashSet<>(match == null || exists ? Set.of() : tags(match, false)), + excludes = new LinkedHashSet<>(none == null || create ? Set.of() : tags(none, true)); + // The controller evaluates the complete DAV If expression under one snapshot/global + // mutation lock. Refining it with only this resource's ETag would discard sync state tokens. + return new WriteCondition( + create, + exists || match != null, + matches, + excludes, + match == null ? HttpDateValues.parse(request, "If-Unmodified-Since") : null); + } + + static boolean notModified(HttpServletRequest request, String etag, DavPaths paths) { + return notModified(request, etag, null, paths); + } + + static boolean notModified( + HttpServletRequest request, String etag, Instant modifiedAt, DavPaths paths) { + String match = header(request, "If-Match", ","); + if (match != null && !match.trim().equals("*")) { + Set accepted = tags(match, false); + if (etag == null || !accepted.contains(etag)) throw new DavException(412, null); + } + if (match == null) { + Instant unmodified = HttpDateValues.parse(request, "If-Unmodified-Since"); + if (unmodified != null && modifiedAt != null && modifiedAt.isAfter(unmodified)) + throw new DavException(412, null); + } + String none = header(request, "If-None-Match", ","); + if (none != null) { + if (none.trim().equals("*")) return true; + Set excluded = tags(none, true); + return etag != null && excluded.contains(etag); + } + Instant modified = HttpDateValues.parse(request, "If-Modified-Since"); + return modified != null && modifiedAt != null && !modifiedAt.isAfter(modified); + } + + private static String header(HttpServletRequest request, String name, String separator) { + var values = java.util.Collections.list(request.getHeaders(name)); + return values.isEmpty() ? null : String.join(separator, values); + } + + private static Set tags(String header, boolean weak) { + if (header.length() > 8192) throw new DavException(400, null); + Set result = new LinkedHashSet<>(); + int i = 0; + while (i < header.length()) { + while (i < header.length() && (header.charAt(i) == ' ' || header.charAt(i) == '\t')) i++; + boolean isWeak = header.startsWith("W/", i); + if (isWeak) i += 2; + if (i >= header.length() || header.charAt(i) != '"') throw new DavException(400, null); + int start = i++; + while (i < header.length() && header.charAt(i) != '"') { + char c = header.charAt(i++); + if (c < 0x21 || c == 0x7f) throw new DavException(400, null); + } + if (i >= header.length()) throw new DavException(400, null); + i++; + if (weak || !isWeak) result.add(header.substring(start, i)); + while (i < header.length() && (header.charAt(i) == ' ' || header.charAt(i) == '\t')) i++; + if (i < header.length()) { + if (header.charAt(i++) != ',' || i == header.length()) throw new DavException(400, null); + } + } + // A syntactically valid weak-only If-Match must fail, never become an unconditional write. + if (result.isEmpty() && !weak) result.add("\"busystack-impossible-weak-validator\""); + if (header.isBlank()) throw new DavException(400, null); + return Set.copyOf(result); + } +} diff --git a/src/main/java/org/busystack/caldav/protocol/HttpDateValues.java b/src/main/java/org/busystack/caldav/protocol/HttpDateValues.java new file mode 100644 index 0000000..682289c --- /dev/null +++ b/src/main/java/org/busystack/caldav/protocol/HttpDateValues.java @@ -0,0 +1,95 @@ +package org.busystack.caldav.protocol; + +import jakarta.servlet.http.HttpServletRequest; +import java.time.Clock; +import java.time.DayOfWeek; +import java.time.Instant; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeFormatterBuilder; +import java.time.format.TextStyle; +import java.time.temporal.ChronoField; +import java.time.temporal.ChronoUnit; +import java.util.Arrays; +import java.util.Locale; +import java.util.Set; +import java.util.stream.Collectors; +import org.apache.tomcat.util.http.FastHttpDateFormat; +import org.springframework.http.HttpHeaders; + +/** Strict single HTTP-date fields around the embedded Servlet container's established parser. */ +final class HttpDateValues { + private static final DateTimeFormatter RFC850 = + DateTimeFormatter.ofPattern("EEEE, dd-MMM-yy HH:mm:ss 'GMT'", Locale.US) + .withZone(ZoneOffset.UTC); + private static final DateTimeFormatter ASCTIME = + new DateTimeFormatterBuilder() + .appendPattern("EEE MMM ") + .padNext(2) + .appendValue(ChronoField.DAY_OF_MONTH) + .appendPattern(" HH:mm:ss uuuu") + .toFormatter(Locale.US) + .withZone(ZoneOffset.UTC); + + private static final Set LONG_DAYS = + Arrays.stream(DayOfWeek.values()) + .map(day -> day.getDisplayName(TextStyle.FULL, Locale.US)) + .collect(Collectors.toUnmodifiableSet()); + + private HttpDateValues() {} + + static Instant parse(HttpServletRequest request, String headerName) { + return parse(request, headerName, Clock.systemUTC()); + } + + static Instant parse(HttpServletRequest request, String headerName, Clock clock) { + var values = request.getHeaders(headerName); + if (values == null || !values.hasMoreElements()) return null; + String value = values.nextElement(); + if (values.hasMoreElements() || value == null || value.length() > 128) return null; + for (int index = 0; index < value.length(); index++) { + char character = value.charAt(index); + if (character < 0x20 && character != '\t' || character > 0x7e) return null; + } + value = value.strip(); + if (value.isEmpty()) return null; + try { + long parsed = FastHttpDateFormat.parseDate(value); + if (parsed == -1) return null; + Instant instant = seconds(Instant.ofEpochMilli(parsed)); + // The Servlet container parser can accept a date prefix or normalize an invalid date. + // Require the entire single field to be one of the three standard serializations; + // these formatters only format the Servlet result and do not parse input. + if (value.equals(format(instant)) || value.equals(ASCTIME.format(instant))) return instant; + String legacy = RFC850.format(instant); + int comma = value.indexOf(','); + if (comma < 0 + || !LONG_DAYS.contains(value.substring(0, comma)) + || !value.substring(comma).equals(legacy.substring(legacy.indexOf(',')))) return null; + // RFC9110 section5.6.7 requires a two-digit year more than 50 years ahead + // to denote the latest corresponding past year. Servlet date parsers can + // use SimpleDateFormat's different default century window. + ZonedDateTime now = ZonedDateTime.now(clock).withZoneSameInstant(ZoneOffset.UTC); + ZonedDateTime corrected = + instant + .atZone(ZoneOffset.UTC) + .withYear( + (now.getYear() / 100) * 100 + instant.atZone(ZoneOffset.UTC).getYear() % 100); + if (corrected.isAfter(now.plusYears(50))) corrected = corrected.minusYears(100); + return value.equals(RFC850.format(corrected)) ? corrected.toInstant() : null; + } catch (IllegalArgumentException | java.time.DateTimeException exception) { + return null; + } + } + + static String format(Instant instant) { + var headers = new HttpHeaders(); + headers.setInstant(HttpHeaders.DATE, seconds(instant)); + return headers.getFirst(HttpHeaders.DATE); + } + + static Instant seconds(Instant instant) { + return instant == null ? null : instant.truncatedTo(ChronoUnit.SECONDS); + } +} diff --git a/src/main/java/org/busystack/caldav/security/ActiveAccountFilter.java b/src/main/java/org/busystack/caldav/security/ActiveAccountFilter.java new file mode 100644 index 0000000..01dc420 --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/ActiveAccountFilter.java @@ -0,0 +1,40 @@ +package org.busystack.caldav.security; + +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.io.IOException; +import org.busystack.caldav.account.AccountService; +import org.springframework.security.authentication.AnonymousAuthenticationToken; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.web.filter.OncePerRequestFilter; + +/** A previously created browser session must not keep a disabled account active. */ +public final class ActiveAccountFilter extends OncePerRequestFilter { + private final AccountService accounts; + + public ActiveAccountFilter(AccountService accounts) { + this.accounts = accounts; + } + + @Override + protected void doFilterInternal( + HttpServletRequest request, HttpServletResponse response, FilterChain chain) + throws ServletException, IOException { + var authentication = SecurityContextHolder.getContext().getAuthentication(); + if (authentication != null + && authentication.isAuthenticated() + && !(authentication instanceof AnonymousAuthenticationToken)) { + var account = accounts.find(authentication.getName()); + if (account == null || !account.enabled() || !accounts.isClientAccessCurrent(account.id())) { + SecurityContextHolder.clearContext(); + var session = request.getSession(false); + if (session != null) session.invalidate(); + response.sendError(403); + return; + } + } + chain.doFilter(request, response); + } +} diff --git a/src/main/java/org/busystack/caldav/security/DavBearerAuthenticationEntryPoint.java b/src/main/java/org/busystack/caldav/security/DavBearerAuthenticationEntryPoint.java new file mode 100644 index 0000000..d79b47c --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/DavBearerAuthenticationEntryPoint.java @@ -0,0 +1,50 @@ +package org.busystack.caldav.security; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.util.ArrayList; +import java.util.List; +import org.springframework.http.HttpHeaders; +import org.springframework.security.core.AuthenticationException; +import org.springframework.security.oauth2.core.OAuth2AuthenticationException; +import org.springframework.security.oauth2.server.resource.BearerTokenError; +import org.springframework.security.web.AuthenticationEntryPoint; + +/** RFC 6750 challenge for the selected opaque-token DAV profile, without optional metadata. */ +final class DavBearerAuthenticationEntryPoint implements AuthenticationEntryPoint { + @Override + public void commence( + HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) { + int status = HttpServletResponse.SC_UNAUTHORIZED; + List parameters = new ArrayList<>(); + if (exception instanceof OAuth2AuthenticationException authentication) { + var error = authentication.getError(); + parameter(parameters, "error", error.getErrorCode()); + parameter(parameters, "error_description", error.getDescription()); + parameter(parameters, "error_uri", error.getUri()); + if (error instanceof BearerTokenError bearer) { + status = bearer.getHttpStatus().value(); + parameter(parameters, "scope", bearer.getScope()); + } + } + response.addHeader( + HttpHeaders.WWW_AUTHENTICATE, + parameters.isEmpty() ? "Bearer" : "Bearer " + String.join(", ", parameters)); + response.setStatus(status); + } + + private static void parameter(List parameters, String name, String value) { + if (value == null || value.isBlank()) return; + // These values originate in trusted protocol errors, not request-controlled header text. + if (value + .codePoints() + .anyMatch( + character -> + character < 0x20 + || character > 0x7e + || character == '"' + || character == '\\' + || name.equals("error_uri") && character == ' ')) return; + parameters.add(name + "=\"" + value + "\""); + } +} diff --git a/src/main/java/org/busystack/caldav/security/DavOriginFilter.java b/src/main/java/org/busystack/caldav/security/DavOriginFilter.java new file mode 100644 index 0000000..79b9421 --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/DavOriginFilter.java @@ -0,0 +1,55 @@ +package org.busystack.caldav.security; + +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.Set; +import org.springframework.web.filter.OncePerRequestFilter; + +/** DAV credentials are for non-browser clients; block cookie and cached-Basic CSRF contexts. */ +final class DavOriginFilter extends OncePerRequestFilter { + private static final Set SAFE = Set.of("GET", "HEAD", "OPTIONS"); + private final java.net.URI base; + + DavOriginFilter(org.busystack.caldav.config.DeploymentProperties deployment) { + base = deployment.publicBaseUrl(); + } + + @Override + protected void doFilterInternal( + HttpServletRequest request, HttpServletResponse response, FilterChain chain) + throws ServletException, IOException { + String origin = request.getHeader("Origin"), site = request.getHeader("Sec-Fetch-Site"); + boolean trustedOrigin = origin == null || sameOrigin(origin); + boolean trustedSite = site == null || site.equals("none") || site.equals("same-origin"); + if (!SAFE.contains(request.getMethod()) && (!trustedOrigin || !trustedSite)) { + response.sendError(403); + return; + } + chain.doFilter(request, response); + } + + private boolean sameOrigin(String value) { + try { + var origin = java.net.URI.create(value); + return origin.getHost() != null + && origin.getUserInfo() == null + && origin.getRawQuery() == null + && origin.getRawFragment() == null + && origin.getRawPath().isEmpty() + && base.getScheme().equalsIgnoreCase(origin.getScheme()) + && base.getHost().equalsIgnoreCase(origin.getHost()) + && port(base) == port(origin); + } catch (IllegalArgumentException ex) { + return false; + } + } + + private static int port(java.net.URI uri) { + return uri.getPort() >= 0 + ? uri.getPort() + : "https".equalsIgnoreCase(uri.getScheme()) ? 443 : 80; + } +} diff --git a/src/main/java/org/busystack/caldav/security/DavSecretVerifier.java b/src/main/java/org/busystack/caldav/security/DavSecretVerifier.java new file mode 100644 index 0000000..689c880 --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/DavSecretVerifier.java @@ -0,0 +1,46 @@ +package org.busystack.caldav.security; + +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.util.UUID; +import org.busystack.caldav.security.audit.SecurityHashes; +import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.stereotype.Component; + +/** Only for framework-generated 256-bit DAV secrets. Human passwords retain adaptive hashing. */ +@Component +public final class DavSecretVerifier { + public static final String PREFIX = "{dav-sha256-v1}"; + private final PasswordEncoder legacy; + + public DavSecretVerifier(PasswordEncoder legacy) { + this.legacy = legacy; + } + + public String encode(UUID credential, String secret) { + return PREFIX + + SecurityHashes.sha256("BusyStack DAV credential v1:" + credential + ":" + secret); + } + + public boolean current(String verifier) { + return verifier != null && verifier.startsWith(PREFIX); + } + + public boolean matches(UUID credential, String secret, String verifier) { + if (verifier == null) return false; + if (current(verifier)) { + String hash = verifier.substring(PREFIX.length()); + if (hash.length() != 64 + || !hash.chars().allMatch(c -> c >= '0' && c <= '9' || c >= 'a' && c <= 'f')) + return false; + return MessageDigest.isEqual( + verifier.getBytes(StandardCharsets.US_ASCII), + encode(credential, secret).getBytes(StandardCharsets.US_ASCII)); + } + return verifier.length() == 60 + && (verifier.startsWith("$2a$") + || verifier.startsWith("$2b$") + || verifier.startsWith("$2y$")) + && legacy.matches(secret, verifier); + } +} diff --git a/src/main/java/org/busystack/caldav/security/PasswordConfiguration.java b/src/main/java/org/busystack/caldav/security/PasswordConfiguration.java new file mode 100644 index 0000000..145b163 --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/PasswordConfiguration.java @@ -0,0 +1,14 @@ +package org.busystack.caldav.security; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.crypto.password.PasswordEncoder; + +@Configuration +public class PasswordConfiguration { + @Bean + PasswordEncoder passwords() { + return new BCryptPasswordEncoder(12); + } +} diff --git a/src/main/java/org/busystack/caldav/security/RecentAuthentication.java b/src/main/java/org/busystack/caldav/security/RecentAuthentication.java new file mode 100644 index 0000000..f323d99 --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/RecentAuthentication.java @@ -0,0 +1,47 @@ +package org.busystack.caldav.security; + +import java.time.Duration; +import java.time.Instant; +import org.springframework.security.access.AccessDeniedException; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.authority.FactorGrantedAuthority; + +/** One policy shared by the browser and authorization-server security boundaries. */ +public final class RecentAuthentication { + public static final Duration MAX_AGE = Duration.ofMinutes(10); + + private RecentAuthentication() {} + + public static void requirePassword(Authentication authentication) { + if (!has(authentication, FactorGrantedAuthority.PASSWORD_AUTHORITY)) + throw new AccessDeniedException("Recent password authentication is required"); + } + + public static void requireMfa(Authentication authentication) { + requirePrimary(authentication); + if (!has(authentication, FactorGrantedAuthority.WEBAUTHN_AUTHORITY)) + throw new AccessDeniedException("Recent password and passkey authentication are required"); + } + + public static void requirePrimary(Authentication authentication) { + boolean upstream = + has(authentication, FactorGrantedAuthority.AUTHORIZATION_CODE_AUTHORITY) + && authentication.getAuthorities().stream() + .anyMatch( + authority -> "BUSYSTACK_TRUSTED_UPSTREAM".equals(authority.getAuthority())); + if (!has(authentication, FactorGrantedAuthority.PASSWORD_AUTHORITY) && !upstream) + throw new AccessDeniedException("Recent primary authentication is required"); + } + + private static boolean has(Authentication authentication, String authority) { + if (authentication == null || !authentication.isAuthenticated()) return false; + Instant now = Instant.now(); + return authentication.getAuthorities().stream() + .anyMatch( + granted -> + granted instanceof FactorGrantedAuthority factor + && factor.getAuthority().equals(authority) + && factor.getIssuedAt().isAfter(now.minus(MAX_AGE)) + && !factor.getIssuedAt().isAfter(now)); + } +} diff --git a/src/main/java/org/busystack/caldav/security/SecurityConfiguration.java b/src/main/java/org/busystack/caldav/security/SecurityConfiguration.java new file mode 100644 index 0000000..83a2d00 --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/SecurityConfiguration.java @@ -0,0 +1,205 @@ +package org.busystack.caldav.security; + +import jakarta.servlet.http.HttpServletResponse; +import java.util.List; +import org.busystack.caldav.account.AccountService; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.annotation.Order; +import org.springframework.security.authentication.AuthenticationProvider; +import org.springframework.security.authentication.BadCredentialsException; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer; +import org.springframework.security.config.http.SessionCreationPolicy; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.authority.SimpleGrantedAuthority; +import org.springframework.security.core.userdetails.User; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.core.userdetails.UsernameNotFoundException; +import org.springframework.security.web.SecurityFilterChain; +import org.springframework.security.web.firewall.StrictHttpFirewall; + +@Configuration +@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) +public class SecurityConfiguration { + @Bean + UserDetailsService browserUsers(AccountService accounts) { + return username -> { + var account = accounts.find(username); + if (account == null + || account.passwordHash() == null + || !account.enabled() + || accounts.isExternallyManaged(account.id())) + throw new UsernameNotFoundException("Invalid account"); + return User.withUsername(account.username()) + .password(account.passwordHash()) + .disabled(!account.enabled()) + .authorities( + new SimpleGrantedAuthority(account.administrator() ? "ROLE_ADMIN" : "ROLE_USER"), + org.busystack.caldav.security.session.SessionSecurity.authority( + account.securityEpoch())) + .build(); + }; + } + + @Bean + WebSecurityCustomizer firewall() { + var firewall = new StrictHttpFirewall(); + // The DAV parser validates decoded segments once, including traversal and encoded separators. + firewall.setAllowUrlEncodedPercent(true); + firewall.setAllowUrlEncodedPeriod(true); + firewall.setAllowedHttpMethods( + List.of( + "GET", + "HEAD", + "POST", + "OPTIONS", + "PUT", + "DELETE", + "PROPFIND", + "PROPPATCH", + "REPORT", + "MKCALENDAR", + "MKCOL", + "COPY", + "MOVE", + "ACL")); + return web -> web.httpFirewall(firewall); + } + + @Bean + @Order(1) + SecurityFilterChain davSecurity( + HttpSecurity http, + AccountService accounts, + org.busystack.caldav.config.DeploymentProperties deployment, + org.springframework.beans.factory.ObjectProvider< + org.springframework.security.oauth2.server.resource.introspection + .OpaqueTokenIntrospector> + introspectors) + throws Exception { + AuthenticationProvider provider = + new AuthenticationProvider() { + @Override + public Authentication authenticate(Authentication auth) { + if (!accounts.verifyDav(auth.getName(), String.valueOf(auth.getCredentials()))) + throw new BadCredentialsException("Invalid DAV credential"); + var account = accounts.find(auth.getName()); + return UsernamePasswordAuthenticationToken.authenticated( + account.id().toString(), null, List.of(new SimpleGrantedAuthority("DAV"))); + } + + @Override + public boolean supports(Class type) { + return UsernamePasswordAuthenticationToken.class.isAssignableFrom(type); + } + }; + http.securityMatcher("/dav/**") + .authenticationManager( + new org.springframework.security.authentication.ProviderManager(provider)) + .authorizeHttpRequests(auth -> auth.anyRequest().hasAuthority("DAV")) + .httpBasic( + basic -> + basic + .realmName("BusyStack DAV") + .authenticationEntryPoint( + (request, response, ex) -> { + response.setHeader( + "WWW-Authenticate", + "Basic realm=\"BusyStack DAV\", charset=\"UTF-8\""); + response.sendError(HttpServletResponse.SC_UNAUTHORIZED); + })) + .sessionManagement( + session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) + .requestCache(cache -> cache.disable()) + // Every DAV mutation also requires the non-browser Origin boundary below. + .csrf(csrf -> csrf.disable()); + http.addFilterBefore( + new DavOriginFilter(deployment), + org.springframework.security.web.authentication.www.BasicAuthenticationFilter.class); + var introspector = introspectors.getIfAvailable(); + if (introspector != null) + http.oauth2ResourceServer( + oauth -> + oauth + .authenticationEntryPoint(new DavBearerAuthenticationEntryPoint()) + .opaqueToken( + opaque -> + opaque.authenticationManager( + new org.springframework.security.authentication.ProviderManager( + new org.springframework.security.oauth2.server.resource + .authentication.OpaqueTokenAuthenticationProvider( + introspector))))); + return http.build(); + } + + @Bean + @Order(3) + SecurityFilterChain browserSecurity( + HttpSecurity http, + AccountService accounts, + org.busystack.caldav.config.DeploymentProperties deployment, + org.busystack.caldav.security.passkey.AccountPasskeyUsers passkeyUsers, + org.busystack.caldav.security.passkey.CeremonyStore ceremonies, + org.busystack.caldav.security.session.SessionSecurity sessions, + org.busystack.caldav.security.throttle.LoginThrottleService throttle, + org.springframework.beans.factory.ObjectProvider< + org.busystack.caldav.security.oidc.OidcLoginSupport> + upstreamUsers, + org.springframework.beans.factory.ObjectProvider< + org.springframework.security.oauth2.client.registration.ClientRegistrationRepository> + upstreamRegistrations) + throws Exception { + http.authorizeHttpRequests( + auth -> + auth.requestMatchers( + "/.well-known/caldav", + "/assets/**", + "/login", + "/login/webauthn", + "/oauth2/authorization/**", + "/login/oauth2/code/**", + "/webauthn/authenticate/options", + "/actuator/health", + "/actuator/health/liveness", + "/actuator/health/readiness", + "/error") + .permitAll() + .requestMatchers("/admin/**") + .hasRole("ADMIN") + .anyRequest() + .authenticated()) + .formLogin(login -> login.loginPage("/login").permitAll()) + // This endpoint only returns a configured 308; it has no cookie-authenticated mutation. + .csrf(csrf -> csrf.ignoringRequestMatchers("/.well-known/caldav")) + .logout(logout -> logout.invalidateHttpSession(true).deleteCookies("JSESSIONID")) + .sessionManagement(session -> session.sessionFixation(fixation -> fixation.newSession())) + .headers( + headers -> + headers.contentSecurityPolicy( + csp -> + csp.policyDirectives( + "default-src 'self'; frame-ancestors 'none'; object-src 'none'; base-uri 'none'"))); + http.addFilterBefore( + new ActiveAccountFilter(accounts), + org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter.class); + http.addFilterAfter( + new org.busystack.caldav.security.session.SessionEpochFilter(sessions), + org.springframework.security.web.context.SecurityContextHolderFilter.class); + http.addFilterBefore( + new org.busystack.caldav.security.throttle.LoginThrottleFilter(throttle), + org.springframework.security.web.authentication.logout.LogoutFilter.class); + http.setSharedObject( + UserDetailsService.class, + org.busystack.caldav.security.oidc.OidcConfiguration.passkeyUsers(accounts)); + org.busystack.caldav.security.passkey.PasskeyConfiguration.configure( + http, deployment, passkeyUsers, ceremonies); + var upstream = upstreamUsers.getIfAvailable(); + if (upstream != null) + org.busystack.caldav.security.oidc.OidcConfiguration.configure( + http, upstreamRegistrations.getObject(), upstream); + return http.build(); + } +} diff --git a/src/main/java/org/busystack/caldav/security/audit/SecurityAudit.java b/src/main/java/org/busystack/caldav/security/audit/SecurityAudit.java new file mode 100644 index 0000000..94a1c8d --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/audit/SecurityAudit.java @@ -0,0 +1,62 @@ +package org.busystack.caldav.security.audit; + +import java.util.UUID; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +/** Stores bounded structured security metadata; no credentials, assertion bodies, or free text. */ +@Service +public class SecurityAudit { + public enum Action { + PASSWORD_LOGIN, + PASSWORD_CHANGED, + ACCOUNT_DISABLED, + OFFLINE_RECOVERY, + PASSKEY_LOGIN, + SESSION_REVOKED, + RECOVERY_CODES_ISSUED, + RECOVERY_USED, + GROUP_CREATED, + GROUP_UPDATED, + GROUP_DELETED, + GROUP_SHARING_CHANGED + } + + public enum Outcome { + SUCCESS, + FAILURE, + THROTTLED + } + + private final JdbcTemplate jdbc; + + public SecurityAudit(JdbcTemplate jdbc) { + this.jdbc = jdbc; + } + + @Transactional + public void record(UUID account, Action action, Outcome outcome, String peer) { + String peerHash = + peer == null + ? null + : SecurityHashes.sha256(peer.length() > 128 ? peer.substring(0, 128) : peer); + jdbc.update( + "INSERT INTO security_audit(account_id,action,outcome,peer_hash) VALUES (?,?,?,?)", + account, + action.name(), + outcome.name(), + peerHash); + jdbc.update( + "DELETE FROM security_audit WHERE id <= (SELECT id FROM security_audit ORDER BY id DESC OFFSET 9999 LIMIT 1)"); + } + + @Scheduled(fixedDelay = 60000) + @Transactional + public void prune() { + jdbc.update("DELETE FROM security_audit WHERE occurred_at < now()-interval '30 days'"); + jdbc.update("DELETE FROM login_throttle WHERE expires_at < now()"); + jdbc.update("DELETE FROM webauthn_ceremonies WHERE expires_at < now()"); + } +} diff --git a/src/main/java/org/busystack/caldav/security/audit/SecurityHashes.java b/src/main/java/org/busystack/caldav/security/audit/SecurityHashes.java new file mode 100644 index 0000000..e10e479 --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/audit/SecurityHashes.java @@ -0,0 +1,21 @@ +package org.busystack.caldav.security.audit; + +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.HexFormat; + +/** Standard SHA-256 for high-entropy credential verifiers and bounded diagnostic identifiers. */ +public final class SecurityHashes { + private SecurityHashes() {} + + public static String sha256(String value) { + try { + return HexFormat.of() + .formatHex( + MessageDigest.getInstance("SHA-256").digest(value.getBytes(StandardCharsets.UTF_8))); + } catch (NoSuchAlgorithmException ex) { + throw new IllegalStateException("Required SHA-256 unavailable", ex); + } + } +} diff --git a/src/main/java/org/busystack/caldav/security/oauth/FrameworkGrantBoundary.java b/src/main/java/org/busystack/caldav/security/oauth/FrameworkGrantBoundary.java new file mode 100644 index 0000000..d222e1a --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/oauth/FrameworkGrantBoundary.java @@ -0,0 +1,202 @@ +package org.busystack.caldav.security.oauth; + +import java.time.Instant; +import java.util.Objects; +import java.util.UUID; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.security.authentication.AuthenticationProvider; +import org.springframework.security.core.Authentication; +import org.springframework.security.oauth2.core.OAuth2AuthenticationException; +import org.springframework.security.oauth2.core.OAuth2ErrorCodes; +import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames; +import org.springframework.security.oauth2.server.authorization.OAuth2Authorization; +import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService; +import org.springframework.security.oauth2.server.authorization.OAuth2TokenType; +import org.springframework.security.oauth2.server.authorization.authentication.OAuth2AccessTokenAuthenticationToken; +import org.springframework.security.oauth2.server.authorization.authentication.OAuth2AuthorizationCodeAuthenticationToken; +import org.springframework.security.oauth2.server.authorization.authentication.OAuth2ClientAuthenticationToken; +import org.springframework.security.oauth2.server.authorization.authentication.OAuth2RefreshTokenAuthenticationToken; +import org.springframework.security.oauth2.server.authorization.authentication.OAuth2TokenRevocationAuthenticationToken; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.TransactionDefinition; +import org.springframework.transaction.support.TransactionTemplate; + +/** Transaction/replay boundaries around Spring Security's grant implementations. */ +final class FrameworkGrantBoundary implements AuthenticationProvider { + enum Kind { + CODE, + REFRESH, + REVOCATION + } + + private record Outcome(Authentication response, OAuth2AuthenticationException error) {} + + private final AuthenticationProvider delegate; + private final Kind kind; + private final OAuth2AuthorizationService authorizations; + private final RefreshFamilyStore families; + private final OAuthClientLock clients; + private final JdbcTemplate jdbc; + private final TransactionTemplate transaction; + + FrameworkGrantBoundary( + AuthenticationProvider delegate, + Kind kind, + OAuth2AuthorizationService authorizations, + RefreshFamilyStore families, + OAuthClientLock clients, + JdbcTemplate jdbc, + PlatformTransactionManager transactions) { + this.delegate = delegate; + this.kind = kind; + this.authorizations = authorizations; + this.families = families; + this.clients = clients; + this.jdbc = jdbc; + this.transaction = new TransactionTemplate(transactions); + transaction.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); + transaction.setTimeout(15); + } + + @Override + public Authentication authenticate(Authentication authentication) { + try { + return switch (kind) { + case CODE -> exchange((OAuth2AuthorizationCodeAuthenticationToken) authentication); + case REFRESH -> refresh((OAuth2RefreshTokenAuthenticationToken) authentication); + case REVOCATION -> revoke((OAuth2TokenRevocationAuthenticationToken) authentication); + }; + } catch (RefreshRejectedException ex) { + throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_GRANT); + } + } + + private Authentication exchange(OAuth2AuthorizationCodeAuthenticationToken request) { + OAuth2Authorization existing = + authorizations.findByToken( + request.getCode(), new OAuth2TokenType(OAuth2ParameterNames.CODE)); + if (existing == null) return delegate.authenticate(request); + Outcome result = + transaction.execute( + status -> { + OAuth2ClientAuthenticationToken identified = + (OAuth2ClientAuthenticationToken) request.getPrincipal(); + var registration = Objects.requireNonNull(identified.getRegisteredClient()); + if (!clients.matches(registration.getClientId(), registration.getId()) + || !registration.getId().equals(existing.getRegisteredClientId())) + return new Outcome( + null, new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_GRANT)); + families.lockAuthorization(existing.getId()); + String accountId = + existing.getAttribute(PrincipalOAuthAuthorizationService.ACCOUNT_ID); + String accountEpoch = + existing.getAttribute(PrincipalOAuthAuthorizationService.ACCOUNT_EPOCH); + if (accountId == null + || accountEpoch == null + || jdbc + .query( + "SELECT enabled AND security_epoch=? FROM accounts WHERE id=? FOR SHARE", + (rs, row) -> rs.getBoolean(1), + UUID.fromString(accountEpoch), + UUID.fromString(accountId)) + .stream() + .noneMatch(Boolean::booleanValue) + || !families.accountCurrent(UUID.fromString(accountId))) + return new Outcome( + null, new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_GRANT)); + jdbc.query( + "SELECT id FROM oauth2_authorization WHERE id=? FOR UPDATE", + rs -> {}, + existing.getId()); + try { + Authentication authentication = delegate.authenticate(request); + if (!(authentication instanceof OAuth2AccessTokenAuthenticationToken response) + || response.getRefreshToken() == null) + throw new IllegalStateException("Native grant did not issue a refresh token"); + OAuth2Authorization stored = + Objects.requireNonNull(authorizations.findById(existing.getId())); + OAuth2ClientAuthenticationToken client = + (OAuth2ClientAuthenticationToken) request.getPrincipal(); + Instant absoluteExpiry = + Instant.parse( + stored.getAttribute(PrincipalOAuthAuthorizationService.ABSOLUTE_EXPIRY)); + families.register( + new RefreshFamilyStore.Family( + stored.getId(), + UUID.fromString(accountId), + Objects.requireNonNull(client.getRegisteredClient()).getClientId(), + absoluteExpiry), + response.getRefreshToken().getTokenValue(), + Objects.requireNonNull(response.getRefreshToken().getExpiresAt()), + () -> {}); + return new Outcome(response, null); + } catch (OAuth2AuthenticationException ex) { + OAuth2Authorization changed = authorizations.findById(existing.getId()); + if (changed != null + && changed.getRefreshToken() != null + && !changed.getRefreshToken().isActive()) + families.invalidateAuthorization(changed.getId()); + // Preserve the framework's deliberate code-replay invalidation before returning its + // error. + return new Outcome(null, ex); + } + }); + if (result == null) throw new IllegalStateException("Code transaction returned no outcome"); + if (result.error() != null) throw result.error(); + return result.response(); + } + + private Authentication refresh(OAuth2RefreshTokenAuthenticationToken request) { + OAuth2ClientAuthenticationToken client = + (OAuth2ClientAuthenticationToken) request.getPrincipal(); + return families.refresh( + request.getRefreshToken(), + Objects.requireNonNull(client.getRegisteredClient()).getClientId(), + client.getRegisteredClient().getId(), + family -> { + Authentication authentication = delegate.authenticate(request); + if (!(authentication instanceof OAuth2AccessTokenAuthenticationToken response) + || response.getRefreshToken() == null) + throw new IllegalStateException("Framework refresh did not rotate the token"); + return new RefreshFamilyStore.Rotation<>( + response, + response.getRefreshToken().getTokenValue(), + Objects.requireNonNull(response.getRefreshToken().getExpiresAt())); + }); + } + + private Authentication revoke(OAuth2TokenRevocationAuthenticationToken request) { + OAuth2Authorization existing = authorizations.findByToken(request.getToken(), null); + if (existing == null) return delegate.authenticate(request); + return transaction.execute( + status -> { + OAuth2ClientAuthenticationToken identified = + (OAuth2ClientAuthenticationToken) request.getPrincipal(); + var registration = Objects.requireNonNull(identified.getRegisteredClient()); + if (!clients.matches(registration.getClientId(), registration.getId())) + throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_CLIENT); + families.lockAuthorization(existing.getId()); + String accountId = existing.getAttribute(PrincipalOAuthAuthorizationService.ACCOUNT_ID); + if (accountId != null) + // Match code/refresh/reset ordering: account, then grant/family rows. Revocation + // remains permitted when the account has already been disabled. + jdbc.query( + "SELECT id FROM accounts WHERE id=? FOR SHARE", + rs -> {}, + UUID.fromString(accountId)); + Authentication response = delegate.authenticate(request); + OAuth2ClientAuthenticationToken client = + (OAuth2ClientAuthenticationToken) request.getPrincipal(); + if (Objects.requireNonNull(client.getRegisteredClient()) + .getId() + .equals(existing.getRegisteredClientId())) + families.invalidateAuthorization(existing.getId()); + return response; + }); + } + + @Override + public boolean supports(Class type) { + return delegate.supports(type); + } +} diff --git a/src/main/java/org/busystack/caldav/security/oauth/OAuthClientLock.java b/src/main/java/org/busystack/caldav/security/oauth/OAuthClientLock.java new file mode 100644 index 0000000..d9a9dba --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/oauth/OAuthClientLock.java @@ -0,0 +1,44 @@ +package org.busystack.caldav.security.oauth; + +import java.util.List; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Service; +import org.springframework.transaction.support.TransactionSynchronizationManager; + +/** Client, grant, account, then data rows: one transaction lock order for OAuth mutations. */ +@Service +public class OAuthClientLock { + private final JdbcTemplate jdbc; + + public OAuthClientLock(JdbcTemplate jdbc) { + this.jdbc = jdbc; + } + + public void lock(String clientId) { + if (!TransactionSynchronizationManager.isActualTransactionActive()) + throw new IllegalStateException("OAuth client lock requires an active transaction"); + jdbc.query( + "SELECT pg_advisory_xact_lock(hashtextextended(?,0))", + rs -> {}, + "busystack.oauth.client:" + clientId); + } + + /** Recheck after locking: an authentication object may refer to a removed registration. */ + public boolean matches(String clientId, String registrationId) { + lock(clientId); + List current = + jdbc.queryForList( + "SELECT id FROM oauth2_registered_client WHERE client_id=?", String.class, clientId); + return current.size() == 1 + && (registrationId == null || current.getFirst().equals(registrationId)); + } + + public boolean registrationExists(String registrationId) { + List ids = + jdbc.queryForList( + "SELECT client_id FROM oauth2_registered_client WHERE id=?", + String.class, + registrationId); + return ids.size() == 1 && matches(ids.getFirst(), registrationId); + } +} diff --git a/src/main/java/org/busystack/caldav/security/oauth/OAuthClientService.java b/src/main/java/org/busystack/caldav/security/oauth/OAuthClientService.java new file mode 100644 index 0000000..64917aa --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/oauth/OAuthClientService.java @@ -0,0 +1,169 @@ +package org.busystack.caldav.security.oauth; + +import java.net.URI; +import java.time.Duration; +import java.util.List; +import java.util.Set; +import java.util.UUID; +import org.busystack.caldav.account.AccountService; +import org.busystack.caldav.security.RecentAuthentication; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.security.access.AccessDeniedException; +import org.springframework.security.core.Authentication; +import org.springframework.security.oauth2.core.AuthorizationGrantType; +import org.springframework.security.oauth2.core.ClientAuthenticationMethod; +import org.springframework.security.oauth2.server.authorization.client.RegisteredClient; +import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository; +import org.springframework.security.oauth2.server.authorization.settings.ClientSettings; +import org.springframework.security.oauth2.server.authorization.settings.OAuth2TokenFormat; +import org.springframework.security.oauth2.server.authorization.settings.TokenSettings; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +/** Administrator-controlled registration; no dynamic registration endpoint is exposed. */ +@Service +public class OAuthClientService { + public record Client(String id, String name, String redirects) {} + + private final RegisteredClientRepository clients; + private final AccountService accounts; + private final JdbcTemplate jdbc; + private final org.busystack.caldav.security.session.SessionSecurity sessions; + private final OAuthClientLock clientLocks; + + public OAuthClientService( + RegisteredClientRepository clients, + AccountService accounts, + JdbcTemplate jdbc, + org.busystack.caldav.security.session.SessionSecurity sessions, + OAuthClientLock clientLocks) { + this.clients = clients; + this.accounts = accounts; + this.jdbc = jdbc; + this.sessions = sessions; + this.clientLocks = clientLocks; + } + + public List list(Authentication authentication) { + return list(actor(authentication)); + } + + @Transactional + public void registerNative( + Authentication authentication, String clientId, String name, Set redirects) { + RecentAuthentication.requireMfa(authentication); + clientLocks.lock(clientId); + registerNative(actor(authentication), clientId, name, redirects); + } + + @Transactional + public void remove(Authentication authentication, String clientId) { + RecentAuthentication.requireMfa(authentication); + clientLocks.lock(clientId); + remove(actor(authentication), clientId); + } + + List list(UUID administrator) { + requireAdministrator(administrator); + return jdbc.query( + "SELECT client_id,client_name,redirect_uris FROM oauth2_registered_client ORDER BY client_name,client_id", + (rs, row) -> new Client(rs.getString(1), rs.getString(2), rs.getString(3))); + } + + @Transactional + void registerNative(UUID administrator, String clientId, String name, Set redirects) { + clientLocks.lock(clientId); + requireAdministrator(administrator); + if (clientId == null + || !clientId.matches("[A-Za-z0-9._-]{1,100}") + || name == null + || name.isBlank() + || name.length() > 100 + || redirects == null + || redirects.isEmpty() + || redirects.size() > 5 + || redirects.stream().mapToInt(String::length).sum() > 950) + throw new IllegalArgumentException("Invalid OAuth client registration"); + redirects.forEach(OAuthClientService::validateRedirect); + if (clients.findByClientId(clientId) != null) + throw new IllegalArgumentException("OAuth client already exists"); + var builder = + RegisteredClient.withId(UUID.randomUUID().toString()) + .clientId(clientId) + .clientName(name) + .clientAuthenticationMethod(ClientAuthenticationMethod.NONE) + .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE) + .authorizationGrantType(AuthorizationGrantType.REFRESH_TOKEN) + .scope("dav") + .clientSettings( + ClientSettings.builder() + .requireProofKey(true) + .requireAuthorizationConsent(true) + .build()) + .tokenSettings( + TokenSettings.builder() + .accessTokenFormat(OAuth2TokenFormat.REFERENCE) + .accessTokenTimeToLive(Duration.ofMinutes(10)) + .refreshTokenTimeToLive(Duration.ofDays(30)) + .reuseRefreshTokens(false) + .build()); + redirects.forEach(builder::redirectUri); + clients.save(builder.build()); + } + + @Transactional + void remove(UUID administrator, String clientId) { + clientLocks.lock(clientId); + requireAdministrator(administrator); + RegisteredClient client = clients.findByClientId(clientId); + if (client == null) return; + jdbc.update( + "UPDATE oauth_refresh_families SET revoked_at=COALESCE(revoked_at,now()) WHERE client_id=?", + clientId); + jdbc.update( + "DELETE FROM oauth2_authorization_consent WHERE registered_client_id=?", client.getId()); + jdbc.update("DELETE FROM oauth2_authorization WHERE registered_client_id=?", client.getId()); + jdbc.update("DELETE FROM oauth2_registered_client WHERE id=?", client.getId()); + } + + static void validateRedirect(String value) { + URI uri; + try { + uri = URI.create(value); + } catch (IllegalArgumentException ex) { + throw new IllegalArgumentException("Invalid OAuth redirect URI"); + } + if (!uri.isAbsolute() + || uri.getUserInfo() != null + || uri.getFragment() != null + || uri.getScheme() == null + || uri.getRawPath() == null + || !uri.getRawPath().startsWith("/")) + throw new IllegalArgumentException( + "OAuth redirect must be an absolute URI without credentials or fragment"); + if ("https".equals(uri.getScheme()) && uri.getHost() != null) return; + if ("http".equals(uri.getScheme()) + && ("127.0.0.1".equals(uri.getHost()) || "[::1]".equals(uri.getHost())) + && uri.getPort() > 0) return; + if (uri.getScheme().matches("[a-z][a-z0-9+.-]*\\.[a-z0-9+.-]+") + && uri.getRawAuthority() == null) return; + throw new IllegalArgumentException( + "Native redirect requires HTTPS, an IP loopback HTTP URI, or an application-owned reverse-domain scheme"); + } + + private UUID actor(Authentication authentication) { + sessions.assertCurrent(authentication); + if (authentication == null || !authentication.isAuthenticated()) + throw new AccessDeniedException("Account authentication required"); + var account = accounts.find(authentication.getName()); + if (account == null || !account.enabled() || !account.administrator()) + throw new AccessDeniedException("Administrator privilege required"); + return account.id(); + } + + private void requireAdministrator(UUID id) { + var account = accounts.find(id); + if (account == null || !account.enabled() || !account.administrator()) + throw new AccessDeniedException("Administrator privilege required"); + } +} diff --git a/src/main/java/org/busystack/caldav/security/oauth/OAuthConfiguration.java b/src/main/java/org/busystack/caldav/security/oauth/OAuthConfiguration.java new file mode 100644 index 0000000..ac05722 --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/oauth/OAuthConfiguration.java @@ -0,0 +1,261 @@ +package org.busystack.caldav.security.oauth; + +import java.time.Instant; +import java.util.Base64; +import java.util.List; +import org.busystack.caldav.account.AccountService; +import org.busystack.caldav.config.DeploymentProperties; +import org.busystack.caldav.security.ActiveAccountFilter; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.annotation.Order; +import org.springframework.http.MediaType; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.crypto.keygen.Base64StringKeyGenerator; +import org.springframework.security.oauth2.core.OAuth2RefreshToken; +import org.springframework.security.oauth2.server.authorization.JdbcOAuth2AuthorizationConsentService; +import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationConsentService; +import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService; +import org.springframework.security.oauth2.server.authorization.OAuth2TokenType; +import org.springframework.security.oauth2.server.authorization.authentication.OAuth2AuthorizationCodeAuthenticationProvider; +import org.springframework.security.oauth2.server.authorization.authentication.OAuth2RefreshTokenAuthenticationProvider; +import org.springframework.security.oauth2.server.authorization.authentication.OAuth2TokenRevocationAuthenticationProvider; +import org.springframework.security.oauth2.server.authorization.authentication.PublicClientAuthenticationProvider; +import org.springframework.security.oauth2.server.authorization.client.JdbcRegisteredClientRepository; +import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository; +import org.springframework.security.oauth2.server.authorization.settings.AuthorizationServerSettings; +import org.springframework.security.oauth2.server.authorization.token.DelegatingOAuth2TokenGenerator; +import org.springframework.security.oauth2.server.authorization.token.OAuth2AccessTokenGenerator; +import org.springframework.security.oauth2.server.authorization.token.OAuth2TokenGenerator; +import org.springframework.security.web.SecurityFilterChain; +import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint; +import org.springframework.security.web.util.matcher.MediaTypeRequestMatcher; +import org.springframework.transaction.PlatformTransactionManager; + +@Configuration +public class OAuthConfiguration { + @Bean + RegisteredClientRepository registeredClients(JdbcTemplate jdbc) { + return new JdbcRegisteredClientRepository(jdbc); + } + + @Bean + OAuth2AuthorizationService authorizations( + JdbcTemplate jdbc, + RegisteredClientRepository clients, + AccountService accounts, + OAuthClientLock clientLocks, + PlatformTransactionManager transactions) { + return new PrincipalOAuthAuthorizationService( + OAuthJdbcSerialization.service(jdbc, clients), accounts, clientLocks, transactions); + } + + @Bean + OAuth2AuthorizationConsentService consents( + JdbcTemplate jdbc, RegisteredClientRepository clients) { + return new JdbcOAuth2AuthorizationConsentService(jdbc, clients); + } + + @Bean + AuthorizationServerSettings authorizationSettings(DeploymentProperties deployment) { + return AuthorizationServerSettings.builder().issuer(deployment.issuer().toString()).build(); + } + + @Bean + OAuth2TokenGenerator oauthTokens(DeploymentProperties deployment) { + OAuth2AccessTokenGenerator access = new OAuth2AccessTokenGenerator(); + access.setAccessTokenCustomizer( + context -> { + String accountId = + context + .getAuthorization() + .getAttribute(PrincipalOAuthAuthorizationService.ACCOUNT_ID); + if (accountId == null) + throw new IllegalStateException("OAuth grant has no internal principal"); + context + .getClaims() + .subject(accountId) + .audience(new java.util.ArrayList<>(List.of(deployment.publicBaseUrl() + "/dav"))); + }); + Base64StringKeyGenerator refreshKeys = + new Base64StringKeyGenerator(Base64.getUrlEncoder().withoutPadding(), 96); + OAuth2TokenGenerator refresh = + context -> { + if (!OAuth2TokenType.REFRESH_TOKEN.equals(context.getTokenType())) return null; + if (context.getAuthorization() == null) + throw new IllegalStateException("Refresh token has no persisted authorization"); + String absolute = + context + .getAuthorization() + .getAttribute(PrincipalOAuthAuthorizationService.ABSOLUTE_EXPIRY); + if (absolute == null) + throw new IllegalStateException("Refresh family has no absolute expiry"); + Instant now = Instant.now(); + Instant limit = Instant.parse(absolute); + Instant configured = + now.plus( + context.getRegisteredClient().getTokenSettings().getRefreshTokenTimeToLive()); + return new OAuth2RefreshToken( + refreshKeys.generateKey(), now, configured.isBefore(limit) ? configured : limit); + }; + return new DelegatingOAuth2TokenGenerator(access, refresh); + } + + @Bean + @Order(2) + @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) + SecurityFilterChain authorizationServerSecurity( + HttpSecurity http, + OAuth2AuthorizationService authorizations, + RegisteredClientRepository clients, + OAuth2AuthorizationConsentService consents, + OAuth2TokenGenerator tokens, + RefreshFamilyStore families, + OAuthClientLock clientLocks, + JdbcTemplate jdbc, + PlatformTransactionManager transactions, + org.busystack.caldav.security.session.SessionSecurity sessions, + AccountService accounts) + throws Exception { + http.oauth2AuthorizationServer( + server -> { + http.securityMatcher(server.getEndpointsMatcher()); + server + .authorizationService(authorizations) + .registeredClientRepository(clients) + .authorizationConsentService(consents) + .tokenGenerator(tokens) + .authorizationEndpoint(endpoint -> endpoint.consentPage("/oauth/consent")) + .clientAuthentication( + client -> + client + .authenticationConverter( + new PublicNativeClientAuthentication.Converter()) + .authenticationProvider( + new PublicNativeClientAuthentication.Provider(clients)) + .authenticationProviders( + providers -> { + providers.removeIf( + provider -> + !(provider + instanceof PublicClientAuthenticationProvider) + && !(provider + instanceof + PublicNativeClientAuthentication.Provider)); + providers.add( + new PublicNativeClientAuthentication.RejectUnsupported()); + })) + .tokenEndpoint( + endpoint -> + endpoint.authenticationProviders( + providers -> { + providers.removeIf( + provider -> + !(provider + instanceof + OAuth2AuthorizationCodeAuthenticationProvider) + && !(provider + instanceof + OAuth2RefreshTokenAuthenticationProvider)); + providers.replaceAll( + provider -> + new FrameworkGrantBoundary( + provider, + provider + instanceof + OAuth2AuthorizationCodeAuthenticationProvider + ? FrameworkGrantBoundary.Kind.CODE + : FrameworkGrantBoundary.Kind.REFRESH, + authorizations, + families, + clientLocks, + jdbc, + transactions)); + })) + .tokenRevocationEndpoint( + endpoint -> + endpoint.authenticationProviders( + providers -> + providers.replaceAll( + provider -> + provider + instanceof + OAuth2TokenRevocationAuthenticationProvider + ? new FrameworkGrantBoundary( + provider, + FrameworkGrantBoundary.Kind.REVOCATION, + authorizations, + families, + clientLocks, + jdbc, + transactions) + : provider))) + .authorizationServerMetadataEndpoint( + endpoint -> + endpoint.authorizationServerMetadataCustomizer( + metadata -> + metadata.claims( + claims -> { + claims.put( + "grant_types_supported", + List.of("authorization_code", "refresh_token")); + claims.put("response_types_supported", List.of("code")); + claims.put("response_modes_supported", List.of("query")); + claims.put( + "token_endpoint_auth_methods_supported", + List.of("none")); + claims.put( + "revocation_endpoint_auth_methods_supported", + List.of("none")); + claims.put( + "code_challenge_methods_supported", List.of("S256")); + claims.put("scopes_supported", List.of("dav")); + claims.remove("jwks_uri"); + claims.remove("tls_client_certificate_bound_access_tokens"); + claims.remove("dpop_signing_alg_values_supported"); + claims.remove("introspection_endpoint"); + claims.remove( + "introspection_endpoint_auth_methods_supported"); + claims.remove("device_authorization_endpoint"); + claims.remove("pushed_authorization_request_endpoint"); + }))); + }) + .authorizeHttpRequests(authorize -> authorize.anyRequest().authenticated()) + .exceptionHandling( + exceptions -> + exceptions.defaultAuthenticationEntryPointFor( + new LoginUrlAuthenticationEntryPoint("/login"), + new MediaTypeRequestMatcher(MediaType.TEXT_HTML))); + // Before client authentication replaces a browser principal with the public client's identity. + http.addFilterAfter( + new ActiveAccountFilter(accounts), + org.springframework.security.web.context.SecurityContextHolderFilter.class); + http.addFilterAfter( + new org.busystack.caldav.security.session.SessionEpochFilter(sessions), + org.springframework.security.web.context.SecurityContextHolderFilter.class); + http.addFilterBefore( + new RecentMfaAuthorizationFilter(), + org.springframework.security.web.authentication.logout.LogoutFilter.class); + // Framework CSRF protection for the cookie-authenticated consent form. Token exchanges + // retain the authorization server's protocol authentication and do not require a cookie. + var consentCsrf = + new org.springframework.security.web.csrf.CsrfFilter( + new org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository()); + consentCsrf.setBeanName("busyStackConsentCsrf"); + consentCsrf.setRequireCsrfProtectionMatcher( + request -> + "POST".equals(request.getMethod()) + && "/oauth2/authorize".equals(request.getServletPath())); + http.addFilterBefore( + consentCsrf, org.springframework.security.web.authentication.logout.LogoutFilter.class); + http.headers( + headers -> + headers.contentSecurityPolicy( + csp -> + csp.policyDirectives( + "default-src 'self'; frame-ancestors 'none'; object-src 'none'; base-uri 'none'"))); + return http.build(); + } +} diff --git a/src/main/java/org/busystack/caldav/security/oauth/OAuthConsentController.java b/src/main/java/org/busystack/caldav/security/oauth/OAuthConsentController.java new file mode 100644 index 0000000..dc8340e --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/oauth/OAuthConsentController.java @@ -0,0 +1,52 @@ +package org.busystack.caldav.security.oauth; + +import org.busystack.caldav.security.RecentAuthentication; +import org.springframework.security.access.AccessDeniedException; +import org.springframework.security.core.Authentication; +import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest; +import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService; +import org.springframework.security.oauth2.server.authorization.OAuth2TokenType; +import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; + +/** Local consent UI reads the pending framework request, never trusts requested display values. */ +@Controller +public class OAuthConsentController { + private final OAuth2AuthorizationService authorizations; + private final RegisteredClientRepository clients; + + public OAuthConsentController( + OAuth2AuthorizationService authorizations, RegisteredClientRepository clients) { + this.authorizations = authorizations; + this.clients = clients; + } + + @GetMapping("/oauth/consent") + String consent( + Authentication authentication, + @RequestParam("client_id") String clientId, + @RequestParam String state, + Model model) { + RecentAuthentication.requireMfa(authentication); + if (state.length() > 1024 || clientId.length() > 200) + throw new AccessDeniedException("Invalid consent request"); + var pending = authorizations.findByToken(state, new OAuth2TokenType("state")); + var client = clients.findByClientId(clientId); + if (pending == null + || client == null + || !client.getId().equals(pending.getRegisteredClientId()) + || !authentication.getName().equals(pending.getPrincipalName())) + throw new AccessDeniedException("Invalid consent request"); + OAuth2AuthorizationRequest request = + pending.getAttribute(OAuth2AuthorizationRequest.class.getName()); + if (request == null || !java.util.Set.of("dav").containsAll(request.getScopes())) + throw new AccessDeniedException("Unsupported consent scope"); + model.addAttribute("clientName", client.getClientName()); + model.addAttribute("clientId", clientId); + model.addAttribute("state", state); + return "oauth-consent"; + } +} diff --git a/src/main/java/org/busystack/caldav/security/oauth/OAuthDavIntrospector.java b/src/main/java/org/busystack/caldav/security/oauth/OAuthDavIntrospector.java new file mode 100644 index 0000000..98a05b6 --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/oauth/OAuthDavIntrospector.java @@ -0,0 +1,79 @@ +package org.busystack.caldav.security.oauth; + +import java.util.List; +import java.util.Map; +import java.util.UUID; +import org.busystack.caldav.account.AccountService; +import org.busystack.caldav.config.DeploymentProperties; +import org.springframework.security.core.authority.SimpleGrantedAuthority; +import org.springframework.security.oauth2.core.DefaultOAuth2AuthenticatedPrincipal; +import org.springframework.security.oauth2.core.OAuth2AuthenticatedPrincipal; +import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService; +import org.springframework.security.oauth2.server.authorization.OAuth2TokenType; +import org.springframework.security.oauth2.server.resource.introspection.BadOpaqueTokenException; +import org.springframework.security.oauth2.server.resource.introspection.OpaqueTokenIntrospector; +import org.springframework.stereotype.Service; + +/** + * Validates local opaque access grants on every request, including account and family revocation. + */ +@Service +public class OAuthDavIntrospector implements OpaqueTokenIntrospector { + private final OAuth2AuthorizationService authorizations; + private final RefreshFamilyStore families; + private final AccountService accounts; + private final String audience; + private final String issuer; + + public OAuthDavIntrospector( + OAuth2AuthorizationService authorizations, + RefreshFamilyStore families, + AccountService accounts, + DeploymentProperties deployment) { + this.authorizations = authorizations; + this.families = families; + this.accounts = accounts; + this.audience = deployment.publicBaseUrl() + "/dav"; + this.issuer = deployment.issuer().toString(); + } + + @Override + public OAuth2AuthenticatedPrincipal introspect(String token) { + if (token == null || token.length() > 4096) throw invalid(); + var authorization = authorizations.findByToken(token, OAuth2TokenType.ACCESS_TOKEN); + if (authorization == null + || authorization.getAccessToken() == null + || !authorization.getAccessToken().isActive() + || !authorization.getAuthorizedScopes().contains("dav") + || !families.isActive(authorization.getId())) throw invalid(); + String accountId = authorization.getAttribute(PrincipalOAuthAuthorizationService.ACCOUNT_ID); + Map claims = authorization.getAccessToken().getClaims(); + if (accountId == null + || claims == null + || !issuer.equals(String.valueOf(claims.get("iss"))) + || !accountId.equals(claims.get("sub")) + || !(claims.get("aud") instanceof List audiences) + || !audiences.contains(audience)) throw invalid(); + AccountService.Account account; + try { + account = accounts.find(UUID.fromString(accountId)); + } catch (IllegalArgumentException ex) { + throw invalid(); + } + String capturedEpoch = + authorization.getAttribute(PrincipalOAuthAuthorizationService.ACCOUNT_EPOCH); + if (account == null + || !account.enabled() + || !accounts.isClientAccessCurrent(account.id()) + || capturedEpoch == null + || !capturedEpoch.equals(account.securityEpoch().toString())) throw invalid(); + return new DefaultOAuth2AuthenticatedPrincipal( + accountId, + claims, + List.of(new SimpleGrantedAuthority("DAV"), new SimpleGrantedAuthority("SCOPE_dav"))); + } + + private static BadOpaqueTokenException invalid() { + return new BadOpaqueTokenException("Invalid access token"); + } +} diff --git a/src/main/java/org/busystack/caldav/security/oauth/OAuthGrantService.java b/src/main/java/org/busystack/caldav/security/oauth/OAuthGrantService.java new file mode 100644 index 0000000..1037c39 --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/oauth/OAuthGrantService.java @@ -0,0 +1,98 @@ +package org.busystack.caldav.security.oauth; + +import java.time.Instant; +import java.util.List; +import java.util.UUID; +import org.busystack.caldav.account.AccountService; +import org.busystack.caldav.security.RecentAuthentication; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.security.access.AccessDeniedException; +import org.springframework.security.core.Authentication; +import org.springframework.stereotype.Service; + +/** Account-interface operations expose grant metadata, never access or refresh token values. */ +@Service +public class OAuthGrantService { + public record Grant( + String id, + String clientId, + String clientName, + Instant createdAt, + Instant expiresAt, + Instant revokedAt) {} + + private final JdbcTemplate jdbc; + private final AccountService accounts; + private final RefreshFamilyStore families; + private final org.busystack.caldav.security.session.SessionSecurity sessions; + + public OAuthGrantService( + JdbcTemplate jdbc, + AccountService accounts, + RefreshFamilyStore families, + org.busystack.caldav.security.session.SessionSecurity sessions) { + this.jdbc = jdbc; + this.accounts = accounts; + this.families = families; + this.sessions = sessions; + } + + public List list(Authentication authentication) { + return list(actor(authentication)); + } + + public void revoke(Authentication authentication, String authorizationId) { + RecentAuthentication.requireMfa(authentication); + UUID accountId = actor(authentication); + families.revoke( + accountId, + authorizationId, + () -> { + sessions.assertCurrent(authentication); + removeConsent(accountId, authorizationId); + }); + } + + List list(UUID accountId) { + requireAccount(accountId); + return jdbc.query( + "SELECT f.authorization_id,f.client_id,COALESCE(c.client_name,f.client_id),f.created_at,f.expires_at,f.revoked_at FROM oauth_refresh_families f LEFT JOIN oauth2_registered_client c ON c.client_id=f.client_id WHERE f.account_id=? ORDER BY f.created_at DESC", + (rs, row) -> + new Grant( + rs.getString(1), + rs.getString(2), + rs.getString(3), + rs.getTimestamp(4).toInstant(), + rs.getTimestamp(5).toInstant(), + rs.getTimestamp(6) == null ? null : rs.getTimestamp(6).toInstant()), + accountId); + } + + void revoke(UUID accountId, String authorizationId) { + requireAccount(accountId); + families.revoke(accountId, authorizationId, () -> removeConsent(accountId, authorizationId)); + } + + private void removeConsent(UUID accountId, String authorizationId) { + jdbc.update( + "DELETE FROM oauth2_authorization_consent c USING oauth2_authorization a,oauth_refresh_families f WHERE a.id=f.authorization_id AND f.authorization_id=? AND f.account_id=? AND c.registered_client_id=a.registered_client_id AND c.principal_name=a.principal_name", + authorizationId, + accountId); + } + + private UUID actor(Authentication authentication) { + sessions.assertCurrent(authentication); + if (authentication == null || !authentication.isAuthenticated()) + throw new AccessDeniedException("Account authentication required"); + var account = accounts.find(authentication.getName()); + if (account == null || !account.enabled()) + throw new AccessDeniedException("Account unavailable"); + return account.id(); + } + + private void requireAccount(UUID id) { + var account = accounts.find(id); + if (account == null || !account.enabled()) + throw new AccessDeniedException("Account unavailable"); + } +} diff --git a/src/main/java/org/busystack/caldav/security/oauth/OAuthJdbcSerialization.java b/src/main/java/org/busystack/caldav/security/oauth/OAuthJdbcSerialization.java new file mode 100644 index 0000000..15d6e6a --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/oauth/OAuthJdbcSerialization.java @@ -0,0 +1,30 @@ +package org.busystack.caldav.security.oauth; + +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.security.jackson.SecurityJacksonModules; +import org.springframework.security.oauth2.server.authorization.JdbcOAuth2AuthorizationService; +import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository; +import org.springframework.security.web.webauthn.jackson.WebauthnJacksonModule; +import tools.jackson.databind.json.JsonMapper; +import tools.jackson.databind.jsontype.BasicPolymorphicTypeValidator; + +/** Add the framework's WebAuthn module to its JDBC allowlist through supported mapper hooks. */ +final class OAuthJdbcSerialization { + private OAuthJdbcSerialization() {} + + static JdbcOAuth2AuthorizationService service( + JdbcTemplate jdbc, RegisteredClientRepository clients) { + var webauthn = new WebauthnJacksonModule(); + var validator = BasicPolymorphicTypeValidator.builder(); + webauthn.configurePolymorphicTypeValidator(validator); + var modules = + SecurityJacksonModules.getModules(OAuthJdbcSerialization.class.getClassLoader(), validator); + var mapper = JsonMapper.builder().addModules(modules).addModule(webauthn).build(); + var service = new JdbcOAuth2AuthorizationService(jdbc, clients); + service.setAuthorizationRowMapper( + new JdbcOAuth2AuthorizationService.JsonMapperOAuth2AuthorizationRowMapper(clients, mapper)); + service.setAuthorizationParametersMapper( + new JdbcOAuth2AuthorizationService.JsonMapperOAuth2AuthorizationParametersMapper(mapper)); + return service; + } +} diff --git a/src/main/java/org/busystack/caldav/security/oauth/OAuthManagementController.java b/src/main/java/org/busystack/caldav/security/oauth/OAuthManagementController.java new file mode 100644 index 0000000..b045dff --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/oauth/OAuthManagementController.java @@ -0,0 +1,73 @@ +package org.busystack.caldav.security.oauth; + +import jakarta.servlet.http.HttpServletResponse; +import java.util.stream.Collectors; +import org.springframework.security.core.Authentication; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; + +@Controller +public class OAuthManagementController { + private final OAuthClientService clients; + private final OAuthGrantService grants; + + public OAuthManagementController(OAuthClientService clients, OAuthGrantService grants) { + this.clients = clients; + this.grants = grants; + } + + @GetMapping("/admin/oauth-clients") + String clients(Authentication authentication, Model model) { + model.addAttribute("clients", clients.list(authentication)); + return "oauth-clients"; + } + + @PostMapping("/admin/oauth-clients") + String register( + Authentication authentication, + @RequestParam String clientId, + @RequestParam String name, + @RequestParam String redirects) { + clients.registerNative( + authentication, + clientId, + name, + redirects + .lines() + .map(String::strip) + .filter(value -> !value.isEmpty()) + .collect(Collectors.toSet())); + return "redirect:/admin/oauth-clients"; + } + + @PostMapping("/admin/oauth-clients/{id}/remove") + String remove(Authentication authentication, @PathVariable String id) { + clients.remove(authentication, id); + return "redirect:/admin/oauth-clients"; + } + + @GetMapping("/oauth-grants") + String grants(Authentication authentication, Model model, HttpServletResponse response) { + response.setHeader("Cache-Control", "no-store"); + model.addAttribute("grants", grants.list(authentication)); + return "oauth-grants"; + } + + @PostMapping("/oauth-grants/{id}/revoke") + String revoke(Authentication authentication, @PathVariable String id) { + grants.revoke(authentication, id); + return "redirect:/oauth-grants"; + } + + @ExceptionHandler(IllegalArgumentException.class) + String invalid(IllegalArgumentException failure, Model model, HttpServletResponse response) { + response.setStatus(400); + model.addAttribute("message", failure.getMessage()); + return "oauth-error"; + } +} diff --git a/src/main/java/org/busystack/caldav/security/oauth/PrincipalOAuthAuthorizationService.java b/src/main/java/org/busystack/caldav/security/oauth/PrincipalOAuthAuthorizationService.java new file mode 100644 index 0000000..16be3d7 --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/oauth/PrincipalOAuthAuthorizationService.java @@ -0,0 +1,108 @@ +package org.busystack.caldav.security.oauth; + +import java.time.Instant; +import java.time.temporal.ChronoUnit; +import java.util.UUID; +import org.busystack.caldav.account.AccountService; +import org.busystack.caldav.security.session.SessionSecurity; +import org.springframework.security.core.Authentication; +import org.springframework.security.oauth2.server.authorization.JdbcOAuth2AuthorizationService; +import org.springframework.security.oauth2.server.authorization.OAuth2Authorization; +import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService; +import org.springframework.security.oauth2.server.authorization.OAuth2TokenType; + +/** Adds stable internal account identity before framework grant state reaches PostgreSQL. */ +final class PrincipalOAuthAuthorizationService implements OAuth2AuthorizationService { + static final String ACCOUNT_ID = "busystack.account_id"; + static final String ACCOUNT_EPOCH = "busystack.account_epoch"; + static final String ABSOLUTE_EXPIRY = "busystack.refresh_absolute_expiry"; + private final JdbcOAuth2AuthorizationService delegate; + private final AccountService accounts; + private final OAuthClientLock clients; + private final org.springframework.transaction.support.TransactionTemplate transaction; + private final org.springframework.transaction.support.TransactionTemplate readSnapshot; + + PrincipalOAuthAuthorizationService( + JdbcOAuth2AuthorizationService delegate, + AccountService accounts, + OAuthClientLock clients, + org.springframework.transaction.PlatformTransactionManager transactions) { + this.delegate = delegate; + this.accounts = accounts; + this.clients = clients; + this.transaction = + new org.springframework.transaction.support.TransactionTemplate(transactions); + this.transaction.setTimeout(15); + this.readSnapshot = + new org.springframework.transaction.support.TransactionTemplate(transactions); + this.readSnapshot.setReadOnly(true); + this.readSnapshot.setIsolationLevel( + org.springframework.transaction.TransactionDefinition.ISOLATION_REPEATABLE_READ); + this.readSnapshot.setTimeout(15); + } + + @Override + public void save(OAuth2Authorization authorization) { + transaction.executeWithoutResult( + status -> { + if (!clients.registrationExists(authorization.getRegisteredClientId())) + throw new org.springframework.security.oauth2.server.authorization.authentication + .OAuth2AuthorizationCodeRequestAuthenticationException( + new org.springframework.security.oauth2.core.OAuth2Error( + org.springframework.security.oauth2.core.OAuth2ErrorCodes.INVALID_CLIENT), + null); + saveLocked(authorization); + }); + } + + private void saveLocked(OAuth2Authorization authorization) { + if (authorization.getAttribute(ACCOUNT_ID) == null) { + var account = accounts.find(authorization.getPrincipalName()); + if (account == null) { + try { + account = accounts.find(UUID.fromString(authorization.getPrincipalName())); + } catch (IllegalArgumentException ex) { + /* Not an internal UUID principal. */ + } + } + if (account == null || !account.enabled()) + throw new IllegalStateException("OAuth principal is unavailable"); + authorization = + OAuth2Authorization.from(authorization) + .attribute(ACCOUNT_ID, account.id().toString()) + .attribute(ABSOLUTE_EXPIRY, Instant.now().plus(30, ChronoUnit.DAYS).toString()) + .build(); + } + if (authorization.getAttribute(ACCOUNT_EPOCH) == null) { + Object principal = authorization.getAttribute(java.security.Principal.class.getName()); + UUID captured = + principal instanceof Authentication authentication + ? SessionSecurity.capturedEpoch(authentication.getAuthorities()) + : null; + if (captured == null) + throw new IllegalStateException("OAuth authorization has no captured account generation"); + authorization = + OAuth2Authorization.from(authorization) + .attribute(ACCOUNT_EPOCH, captured.toString()) + .build(); + } + delegate.save(authorization); + } + + @Override + public void remove(OAuth2Authorization authorization) { + delegate.remove(authorization); + } + + @Override + public OAuth2Authorization findById(String id) { + // REQUIRED joins grant writes so their uncommitted state remains visible. Standalone reads + // keep the authorization row and its separately loaded registration in one MVCC snapshot. + return readSnapshot.execute(status -> delegate.findById(id)); + } + + @Override + public OAuth2Authorization findByToken(String token, OAuth2TokenType type) { + return readSnapshot.execute(status -> delegate.findByToken(token, type)); + } +} diff --git a/src/main/java/org/busystack/caldav/security/oauth/PublicNativeClientAuthentication.java b/src/main/java/org/busystack/caldav/security/oauth/PublicNativeClientAuthentication.java new file mode 100644 index 0000000..83ba608 --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/oauth/PublicNativeClientAuthentication.java @@ -0,0 +1,129 @@ +package org.busystack.caldav.security.oauth; + +import jakarta.servlet.http.HttpServletRequest; +import org.springframework.security.authentication.AuthenticationProvider; +import org.springframework.security.core.Authentication; +import org.springframework.security.oauth2.core.AuthorizationGrantType; +import org.springframework.security.oauth2.core.ClientAuthenticationMethod; +import org.springframework.security.oauth2.core.OAuth2AuthenticationException; +import org.springframework.security.oauth2.core.OAuth2ErrorCodes; +import org.springframework.security.oauth2.server.authorization.authentication.OAuth2ClientAuthenticationToken; +import org.springframework.security.oauth2.server.authorization.client.RegisteredClient; +import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository; +import org.springframework.security.web.authentication.AuthenticationConverter; + +/** + * Public-client identification; possession and binding of the grant are checked by grant providers. + */ +final class PublicNativeClientAuthentication { + private PublicNativeClientAuthentication() {} + + private static final class PublicClientToken extends OAuth2ClientAuthenticationToken { + PublicClientToken(String clientId) { + super(clientId, ClientAuthenticationMethod.NONE, null, null); + } + } + + static final class Converter implements AuthenticationConverter { + @Override + public Authentication convert(HttpServletRequest request) { + if (!"POST".equals(request.getMethod())) return null; + boolean token = "/oauth2/token".equals(request.getServletPath()); + boolean revoke = "/oauth2/revoke".equals(request.getServletPath()); + if (!token && !revoke) return null; + String[] secrets = request.getParameterValues("client_secret"); + if (secrets != null && secrets.length != 1) + throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_REQUEST); + if (token) { + // Validate the selected profile before a framework PKCE matcher can decline a malformed + // request and let it reach the browser entry point. Unknown extension parameters remain + // untouched; only the defined single-valued token parameters are checked here. + for (String parameter : + java.util.List.of( + "grant_type", + "client_id", + "code", + "redirect_uri", + "code_verifier", + "refresh_token", + "scope")) { + String[] values = request.getParameterValues(parameter); + if (values != null && values.length != 1) + throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_REQUEST); + } + String grant = request.getParameter("grant_type"); + if (grant == null || grant.isBlank()) + throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_REQUEST); + if (!"authorization_code".equals(grant) && !"refresh_token".equals(grant)) + throw new OAuth2AuthenticationException(OAuth2ErrorCodes.UNSUPPORTED_GRANT_TYPE); + } + if (request.getHeader("Authorization") != null) return null; + // RFC 6749 section 3.2 treats an empty optional value as omitted. Actual secrets, + // including whitespace, remain unsupported by this public-client profile. + String secret = request.getParameter("client_secret"); + if (secret != null && !secret.isEmpty()) + throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_CLIENT); + if (token && "authorization_code".equals(request.getParameter("grant_type"))) { + for (String required : java.util.List.of("client_id", "code")) { + String value = request.getParameter(required); + if (value == null || value.isBlank()) + throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_REQUEST); + } + if (request.getParameter("code_verifier") == null) + throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_GRANT); + // The configured Spring public-client provider still performs the single PKCE check. + return null; + } + boolean refresh = token && "refresh_token".equals(request.getParameter("grant_type")); + if (!refresh && !revoke) return null; + String[] ids = request.getParameterValues("client_id"); + String[] grants = request.getParameterValues("grant_type"); + if (ids == null + || ids.length != 1 + || ids[0].isBlank() + || ids[0].length() > 100 + || (refresh && (grants == null || grants.length != 1))) + throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_REQUEST); + return new PublicClientToken(ids[0]); + } + } + + static final class Provider implements AuthenticationProvider { + private final RegisteredClientRepository clients; + + Provider(RegisteredClientRepository clients) { + this.clients = clients; + } + + @Override + public Authentication authenticate(Authentication authentication) { + RegisteredClient client = clients.findByClientId(authentication.getName()); + if (client == null + || !client.getClientAuthenticationMethods().contains(ClientAuthenticationMethod.NONE) + || !client.getAuthorizationGrantTypes().contains(AuthorizationGrantType.REFRESH_TOKEN)) + throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_CLIENT); + return new OAuth2ClientAuthenticationToken(client, ClientAuthenticationMethod.NONE, null); + } + + @Override + public boolean supports(Class type) { + return PublicClientToken.class.isAssignableFrom(type); + } + } + + /** Terminal failure mapping after the supported public-client providers have declined a token. */ + static final class RejectUnsupported implements AuthenticationProvider { + @Override + public Authentication authenticate(Authentication authentication) { + if (ClientAuthenticationMethod.NONE.equals( + ((OAuth2ClientAuthenticationToken) authentication).getClientAuthenticationMethod())) + return null; // Preserve a supported public provider's precise PKCE/grant failure. + throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_CLIENT); + } + + @Override + public boolean supports(Class type) { + return OAuth2ClientAuthenticationToken.class.isAssignableFrom(type); + } + } +} diff --git a/src/main/java/org/busystack/caldav/security/oauth/RecentMfaAuthorizationFilter.java b/src/main/java/org/busystack/caldav/security/oauth/RecentMfaAuthorizationFilter.java new file mode 100644 index 0000000..f71479a --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/oauth/RecentMfaAuthorizationFilter.java @@ -0,0 +1,46 @@ +package org.busystack.caldav.security.oauth; + +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.io.IOException; +import org.springframework.security.authentication.AnonymousAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.web.filter.OncePerRequestFilter; + +/** + * OAuth client access requires recent primary sign-in plus passkey verification, including consent + * reuse. + */ +final class RecentMfaAuthorizationFilter extends OncePerRequestFilter { + private final org.springframework.security.web.savedrequest.HttpSessionRequestCache requestCache = + new org.springframework.security.web.savedrequest.HttpSessionRequestCache(); + + @Override + protected boolean shouldNotFilter(HttpServletRequest request) { + return !"/oauth2/authorize".equals(request.getServletPath()); + } + + @Override + protected void doFilterInternal( + HttpServletRequest request, HttpServletResponse response, FilterChain chain) + throws ServletException, IOException { + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + if (authentication != null + && authentication.isAuthenticated() + && !(authentication instanceof AnonymousAuthenticationToken)) { + try { + org.busystack.caldav.security.RecentAuthentication.requireMfa(authentication); + } catch (org.springframework.security.access.AccessDeniedException ex) { + if ("GET".equals(request.getMethod())) { + requestCache.saveRequest(request, response); + response.sendRedirect(request.getContextPath() + "/login?factor=required"); + } else response.sendError(403, "Recent primary and passkey verification required"); + return; + } + } + chain.doFilter(request, response); + } +} diff --git a/src/main/java/org/busystack/caldav/security/oauth/RefreshFamilyStore.java b/src/main/java/org/busystack/caldav/security/oauth/RefreshFamilyStore.java new file mode 100644 index 0000000..91dbcec --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/oauth/RefreshFamilyStore.java @@ -0,0 +1,273 @@ +package org.busystack.caldav.security.oauth; + +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.sql.Timestamp; +import java.time.Instant; +import java.util.HexFormat; +import java.util.List; +import java.util.Objects; +import java.util.UUID; +import java.util.function.Function; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Service; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.TransactionDefinition; +import org.springframework.transaction.support.TransactionTemplate; + +/** + * Durable replay protection around a framework authorization mutation. The callback must use the + * same transaction manager and datasource. Token generation and OAuth parsing belong to the + * framework. + */ +@Service +public class RefreshFamilyStore { + public record Family( + String authorizationId, UUID accountId, String clientId, Instant expiresAt) {} + + public record Rotation(T response, String successorToken, Instant successorExpiresAt) {} + + private record Attempt(T response, RefreshRejectedException.Reason rejection) {} + + private record LockedFamily(Family family, boolean revoked, boolean epochValid) {} + + private final JdbcTemplate jdbc; + private final TransactionTemplate transaction; + private final TransactionTemplate joiningTransaction; + private final org.busystack.caldav.account.AccountService accounts; + private final OAuthClientLock clients; + + public RefreshFamilyStore( + JdbcTemplate jdbc, + PlatformTransactionManager transactions, + org.busystack.caldav.account.AccountService accounts, + OAuthClientLock clients) { + this.jdbc = jdbc; + this.accounts = accounts; + this.clients = clients; + this.transaction = new TransactionTemplate(transactions); + this.joiningTransaction = new TransactionTemplate(transactions); + joiningTransaction.setTimeout(15); + // A committed replay revocation must survive the adapter throwing invalid_grant afterwards. + transaction.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); + transaction.setTimeout(15); + } + + /** Persists initial framework authorization and family state in one transaction. */ + public void register( + Family family, String token, Instant tokenExpiresAt, Runnable persistAuthorization) { + Objects.requireNonNull(family); + Objects.requireNonNull(persistAuthorization); + String hash = tokenHash(token); + if (!family.expiresAt().isAfter(Instant.now()) || !tokenExpiresAt.isAfter(Instant.now())) { + throw new IllegalArgumentException("Refresh expiry must be in the future"); + } + joiningTransaction.executeWithoutResult( + status -> { + if (!clients.matches(family.clientId(), null)) + throw new RefreshRejectedException(RefreshRejectedException.Reason.INVALID); + lockAuthorization(family.authorizationId()); + if (!lockEnabledAccount(family.accountId())) + throw new RefreshRejectedException(RefreshRejectedException.Reason.DISABLED); + jdbc.update( + "INSERT INTO oauth_refresh_families(authorization_id,account_id,client_id,recovery_epoch,expires_at) SELECT ?,?,?,recovery_epoch,? FROM system_state WHERE id=1", + family.authorizationId(), + family.accountId(), + family.clientId(), + Timestamp.from(family.expiresAt())); + jdbc.update( + "INSERT INTO oauth_refresh_tokens(token_hash,authorization_id,expires_at) VALUES (?,?,?)", + hash, + family.authorizationId(), + Timestamp.from(earlier(tokenExpiresAt, family.expiresAt()))); + persistAuthorization.run(); + }); + } + + /** + * Serializes validation, consumption, framework grant persistence, and successor registration. + * Reusing a valid spent token revokes the entire family before this method throws. + */ + public T refresh( + String token, String clientId, Function> mutateAuthorization) { + return refresh(token, clientId, null, mutateAuthorization); + } + + public T refresh( + String token, + String clientId, + String registeredClientId, + Function> mutateAuthorization) { + Objects.requireNonNull(mutateAuthorization); + String hash = tokenHash(token); + Attempt outcome = + transaction.execute( + status -> { + if (!clients.matches(clientId, registeredClientId)) + return rejected(RefreshRejectedException.Reason.INVALID); + List candidates = + jdbc.query( + "SELECT f.authorization_id,f.account_id,f.client_id,f.expires_at FROM oauth_refresh_families f JOIN oauth_refresh_tokens t USING(authorization_id) WHERE t.token_hash=?", + (rs, row) -> + new Family( + rs.getString(1), + rs.getObject(2, UUID.class), + rs.getString(3), + rs.getTimestamp(4).toInstant()), + hash); + if (candidates.isEmpty() + || !Objects.equals(candidates.getFirst().clientId(), clientId)) + return rejected(RefreshRejectedException.Reason.INVALID); + Family candidate = candidates.getFirst(); + lockAuthorization(candidate.authorizationId()); + if (!lockEnabledAccount(candidate.accountId())) + return rejected(RefreshRejectedException.Reason.DISABLED); + List families = + jdbc.query( + "SELECT f.authorization_id,f.account_id,f.client_id,f.expires_at,f.revoked_at,(f.recovery_epoch=s.recovery_epoch) AS epoch_valid FROM oauth_refresh_families f CROSS JOIN system_state s WHERE f.authorization_id=? AND s.id=1 FOR UPDATE OF f", + (rs, row) -> + new LockedFamily( + new Family( + rs.getString(1), + rs.getObject(2, UUID.class), + rs.getString(3), + rs.getTimestamp(4).toInstant()), + rs.getTimestamp(5) != null, + rs.getBoolean(6)), + candidate.authorizationId()); + if (families.isEmpty()) return rejected(RefreshRejectedException.Reason.INVALID); + LockedFamily locked = families.getFirst(); + Family family = locked.family(); + if (locked.revoked() || !locked.epochValid()) + return rejected(RefreshRejectedException.Reason.REVOKED); + Instant now = Instant.now(); + if (!family.expiresAt().isAfter(now)) + return rejected(RefreshRejectedException.Reason.EXPIRED); + var tokens = + jdbc.query( + "SELECT expires_at,consumed_at FROM oauth_refresh_tokens WHERE token_hash=? AND authorization_id=?", + (rs, row) -> + new Instant[] { + rs.getTimestamp(1).toInstant(), + rs.getTimestamp(2) == null ? null : rs.getTimestamp(2).toInstant() + }, + hash, + family.authorizationId()); + if (tokens.isEmpty()) return rejected(RefreshRejectedException.Reason.INVALID); + Instant[] stored = tokens.getFirst(); + if (stored[1] != null) { + jdbc.update( + "UPDATE oauth_refresh_families SET revoked_at=now() WHERE authorization_id=?", + family.authorizationId()); + return rejected(RefreshRejectedException.Reason.REPLAY); + } + if (!stored[0].isAfter(now)) return rejected(RefreshRejectedException.Reason.EXPIRED); + jdbc.update( + "UPDATE oauth_refresh_tokens SET consumed_at=now() WHERE token_hash=?", hash); + Rotation rotation = + Objects.requireNonNull( + mutateAuthorization.apply(family), + "Framework mutation must return a rotation"); + String successorHash = tokenHash(rotation.successorToken()); + if (hash.equals(successorHash) || !rotation.successorExpiresAt().isAfter(now)) + throw new IllegalStateException( + "Framework mutation did not rotate the refresh token"); + jdbc.update( + "INSERT INTO oauth_refresh_tokens(token_hash,authorization_id,expires_at) VALUES (?,?,?)", + successorHash, + family.authorizationId(), + Timestamp.from(earlier(rotation.successorExpiresAt(), family.expiresAt()))); + return new Attempt<>(Objects.requireNonNull(rotation.response()), null); + }); + if (outcome == null) throw new IllegalStateException("Refresh transaction returned no outcome"); + if (outcome.rejection() != null) throw new RefreshRejectedException(outcome.rejection()); + return outcome.response(); + } + + /** One-statement check for reference-token authentication; no process-local validity cache. */ + public boolean isActive(String authorizationId) { + Boolean enabled = + jdbc.queryForObject( + "SELECT EXISTS(SELECT 1 FROM oauth_refresh_families f JOIN accounts a ON a.id=f.account_id CROSS JOIN system_state s WHERE f.authorization_id=? AND a.enabled AND f.revoked_at IS NULL AND f.expires_at>now() AND f.recovery_epoch=s.recovery_epoch AND s.id=1)", + Boolean.class, + authorizationId); + return Boolean.TRUE.equals(enabled); + } + + public void revoke(UUID accountId, String authorizationId) { + revoke(accountId, authorizationId, () -> {}); + } + + public void revoke(UUID accountId, String authorizationId, Runnable afterLock) { + transaction.executeWithoutResult( + status -> { + var registrations = + jdbc.queryForList( + "SELECT client_id FROM oauth_refresh_families WHERE authorization_id=? AND account_id=?", + String.class, + authorizationId, + accountId); + if (registrations.isEmpty()) return; + clients.lock(registrations.getFirst()); + lockAuthorization(authorizationId); + lockEnabledAccount(accountId); + afterLock.run(); + jdbc.update( + "UPDATE oauth_refresh_families SET revoked_at=COALESCE(revoked_at,now()) WHERE authorization_id=? AND account_id=?", + authorizationId, + accountId); + }); + } + + /** Serializes code exchange and refresh for the same grant across application processes. */ + void lockAuthorization(String authorizationId) { + if (!org.springframework.transaction.support.TransactionSynchronizationManager + .isActualTransactionActive()) + throw new IllegalStateException("Grant lock requires an active transaction"); + jdbc.query("SELECT pg_advisory_xact_lock(hashtextextended(?,0))", rs -> {}, authorizationId); + } + + void invalidateAuthorization(String authorizationId) { + lockAuthorization(authorizationId); + jdbc.update( + "UPDATE oauth_refresh_families SET revoked_at=COALESCE(revoked_at,now()) WHERE authorization_id=?", + authorizationId); + } + + private boolean lockEnabledAccount(UUID accountId) { + return jdbc + .query( + "SELECT enabled FROM accounts WHERE id=? FOR SHARE", + (rs, row) -> rs.getBoolean(1), + accountId) + .stream() + .findFirst() + .orElse(false) + && accountCurrent(accountId); + } + + boolean accountCurrent(UUID accountId) { + return accounts.isClientAccessCurrent(accountId); + } + + private static Instant earlier(Instant first, Instant second) { + return first.isBefore(second) ? first : second; + } + + private static Attempt rejected(RefreshRejectedException.Reason reason) { + return new Attempt<>(null, reason); + } + + private static String tokenHash(String token) { + if (token == null || token.isBlank() || token.length() > 4096) + throw new RefreshRejectedException(RefreshRejectedException.Reason.INVALID); + try { + return HexFormat.of() + .formatHex( + MessageDigest.getInstance("SHA-256").digest(token.getBytes(StandardCharsets.UTF_8))); + } catch (NoSuchAlgorithmException ex) { + throw new IllegalStateException("Required SHA-256 unavailable", ex); + } + } +} diff --git a/src/main/java/org/busystack/caldav/security/oauth/RefreshRejectedException.java b/src/main/java/org/busystack/caldav/security/oauth/RefreshRejectedException.java new file mode 100644 index 0000000..2f9f8ca --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/oauth/RefreshRejectedException.java @@ -0,0 +1,23 @@ +package org.busystack.caldav.security.oauth; + +/** Adapters must map every reason to the same OAuth invalid_grant response. */ +public final class RefreshRejectedException extends RuntimeException { + public enum Reason { + INVALID, + REVOKED, + EXPIRED, + DISABLED, + REPLAY + } + + private final Reason reason; + + public RefreshRejectedException(Reason reason) { + super("Refresh grant rejected"); + this.reason = reason; + } + + public Reason reason() { + return reason; + } +} diff --git a/src/main/java/org/busystack/caldav/security/oidc/ConfiguredOidcRegistrationRepository.java b/src/main/java/org/busystack/caldav/security/oidc/ConfiguredOidcRegistrationRepository.java new file mode 100644 index 0000000..d59d73d --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/oidc/ConfiguredOidcRegistrationRepository.java @@ -0,0 +1,50 @@ +package org.busystack.caldav.security.oidc; + +import org.busystack.caldav.config.DeploymentProperties; +import org.springframework.security.oauth2.client.registration.ClientRegistration; +import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository; +import org.springframework.security.oauth2.client.registration.ClientRegistrations; + +/** + * Immutable operator configuration; discovery is deferred so outages do not block local startup. + */ +public class ConfiguredOidcRegistrationRepository implements ClientRegistrationRepository { + private final OidcProperties properties; + private final DeploymentProperties deployment; + private volatile ClientRegistration registration; + + public ConfiguredOidcRegistrationRepository( + OidcProperties properties, DeploymentProperties deployment) { + this.properties = properties; + this.deployment = deployment; + var issuer = properties.issuer(); + boolean loopback = + "localhost".equals(issuer.getHost()) + || "127.0.0.1".equals(issuer.getHost()) + || "[::1]".equals(issuer.getHost()); + if (!"https".equals(issuer.getScheme()) + && !(deployment.allowInsecureLocal() && loopback && "http".equals(issuer.getScheme()))) + throw new IllegalArgumentException( + "OIDC issuer must use HTTPS; loopback HTTP requires explicit development mode"); + } + + @Override + public ClientRegistration findByRegistrationId(String id) { + if (!"upstream".equals(id)) return null; + var current = registration; + if (current != null) return current; + synchronized (this) { + if (registration == null) + registration = + ClientRegistrations.fromIssuerLocation(properties.issuer().toString()) + .registrationId("upstream") + .clientId(properties.clientId()) + .clientSecret(properties.clientSecret()) + .scope("openid", "profile") + .redirectUri(deployment.publicBaseUrl() + "/login/oauth2/code/upstream") + .clientName("Organization sign-in") + .build(); + return registration; + } + } +} diff --git a/src/main/java/org/busystack/caldav/security/oidc/ExternalAccessPolicy.java b/src/main/java/org/busystack/caldav/security/oidc/ExternalAccessPolicy.java new file mode 100644 index 0000000..8179ce9 --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/oidc/ExternalAccessPolicy.java @@ -0,0 +1,51 @@ +package org.busystack.caldav.security.oidc; + +import java.time.Instant; +import java.util.UUID; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Service; + +/** Durable per-account boundary for independently issued DAV and OAuth credentials. */ +@Service +public class ExternalAccessPolicy { + private final JdbcTemplate jdbc; + private final OidcProperties properties; + + public ExternalAccessPolicy(JdbcTemplate jdbc, OidcProperties properties) { + this.jdbc = jdbc; + this.properties = properties; + } + + public boolean isCurrent(UUID accountId) { + var mappings = + jdbc.query( + "SELECT issuer,subject,source,last_verified_at FROM external_identities WHERE account_id=?", + (rs, row) -> + new Mapping( + rs.getString("issuer"), + rs.getString("subject"), + rs.getString("source"), + rs.getTimestamp("last_verified_at").toInstant()), + accountId); + if (mappings.isEmpty()) return true; + if (!properties.enabled()) return false; + var mapping = mappings.getFirst(); + return properties.issuer().toString().equals(mapping.issuer()) + && (!mapping.source().equals("ALLOWLIST") + || properties.allowedUsers().stream() + .anyMatch(user -> user.subject().equals(mapping.subject()))) + && mapping + .lastVerified() + .isAfter(Instant.now().minus(properties.maximumAuthenticationAge())); + } + + public boolean isExternal(UUID accountId) { + return Boolean.TRUE.equals( + jdbc.queryForObject( + "SELECT EXISTS(SELECT 1 FROM external_identities WHERE account_id=?)", + Boolean.class, + accountId)); + } + + private record Mapping(String issuer, String subject, String source, Instant lastVerified) {} +} diff --git a/src/main/java/org/busystack/caldav/security/oidc/ExternalIdentityService.java b/src/main/java/org/busystack/caldav/security/oidc/ExternalIdentityService.java new file mode 100644 index 0000000..af133af --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/oidc/ExternalIdentityService.java @@ -0,0 +1,149 @@ +package org.busystack.caldav.security.oidc; + +import java.time.Instant; +import java.util.UUID; +import org.busystack.caldav.account.AccountService; +import org.busystack.caldav.security.RecentAuthentication; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.security.core.Authentication; +import org.springframework.security.oauth2.core.OAuth2AuthenticationException; +import org.springframework.security.oauth2.core.OAuth2Error; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +/** Only the configured issuer and exact subject pair can select an internal account. */ +@Service +public class ExternalIdentityService { + public record LinkIntent(UUID accountId, String username, Instant issuedAt) + implements java.io.Serializable {} + + private final JdbcTemplate jdbc; + private final OidcProperties properties; + private final AccountService accounts; + private final org.busystack.caldav.security.session.ClientAccessRevocation revocation; + private final org.busystack.caldav.security.session.SessionSecurity sessions; + + public ExternalIdentityService( + JdbcTemplate jdbc, + OidcProperties properties, + AccountService accounts, + org.busystack.caldav.security.session.ClientAccessRevocation revocation, + org.busystack.caldav.security.session.SessionSecurity sessions) { + this.jdbc = jdbc; + this.properties = properties; + this.accounts = accounts; + this.revocation = revocation; + this.sessions = sessions; + } + + public LinkIntent prepareLink(Authentication authentication) { + RecentAuthentication.requireMfa(authentication); + sessions.assertCurrent(authentication); + var account = accounts.find(authentication.getName()); + if (account == null || !account.enabled() || !properties.enabled()) throw denied(); + return new LinkIntent(account.id(), account.username(), Instant.now()); + } + + @Transactional + public AccountService.Account verifiedLogin( + String issuer, + String subject, + Instant authenticatedAt, + LinkIntent link, + Authentication current) { + Instant now = Instant.now(); + if (!properties.enabled() + || !properties.issuer().toString().equals(issuer) + || subject == null + || subject.isBlank() + || subject.length() > 255 + || authenticatedAt == null + || authenticatedAt.isBefore(now.minusSeconds(300)) + || authenticatedAt.isAfter(now.plusSeconds(60))) throw denied(); + // One serialized decision per external identity covers creation and explicit linking. + jdbc.query( + "SELECT pg_advisory_xact_lock(hashtextextended(?,0))", rs -> {}, issuer + "\n" + subject); + var existing = + jdbc.queryForList( + "SELECT account_id FROM external_identities WHERE issuer=? AND subject=?", + UUID.class, + issuer, + subject); + UUID accountId; + if (link != null) { + RecentAuthentication.requireMfa(current); + jdbc.query("SELECT id FROM accounts WHERE id=? FOR UPDATE", rs -> {}, link.accountId()); + try { + sessions.assertCurrent(current); + } catch (org.springframework.security.access.AccessDeniedException ex) { + throw denied(); + } + if (!link.issuedAt().isAfter(now.minusSeconds(300)) + || current == null + || !link.username().equals(current.getName())) throw denied(); + var target = accounts.find(link.accountId()); + if (target == null || !target.enabled() || !target.username().equals(link.username())) + throw denied(); + if (!existing.isEmpty() && !existing.getFirst().equals(target.id())) throw denied(); + var previous = + jdbc.queryForList( + "SELECT subject FROM external_identities WHERE account_id=? FOR UPDATE", + String.class, + target.id()); + if (!previous.isEmpty() && (existing.isEmpty() || !existing.getFirst().equals(target.id()))) + throw denied(); + accountId = target.id(); + if (existing.isEmpty()) { + jdbc.update( + "INSERT INTO external_identities(issuer,subject,account_id,source,last_verified_at) VALUES (?,?,?,'LINKED',?)", + issuer, + subject, + accountId, + java.sql.Timestamp.from(authenticatedAt)); + // Linking deliberately converts the account to upstream-managed primary authentication. + jdbc.update("UPDATE accounts SET password_hash=NULL WHERE id=?", accountId); + revocation.revoke(accountId, target.username()); + } + } else if (!existing.isEmpty()) { + accountId = existing.getFirst(); + String source = + jdbc.queryForObject( + "SELECT source FROM external_identities WHERE account_id=?", String.class, accountId); + if ("ALLOWLIST".equals(source) + && properties.allowedUsers().stream().noneMatch(user -> user.subject().equals(subject))) + throw denied(); + } else { + var allowed = + properties.allowedUsers().stream() + .filter(user -> user.subject().equals(subject)) + .findFirst() + .orElseThrow(ExternalIdentityService::denied); + if (accounts.find(allowed.username()) != null) throw denied(); + accountId = UUID.randomUUID(); + jdbc.update( + "INSERT INTO accounts(id,username,display_name,password_hash,administrator) VALUES (?,?,?,NULL,false)", + accountId, + allowed.username(), + allowed.displayName()); + jdbc.update( + "INSERT INTO external_identities(issuer,subject,account_id,source,last_verified_at) VALUES (?,?,?,'ALLOWLIST',?)", + issuer, + subject, + accountId, + java.sql.Timestamp.from(authenticatedAt)); + } + var account = accounts.find(accountId); + if (account == null || !account.enabled()) throw denied(); + jdbc.update( + "UPDATE external_identities SET last_verified_at=? WHERE issuer=? AND subject=?", + java.sql.Timestamp.from(authenticatedAt.isAfter(now) ? now : authenticatedAt), + issuer, + subject); + return account; + } + + static OAuth2AuthenticationException denied() { + return new OAuth2AuthenticationException( + new OAuth2Error("access_denied"), "Upstream identity is not permitted"); + } +} diff --git a/src/main/java/org/busystack/caldav/security/oidc/LinkingAuthorizationRequestResolver.java b/src/main/java/org/busystack/caldav/security/oidc/LinkingAuthorizationRequestResolver.java new file mode 100644 index 0000000..ed4b6a0 --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/oidc/LinkingAuthorizationRequestResolver.java @@ -0,0 +1,63 @@ +package org.busystack.caldav.security.oidc; + +import jakarta.servlet.http.HttpServletRequest; +import java.io.Serializable; +import org.springframework.security.oauth2.client.web.OAuth2AuthorizationRequestResolver; +import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest; + +/** Associates an explicit linking intent with its own upstream authorization response. */ +final class LinkingAuthorizationRequestResolver implements OAuth2AuthorizationRequestResolver { + static final String PENDING_LINK = + LinkingAuthorizationRequestResolver.class.getName() + ".pending"; + private static final String BOUND_LINK = + LinkingAuthorizationRequestResolver.class.getName() + ".bound"; + + private record BoundLink(String state, ExternalIdentityService.LinkIntent intent) + implements Serializable {} + + private final OAuth2AuthorizationRequestResolver delegate; + + LinkingAuthorizationRequestResolver(OAuth2AuthorizationRequestResolver delegate) { + this.delegate = delegate; + } + + @Override + public OAuth2AuthorizationRequest resolve(HttpServletRequest request) { + return bind(request, delegate.resolve(request)); + } + + @Override + public OAuth2AuthorizationRequest resolve(HttpServletRequest request, String registrationId) { + return bind(request, delegate.resolve(request, registrationId)); + } + + private OAuth2AuthorizationRequest bind( + HttpServletRequest request, OAuth2AuthorizationRequest authorization) { + var session = request.getSession(false); + if (authorization == null || session == null) return authorization; + synchronized (session) { + Object pending = session.getAttribute(PENDING_LINK); + if (pending instanceof ExternalIdentityService.LinkIntent intent) { + if (authorization.getState() == null || authorization.getState().isBlank()) + throw new IllegalStateException("Linking requires authorization response state"); + session.removeAttribute(PENDING_LINK); + session.setAttribute(BOUND_LINK, new BoundLink(authorization.getState(), intent)); + } + } + return authorization; + } + + static ExternalIdentityService.LinkIntent consume(HttpServletRequest request) { + var session = request.getSession(false); + if (session == null) return null; + synchronized (session) { + Object pending = session.getAttribute(BOUND_LINK); + if (pending instanceof BoundLink bound + && bound.state().equals(request.getParameter("state"))) { + session.removeAttribute(BOUND_LINK); + return bound.intent(); + } + return null; + } + } +} diff --git a/src/main/java/org/busystack/caldav/security/oidc/OidcConfiguration.java b/src/main/java/org/busystack/caldav/security/oidc/OidcConfiguration.java new file mode 100644 index 0000000..9f5444f --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/oidc/OidcConfiguration.java @@ -0,0 +1,94 @@ +package org.busystack.caldav.security.oidc; + +import jakarta.servlet.http.HttpServletRequest; +import org.busystack.caldav.account.AccountService; +import org.busystack.caldav.config.DeploymentProperties; +import org.springframework.beans.factory.ObjectProvider; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.core.userdetails.User; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.core.userdetails.UsernameNotFoundException; +import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository; +import org.springframework.security.oauth2.client.web.DefaultOAuth2AuthorizationRequestResolver; +import org.springframework.security.oauth2.client.web.HttpSessionOAuth2AuthorizedClientRepository; +import org.springframework.security.oauth2.client.web.OAuth2AuthorizationRequestCustomizers; +import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler; + +@Configuration +public class OidcConfiguration { + @Configuration + @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) + @ConditionalOnProperty(name = "busystack.oidc.enabled", havingValue = "true") + static class EnabledOidc { + @Bean + ClientRegistrationRepository upstreamRegistration( + OidcProperties properties, DeploymentProperties deployment) { + return new ConfiguredOidcRegistrationRepository(properties, deployment); + } + + @Bean + OidcLoginSupport upstreamUsers( + ExternalIdentityService identities, + OidcProperties properties, + ObjectProvider requests) { + return new OidcLoginSupport(identities, properties, requests); + } + } + + public static void configure( + HttpSecurity http, ClientRegistrationRepository registrations, OidcLoginSupport users) + throws Exception { + var resolver = + new DefaultOAuth2AuthorizationRequestResolver(registrations, "/oauth2/authorization"); + resolver.setAuthorizationRequestCustomizer( + builder -> { + OAuth2AuthorizationRequestCustomizers.withPkce().accept(builder); + builder.additionalParameters( + parameters -> { + parameters.put("prompt", "login"); + parameters.put("max_age", 0); + }); + }); + var repository = new HttpSessionOAuth2AuthorizedClientRepository(); + var success = new SavedRequestAwareAuthenticationSuccessHandler(); + http.oauth2Login( + login -> + login + .loginPage("/login") + .authorizationEndpoint( + endpoint -> + endpoint.authorizationRequestResolver( + new LinkingAuthorizationRequestResolver(resolver))) + .userInfoEndpoint(endpoint -> endpoint.oidcUserService(users)) + .authorizedClientRepository(repository) + .successHandler( + (request, response, authentication) -> { + // This service uses upstream OIDC only for login, so discard access/refresh + // tokens. + repository.removeAuthorizedClient( + "upstream", authentication, request, response); + success.onAuthenticationSuccess(request, response, authentication); + })); + } + + /** Used only by WebAuthn; the form-password provider retains the local-only service. */ + public static UserDetailsService passkeyUsers(AccountService accounts) { + return username -> { + var account = accounts.find(username); + if (account == null || !account.enabled() || !accounts.isClientAccessCurrent(account.id())) + throw new UsernameNotFoundException("Account unavailable"); + return User.withUsername(account.username()) + .password(account.passwordHash() == null ? "" : account.passwordHash()) + .authorities( + new org.springframework.security.core.authority.SimpleGrantedAuthority( + account.administrator() ? "ROLE_ADMIN" : "ROLE_USER"), + org.busystack.caldav.security.session.SessionSecurity.authority( + account.securityEpoch())) + .build(); + }; + } +} diff --git a/src/main/java/org/busystack/caldav/security/oidc/OidcLinkController.java b/src/main/java/org/busystack/caldav/security/oidc/OidcLinkController.java new file mode 100644 index 0000000..08ff114 --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/oidc/OidcLinkController.java @@ -0,0 +1,24 @@ +package org.busystack.caldav.security.oidc; + +import jakarta.servlet.http.HttpServletRequest; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.security.core.Authentication; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PostMapping; + +@Controller +@ConditionalOnProperty(name = "busystack.oidc.enabled", havingValue = "true") +public class OidcLinkController { + private final ExternalIdentityService identities; + + public OidcLinkController(ExternalIdentityService identities) { + this.identities = identities; + } + + @PostMapping("/security/identities/link") + String link(Authentication authentication, HttpServletRequest request) { + var intent = identities.prepareLink(authentication); + request.getSession().setAttribute(LinkingAuthorizationRequestResolver.PENDING_LINK, intent); + return "redirect:/oauth2/authorization/upstream"; + } +} diff --git a/src/main/java/org/busystack/caldav/security/oidc/OidcLoginSupport.java b/src/main/java/org/busystack/caldav/security/oidc/OidcLoginSupport.java new file mode 100644 index 0000000..7136000 --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/oidc/OidcLoginSupport.java @@ -0,0 +1,90 @@ +package org.busystack.caldav.security.oidc; + +import jakarta.servlet.http.HttpServletRequest; +import java.time.Instant; +import java.util.HashMap; +import java.util.List; +import org.springframework.beans.factory.ObjectProvider; +import org.springframework.dao.DuplicateKeyException; +import org.springframework.security.core.authority.FactorGrantedAuthority; +import org.springframework.security.core.authority.SimpleGrantedAuthority; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.oauth2.client.oidc.userinfo.OidcUserRequest; +import org.springframework.security.oauth2.client.oidc.userinfo.OidcUserService; +import org.springframework.security.oauth2.client.userinfo.OAuth2UserService; +import org.springframework.security.oauth2.core.oidc.OidcIdToken; +import org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser; +import org.springframework.security.oauth2.core.oidc.user.OidcUser; + +/** Maps an already framework-verified OIDC identity to an application principal. */ +public class OidcLoginSupport implements OAuth2UserService { + public static final String TRUSTED_UPSTREAM_AUTHORITY = "BUSYSTACK_TRUSTED_UPSTREAM"; + private static final String LOCAL_NAME = "busystack.internal.username"; + private final OidcUserService delegate = new OidcUserService(); + private final ExternalIdentityService identities; + private final OidcProperties properties; + private final ObjectProvider requests; + + public OidcLoginSupport( + ExternalIdentityService identities, + OidcProperties properties, + ObjectProvider requests) { + this.identities = identities; + this.properties = properties; + this.requests = requests; + } + + @Override + public OidcUser loadUser(OidcUserRequest request) { + if (!"upstream".equals(request.getClientRegistration().getRegistrationId()) + || !properties + .issuer() + .toString() + .equals(request.getClientRegistration().getProviderDetails().getIssuerUri())) + throw ExternalIdentityService.denied(); + OidcUser verified = delegate.loadUser(request); + var servletRequest = requests.getObject(); + var session = servletRequest.getSession(false); + var link = LinkingAuthorizationRequestResolver.consume(servletRequest); + Instant authenticationTime = verified.getClaimAsInstant("auth_time"); + org.busystack.caldav.account.AccountService.Account account; + try { + account = + identities.verifiedLogin( + verified.getIssuer().toString(), + verified.getSubject(), + authenticationTime, + link, + SecurityContextHolder.getContext().getAuthentication()); + } catch (DuplicateKeyException ex) { + throw ExternalIdentityService.denied(); + } + if (link != null) { + // Linking changes the security generation and deletes existing sessions. + // Start a fresh login before framework MFA merging can retain obsolete factors. + if (session != null) session.invalidate(); + SecurityContextHolder.clearContext(); + } + var authorities = + List.of( + new SimpleGrantedAuthority(account.administrator() ? "ROLE_ADMIN" : "ROLE_USER"), + new SimpleGrantedAuthority(TRUSTED_UPSTREAM_AUTHORITY), + FactorGrantedAuthority.withAuthority( + FactorGrantedAuthority.AUTHORIZATION_CODE_AUTHORITY) + .issuedAt(authenticationTime) + .build(), + org.busystack.caldav.security.session.SessionSecurity.authority( + account.securityEpoch())); + // The raw ID token is unchanged. This local principal view adds an application-only + // name after verified claims; no upstream role or username claim selects an account. + var claims = new HashMap(verified.getIdToken().getClaims()); + claims.put(LOCAL_NAME, account.username()); + var view = + new OidcIdToken( + verified.getIdToken().getTokenValue(), + verified.getIdToken().getIssuedAt(), + verified.getIdToken().getExpiresAt(), + claims); + return new DefaultOidcUser(authorities, view, LOCAL_NAME); + } +} diff --git a/src/main/java/org/busystack/caldav/security/oidc/OidcPageAdvice.java b/src/main/java/org/busystack/caldav/security/oidc/OidcPageAdvice.java new file mode 100644 index 0000000..8b847c0 --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/oidc/OidcPageAdvice.java @@ -0,0 +1,20 @@ +package org.busystack.caldav.security.oidc; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ModelAttribute; + +@ControllerAdvice(annotations = Controller.class) +public class OidcPageAdvice { + private final OidcProperties properties; + + public OidcPageAdvice(OidcProperties properties) { + this.properties = properties; + } + + @ModelAttribute + void upstreamLogin(Model model) { + model.addAttribute("oidcEnabled", properties.enabled()); + } +} diff --git a/src/main/java/org/busystack/caldav/security/oidc/OidcProperties.java b/src/main/java/org/busystack/caldav/security/oidc/OidcProperties.java new file mode 100644 index 0000000..43ab784 --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/oidc/OidcProperties.java @@ -0,0 +1,63 @@ +package org.busystack.caldav.security.oidc; + +import java.net.URI; +import java.time.Duration; +import java.util.List; +import org.busystack.caldav.xml.XmlCharacters; +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties("busystack.oidc") +public record OidcProperties( + boolean enabled, + URI issuer, + String clientId, + String clientSecret, + List allowedUsers, + Duration maximumAuthenticationAge) { + public record AllowedUser(String subject, String username, String displayName) { + public AllowedUser { + if (subject == null + || subject.isBlank() + || subject.length() > 255 + || username == null + || !username.matches("[a-zA-Z0-9][a-zA-Z0-9._-]{0,63}") + || displayName == null + || displayName.isBlank() + || displayName.length() > 200 + || !XmlCharacters.valid(displayName)) + throw new IllegalArgumentException( + "OIDC allowed users require a subject, valid local username and display name"); + } + } + + public OidcProperties { + allowedUsers = allowedUsers == null ? List.of() : List.copyOf(allowedUsers); + maximumAuthenticationAge = + maximumAuthenticationAge == null ? Duration.ofHours(24) : maximumAuthenticationAge; + if (maximumAuthenticationAge.compareTo(Duration.ofMinutes(1)) < 0 + || maximumAuthenticationAge.compareTo(Duration.ofHours(24)) > 0) + throw new IllegalArgumentException( + "OIDC maximum authentication age must be between one minute and 24 hours"); + if (enabled + && (issuer == null + || issuer.getHost() == null + || issuer.getUserInfo() != null + || issuer.getQuery() != null + || issuer.getFragment() != null + || clientId == null + || clientId.isBlank() + || clientSecret == null + || clientSecret.isBlank())) + throw new IllegalArgumentException( + "Enabled OIDC requires a canonical issuer, client ID and client secret"); + if (allowedUsers.stream().map(AllowedUser::subject).distinct().count() != allowedUsers.size() + || allowedUsers.stream().map(AllowedUser::username).distinct().count() + != allowedUsers.size()) + throw new IllegalArgumentException("OIDC allowlist subjects and usernames must be unique"); + } + + @Override + public String toString() { + return "OidcProperties[enabled=" + enabled + ", issuer=" + issuer + ", clientSecret=REDACTED]"; + } +} diff --git a/src/main/java/org/busystack/caldav/security/passkey/AccountPasskeyCredentials.java b/src/main/java/org/busystack/caldav/security/passkey/AccountPasskeyCredentials.java new file mode 100644 index 0000000..7365e4e --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/passkey/AccountPasskeyCredentials.java @@ -0,0 +1,68 @@ +package org.busystack.caldav.security.passkey; + +import java.util.Arrays; +import java.util.List; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.security.authentication.BadCredentialsException; +import org.springframework.security.web.webauthn.api.Bytes; +import org.springframework.security.web.webauthn.api.CredentialRecord; +import org.springframework.security.web.webauthn.management.JdbcUserCredentialRepository; +import org.springframework.security.web.webauthn.management.UserCredentialRepository; +import org.springframework.transaction.annotation.Transactional; + +/** Spring's JDBC representation with immutable ownership/key and monotonic counters. */ +public class AccountPasskeyCredentials implements UserCredentialRepository { + private final JdbcTemplate jdbc; + private final JdbcUserCredentialRepository delegate; + + public AccountPasskeyCredentials(JdbcTemplate jdbc) { + this.jdbc = jdbc; + this.delegate = new JdbcUserCredentialRepository(jdbc); + } + + @Override + public CredentialRecord findByCredentialId(Bytes id) { + return delegate.findByCredentialId(id); + } + + @Override + public List findByUserId(Bytes id) { + return delegate.findByUserId(id); + } + + @Override + @Transactional + public void save(CredentialRecord record) { + jdbc.query( + "SELECT pg_advisory_xact_lock(hashtextextended(?,0))", + rs -> {}, + record.getCredentialId().toBase64UrlString()); + if (Boolean.TRUE.equals( + jdbc.queryForObject( + "SELECT EXISTS(SELECT 1 FROM passkey_revocations WHERE credential_id=?)", + Boolean.class, + record.getCredentialId().toBase64UrlString()))) + throw new BadCredentialsException("Credential revoked"); + var old = delegate.findByCredentialId(record.getCredentialId()); + if (old != null + && (!old.getUserEntityUserId().equals(record.getUserEntityUserId()) + || !Arrays.equals(old.getPublicKey().getBytes(), record.getPublicKey().getBytes()) + || !old.getCreated().equals(record.getCreated()) + || old.isBackupEligible() != record.isBackupEligible() + || old.isUvInitialized() && !record.isUvInitialized() + || old.getSignatureCount() > record.getSignatureCount())) + throw new BadCredentialsException("Credential state changed"); + delegate.save(record); + } + + @Override + @Transactional + public void delete(Bytes id) { + jdbc.query( + "SELECT pg_advisory_xact_lock(hashtextextended(?,0))", rs -> {}, id.toBase64UrlString()); + jdbc.update( + "INSERT INTO passkey_revocations(credential_id) VALUES (?) ON CONFLICT DO NOTHING", + id.toBase64UrlString()); + delegate.delete(id); + } +} diff --git a/src/main/java/org/busystack/caldav/security/passkey/AccountPasskeyUsers.java b/src/main/java/org/busystack/caldav/security/passkey/AccountPasskeyUsers.java new file mode 100644 index 0000000..f9181ef --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/passkey/AccountPasskeyUsers.java @@ -0,0 +1,81 @@ +package org.busystack.caldav.security.passkey; + +import java.util.List; +import java.util.UUID; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.security.authentication.DisabledException; +import org.springframework.security.web.webauthn.api.Bytes; +import org.springframework.security.web.webauthn.api.ImmutablePublicKeyCredentialUserEntity; +import org.springframework.security.web.webauthn.api.PublicKeyCredentialUserEntity; +import org.springframework.security.web.webauthn.management.PublicKeyCredentialUserEntityRepository; +import org.springframework.transaction.annotation.Transactional; + +/** Resolves mutable login names through stable internal account IDs. */ +public class AccountPasskeyUsers implements PublicKeyCredentialUserEntityRepository { + private final JdbcTemplate jdbc; + + public AccountPasskeyUsers(JdbcTemplate jdbc) { + this.jdbc = jdbc; + } + + @Override + public PublicKeyCredentialUserEntity findById(Bytes id) { + return find("u.id=?", id.toBase64UrlString()); + } + + @Override + public PublicKeyCredentialUserEntity findByUsername(String username) { + return find("a.username=?", username); + } + + private PublicKeyCredentialUserEntity find(String predicate, String value) { + return jdbc + .query( + "SELECT u.id,a.username,a.display_name FROM user_entities u JOIN accounts a ON a.id=u.account_id WHERE " + + predicate, + (rs, row) -> + ImmutablePublicKeyCredentialUserEntity.builder() + .id(Bytes.fromBase64(rs.getString("id"))) + .name(rs.getString("username")) + .displayName(rs.getString("display_name")) + .build(), + value) + .stream() + .findFirst() + .orElse(null); + } + + @Override + @Transactional + public void save(PublicKeyCredentialUserEntity entity) { + List accounts = + jdbc.queryForList( + "SELECT id FROM accounts WHERE username=? AND enabled FOR UPDATE", + UUID.class, + entity.getName()); + if (accounts.size() != 1) throw new DisabledException("Account unavailable"); + var existing = findByUsername(entity.getName()); + if (existing != null && !existing.getId().equals(entity.getId())) + throw new IllegalStateException("Passkey enrollment changed; start again"); + jdbc.update( + "INSERT INTO user_entities(id,account_id,name,display_name) VALUES (?,?,?,?) ON CONFLICT(id) DO UPDATE SET name=EXCLUDED.name,display_name=EXCLUDED.display_name", + entity.getId().toBase64UrlString(), + accounts.getFirst(), + entity.getName(), + entity.getDisplayName()); + } + + @Override + @Transactional + public void delete(Bytes id) { + jdbc.update("DELETE FROM user_entities WHERE id=?", id.toBase64UrlString()); + } + + public boolean hasCredentials(String username) { + return Boolean.TRUE.equals( + jdbc.queryForObject( + "SELECT EXISTS(SELECT 1 FROM user_credentials c JOIN user_entities u ON u.id=c.user_entity_user_id JOIN accounts a ON a.id=u.account_id WHERE a.username=?)", + Boolean.class, + username)); + } +} diff --git a/src/main/java/org/busystack/caldav/security/passkey/AccountRelyingPartyOperations.java b/src/main/java/org/busystack/caldav/security/passkey/AccountRelyingPartyOperations.java new file mode 100644 index 0000000..c7fe9a8 --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/passkey/AccountRelyingPartyOperations.java @@ -0,0 +1,147 @@ +package org.busystack.caldav.security.passkey; + +import org.busystack.caldav.security.session.SessionSecurity; +import org.springframework.security.authentication.AnonymousAuthenticationToken; +import org.springframework.security.authentication.BadCredentialsException; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.web.webauthn.api.CredentialRecord; +import org.springframework.security.web.webauthn.api.ImmutableCredentialRecord; +import org.springframework.security.web.webauthn.api.PublicKeyCredentialCreationOptions; +import org.springframework.security.web.webauthn.api.PublicKeyCredentialRequestOptions; +import org.springframework.security.web.webauthn.api.PublicKeyCredentialUserEntity; +import org.springframework.security.web.webauthn.management.PublicKeyCredentialCreationOptionsRequest; +import org.springframework.security.web.webauthn.management.PublicKeyCredentialRequestOptionsRequest; +import org.springframework.security.web.webauthn.management.RelyingPartyAuthenticationRequest; +import org.springframework.security.web.webauthn.management.RelyingPartyRegistrationRequest; +import org.springframework.security.web.webauthn.management.WebAuthnRelyingPartyOperations; +import org.springframework.transaction.annotation.Transactional; + +/** Account binding and verified credential state around the supported Spring RP interface. */ +public class AccountRelyingPartyOperations implements WebAuthnRelyingPartyOperations { + private final WebAuthnRelyingPartyOperations delegate; + private final AccountPasskeyUsers users; + private final AccountPasskeyCredentials credentials; + private final VerifiedCredentialRepository updates; + private final UserDetailsService currentUsers; + private final SessionSecurity sessions; + + AccountRelyingPartyOperations( + WebAuthnRelyingPartyOperations delegate, + AccountPasskeyUsers users, + AccountPasskeyCredentials credentials, + VerifiedCredentialRepository updates, + UserDetailsService currentUsers, + SessionSecurity sessions) { + this.delegate = delegate; + this.users = users; + this.credentials = credentials; + this.updates = updates; + this.currentUsers = currentUsers; + this.sessions = sessions; + } + + @Override + public PublicKeyCredentialCreationOptions createPublicKeyCredentialCreationOptions( + PublicKeyCredentialCreationOptionsRequest request) { + return delegate.createPublicKeyCredentialCreationOptions(request); + } + + @Override + @Transactional + public CredentialRecord registerCredential(RelyingPartyRegistrationRequest request) { + var authentication = SecurityContextHolder.getContext().getAuthentication(); + sessions.assertCurrent(authentication); + var account = users.findByUsername(authentication.getName()); + if (account == null || !account.getId().equals(request.getCreationOptions().getUser().getId())) + throw new PasskeyRegistrationRejectedException( + "Registration does not belong to the ceremony account"); + if (credentials.findByCredentialId(request.getPublicKey().getCredential().getRawId()) != null) + throw new PasskeyRegistrationRejectedException("Credential is already registered"); + try (var update = updates.verification(request.getPublicKey().getCredential().getRawId())) { + var registered = delegate.registerCredential(request); + var verified = update.verifiedRecord(); + if (!account.getId().equals(registered.getUserEntityUserId()) + || registered.isBackupState() && !registered.isBackupEligible()) + throw new PasskeyRegistrationRejectedException("Invalid registered credential state"); + credentials.save(verified); + return registered; + } catch (BadCredentialsException rejected) { + throw new PasskeyRegistrationRejectedException(rejected); + } + } + + @Override + public PublicKeyCredentialRequestOptions createCredentialRequestOptions( + PublicKeyCredentialRequestOptionsRequest request) { + var options = delegate.createCredentialRequestOptions(request); + // An identified account without credentials must not fall back to a discoverable assertion + // for an unrelated account. The allow-list is kept in the framework's server-side session. + if (identified(request.getAuthentication()) && options.getAllowCredentials().isEmpty()) + throw new BadCredentialsException("Account has no available passkey"); + return options; + } + + @Override + @Transactional + public PublicKeyCredentialUserEntity authenticate(RelyingPartyAuthenticationRequest request) { + var key = request.getPublicKey(); + var record = credentials.findByCredentialId(key.getRawId()); + if (record == null) throw new BadCredentialsException("Passkey unavailable"); + var owner = users.findById(record.getUserEntityUserId()); + if (owner == null) throw new BadCredentialsException("Passkey account unavailable"); + + var handle = key.getResponse().getUserHandle(); + var allowed = request.getRequestOptions().getAllowCredentials(); + boolean accountIdentified = !allowed.isEmpty(); + if ((handle == null && !accountIdentified) + || (handle != null && !handle.equals(owner.getId())) + || (accountIdentified + && allowed.stream().noneMatch(item -> key.getRawId().equals(item.getId())))) + throw new BadCredentialsException("Passkey does not belong to the ceremony account"); + + Authentication existing = SecurityContextHolder.getContext().getAuthentication(); + if (identified(existing)) { + sessions.assertCurrent(existing); + var expected = users.findByUsername(existing.getName()); + if (!accountIdentified || expected == null || !expected.getId().equals(owner.getId())) + throw new BadCredentialsException("Passkey does not belong to the authenticated account"); + } + // Check local/upstream state and hold the account generation stable through the state write. + var current = currentUsers.loadUserByUsername(owner.getName()); + sessions.assertCurrent( + UsernamePasswordAuthenticationToken.authenticated( + current.getUsername(), null, current.getAuthorities())); + + try (var update = updates.verification(key.getRawId())) { + var authenticated = delegate.authenticate(request); // One cryptographic verification only. + if (!owner.getId().equals(authenticated.getId())) + throw new BadCredentialsException("Passkey ownership changed"); + var verified = update.verifiedRecord(); + // These are exactly the authenticator-data bytes whose signature and flags the delegate + // accepted above; no state is read from unverified data or saved on a rejected assertion. + byte[] authenticatorData = key.getResponse().getAuthenticatorData().getBytes(); + if (authenticatorData.length < 37) + throw new BadCredentialsException("Invalid authenticator data"); + int flags = Byte.toUnsignedInt(authenticatorData[32]); + boolean backupEligible = (flags & 0x08) != 0; + boolean backupState = (flags & 0x10) != 0; + if (backupEligible != record.isBackupEligible() || backupState && !backupEligible) + throw new BadCredentialsException("Invalid credential backup eligibility"); + credentials.save( + ImmutableCredentialRecord.fromCredentialRecord(verified) + .backupState(backupState) + .uvInitialized(record.isUvInitialized() || (flags & 0x04) != 0) + .build()); + return authenticated; + } + } + + private static boolean identified(Authentication authentication) { + return authentication != null + && authentication.isAuthenticated() + && !(authentication instanceof AnonymousAuthenticationToken); + } +} diff --git a/src/main/java/org/busystack/caldav/security/passkey/BoundedJsonRequest.java b/src/main/java/org/busystack/caldav/security/passkey/BoundedJsonRequest.java new file mode 100644 index 0000000..1173652 --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/passkey/BoundedJsonRequest.java @@ -0,0 +1,69 @@ +package org.busystack.caldav.security.passkey; + +import jakarta.servlet.ReadListener; +import jakarta.servlet.ServletInputStream; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletRequestWrapper; +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; + +/** A small, already bounded JSON body for synchronous framework converters. */ +final class BoundedJsonRequest extends HttpServletRequestWrapper { + private final byte[] body; + private final ServletInputStream input; + + BoundedJsonRequest(HttpServletRequest request, byte[] body) { + super(request); + this.body = body; + var bytes = new ByteArrayInputStream(body); + input = + new ServletInputStream() { + @Override + public int read() { + return bytes.read(); + } + + @Override + public int read(byte[] buffer, int offset, int length) { + return bytes.read(buffer, offset, length); + } + + @Override + public boolean isFinished() { + return bytes.available() == 0; + } + + @Override + public boolean isReady() { + return true; + } + + @Override + public void setReadListener(ReadListener listener) { + throw new IllegalStateException("Authentication reads the request synchronously"); + } + }; + } + + @Override + public ServletInputStream getInputStream() { + return input; + } + + @Override + public BufferedReader getReader() { + return new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8)); + } + + @Override + public int getContentLength() { + return body.length; + } + + @Override + public long getContentLengthLong() { + return body.length; + } +} diff --git a/src/main/java/org/busystack/caldav/security/passkey/CeremonyStore.java b/src/main/java/org/busystack/caldav/security/passkey/CeremonyStore.java new file mode 100644 index 0000000..5556b81 --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/passkey/CeremonyStore.java @@ -0,0 +1,61 @@ +package org.busystack.caldav.security.passkey; + +import java.util.UUID; +import org.busystack.caldav.security.session.SessionSecurity; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.security.authentication.AnonymousAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; + +/** Durable single-use guard around the framework's session-bound, cryptographic challenge check. */ +@Service +public class CeremonyStore { + public enum Purpose { + AUTHENTICATE, + REGISTER + } + + private final JdbcTemplate jdbc; + private final SessionSecurity sessions; + + public CeremonyStore(JdbcTemplate jdbc, SessionSecurity sessions) { + this.jdbc = jdbc; + this.sessions = sessions; + } + + @Transactional + public void issue( + String sessionId, Purpose purpose, String challengeHash, Authentication authentication) { + SessionSecurity.Account account = null; + if (authentication != null + && authentication.isAuthenticated() + && !(authentication instanceof AnonymousAuthenticationToken)) + account = sessions.assertCurrent(authentication); + if (purpose == Purpose.REGISTER && account == null) + throw new org.springframework.security.access.AccessDeniedException( + "Primary authentication required"); + jdbc.update( + "INSERT INTO webauthn_ceremonies(id,session_id,purpose,account_id,security_epoch,recovery_epoch,challenge_hash,expires_at) SELECT ?,?,?,?,?,recovery_epoch,?,now()+interval '5 minutes' FROM system_state WHERE id=1 ON CONFLICT(session_id,purpose) DO UPDATE SET id=EXCLUDED.id,account_id=EXCLUDED.account_id,security_epoch=EXCLUDED.security_epoch,recovery_epoch=EXCLUDED.recovery_epoch,challenge_hash=EXCLUDED.challenge_hash,created_at=now(),expires_at=EXCLUDED.expires_at", + UUID.randomUUID(), + sessionId, + purpose.name(), + account == null ? null : account.id(), + account == null ? null : account.epoch(), + challengeHash); + } + + @Transactional(propagation = Propagation.REQUIRES_NEW) + public boolean consume(String sessionId, Purpose purpose, String challengeHash) { + if (sessionId == null || challengeHash == null) return false; + var removed = + jdbc.query( + "DELETE FROM webauthn_ceremonies c USING system_state s WHERE c.session_id=? AND c.purpose=? AND c.challenge_hash=? AND c.expires_at>now() AND c.recovery_epoch=s.recovery_epoch AND s.id=1 AND (c.account_id IS NULL OR EXISTS(SELECT 1 FROM accounts a WHERE a.id=c.account_id AND a.enabled AND a.security_epoch=c.security_epoch)) RETURNING c.id", + (rs, row) -> rs.getObject(1, UUID.class), + sessionId, + purpose.name(), + challengeHash); + return removed.size() == 1; + } +} diff --git a/src/main/java/org/busystack/caldav/security/passkey/ChallengeRecordingRequest.java b/src/main/java/org/busystack/caldav/security/passkey/ChallengeRecordingRequest.java new file mode 100644 index 0000000..53bf123 --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/passkey/ChallengeRecordingRequest.java @@ -0,0 +1,123 @@ +package org.busystack.caldav.security.passkey; + +import jakarta.servlet.ServletContext; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletRequestWrapper; +import jakarta.servlet.http.HttpSession; +import java.util.Base64; +import java.util.Enumeration; +import org.busystack.caldav.security.audit.SecurityHashes; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.web.webauthn.api.Bytes; +import org.springframework.security.web.webauthn.api.PublicKeyCredentialCreationOptions; +import org.springframework.security.web.webauthn.api.PublicKeyCredentialRequestOptions; + +/** Observes Spring's typed options save before the HTTP challenge response is written. */ +final class ChallengeRecordingRequest extends HttpServletRequestWrapper { + private static final String PREFIX = ChallengeRecordingRequest.class.getName() + "."; + private final CeremonyStore ceremonies; + + ChallengeRecordingRequest(HttpServletRequest request, CeremonyStore ceremonies) { + super(request); + this.ceremonies = ceremonies; + } + + static String attribute(CeremonyStore.Purpose purpose) { + return PREFIX + purpose.name(); + } + + @Override + public HttpSession getSession() { + return getSession(true); + } + + @Override + public HttpSession getSession(boolean create) { + HttpSession session = super.getSession(create); + return session == null ? null : new ObservedSession(session); + } + + private final class ObservedSession implements HttpSession { + private final HttpSession delegate; + + ObservedSession(HttpSession delegate) { + this.delegate = delegate; + } + + @Override + public void setAttribute(String name, Object value) { + if (value instanceof PublicKeyCredentialCreationOptions options) + record(CeremonyStore.Purpose.REGISTER, options.getChallenge()); + else if (value instanceof PublicKeyCredentialRequestOptions options) + record(CeremonyStore.Purpose.AUTHENTICATE, options.getChallenge()); + delegate.setAttribute(name, value); + } + + private void record(CeremonyStore.Purpose purpose, Bytes challenge) { + String hash = SecurityHashes.sha256(Base64.getEncoder().encodeToString(challenge.getBytes())); + ceremonies.issue( + delegate.getId(), purpose, hash, SecurityContextHolder.getContext().getAuthentication()); + delegate.setAttribute(attribute(purpose), hash); + } + + @Override + public long getCreationTime() { + return delegate.getCreationTime(); + } + + @Override + public String getId() { + return delegate.getId(); + } + + @Override + public long getLastAccessedTime() { + return delegate.getLastAccessedTime(); + } + + @Override + public ServletContext getServletContext() { + return delegate.getServletContext(); + } + + @Override + public void setMaxInactiveInterval(int interval) { + delegate.setMaxInactiveInterval(interval); + } + + @Override + public int getMaxInactiveInterval() { + return delegate.getMaxInactiveInterval(); + } + + @Override + public Object getAttribute(String name) { + return delegate.getAttribute(name); + } + + @Override + public Enumeration getAttributeNames() { + return delegate.getAttributeNames(); + } + + @Override + public void removeAttribute(String name) { + delegate.removeAttribute(name); + } + + @Override + public void invalidate() { + delegate.invalidate(); + } + + @Override + public boolean isNew() { + return delegate.isNew(); + } + + @Override + public HttpSession.Accessor getAccessor() { + return delegate.getAccessor(); + } + } +} diff --git a/src/main/java/org/busystack/caldav/security/passkey/PasskeyCeremonyGuard.java b/src/main/java/org/busystack/caldav/security/passkey/PasskeyCeremonyGuard.java new file mode 100644 index 0000000..e7874f7 --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/passkey/PasskeyCeremonyGuard.java @@ -0,0 +1,120 @@ +package org.busystack.caldav.security.passkey; + +import com.webauthn4j.verifier.exception.VerificationException; +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.io.IOException; +import org.busystack.caldav.security.RecentAuthentication; +import org.springframework.security.access.AccessDeniedException; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.web.filter.OncePerRequestFilter; + +/** Recent factors, bounded bodies, and durable single-use ceremonies across session wrappers. */ +public class PasskeyCeremonyGuard extends OncePerRequestFilter { + private static final int MAX_JSON_BYTES = 65536; + private final AccountPasskeyUsers users; + private final CeremonyStore ceremonies; + + public PasskeyCeremonyGuard(AccountPasskeyUsers users, CeremonyStore ceremonies) { + this.users = users; + this.ceremonies = ceremonies; + } + + @Override + protected boolean shouldNotFilter(HttpServletRequest request) { + String path = request.getServletPath(); + return !(path.startsWith("/webauthn/") + || path.equals("/login/webauthn") + || path.equals("/security/passkeys")); + } + + @Override + protected void doFilterInternal( + HttpServletRequest request, HttpServletResponse response, FilterChain chain) + throws ServletException, IOException { + String path = request.getServletPath(); + if (path.startsWith("/webauthn/register") || path.equals("/security/passkeys")) { + var authentication = SecurityContextHolder.getContext().getAuthentication(); + try { + RecentAuthentication.requirePrimary(authentication); + if (users.hasCredentials(authentication.getName())) + RecentAuthentication.requireMfa(authentication); + } catch (AccessDeniedException ex) { + if ("GET".equals(request.getMethod())) + response.sendRedirect(request.getContextPath() + "/login?factor=required"); + else response.sendError(403, "Recent authentication is required"); + return; + } + } + boolean post = "POST".equals(request.getMethod()); + if (post && path.equals("/webauthn/authenticate/options")) { + var authentication = SecurityContextHolder.getContext().getAuthentication(); + if (authentication != null + && authentication.isAuthenticated() + && !(authentication + instanceof org.springframework.security.authentication.AnonymousAuthenticationToken) + && !users.hasCredentials(authentication.getName())) { + response.sendError(403, "This account has no available passkey"); + return; + } + } + if (post && (path.equals("/login/webauthn") || path.equals("/webauthn/register"))) { + CeremonyStore.Purpose purpose = + path.equals("/login/webauthn") + ? CeremonyStore.Purpose.AUTHENTICATE + : CeremonyStore.Purpose.REGISTER; + var session = request.getSession(false); + Object challenge = + session == null + ? null + : session.getAttribute(ChallengeRecordingRequest.attribute(purpose)); + if (session == null + || !(challenge instanceof String hash) + || !ceremonies.consume(session.getId(), purpose, hash)) { + response.sendError( + path.equals("/login/webauthn") ? 401 : 400, "Challenge expired or already used"); + return; + } + } + String encoding = request.getHeader("Content-Encoding"); + if (encoding != null && !"identity".equalsIgnoreCase(encoding)) { + response.sendError(415, "Encoded authentication bodies are not supported"); + return; + } + if (request.getContentLengthLong() > MAX_JSON_BYTES) { + response.sendError(413, "Authentication body exceeds 64 KiB"); + return; + } + String contentType = request.getContentType(); + String mediaType = + contentType == null + ? "" + : contentType.split(";", 2)[0].trim().toLowerCase(java.util.Locale.ROOT); + boolean json = + mediaType.equals("application/json") + || (mediaType.startsWith("application/") && mediaType.endsWith("+json")); + HttpServletRequest processed = request; + if (json) { + byte[] body = request.getInputStream().readNBytes(MAX_JSON_BYTES + 1); + if (body.length > MAX_JSON_BYTES) { + response.sendError(413, "Authentication body exceeds 64 KiB"); + return; + } + processed = new BoundedJsonRequest(request, body); + } + if (post + && (path.equals("/webauthn/authenticate/options") + || path.equals("/webauthn/register/options"))) + processed = new ChallengeRecordingRequest(processed, ceremonies); + try { + chain.doFilter(processed, response); + } catch (VerificationException | PasskeyRegistrationRejectedException ex) { + // The configured registration filter has no failure handler. Its supported RP operation + // has already unwound (including transaction rollback) before this boundary is reached. + if (!post || !path.equals("/webauthn/register") || response.isCommitted()) throw ex; + response.sendError(400, "Invalid passkey registration"); + } + } +} diff --git a/src/main/java/org/busystack/caldav/security/passkey/PasskeyConfiguration.java b/src/main/java/org/busystack/caldav/security/passkey/PasskeyConfiguration.java new file mode 100644 index 0000000..1bc54c4 --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/passkey/PasskeyConfiguration.java @@ -0,0 +1,94 @@ +package org.busystack.caldav.security.passkey; + +import java.util.Set; +import org.busystack.caldav.config.DeploymentProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.security.config.ObjectPostProcessor; +import org.springframework.security.config.annotation.authorization.EnableMultiFactorAuthentication; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; +import org.springframework.security.web.authentication.session.SessionFixationProtectionStrategy; +import org.springframework.security.web.webauthn.api.AuthenticatorSelectionCriteria; +import org.springframework.security.web.webauthn.api.PublicKeyCredentialRpEntity; +import org.springframework.security.web.webauthn.api.ResidentKeyRequirement; +import org.springframework.security.web.webauthn.api.UserVerificationRequirement; +import org.springframework.security.web.webauthn.authentication.WebAuthnAuthenticationFilter; +import org.springframework.security.web.webauthn.management.Webauthn4JRelyingPartyOperations; + +@Configuration +@EnableMultiFactorAuthentication(authorities = {}) +public class PasskeyConfiguration { + @Bean + AccountPasskeyUsers passkeyUsers(JdbcTemplate jdbc) { + return new AccountPasskeyUsers(jdbc); + } + + @Bean + AccountPasskeyCredentials passkeyCredentials(JdbcTemplate jdbc) { + return new AccountPasskeyCredentials(jdbc); + } + + @Bean + AccountRelyingPartyOperations relyingParty( + AccountPasskeyUsers users, + AccountPasskeyCredentials credentials, + DeploymentProperties properties, + org.busystack.caldav.account.AccountService accounts, + org.busystack.caldav.security.session.SessionSecurity sessions) { + var base = properties.publicBaseUrl(); + String origin = base.getScheme() + "://" + base.getRawAuthority(); + var updates = new VerifiedCredentialRepository(credentials); + var operations = + new Webauthn4JRelyingPartyOperations( + users, + updates, + PublicKeyCredentialRpEntity.builder() + .id(base.getHost()) + .name("BusyStack CalDAV") + .build(), + Set.of(origin)); + operations.setCustomizeCreationOptions( + options -> + options.authenticatorSelection( + AuthenticatorSelectionCriteria.builder() + .residentKey(ResidentKeyRequirement.REQUIRED) + .userVerification(UserVerificationRequirement.REQUIRED) + .build())); + operations.setCustomizeRequestOptions( + options -> options.userVerification(UserVerificationRequirement.REQUIRED)); + return new AccountRelyingPartyOperations( + operations, + users, + credentials, + updates, + org.busystack.caldav.security.oidc.OidcConfiguration.passkeyUsers(accounts), + sessions); + } + + public static void configure( + HttpSecurity http, + DeploymentProperties properties, + AccountPasskeyUsers users, + CeremonyStore ceremonies) { + var base = properties.publicBaseUrl(); + http.webAuthn( + web -> { + web.rpId(base.getHost()) + .rpName("BusyStack CalDAV") + .allowedOrigins(base.getScheme() + "://" + base.getRawAuthority()) + .disableDefaultRegistrationPage(true); + web.addObjectPostProcessor( + new ObjectPostProcessor() { + @Override + public O postProcess(O filter) { + filter.setSessionAuthenticationStrategy(new SessionFixationProtectionStrategy()); + return filter; + } + }); + }); + http.addFilterBefore( + new PasskeyCeremonyGuard(users, ceremonies), UsernamePasswordAuthenticationFilter.class); + } +} diff --git a/src/main/java/org/busystack/caldav/security/passkey/PasskeyPages.java b/src/main/java/org/busystack/caldav/security/passkey/PasskeyPages.java new file mode 100644 index 0000000..ee0139c --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/passkey/PasskeyPages.java @@ -0,0 +1,33 @@ +package org.busystack.caldav.security.passkey; + +import java.util.List; +import org.springframework.security.core.Authentication; +import org.springframework.security.web.webauthn.api.CredentialRecord; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; + +@Controller +public class PasskeyPages { + private final AccountPasskeyUsers users; + private final AccountPasskeyCredentials credentials; + + public PasskeyPages(AccountPasskeyUsers users, AccountPasskeyCredentials credentials) { + this.users = users; + this.credentials = credentials; + } + + @GetMapping("/login") + public String login() { + return "login"; + } + + @GetMapping("/security/passkeys") + public String passkeys(Authentication authentication, Model model) { + var user = users.findByUsername(authentication.getName()); + List records = + user == null ? List.of() : credentials.findByUserId(user.getId()); + model.addAttribute("credentials", records); + return "passkeys"; + } +} diff --git a/src/main/java/org/busystack/caldav/security/passkey/PasskeyRegistrationRejectedException.java b/src/main/java/org/busystack/caldav/security/passkey/PasskeyRegistrationRejectedException.java new file mode 100644 index 0000000..92c85af --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/passkey/PasskeyRegistrationRejectedException.java @@ -0,0 +1,12 @@ +package org.busystack.caldav.security.passkey; + +/** A rejected registration is a client ceremony error, not a request to restart browser login. */ +final class PasskeyRegistrationRejectedException extends RuntimeException { + PasskeyRegistrationRejectedException(String message) { + super(message); + } + + PasskeyRegistrationRejectedException(Throwable cause) { + super("Invalid passkey registration", cause); + } +} diff --git a/src/main/java/org/busystack/caldav/security/passkey/VerifiedCredentialRepository.java b/src/main/java/org/busystack/caldav/security/passkey/VerifiedCredentialRepository.java new file mode 100644 index 0000000..ef3ccb5 --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/passkey/VerifiedCredentialRepository.java @@ -0,0 +1,69 @@ +package org.busystack.caldav.security.passkey; + +import java.util.List; +import org.springframework.security.authentication.BadCredentialsException; +import org.springframework.security.web.webauthn.api.Bytes; +import org.springframework.security.web.webauthn.api.CredentialRecord; +import org.springframework.security.web.webauthn.management.UserCredentialRepository; + +/** Holds a framework credential update until the application's entire RP decision succeeds. */ +final class VerifiedCredentialRepository implements UserCredentialRepository { + private final AccountPasskeyCredentials delegate; + private final ThreadLocal verifications = new ThreadLocal<>(); + + VerifiedCredentialRepository(AccountPasskeyCredentials delegate) { + this.delegate = delegate; + } + + VerificationUpdate verification(Bytes credentialId) { + if (verifications.get() != null) throw new IllegalStateException("Nested passkey verification"); + var update = new VerificationUpdate(credentialId); + verifications.set(update); + return update; + } + + @Override + public CredentialRecord findByCredentialId(Bytes id) { + return delegate.findByCredentialId(id); + } + + @Override + public List findByUserId(Bytes id) { + return delegate.findByUserId(id); + } + + @Override + public void save(CredentialRecord record) { + var verification = verifications.get(); + if (verification == null) delegate.save(record); + else { + if (verification.record != null || !verification.id.equals(record.getCredentialId())) + throw new BadCredentialsException("Unexpected credential update"); + verification.record = record; + } + } + + @Override + public void delete(Bytes id) { + delegate.delete(id); + } + + final class VerificationUpdate implements AutoCloseable { + private final Bytes id; + private CredentialRecord record; + + private VerificationUpdate(Bytes id) { + this.id = id; + } + + CredentialRecord verifiedRecord() { + if (record == null) throw new BadCredentialsException("Missing credential update"); + return record; + } + + @Override + public void close() { + verifications.remove(); + } + } +} diff --git a/src/main/java/org/busystack/caldav/security/password/AccountPasswordService.java b/src/main/java/org/busystack/caldav/security/password/AccountPasswordService.java new file mode 100644 index 0000000..90d531c --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/password/AccountPasswordService.java @@ -0,0 +1,81 @@ +package org.busystack.caldav.security.password; + +import java.util.UUID; +import org.busystack.caldav.security.RecentAuthentication; +import org.busystack.caldav.security.audit.SecurityAudit; +import org.busystack.caldav.security.session.ClientAccessRevocation; +import org.busystack.caldav.security.session.SessionSecurity; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.security.access.AccessDeniedException; +import org.springframework.security.authentication.CredentialsExpiredException; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.userdetails.User; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UserDetailsPasswordService; +import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +/** Local password lifecycle and the DAO provider's supported adaptive-hash upgrade hook. */ +@Service +public class AccountPasswordService implements UserDetailsPasswordService { + private final JdbcTemplate jdbc; + private final PasswordEncoder passwords; + private final SessionSecurity sessions; + private final ClientAccessRevocation clients; + private final SecurityAudit audit; + + public AccountPasswordService( + JdbcTemplate jdbc, + PasswordEncoder passwords, + SessionSecurity sessions, + ClientAccessRevocation clients, + SecurityAudit audit) { + this.jdbc = jdbc; + this.passwords = passwords; + this.sessions = sessions; + this.clients = clients; + this.audit = audit; + } + + @Transactional + public void change(Authentication authentication, String replacement, String peer) { + RecentAuthentication.requireMfa(authentication); + PasswordPolicy.validate(replacement); + var account = sessions.assertForReset(authentication); + Boolean local = + jdbc.queryForObject( + "SELECT password_hash IS NOT NULL AND NOT EXISTS(SELECT 1 FROM external_identities e WHERE e.account_id=a.id) FROM accounts a WHERE a.id=?", + Boolean.class, + account.id()); + if (!Boolean.TRUE.equals(local)) + throw new AccessDeniedException( + "This account's primary sign-in is managed by its identity provider"); + jdbc.update( + "UPDATE accounts SET password_hash=? WHERE id=?", + passwords.encode(replacement), + account.id()); + jdbc.update("DELETE FROM account_recovery_codes WHERE account_id=?", account.id()); + clients.revoke(account.id(), account.username()); + audit.record( + account.id(), SecurityAudit.Action.PASSWORD_CHANGED, SecurityAudit.Outcome.SUCCESS, peer); + } + + @Override + @Transactional + public UserDetails updatePassword(UserDetails user, String upgradedHash) { + UUID captured = SessionSecurity.capturedEpoch(user.getAuthorities()); + if (captured == null) + throw new CredentialsExpiredException("Account credential changed during sign-in"); + int changed = + jdbc.update( + "UPDATE accounts a SET password_hash=? WHERE username=? AND password_hash=? AND security_epoch=? AND enabled AND NOT EXISTS(SELECT 1 FROM external_identities e WHERE e.account_id=a.id)", + upgradedHash, + user.getUsername(), + user.getPassword(), + captured); + if (changed != 1) + throw new CredentialsExpiredException("Account credential changed during sign-in"); + return User.withUserDetails(user).password(upgradedHash).build(); + } +} diff --git a/src/main/java/org/busystack/caldav/security/password/PasswordController.java b/src/main/java/org/busystack/caldav/security/password/PasswordController.java new file mode 100644 index 0000000..4c7c7b8 --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/password/PasswordController.java @@ -0,0 +1,48 @@ +package org.busystack.caldav.security.password; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; + +@Controller +public class PasswordController { + private final AccountPasswordService passwords; + + public PasswordController(AccountPasswordService passwords) { + this.passwords = passwords; + } + + @GetMapping("/security/password") + String page() { + return "password"; + } + + @PostMapping("/security/password") + String change( + Authentication authentication, + @RequestParam String password, + @RequestParam String confirmation, + HttpServletRequest request) { + if (!password.equals(confirmation)) + throw new IllegalArgumentException("The new password entries must match"); + passwords.change(authentication, password, request.getRemoteAddr()); + SecurityContextHolder.clearContext(); + var session = request.getSession(false); + if (session != null) session.invalidate(); + return "redirect:/login?password=changed"; + } + + @ExceptionHandler(IllegalArgumentException.class) + String invalid(IllegalArgumentException failure, Model model, HttpServletResponse response) { + response.setStatus(400); + model.addAttribute("error", failure.getMessage()); + return "password"; + } +} diff --git a/src/main/java/org/busystack/caldav/security/password/PasswordPolicy.java b/src/main/java/org/busystack/caldav/security/password/PasswordPolicy.java new file mode 100644 index 0000000..42f52da --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/password/PasswordPolicy.java @@ -0,0 +1,18 @@ +package org.busystack.caldav.security.password; + +import java.nio.charset.StandardCharsets; + +/** Validate before BCrypt so Unicode length and its byte limit never silently weaken the policy. */ +public final class PasswordPolicy { + private PasswordPolicy() {} + + public static void validate(String password) { + if (password == null + || password.length() > 72 + || password.codePointCount(0, password.length()) < 14 + || !StandardCharsets.UTF_8.newEncoder().canEncode(password) + || password.getBytes(StandardCharsets.UTF_8).length > 72) + throw new IllegalArgumentException( + "Password must contain at least fourteen characters and at most seventy-two UTF-8 bytes"); + } +} diff --git a/src/main/java/org/busystack/caldav/security/recovery/RecoveryController.java b/src/main/java/org/busystack/caldav/security/recovery/RecoveryController.java new file mode 100644 index 0000000..c321c66 --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/recovery/RecoveryController.java @@ -0,0 +1,48 @@ +package org.busystack.caldav.security.recovery; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; + +@Controller +public class RecoveryController { + private final RecoveryService recovery; + + public RecoveryController(RecoveryService recovery) { + this.recovery = recovery; + } + + @GetMapping("/security/recovery") + String page(Authentication authentication, Model model, HttpServletResponse response) { + response.setHeader("Cache-Control", "no-store"); + model.addAttribute("status", recovery.status(authentication)); + return "recovery"; + } + + @PostMapping("/security/recovery/codes") + String issue( + Authentication authentication, + Model model, + HttpServletRequest request, + HttpServletResponse response) { + model.addAttribute("issued", recovery.issue(authentication, request.getRemoteAddr())); + response.setHeader("Cache-Control", "no-store"); + response.setHeader("Pragma", "no-cache"); + return "recovery-codes"; + } + + @PostMapping("/security/recovery/use") + String use(Authentication authentication, @RequestParam String code, HttpServletRequest request) { + recovery.recover(authentication, code, request.getRemoteAddr()); + SecurityContextHolder.clearContext(); + var session = request.getSession(false); + if (session != null) session.invalidate(); + return "redirect:/login?recovered"; + } +} diff --git a/src/main/java/org/busystack/caldav/security/recovery/RecoveryService.java b/src/main/java/org/busystack/caldav/security/recovery/RecoveryService.java new file mode 100644 index 0000000..53ca113 --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/recovery/RecoveryService.java @@ -0,0 +1,191 @@ +package org.busystack.caldav.security.recovery; + +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.sql.Timestamp; +import java.time.Instant; +import java.util.ArrayList; +import java.util.Base64; +import java.util.List; +import java.util.UUID; +import org.busystack.caldav.account.AccountService; +import org.busystack.caldav.security.RecentAuthentication; +import org.busystack.caldav.security.audit.SecurityAudit; +import org.busystack.caldav.security.audit.SecurityHashes; +import org.busystack.caldav.security.passkey.AccountPasskeyCredentials; +import org.busystack.caldav.security.session.SessionSecurity; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.security.access.AccessDeniedException; +import org.springframework.security.core.Authentication; +import org.springframework.security.crypto.keygen.Base64StringKeyGenerator; +import org.springframework.security.web.webauthn.api.Bytes; +import org.springframework.stereotype.Service; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.TransactionDefinition; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.transaction.support.TransactionTemplate; + +/** + * Recovery is a second factor after recent primary authentication; it never replaces primary login. + */ +@Service +public class RecoveryService { + public record IssuedCodes(List codes, Instant expiresAt) { + public IssuedCodes { + codes = List.copyOf(codes); + } + + @Override + public String toString() { + return "IssuedCodes[count=" + codes.size() + ", secrets=]"; + } + } + + public record Status(int remaining, Instant expiresAt) {} + + private record StoredCode( + String hash, Instant expiresAt, boolean available, boolean epochValid) {} + + private final JdbcTemplate jdbc; + private final SessionSecurity sessions; + private final AccountService accounts; + private final org.busystack.caldav.security.session.ClientAccessRevocation clients; + private final AccountPasskeyCredentials passkeys; + private final SecurityAudit audit; + private final TransactionTemplate transaction; + private final Base64StringKeyGenerator secrets = + new Base64StringKeyGenerator(Base64.getUrlEncoder().withoutPadding(), 32); + + public RecoveryService( + JdbcTemplate jdbc, + SessionSecurity sessions, + AccountService accounts, + AccountPasskeyCredentials passkeys, + SecurityAudit audit, + PlatformTransactionManager transactions, + org.busystack.caldav.security.session.ClientAccessRevocation clients) { + this.jdbc = jdbc; + this.sessions = sessions; + this.accounts = accounts; + this.passkeys = passkeys; + this.audit = audit; + this.clients = clients; + this.transaction = new TransactionTemplate(transactions); + transaction.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); + transaction.setTimeout(15); + } + + public Status status(Authentication authentication) { + var account = sessions.assertCurrent(authentication); + return jdbc.queryForObject( + "SELECT count(*),max(c.expires_at) FROM account_recovery_codes c CROSS JOIN system_state s WHERE c.account_id=? AND c.used_at IS NULL AND c.revoked_at IS NULL AND c.expires_at>now() AND c.recovery_epoch=s.recovery_epoch AND s.id=1", + (rs, row) -> + new Status( + rs.getInt(1), rs.getTimestamp(2) == null ? null : rs.getTimestamp(2).toInstant()), + account.id()); + } + + @Transactional + public IssuedCodes issue(Authentication authentication, String peer) { + RecentAuthentication.requireMfa(authentication); + var account = sessions.assertForReset(authentication); + requireCurrentAccess(account.id()); + jdbc.update( + "UPDATE account_recovery_codes SET revoked_at=now() WHERE account_id=? AND used_at IS NULL AND revoked_at IS NULL", + account.id()); + Instant expires = Instant.now().plusSeconds(365L * 86400); + List issued = new ArrayList<>(10); + for (int index = 0; index < 10; index++) { + UUID id = UUID.randomUUID(); + String code = id + "." + secrets.generateKey(); + jdbc.update( + "INSERT INTO account_recovery_codes(id,account_id,token_hash,recovery_epoch,expires_at) SELECT ?,?,?,recovery_epoch,? FROM system_state WHERE id=1", + id, + account.id(), + SecurityHashes.sha256(code), + Timestamp.from(expires)); + issued.add(code); + } + audit.record( + account.id(), + SecurityAudit.Action.RECOVERY_CODES_ISSUED, + SecurityAudit.Outcome.SUCCESS, + peer); + return new IssuedCodes(List.copyOf(issued), expires); + } + + public void recover(Authentication authentication, String code, String peer) { + RecentAuthentication.requirePrimary(authentication); + var account = sessions.assertCurrent(authentication); + UUID codeId = codeId(code); + Boolean accepted = + transaction.execute( + status -> { + lock(account.id()); + sessions.assertCurrent(authentication); + requireCurrentAccess(account.id()); + List matches = + codeId == null + ? List.of() + : jdbc.query( + "SELECT c.token_hash,c.expires_at,(c.used_at IS NULL AND c.revoked_at IS NULL),(c.recovery_epoch=s.recovery_epoch) FROM account_recovery_codes c CROSS JOIN system_state s WHERE c.id=? AND c.account_id=? AND s.id=1 FOR UPDATE OF c", + (rs, row) -> + new StoredCode( + rs.getString(1), + rs.getTimestamp(2).toInstant(), + rs.getBoolean(3), + rs.getBoolean(4)), + codeId, + account.id()); + if (matches.size() != 1 + || !matches.getFirst().available() + || !matches.getFirst().epochValid() + || !matches.getFirst().expiresAt().isAfter(Instant.now()) + || !MessageDigest.isEqual( + matches.getFirst().hash().getBytes(StandardCharsets.US_ASCII), + SecurityHashes.sha256(code).getBytes(StandardCharsets.US_ASCII))) { + audit.record( + account.id(), + SecurityAudit.Action.RECOVERY_USED, + SecurityAudit.Outcome.FAILURE, + peer); + return false; + } + jdbc.update("UPDATE account_recovery_codes SET used_at=now() WHERE id=?", codeId); + clients.revoke(account.id(), account.username()); + var keys = + jdbc.queryForList( + "SELECT c.credential_id FROM user_credentials c JOIN user_entities u ON u.id=c.user_entity_user_id WHERE u.account_id=?", + String.class, + account.id()); + keys.forEach(key -> passkeys.delete(Bytes.fromBase64(key))); + jdbc.update("DELETE FROM user_entities WHERE account_id=?", account.id()); + audit.record( + account.id(), + SecurityAudit.Action.RECOVERY_USED, + SecurityAudit.Outcome.SUCCESS, + peer); + return true; + }); + if (!Boolean.TRUE.equals(accepted)) + throw new AccessDeniedException("Recovery code is invalid or expired"); + } + + private void lock(UUID account) { + jdbc.query("SELECT id FROM accounts WHERE id=? FOR UPDATE", rs -> {}, account); + } + + private void requireCurrentAccess(UUID account) { + if (!accounts.isClientAccessCurrent(account)) + throw new AccessDeniedException("Current primary authentication is required"); + } + + private static UUID codeId(String code) { + if (code == null || code.length() != 80 || code.charAt(36) != '.') return null; + try { + return UUID.fromString(code.substring(0, 36)); + } catch (IllegalArgumentException ex) { + return null; + } + } +} diff --git a/src/main/java/org/busystack/caldav/security/session/ClientAccessRevocation.java b/src/main/java/org/busystack/caldav/security/session/ClientAccessRevocation.java new file mode 100644 index 0000000..3debce4 --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/session/ClientAccessRevocation.java @@ -0,0 +1,42 @@ +package org.busystack.caldav.security.session; + +import java.util.UUID; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +/** Internal lifecycle operation; the caller owns the authenticated account/admin decision. */ +@Service +public class ClientAccessRevocation { + private final JdbcTemplate jdbc; + private final SessionSecurity sessions; + + public ClientAccessRevocation(JdbcTemplate jdbc, SessionSecurity sessions) { + this.jdbc = jdbc; + this.sessions = sessions; + } + + @Transactional + public void revoke(UUID account, String username) { + jdbc.query("SELECT id FROM accounts WHERE id=? FOR UPDATE", rs -> {}, account); + jdbc.update( + "UPDATE dav_credentials SET revoked_at=COALESCE(revoked_at,now()) WHERE account_id=?", + account); + jdbc.update( + "UPDATE oauth_refresh_families SET revoked_at=COALESCE(revoked_at,now()) WHERE account_id=?", + account); + jdbc.update( + "DELETE FROM oauth2_authorization_consent c USING oauth2_authorization a WHERE (a.attributes::jsonb ->> 'busystack.account_id')=? AND c.registered_client_id=a.registered_client_id AND c.principal_name=a.principal_name", + account.toString()); + jdbc.update( + "DELETE FROM oauth2_authorization WHERE principal_name IN (?,?) OR attributes::jsonb ->> 'busystack.account_id'=?", + username, + account.toString(), + account.toString()); + jdbc.update( + "DELETE FROM oauth2_authorization_consent WHERE principal_name IN (?,?)", + username, + account.toString()); + sessions.invalidateAll(account); + } +} diff --git a/src/main/java/org/busystack/caldav/security/session/SecurityWorkflowsConfiguration.java b/src/main/java/org/busystack/caldav/security/session/SecurityWorkflowsConfiguration.java new file mode 100644 index 0000000..f9e899e --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/session/SecurityWorkflowsConfiguration.java @@ -0,0 +1,20 @@ +package org.busystack.caldav.security.session; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.session.config.SessionRepositoryCustomizer; +import org.springframework.session.jdbc.JdbcIndexedSessionRepository; + +@Configuration +@EnableScheduling +public class SecurityWorkflowsConfiguration { + @Bean + SessionRepositoryCustomizer sessionAttributeConcurrency() { + // Supported PostgreSQL customization: concurrent attribute creation must not fail a session + // save. + return repository -> + repository.setCreateSessionAttributeQuery( + "INSERT INTO %TABLE_NAME%_ATTRIBUTES (SESSION_PRIMARY_ID,ATTRIBUTE_NAME,ATTRIBUTE_BYTES) VALUES (?,?,?) ON CONFLICT(SESSION_PRIMARY_ID,ATTRIBUTE_NAME) DO UPDATE SET ATTRIBUTE_BYTES=EXCLUDED.ATTRIBUTE_BYTES"); + } +} diff --git a/src/main/java/org/busystack/caldav/security/session/SessionController.java b/src/main/java/org/busystack/caldav/security/session/SessionController.java new file mode 100644 index 0000000..e4b9186 --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/session/SessionController.java @@ -0,0 +1,46 @@ +package org.busystack.caldav.security.session; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.util.UUID; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; + +@Controller +public class SessionController { + private final SessionService sessions; + + public SessionController(SessionService sessions) { + this.sessions = sessions; + } + + @GetMapping("/security/sessions") + String list( + Authentication authentication, + Model model, + HttpServletRequest request, + HttpServletResponse response) { + var session = request.getSession(false); + model.addAttribute( + "sessions", sessions.list(authentication, session == null ? null : session.getId())); + response.setHeader("Cache-Control", "no-store"); + return "sessions"; + } + + @PostMapping("/security/sessions/{id}/revoke") + String revoke(Authentication authentication, @PathVariable UUID id, HttpServletRequest request) { + var session = request.getSession(false); + if (sessions.revoke( + authentication, id, session == null ? null : session.getId(), request.getRemoteAddr())) { + SecurityContextHolder.clearContext(); + if (session != null) session.invalidate(); + return "redirect:/login?session=ended"; + } + return "redirect:/security/sessions"; + } +} diff --git a/src/main/java/org/busystack/caldav/security/session/SessionEpochFilter.java b/src/main/java/org/busystack/caldav/security/session/SessionEpochFilter.java new file mode 100644 index 0000000..268838d --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/session/SessionEpochFilter.java @@ -0,0 +1,41 @@ +package org.busystack.caldav.security.session; + +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.io.IOException; +import org.springframework.security.access.AccessDeniedException; +import org.springframework.security.authentication.AnonymousAuthenticationToken; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.web.filter.OncePerRequestFilter; + +/** Stops an already-running old login/session from surviving a committed account reset. */ +public final class SessionEpochFilter extends OncePerRequestFilter { + private final SessionSecurity sessions; + + public SessionEpochFilter(SessionSecurity sessions) { + this.sessions = sessions; + } + + @Override + protected void doFilterInternal( + HttpServletRequest request, HttpServletResponse response, FilterChain chain) + throws ServletException, IOException { + var authentication = SecurityContextHolder.getContext().getAuthentication(); + if (authentication != null + && authentication.isAuthenticated() + && !(authentication instanceof AnonymousAuthenticationToken)) { + try { + sessions.assertCurrent(authentication); + } catch (AccessDeniedException ex) { + SecurityContextHolder.clearContext(); + var session = request.getSession(false); + if (session != null) session.invalidate(); + response.sendError(403, "Account session has ended"); + return; + } + } + chain.doFilter(request, response); + } +} diff --git a/src/main/java/org/busystack/caldav/security/session/SessionSecurity.java b/src/main/java/org/busystack/caldav/security/session/SessionSecurity.java new file mode 100644 index 0000000..4b6c2c7 --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/session/SessionSecurity.java @@ -0,0 +1,105 @@ +package org.busystack.caldav.security.session; + +import java.util.UUID; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.security.access.AccessDeniedException; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.authority.SimpleGrantedAuthority; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +/** + * A trusted generation captured with credential state, rather than after credential verification. + */ +@Service +public class SessionSecurity { + private static final String PREFIX = "BUSYSTACK_SESSION_"; + + public record Account(UUID id, String username, UUID epoch) {} + + private final JdbcTemplate jdbc; + private final org.busystack.caldav.security.oidc.ExternalAccessPolicy externalAccess; + + public SessionSecurity( + JdbcTemplate jdbc, org.busystack.caldav.security.oidc.ExternalAccessPolicy externalAccess) { + this.jdbc = jdbc; + this.externalAccess = externalAccess; + } + + public static SimpleGrantedAuthority authority(UUID capturedEpoch) { + return new SimpleGrantedAuthority(PREFIX + capturedEpoch); + } + + public static UUID capturedEpoch( + java.util.Collection + authorities) { + var generations = + authorities.stream() + .map(granted -> granted.getAuthority()) + .filter(value -> value != null && value.startsWith(PREFIX)) + .distinct() + .toList(); + if (generations.size() != 1) return null; + try { + return UUID.fromString(generations.getFirst().substring(PREFIX.length())); + } catch (IllegalArgumentException ex) { + return null; + } + } + + public Account assertCurrent(Authentication authentication) { + // A service mutation must keep the captured generation stable until its transaction commits. + boolean transactional = + org.springframework.transaction.support.TransactionSynchronizationManager + .isActualTransactionActive(); + return current(authentication, transactional ? " FOR SHARE" : ""); + } + + public Account assertForReset(Authentication authentication) { + if (!org.springframework.transaction.support.TransactionSynchronizationManager + .isActualTransactionActive()) + throw new IllegalStateException("Security reset requires a transaction"); + return current(authentication, " FOR UPDATE"); + } + + private Account current(Authentication authentication, String lock) { + if (authentication == null || !authentication.isAuthenticated()) + throw new AccessDeniedException("Current account authentication is required"); + var rows = + jdbc.query( + "SELECT id,username,security_epoch FROM accounts WHERE username=? AND enabled" + lock, + (rs, row) -> + new Account( + rs.getObject(1, UUID.class), rs.getString(2), rs.getObject(3, UUID.class)), + authentication.getName()); + if (rows.size() != 1) throw new AccessDeniedException("Account session is unavailable"); + Account account = rows.getFirst(); + if (!externalAccess.isCurrent(account.id())) + throw new AccessDeniedException("Current upstream authentication is required"); + var generations = + authentication.getAuthorities().stream() + .map(granted -> granted.getAuthority()) + .filter(value -> value != null && value.startsWith(PREFIX)) + .distinct() + .toList(); + if (generations.size() != 1 + || !generations.getFirst().equals(authority(account.epoch()).getAuthority())) + throw new AccessDeniedException("Account session has ended"); + return account; + } + + /** Internal lifecycle hook: call inside account disable/recovery transactions. */ + @Transactional + public void invalidateAll(UUID accountId) { + var names = + jdbc.queryForList( + "SELECT username FROM accounts WHERE id=? FOR UPDATE", String.class, accountId); + if (names.isEmpty()) return; + jdbc.update("UPDATE accounts SET security_epoch=gen_random_uuid() WHERE id=?", accountId); + jdbc.update( + "DELETE FROM spring_session WHERE principal_name IN (?,?)", + names.getFirst(), + accountId.toString()); + jdbc.update("DELETE FROM webauthn_ceremonies WHERE account_id=?", accountId); + } +} diff --git a/src/main/java/org/busystack/caldav/security/session/SessionService.java b/src/main/java/org/busystack/caldav/security/session/SessionService.java new file mode 100644 index 0000000..07f4eac --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/session/SessionService.java @@ -0,0 +1,59 @@ +package org.busystack.caldav.security.session; + +import java.time.Instant; +import java.util.List; +import java.util.UUID; +import org.busystack.caldav.security.RecentAuthentication; +import org.busystack.caldav.security.audit.SecurityAudit; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.security.core.Authentication; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +public class SessionService { + public record SessionView( + UUID id, Instant createdAt, Instant lastAccessAt, Instant expiresAt, boolean current) {} + + private final JdbcTemplate jdbc; + private final SessionSecurity security; + private final SecurityAudit audit; + + public SessionService(JdbcTemplate jdbc, SessionSecurity security, SecurityAudit audit) { + this.jdbc = jdbc; + this.security = security; + this.audit = audit; + } + + public List list(Authentication authentication, String currentId) { + var account = security.assertCurrent(authentication); + return jdbc.query( + "SELECT primary_id,creation_time,last_access_time,expiry_time,session_id FROM spring_session WHERE principal_name=? AND expiry_time>? ORDER BY last_access_time DESC", + (rs, row) -> + new SessionView( + UUID.fromString(rs.getString(1)), + Instant.ofEpochMilli(rs.getLong(2)), + Instant.ofEpochMilli(rs.getLong(3)), + Instant.ofEpochMilli(rs.getLong(4)), + rs.getString(5).equals(currentId)), + account.username(), + System.currentTimeMillis()); + } + + @Transactional + public boolean revoke( + Authentication authentication, UUID primaryId, String currentId, String peer) { + RecentAuthentication.requireMfa(authentication); + var account = security.assertCurrent(authentication); + var deleted = + jdbc.query( + "DELETE FROM spring_session WHERE primary_id=? AND principal_name=? RETURNING session_id", + (rs, row) -> rs.getString(1), + primaryId.toString(), + account.username()); + if (!deleted.isEmpty()) + audit.record( + account.id(), SecurityAudit.Action.SESSION_REVOKED, SecurityAudit.Outcome.SUCCESS, peer); + return !deleted.isEmpty() && deleted.getFirst().equals(currentId); + } +} diff --git a/src/main/java/org/busystack/caldav/security/throttle/LoginThrottleFilter.java b/src/main/java/org/busystack/caldav/security/throttle/LoginThrottleFilter.java new file mode 100644 index 0000000..5853e56 --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/throttle/LoginThrottleFilter.java @@ -0,0 +1,57 @@ +package org.busystack.caldav.security.throttle; + +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.io.IOException; +import org.busystack.caldav.security.audit.SecurityAudit; +import org.springframework.security.authentication.AnonymousAuthenticationToken; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.web.filter.OncePerRequestFilter; + +/** + * Surrounds the framework authentication filters without replacing their handlers or factor merge. + */ +public final class LoginThrottleFilter extends OncePerRequestFilter { + private final LoginThrottleService throttle; + + public LoginThrottleFilter(LoginThrottleService throttle) { + this.throttle = throttle; + } + + @Override + protected boolean shouldNotFilter(HttpServletRequest request) { + return !"POST".equals(request.getMethod()) + || !("/login".equals(request.getServletPath()) + || "/login/webauthn".equals(request.getServletPath())); + } + + @Override + protected void doFilterInternal( + HttpServletRequest request, HttpServletResponse response, FilterChain chain) + throws ServletException, IOException { + boolean password = "/login".equals(request.getServletPath()); + String username = password ? request.getParameter("username") : null; + String peer = request.getRemoteAddr(); + SecurityAudit.Action action = + password ? SecurityAudit.Action.PASSWORD_LOGIN : SecurityAudit.Action.PASSKEY_LOGIN; + var decision = throttle.reserve(username, peer); + if (!decision.allowed()) { + throttle.denied(peer, action); + response.setHeader("Retry-After", Long.toString(decision.retryAfterSeconds())); + response.setStatus(429); + response.setContentType("text/plain;charset=UTF-8"); + response.getWriter().write("Too many sign-in attempts. Try again shortly."); + return; + } + chain.doFilter(request, response); + var authentication = SecurityContextHolder.getContext().getAuthentication(); + boolean success = + response.getStatus() < 400 + && authentication != null + && authentication.isAuthenticated() + && !(authentication instanceof AnonymousAuthenticationToken); + throttle.result(success ? authentication.getName() : username, peer, action, success); + } +} diff --git a/src/main/java/org/busystack/caldav/security/throttle/LoginThrottleService.java b/src/main/java/org/busystack/caldav/security/throttle/LoginThrottleService.java new file mode 100644 index 0000000..0444279 --- /dev/null +++ b/src/main/java/org/busystack/caldav/security/throttle/LoginThrottleService.java @@ -0,0 +1,139 @@ +package org.busystack.caldav.security.throttle; + +import java.sql.Timestamp; +import java.text.Normalizer; +import java.time.Duration; +import java.time.Instant; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.Locale; +import java.util.UUID; +import org.busystack.caldav.security.audit.SecurityAudit; +import org.busystack.caldav.security.audit.SecurityHashes; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +/** + * Bounded, temporary account/connection-peer backoff; attempted blocked requests never extend it. + */ +@Service +public class LoginThrottleService { + public record Decision(boolean allowed, long retryAfterSeconds) {} + + private record Limit(String key, int attempts, Duration window, Duration cooldown) {} + + private record Bucket(Instant started, int attempts, Instant blocked) {} + + private final JdbcTemplate jdbc; + private final SecurityAudit audit; + + public LoginThrottleService(JdbcTemplate jdbc, SecurityAudit audit) { + this.jdbc = jdbc; + this.audit = audit; + } + + @Transactional + public Decision reserve(String username, String peer) { + jdbc.query( + "SELECT pg_advisory_xact_lock(hashtextextended('busystack.login-throttle.capacity',0))", + rs -> {}); + jdbc.update("DELETE FROM login_throttle WHERE expires_at < now()"); + Instant now = jdbc.queryForObject("SELECT now()", (rs, row) -> rs.getTimestamp(1).toInstant()); + List limits = new ArrayList<>(); + if (username != null && !username.isBlank()) + limits.add(new Limit(accountKey(username), 5, Duration.ofMinutes(5), Duration.ofSeconds(30))); + limits.add( + new Limit( + SecurityHashes.sha256("peer:" + peer(peer)), + 30, + Duration.ofMinutes(1), + Duration.ofSeconds(60))); + limits.sort(Comparator.comparing(Limit::key)); + long existing = jdbc.queryForObject("SELECT count(*) FROM login_throttle", Long.class); + long required = + limits.stream() + .filter( + limit -> + !Boolean.TRUE.equals( + jdbc.queryForObject( + "SELECT EXISTS(SELECT 1 FROM login_throttle WHERE bucket_key=?)", + Boolean.class, + limit.key()))) + .count(); + if (existing + required > 10000) return new Decision(false, 60); + long retry = 0; + for (Limit limit : limits) { + jdbc.update( + "INSERT INTO login_throttle(bucket_key,window_started,attempts,expires_at) VALUES (?,?,0,?) ON CONFLICT DO NOTHING", + limit.key(), + Timestamp.from(now), + Timestamp.from(now.plusSeconds(3600))); + Bucket bucket = + jdbc.queryForObject( + "SELECT window_started,attempts,blocked_until FROM login_throttle WHERE bucket_key=? FOR UPDATE", + (rs, row) -> + new Bucket( + rs.getTimestamp(1).toInstant(), + rs.getInt(2), + rs.getTimestamp(3) == null ? null : rs.getTimestamp(3).toInstant()), + limit.key()); + if (bucket.blocked() != null && bucket.blocked().isAfter(now)) { + retry = + Math.max(retry, Math.max(1, Duration.between(now, bucket.blocked()).toSeconds() + 1)); + continue; + } + boolean reset = + bucket.blocked() != null || !bucket.started().plus(limit.window()).isAfter(now); + int attempted = reset ? 0 : bucket.attempts(); + Instant start = reset ? now : bucket.started(); + if (attempted >= limit.attempts()) { + Instant blocked = now.plus(limit.cooldown()); + jdbc.update( + "UPDATE login_throttle SET blocked_until=?,expires_at=? WHERE bucket_key=?", + Timestamp.from(blocked), + Timestamp.from(now.plusSeconds(3600)), + limit.key()); + retry = Math.max(retry, limit.cooldown().toSeconds()); + } else + jdbc.update( + "UPDATE login_throttle SET window_started=?,attempts=?,blocked_until=NULL,expires_at=? WHERE bucket_key=?", + Timestamp.from(start), + attempted + 1, + Timestamp.from(now.plusSeconds(3600)), + limit.key()); + } + return new Decision(retry == 0, retry); + } + + @Transactional + public void result(String username, String peer, SecurityAudit.Action action, boolean success) { + List accounts = + username == null + ? List.of() + : jdbc.queryForList("SELECT id FROM accounts WHERE username=?", UUID.class, username); + audit.record( + accounts.isEmpty() ? null : accounts.getFirst(), + action, + success ? SecurityAudit.Outcome.SUCCESS : SecurityAudit.Outcome.FAILURE, + peer(peer)); + if (success && username != null) + jdbc.update("DELETE FROM login_throttle WHERE bucket_key=?", accountKey(username)); + } + + public void denied(String peer, SecurityAudit.Action action) { + audit.record(null, action, SecurityAudit.Outcome.THROTTLED, peer(peer)); + } + + static String accountKey(String username) { + String bounded = username.length() > 256 ? username.substring(0, 256) : username; + return SecurityHashes.sha256( + "account:" + + Normalizer.normalize(bounded.strip(), Normalizer.Form.NFKC).toLowerCase(Locale.ROOT)); + } + + private static String peer(String peer) { + return peer == null ? "unknown" : peer.length() > 128 ? peer.substring(0, 128) : peer; + } +} diff --git a/src/main/java/org/busystack/caldav/sharing/GroupService.java b/src/main/java/org/busystack/caldav/sharing/GroupService.java new file mode 100644 index 0000000..4e34b97 --- /dev/null +++ b/src/main/java/org/busystack/caldav/sharing/GroupService.java @@ -0,0 +1,307 @@ +package org.busystack.caldav.sharing; + +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; +import java.util.UUID; +import org.busystack.caldav.security.RecentAuthentication; +import org.busystack.caldav.security.session.SessionSecurity; +import org.busystack.caldav.storage.CalendarStore; +import org.busystack.caldav.storage.DavMutationLock; +import org.busystack.caldav.storage.StoreException; +import org.busystack.caldav.xml.XmlCharacters; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.security.access.AccessDeniedException; +import org.springframework.security.core.Authentication; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +/** Local sharing groups are directory metadata; administration never bypasses calendar ACLs. */ +@Service +public class GroupService { + public record Member(UUID account, String username, String displayName) {} + + public record Group(UUID id, String name, List members) { + public Group { + members = List.copyOf(members); + } + } + + public record Choice(UUID id, String name) {} + + public record Grant( + UUID collection, String collectionName, UUID group, String groupName, String permission) {} + + public record GrantPage(List rows, int page, boolean hasNext) { + public GrantPage { + rows = List.copyOf(rows); + } + } + + private final JdbcTemplate jdbc; + private final SessionSecurity sessions; + private final CalendarStore calendars; + private final org.busystack.caldav.security.audit.SecurityAudit audit; + + public GroupService( + JdbcTemplate jdbc, + SessionSecurity sessions, + CalendarStore calendars, + org.busystack.caldav.security.audit.SecurityAudit audit) { + this.jdbc = jdbc; + this.sessions = sessions; + this.calendars = calendars; + this.audit = audit; + } + + @Transactional(isolation = org.springframework.transaction.annotation.Isolation.REPEATABLE_READ) + public List list(Authentication authentication) { + administrator(authentication); + var groups = + jdbc.query( + "SELECT id,name FROM sharing_groups ORDER BY name LIMIT 1001", + (rs, row) -> new Choice(rs.getObject(1, UUID.class), rs.getString(2))); + if (groups.size() > 1000) limit(); + return groups.stream() + .map( + group -> + new Group( + group.id(), + group.name(), + jdbc.query( + "SELECT a.id,a.username,a.display_name FROM accounts a JOIN sharing_group_members m ON m.account_id=a.id WHERE m.group_id=? ORDER BY a.username", + (rs, row) -> + new Member( + rs.getObject(1, UUID.class), rs.getString(2), rs.getString(3)), + group.id()))) + .toList(); + } + + public List choices(Authentication authentication) { + sessions.assertCurrent(authentication); + var choices = + jdbc.query( + "SELECT id,name FROM sharing_groups ORDER BY name LIMIT 1001", + (rs, row) -> new Choice(rs.getObject(1, UUID.class), rs.getString(2))); + if (choices.size() > 1000) limit(); + return List.copyOf(choices); + } + + public List ownedGrants(Authentication authentication) { + return ownedGrants(authentication, 0).rows(); + } + + public GrantPage ownedGrants(Authentication authentication, int page) { + if (page < 0 || page > Integer.MAX_VALUE / 50) + throw new IllegalArgumentException("Invalid group sharing page"); + var actor = sessions.assertCurrent(authentication); + var grants = + jdbc.query( + "SELECT c.id,c.name,g.id,g.name,m.permission FROM collection_group_members m JOIN collections c ON c.id=m.collection_id JOIN sharing_groups g ON g.id=m.group_id WHERE c.owner_id=? ORDER BY c.id,g.id LIMIT 51 OFFSET ?", + (rs, row) -> + new Grant( + rs.getObject(1, UUID.class), + rs.getString(2), + rs.getObject(3, UUID.class), + rs.getString(4), + rs.getString(5)), + actor.id(), + page * 50); + return new GrantPage( + grants.size() > 50 ? grants.subList(0, 50) : grants, page, grants.size() > 50); + } + + @Transactional(timeout = 30) + public UUID create(Authentication authentication, String name, Set usernames) { + RecentAuthentication.requireMfa(authentication); + DavMutationLock.exclusive(jdbc); + UUID actor = administrator(authentication); + validateName(name); + var members = accounts(usernames); + if (jdbc.queryForObject("SELECT count(*) FROM sharing_groups", Long.class) >= 1000) limit(); + if (jdbc.queryForObject("SELECT count(*) FROM sharing_group_members", Long.class) + + members.size() + > 10000) limit(); + uniqueName(name, null); + UUID id = UUID.randomUUID(); + jdbc.update("INSERT INTO sharing_groups(id,name,created_by) VALUES (?,?,?)", id, name, actor); + for (UUID member : members) + jdbc.update( + "INSERT INTO sharing_group_members(group_id,account_id) VALUES (?,?)", id, member); + audit.record( + actor, + org.busystack.caldav.security.audit.SecurityAudit.Action.GROUP_CREATED, + org.busystack.caldav.security.audit.SecurityAudit.Outcome.SUCCESS, + null); + return id; + } + + @Transactional(timeout = 30) + public void update( + Authentication authentication, UUID group, String name, Set usernames) { + RecentAuthentication.requireMfa(authentication); + DavMutationLock.exclusive(jdbc); + UUID actor = administrator(authentication); + validateName(name); + var desired = accounts(usernames); + requireGroup(group); + uniqueName(name, group); + lockAffected(group); + long existingCount = + jdbc.queryForObject( + "SELECT count(*) FROM sharing_group_members WHERE group_id=?", Long.class, group); + if (jdbc.queryForObject("SELECT count(*) FROM sharing_group_members", Long.class) + - existingCount + + desired.size() + > 10000) limit(); + jdbc.query("SELECT set_config('busystack.defer_dav_bindings','on',true)", rs -> {}); + jdbc.update("UPDATE sharing_groups SET name=? WHERE id=?", name, group); + var existing = + jdbc.queryForList( + "SELECT account_id FROM sharing_group_members WHERE group_id=?", UUID.class, group); + for (UUID member : existing) + if (!desired.contains(member)) + jdbc.update( + "DELETE FROM sharing_group_members WHERE group_id=? AND account_id=?", group, member); + for (UUID member : desired) + if (!existing.contains(member)) + jdbc.update( + "INSERT INTO sharing_group_members(group_id,account_id) VALUES (?,?)", group, member); + jdbc.query("SELECT set_config('busystack.defer_dav_bindings','off',true)", rs -> {}); + reconcileAffected(group); + audit.record( + actor, + org.busystack.caldav.security.audit.SecurityAudit.Action.GROUP_UPDATED, + org.busystack.caldav.security.audit.SecurityAudit.Outcome.SUCCESS, + null); + } + + @Transactional(timeout = 30) + public void delete(Authentication authentication, UUID group) { + RecentAuthentication.requireMfa(authentication); + DavMutationLock.exclusive(jdbc); + UUID actor = administrator(authentication); + requireGroup(group); + lockAffected(group); + jdbc.query("SELECT set_config('busystack.defer_dav_bindings','on',true)", rs -> {}); + // Reconcile with no members while grants still identify the complete affected set. + // Other groups and direct grants remain effective throughout this indivisible commit. + jdbc.update("DELETE FROM sharing_group_members WHERE group_id=?", group); + reconcileAffected(group); + jdbc.update("DELETE FROM sharing_groups WHERE id=?", group); + jdbc.query("SELECT set_config('busystack.defer_dav_bindings','off',true)", rs -> {}); + audit.record( + actor, + org.busystack.caldav.security.audit.SecurityAudit.Action.GROUP_DELETED, + org.busystack.caldav.security.audit.SecurityAudit.Outcome.SUCCESS, + null); + } + + @Transactional(timeout = 30) + public void grant(Authentication authentication, UUID collection, UUID group, String permission) { + RecentAuthentication.requireMfa(authentication); + DavMutationLock.shared(jdbc); + UUID actor = sessions.assertCurrent(authentication).id(); + if (jdbc.queryForList( + "SELECT id FROM collections WHERE id=? FOR UPDATE", UUID.class, collection) + .isEmpty()) throw new StoreException(StoreException.Kind.NOT_FOUND); + if (!calendars.collection(actor, collection).owner().equals(actor)) + throw new AccessDeniedException("Only the collection owner may manage sharing"); + requireGroup(group); + if ("REVOKE".equals(permission)) + jdbc.update( + "DELETE FROM collection_group_members WHERE collection_id=? AND group_id=?", + collection, + group); + else { + if (!Set.of("READ", "WRITE").contains(permission)) + throw new IllegalArgumentException("Group permission must be READ, WRITE or REVOKE"); + jdbc.update( + "INSERT INTO collection_group_members(collection_id,group_id,permission) VALUES (?,?,?) ON CONFLICT(collection_id,group_id) DO UPDATE SET permission=EXCLUDED.permission", + collection, + group, + permission); + } + audit.record( + actor, + org.busystack.caldav.security.audit.SecurityAudit.Action.GROUP_SHARING_CHANGED, + org.busystack.caldav.security.audit.SecurityAudit.Outcome.SUCCESS, + null); + } + + private UUID administrator(Authentication authentication) { + RecentAuthentication.requireMfa(authentication); + var actor = sessions.assertCurrent(authentication); + if (!Boolean.TRUE.equals( + jdbc.queryForObject( + "SELECT administrator FROM accounts WHERE id=? AND enabled", + Boolean.class, + actor.id()))) + throw new AccessDeniedException("Current administrator authentication is required"); + return actor.id(); + } + + private Set accounts(Set usernames) { + if (usernames == null || usernames.size() > 1000) + throw new IllegalArgumentException("A group supports at most 1000 members"); + Set members = new LinkedHashSet<>(); + for (String username : usernames) { + var found = + jdbc.queryForList( + "SELECT id FROM accounts WHERE username=? AND enabled", UUID.class, username); + if (found.size() != 1) + throw new IllegalArgumentException("Every group member must name an active account"); + members.add(found.getFirst()); + } + return Set.copyOf(members); + } + + private void requireGroup(UUID id) { + if (!Boolean.TRUE.equals( + jdbc.queryForObject( + "SELECT EXISTS(SELECT 1 FROM sharing_groups WHERE id=?)", Boolean.class, id))) + throw new IllegalArgumentException("Unknown sharing group"); + } + + private void lockAffected(UUID group) { + streamGroupCollections( + "SELECT c.id FROM collections c JOIN collection_group_members m ON m.collection_id=c.id WHERE m.group_id=? ORDER BY c.id FOR UPDATE OF c", + group); + } + + private void reconcileAffected(UUID group) { + jdbc.query("SELECT dav_reconcile_group_memberships(?)", rs -> {}, group); + } + + private void streamGroupCollections(String sql, UUID group) { + // A server cursor bounds JDBC result buffering while the exclusive mutation lock makes + // membership, effective ACLs, bindings and token invalidation one indivisible commit. + jdbc.query( + connection -> { + var statement = connection.prepareStatement(sql); + statement.setObject(1, group); + statement.setFetchSize(128); + return statement; + }, + (org.springframework.jdbc.core.RowCallbackHandler) rs -> {}); + } + + private void uniqueName(String name, UUID except) { + if (Boolean.TRUE.equals( + jdbc.queryForObject( + "SELECT EXISTS(SELECT 1 FROM sharing_groups WHERE name=? AND (?::uuid IS NULL OR id<>?))", + Boolean.class, + name, + except, + except))) throw new IllegalArgumentException("Group name is already in use"); + } + + private static void validateName(String name) { + if (name == null || name.isBlank() || name.length() > 200 || !XmlCharacters.valid(name)) + throw new IllegalArgumentException("Group name must contain 1–200 characters"); + } + + private static void limit() { + throw new StoreException(StoreException.Kind.LIMIT); + } +} diff --git a/src/main/java/org/busystack/caldav/storage/CalendarStore.java b/src/main/java/org/busystack/caldav/storage/CalendarStore.java new file mode 100644 index 0000000..c520fce --- /dev/null +++ b/src/main/java/org/busystack/caldav/storage/CalendarStore.java @@ -0,0 +1,701 @@ +package org.busystack.caldav.storage; + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.time.Instant; +import java.util.ArrayList; +import java.util.HexFormat; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.UUID; +import java.util.function.ToLongFunction; +import javax.xml.namespace.QName; +import org.busystack.caldav.xml.XmlCharacters; +import org.springframework.dao.DuplicateKeyException; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Isolation; +import org.springframework.transaction.annotation.Transactional; + +@Service +public class CalendarStore { + public record Collection( + UUID id, + UUID owner, + String name, + String component, + UUID generation, + long revision, + long minimum) {} + + /** Metadata projections never contain a calendar body or a placeholder byte array. */ + public record Metadata( + String name, + String uid, + String etag, + long revision, + Instant modifiedAt, + long contentLength) {} + + public record Resource( + String name, String uid, byte[] content, String etag, long revision, Instant modifiedAt) { + public Resource(String name, String uid, byte[] content, String etag, long revision) { + this(name, uid, content, etag, revision, null); + } + + public Resource { + content = content.clone(); + } + + public Metadata metadata() { + return new Metadata(name, uid, etag, revision, modifiedAt, content.length); + } + + @Override + public byte[] content() { + return content.clone(); + } + } + + public record Change(String name, boolean deleted, Metadata metadata, Resource resource) { + public Change(String name, boolean deleted, Resource resource) { + this(name, deleted, resource == null ? null : resource.metadata(), resource); + } + } + + public record SyncPage(String token, List changes, boolean truncated) {} + + private record Traversal(long lower, long upper, long floor, boolean initial, String lastName) {} + + private static final long MAX_RETAINED_CONTENT_BYTES = 4L * 1024 * 1024; + + private final JdbcTemplate jdbc; + private final SyncContinuationStore continuations; + private final org.busystack.caldav.calendar.CalendarAdmission admission; + + public CalendarStore(JdbcTemplate jdbc, SyncContinuationStore continuations) { + this( + jdbc, + continuations, + new org.busystack.caldav.calendar.CalendarAdmission( + new org.busystack.caldav.config.DavCompatibilityProperties(true, true))); + } + + @org.springframework.beans.factory.annotation.Autowired + public CalendarStore( + JdbcTemplate jdbc, + SyncContinuationStore continuations, + org.busystack.caldav.calendar.CalendarAdmission admission) { + this.jdbc = jdbc; + this.continuations = continuations; + this.admission = admission; + } + + @Transactional + public UUID createCollection(UUID actor, String name, String component) { + org.busystack.caldav.storage.DavMutationLock.shared(jdbc); + requireEnabled(actor); + if (!XmlCharacters.valid(name)) throw new IllegalArgumentException("Invalid display name"); + UUID id = UUID.randomUUID(); + jdbc.update( + "INSERT INTO collections(id,owner_id,name,component) VALUES (?,?,?,?)", + id, + actor, + name, + component); + return id; + } + + public List collections(UUID actor) { + requireEnabled(actor); + return jdbc.query( + "SELECT c.* FROM collections c WHERE c.owner_id=? OR EXISTS (SELECT 1 FROM collection_effective_members m WHERE m.collection_id=c.id AND m.account_id=?) ORDER BY c.id", + CalendarStore::collection, + actor, + actor); + } + + @Transactional + public void share(UUID actor, UUID collection, UUID member, String permission) { + org.busystack.caldav.storage.DavMutationLock.shared(jdbc); + Collection c = lock(collection); + requireEnabled(actor); + if (!c.owner().equals(actor)) + throw new StoreException( + StoreException.Kind.FORBIDDEN, StoreException.RequiredAccess.OWNER, c.id()); + if (actor.equals(member)) + throw new IllegalArgumentException("Owner permissions cannot be replaced"); + var previous = + jdbc.queryForList( + "SELECT permission FROM collection_members WHERE collection_id=? AND account_id=?", + String.class, + collection, + member); + if (java.util.Objects.equals(permission, previous.isEmpty() ? null : previous.getFirst())) + return; + if (permission == null) + jdbc.update( + "DELETE FROM collection_members WHERE collection_id=? AND account_id=?", + collection, + member); + else + jdbc.update( + "INSERT INTO collection_members(collection_id,account_id,permission) VALUES (?,?,?) ON CONFLICT (collection_id,account_id) DO UPDATE SET permission=EXCLUDED.permission", + collection, + member, + permission); + // Collection-scoped generation invalidates existing cursors on any permission change. + jdbc.update("UPDATE collections SET generation=gen_random_uuid() WHERE id=?", collection); + } + + public Collection collection(UUID actor, UUID id) { + Collection c = find(id); + authorize(actor, c, false); + return c; + } + + public Resource get(UUID actor, UUID collectionId, String name) { + collection(actor, collectionId); + return resource(collectionId, name); + } + + public Metadata metadata(UUID actor, UUID collectionId, String name) { + collection(actor, collectionId); + var rows = + jdbc.query( + "SELECT name,uid,etag,revision,updated_at,octet_length(content) AS content_length FROM calendar_resources WHERE collection_id=? AND name=?", + CalendarStore::readMetadata, + collectionId, + name); + return rows.isEmpty() ? null : rows.getFirst(); + } + + /** + * Stable keyset batch within the caller's report transaction; calendar bodies are not selected. + */ + public List metadataBatch(UUID actor, UUID collectionId, String after, int limit) { + if (limit < 1 || limit > 10001) throw new IllegalArgumentException("Invalid metadata batch"); + collection(actor, collectionId); + return jdbc.query( + "SELECT name,uid,etag,revision,updated_at,octet_length(content) AS content_length FROM calendar_resources WHERE collection_id=? AND name COLLATE \"C\">? COLLATE \"C\" ORDER BY name COLLATE \"C\" LIMIT ?", + CalendarStore::readMetadata, + collectionId, + after, + limit); + } + + private static Metadata readMetadata(java.sql.ResultSet rs, int row) + throws java.sql.SQLException { + return new Metadata( + rs.getString("name"), + rs.getString("uid"), + rs.getString("etag"), + rs.getLong("revision"), + rs.getTimestamp("updated_at").toInstant(), + rs.getLong("content_length")); + } + + /** Fetch a bounded query batch after one authorization check, without per-object lookups. */ + public List resourceBatch(UUID actor, UUID collectionId, String after) { + var metadata = metadataBatch(actor, collectionId, after, 16); + List names = new ArrayList<>(); + long selectedBytes = 0; + for (var item : metadata) { + if (item.contentLength() > 4L * 1024 * 1024 - selectedBytes) break; + names.add(item.name()); + selectedBytes += item.contentLength(); + } + if (names.isEmpty()) { + if (!metadata.isEmpty()) throw new StoreException(StoreException.Kind.LIMIT); + return List.of(); + } + String placeholders = String.join(",", java.util.Collections.nCopies(names.size(), "?")); + List arguments = new ArrayList<>(); + arguments.add(collectionId); + arguments.addAll(names); + return jdbc.query( + "SELECT name,uid,content,etag,revision,updated_at FROM calendar_resources WHERE collection_id=? AND name IN (" + + placeholders + + ") ORDER BY name COLLATE \"C\"", + (rs, row) -> readResource(rs), + arguments.toArray()); + } + + @Transactional + public Resource put( + UUID actor, + UUID collectionId, + String name, + String uid, + byte[] content, + WriteCondition condition) { + return putAdmitted(actor, collectionId, name, uid, content, condition).resource(); + } + + public record WriteResult(Resource resource, boolean transformed) {} + + @Transactional + public WriteResult putAdmitted( + UUID actor, + UUID collectionId, + String name, + String uid, + byte[] content, + WriteCondition condition) { + org.busystack.caldav.storage.DavMutationLock.shared(jdbc); + checkName(name); + if (content == null || content.length == 0) + throw new org.busystack.caldav.calendar.CalendarValidationException( + org.busystack.caldav.calendar.CalendarValidationException.Reason.INVALID_DATA); + if (content.length > 1048576) throw new StoreException(StoreException.Kind.LIMIT); + Collection c = lock(collectionId); + authorize(actor, c, true); + DavNamespace.resourceBindings(jdbc, collectionId, name); + var accepted = admission.admit(content, c.component()); + var parsed = accepted.parsed(); + if (uid != null && !parsed.uid().equals(uid)) + throw new IllegalArgumentException("UID index does not match the resource"); + uid = parsed.uid(); + content = accepted.content(); + Resource old = resource(collectionId, name); + condition.check( + old == null ? null : old.etag(), old != null, old == null ? null : old.modifiedAt()); + if (old != null && !old.uid().equals(uid)) + throw new StoreException(StoreException.Kind.UID_CONFLICT); + long next = c.revision() + 1; + String etag = etag(content); + Instant modifiedAt; + try { + modifiedAt = + jdbc.queryForObject( + "INSERT INTO calendar_resources(collection_id,name,uid,content,etag,revision,updated_at) VALUES (?,?,?,?,?,?,clock_timestamp()) ON CONFLICT (collection_id,name) DO UPDATE SET uid=EXCLUDED.uid,content=EXCLUDED.content,etag=EXCLUDED.etag,revision=EXCLUDED.revision,updated_at=CASE WHEN calendar_resources.content IS DISTINCT FROM EXCLUDED.content THEN clock_timestamp() ELSE calendar_resources.updated_at END RETURNING updated_at", + (rs, row) -> rs.getTimestamp(1).toInstant(), + collectionId, + name, + uid, + content, + etag, + next); + } catch (DuplicateKeyException ex) { + throw new StoreException(StoreException.Kind.UID_CONFLICT); + } + changed(collectionId, next, name, false); + if (accepted.transformed()) + org.slf4j.LoggerFactory.getLogger(CalendarStore.class) + .info( + "calendar_input_repaired repair=local_observance_until rules={}", + accepted.repairedRules()); + return new WriteResult( + new Resource(name, uid, content, etag, next, modifiedAt), accepted.transformed()); + } + + @Transactional + public void delete(UUID actor, UUID collectionId, String name, WriteCondition condition) { + org.busystack.caldav.storage.DavMutationLock.shared(jdbc); + Collection c = lock(collectionId); + authorize(actor, c, true); + Resource old = resource(collectionId, name); + condition.check( + old == null ? null : old.etag(), old != null, old == null ? null : old.modifiedAt()); + if (old == null) throw new StoreException(StoreException.Kind.NOT_FOUND); + jdbc.update( + "DELETE FROM calendar_resources WHERE collection_id=? AND name=?", collectionId, name); + changed(collectionId, c.revision() + 1, name, true); + } + + /** Current collection state without loading bodies or imposing a report-result budget. */ + @Transactional(readOnly = true, isolation = Isolation.REPEATABLE_READ) + public String currentSyncToken(UUID actor, UUID collectionId) { + Collection c = collection(actor, collectionId); + return tokenPrefix(c) + c.revision(); + } + + private String tokenPrefix(Collection c) { + return "urn:busystack:sync:" + + jdbc.queryForObject("SELECT recovery_epoch FROM system_state WHERE id=1", UUID.class) + + ":" + + c.id() + + ":" + + c.generation() + + ":"; + } + + /** Backwards-compatible content-bearing storage entry point; HTTP supplies exact XML sizing. */ + @Transactional(readOnly = true, isolation = Isolation.REPEATABLE_READ) + public SyncPage sync(UUID actor, UUID collectionId, String token, int limit) { + return sync( + actor, + collectionId, + token, + limit, + true, + change -> 512L + (change.metadata() == null ? 0 : change.metadata().contentLength() * 6), + 32L * 1024 * 1024); + } + + /** + * A frozen watermark plus immutable versions gives each continuation an identical view across + * requests. The sizing callback runs with one bounded body at a time, before accepting a row. + */ + @Transactional(readOnly = true, isolation = Isolation.REPEATABLE_READ) + public SyncPage sync( + UUID actor, + UUID collectionId, + String token, + int limit, + boolean includeContent, + ToLongFunction responseBytes, + long maxBytes) { + if (limit < 1 || limit > 10000 || maxBytes < 1) + throw new StoreException(StoreException.Kind.LIMIT); + Collection c = collection(actor, collectionId); + String prefix = tokenPrefix(c); + Traversal traversal = traversal(actor, c, prefix, token); + // Expiry is checked by lookup. Independent read-committed maintenance avoids turning + // concurrent expired-row cleanup into serialization failures in this snapshot. + String lower = traversal.initial() ? "" : " AND revision>?"; + String sql = + "SELECT name,deleted,uid,etag,revision,updated_at,content_length FROM (" + + "SELECT DISTINCT ON (name COLLATE \"C\") name,deleted,uid,etag,revision,updated_at,content_length " + + "FROM calendar_resource_versions WHERE collection_id=? AND revision<=?" + + lower + + " AND name COLLATE \"C\">? COLLATE \"C\" ORDER BY name COLLATE \"C\",revision DESC) latest " + + (traversal.initial() ? "WHERE NOT deleted " : "") + + "ORDER BY name COLLATE \"C\" LIMIT ?"; + List accepted = new ArrayList<>(); + long bytes = 0, retainedContent = 0, retainedMetadata = 0; + boolean truncated = false; + String afterName = traversal.lastName(); + paging: + while (true) { + int batchSize = Math.min(128, limit - accepted.size() + 1); + List arguments = new ArrayList<>(List.of(collectionId, traversal.upper())); + if (!traversal.initial()) arguments.add(traversal.lower()); + arguments.add(afterName); + arguments.add(batchSize); + List candidates = + jdbc.query( + sql, + (rs, row) -> { + String name = rs.getString("name"); + boolean deleted = rs.getBoolean("deleted"); + return new Change( + name, + deleted, + deleted + ? null + : new Metadata( + name, + rs.getString("uid"), + rs.getString("etag"), + rs.getLong("revision"), + rs.getTimestamp("updated_at").toInstant(), + rs.getLong("content_length")), + null); + }, + arguments.toArray()); + for (Change candidate : candidates) { + long bodyLength = + includeContent && candidate.metadata() != null + ? candidate.metadata().contentLength() + : 0; + long metadataWork = + 256L + + candidate.name().length() * 2L + + (candidate.metadata() == null + ? 0 + : candidate.metadata().uid().length() * 2L + + candidate.metadata().etag().length() * 2L); + if (accepted.size() == limit + || retainedContent + bodyLength > MAX_RETAINED_CONTENT_BYTES + || retainedMetadata + metadataWork > MAX_RETAINED_CONTENT_BYTES) { + truncated = true; + break paging; + } + Change item = candidate; + if (includeContent && !candidate.deleted()) { + Resource body = + jdbc.queryForObject( + "SELECT name,uid,content,etag,revision,updated_at FROM calendar_resource_versions WHERE collection_id=? AND name=? AND revision=?", + (rs, row) -> readResource(rs), + collectionId, + candidate.name(), + candidate.metadata().revision()); + item = new Change(candidate.name(), false, candidate.metadata(), body); + } + long selectedBytes = responseBytes.applyAsLong(item); + if (selectedBytes < 0) throw new IllegalArgumentException("Negative response size"); + if (selectedBytes > maxBytes - bytes) { + truncated = true; + break paging; + } + accepted.add(item); + bytes += selectedBytes; + retainedContent += bodyLength; + retainedMetadata += metadataWork; + afterName = candidate.name(); + } + if (candidates.size() < batchSize) break; + } + if (truncated && accepted.isEmpty()) throw new StoreException(StoreException.Kind.LIMIT); + String nextToken = prefix + traversal.upper(); + if (truncated) { + UUID continuation = + continuations.save( + actor, + c, + recoveryEpoch(), + traversal.lower(), + traversal.upper(), + traversal.floor(), + traversal.initial(), + accepted.getLast().name()); + nextToken = prefix + "page:" + continuation; + } + return new SyncPage(nextToken, List.copyOf(accepted), truncated); + } + + private Traversal traversal(UUID actor, Collection c, String prefix, String token) { + if (token == null || token.isEmpty()) + return new Traversal(0, c.revision(), c.minimum(), true, ""); + if (token.length() > 512 || !token.startsWith(prefix)) invalidToken(); + String suffix = token.substring(prefix.length()); + if (suffix.startsWith("page:")) { + UUID id; + try { + id = UUID.fromString(suffix.substring(5)); + } catch (IllegalArgumentException ex) { + throw new StoreException(StoreException.Kind.INVALID_TOKEN); + } + var rows = + jdbc.query( + "SELECT lower_revision,upper_revision,retention_floor,initial_sync,last_name FROM sync_continuations WHERE id=? AND collection_id=? AND account_id=? AND generation=? AND recovery_epoch=? AND expires_at>now()", + (rs, row) -> + new Traversal( + rs.getLong(1), + rs.getLong(2), + rs.getLong(3), + rs.getBoolean(4), + rs.getString(5)), + id, + c.id(), + actor, + c.generation(), + recoveryEpoch()); + if (rows.isEmpty()) invalidToken(); + Traversal found = rows.getFirst(); + if (found.floor() < c.minimum() || found.upper() > c.revision()) invalidToken(); + return found; + } + long after; + try { + if (suffix.isEmpty() || !suffix.chars().allMatch(ch -> ch >= '0' && ch <= '9')) + invalidToken(); + after = Long.parseLong(suffix); + } catch (NumberFormatException ex) { + throw new StoreException(StoreException.Kind.INVALID_TOKEN); + } + if (after < c.minimum() || after > c.revision()) invalidToken(); + return new Traversal(after, c.revision(), c.minimum(), false, ""); + } + + private UUID recoveryEpoch() { + return jdbc.queryForObject("SELECT recovery_epoch FROM system_state WHERE id=1", UUID.class); + } + + private static void invalidToken() { + throw new StoreException(StoreException.Kind.INVALID_TOKEN); + } + + /** Only names are projected from the historical property snapshot. */ + public Set historicalPropertyNames( + UUID actor, UUID collectionId, String name, long revision) { + collection(actor, collectionId); + String from = + " FROM calendar_resource_version_properties WHERE collection_id=? AND resource_name=? AND revision=?"; + List arguments = List.of(collectionId, name, revision); + var size = + jdbc.queryForObject( + "SELECT count(*),COALESCE(sum(octet_length(namespace)+octet_length(property_name)),0)" + + from, + (rs, row) -> new long[] {rs.getLong(1), rs.getLong(2)}, + arguments.toArray()); + if (size[0] > 1000 || size[1] > 1024 * 1024) + throw new StoreException(StoreException.Kind.LIMIT); + return Set.copyOf( + jdbc.query( + "SELECT namespace,property_name" + from, + (rs, row) -> new QName(rs.getString(1), rs.getString(2)), + arguments.toArray())); + } + + /** Null selects all values; explicit names are filtered in SQL before values reach JDBC. */ + public Map historicalProperties( + UUID actor, UUID collectionId, String name, long revision, Set selection) { + collection(actor, collectionId); + if (selection != null && selection.isEmpty()) return Map.of(); + if (selection != null && selection.size() > 1000) + throw new StoreException(StoreException.Kind.LIMIT); + List arguments = new ArrayList<>(List.of(collectionId, name, revision)); + String clause = ""; + if (selection != null) { + clause = + " AND (" + + String.join( + " OR ", + java.util.Collections.nCopies( + selection.size(), "(namespace=? AND property_name=?)")) + + ")"; + for (QName item : selection) { + arguments.add(item.getNamespaceURI()); + arguments.add(item.getLocalPart()); + } + } + String from = + " FROM calendar_resource_version_properties WHERE collection_id=? AND resource_name=? AND revision=?" + + clause; + var size = + jdbc.queryForObject( + "SELECT count(*),COALESCE(sum(octet_length(xml)+octet_length(namespace)+octet_length(property_name)),0)" + + from, + (rs, row) -> new long[] {rs.getLong(1), rs.getLong(2)}, + arguments.toArray()); + if (size[0] > 1000 || size[1] > 1024 * 1024) + throw new StoreException(StoreException.Kind.LIMIT); + Map values = new LinkedHashMap<>(); + jdbc.query( + "SELECT namespace,property_name,xml" + from, + rs -> { + values.put(new QName(rs.getString(1), rs.getString(2)), rs.getString(3)); + }, + arguments.toArray()); + return Map.copyOf(values); + } + + @Transactional + public void pruneHistory(UUID collectionId, long through) { + org.busystack.caldav.storage.DavMutationLock.shared(jdbc); + Collection c = lock(collectionId); + if (through < c.minimum() || through > c.revision()) + throw new IllegalArgumentException("Invalid retention boundary"); + jdbc.update( + "DELETE FROM collection_changes WHERE collection_id=? AND revision<=?", + collectionId, + through); + jdbc.update( + "DELETE FROM calendar_resource_versions v WHERE v.collection_id=? AND v.revisionv.revision)", + collectionId, + through, + through); + jdbc.update( + "DELETE FROM sync_continuations WHERE collection_id=? AND retention_floor list = + jdbc.query( + "SELECT * FROM collections WHERE id=? FOR UPDATE", CalendarStore::collection, id); + if (list.isEmpty()) throw new StoreException(StoreException.Kind.NOT_FOUND); + return list.getFirst(); + } + + private Collection find(UUID id) { + List list = + jdbc.query("SELECT * FROM collections WHERE id=?", CalendarStore::collection, id); + if (list.isEmpty()) throw new StoreException(StoreException.Kind.NOT_FOUND); + return list.getFirst(); + } + + private void requireEnabled(UUID actor) { + if (!Boolean.TRUE.equals( + jdbc.queryForObject( + "SELECT EXISTS(SELECT 1 FROM accounts WHERE id=? AND enabled)", Boolean.class, actor))) + throw new StoreException(StoreException.Kind.FORBIDDEN); + } + + private void authorize(UUID actor, Collection c, boolean write) { + requireEnabled(actor); + if (c.owner().equals(actor)) return; + List permissions = + jdbc.queryForList( + "SELECT permission FROM collection_effective_members WHERE collection_id=? AND account_id=?", + String.class, + c.id(), + actor); + if (permissions.isEmpty()) + throw new StoreException( + StoreException.Kind.FORBIDDEN, StoreException.RequiredAccess.READ, c.id()); + if (write && !"WRITE".equals(permissions.getFirst())) + throw new StoreException( + StoreException.Kind.FORBIDDEN, StoreException.RequiredAccess.WRITE, c.id()); + } + + private Resource resource(UUID collectionId, String name) { + return jdbc + .query( + "SELECT * FROM calendar_resources WHERE collection_id=? AND name=?", + (rs, row) -> readResource(rs), + collectionId, + name) + .stream() + .findFirst() + .orElse(null); + } + + private static Collection collection(java.sql.ResultSet rs, int row) + throws java.sql.SQLException { + return new Collection( + rs.getObject("id", UUID.class), + rs.getObject("owner_id", UUID.class), + rs.getString("name"), + rs.getString("component"), + rs.getObject("generation", UUID.class), + rs.getLong("revision"), + rs.getLong("min_retained_revision")); + } + + private static Resource readResource(java.sql.ResultSet rs) throws java.sql.SQLException { + return new Resource( + rs.getString("name"), + rs.getString("uid"), + rs.getBytes("content"), + rs.getString("etag"), + rs.getLong("revision"), + rs.getTimestamp("updated_at").toInstant()); + } + + private static void checkName(String name) { + if (name == null + || name.isBlank() + || name.length() > 255 + || name.equals(".") + || name.equals("..") + || name.codePoints().anyMatch(c -> c == '/' || c == '\\' || Character.isISOControl(c))) + throw new IllegalArgumentException("Invalid resource name"); + } + + private static String etag(byte[] bytes) { + try { + return "\"" + + HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(bytes)) + + "\""; + } catch (NoSuchAlgorithmException ex) { + throw new IllegalStateException(ex); + } + } +} diff --git a/src/main/java/org/busystack/caldav/storage/DavMutationLock.java b/src/main/java/org/busystack/caldav/storage/DavMutationLock.java new file mode 100644 index 0000000..3781b05 --- /dev/null +++ b/src/main/java/org/busystack/caldav/storage/DavMutationLock.java @@ -0,0 +1,26 @@ +package org.busystack.caldav.storage; + +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.transaction.support.TransactionSynchronizationManager; + +/** Shared ordinary writes; exclusive validation of cross-resource DAV If conditions. */ +public final class DavMutationLock { + public static final long KEY = 0x425553595441434BL; + + private DavMutationLock() {} + + public static void shared(JdbcTemplate jdbc) { + requireTransaction(); + jdbc.query("SELECT pg_advisory_xact_lock_shared(?)", rs -> {}, KEY); + } + + public static void exclusive(JdbcTemplate jdbc) { + requireTransaction(); + jdbc.query("SELECT pg_advisory_xact_lock(?)", rs -> {}, KEY); + } + + private static void requireTransaction() { + if (!TransactionSynchronizationManager.isActualTransactionActive()) + throw new IllegalStateException("DAV mutation lock requires an active transaction"); + } +} diff --git a/src/main/java/org/busystack/caldav/storage/DavNamespace.java b/src/main/java/org/busystack/caldav/storage/DavNamespace.java new file mode 100644 index 0000000..6568aee --- /dev/null +++ b/src/main/java/org/busystack/caldav/storage/DavNamespace.java @@ -0,0 +1,101 @@ +package org.busystack.caldav.storage; + +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; + +/** One namespace budget after /dav/; the deployment prefix has its own validated boundary. */ +public final class DavNamespace { + public static final int MAX_SEGMENTS = 20; + public static final int MAX_ENCODED_LENGTH = 4096; + private static final String PRINCIPAL = "00000000-0000-0000-0000-000000000000"; + // Every accepted 255 UTF-16-unit leaf fits: BMP non-ASCII requires at most nine URI bytes/unit. + private static final String RESERVED_LEAF = "\u0800".repeat(255); + + private DavNamespace() {} + + public static void segment(String value) { + if (value == null + || value.isBlank() + || value.length() > 255 + || value.equals(".") + || value.equals("..") + || value + .codePoints() + .anyMatch( + c -> + c == '/' + || c == ';' + || c == 0x2028 + || c == 0x2029 + || c == 0xFFFE + || c == 0xFFFF + || c == '\\' + || Character.isISOControl(c) + || c >= 0xD800 && c <= 0xDFFF)) + throw new IllegalArgumentException("Invalid DAV path segment"); + } + + public static void parts(List parts) { + if (parts.size() > MAX_SEGMENTS) throw new IllegalArgumentException("DAV path is too deep"); + int length = Math.max(0, parts.size() - 1); + for (String part : parts) { + segment(part); + length += encodedLength(part); + if (length > MAX_ENCODED_LENGTH) + throw new IllegalArgumentException("Encoded DAV path is too long"); + } + } + + public static void binding(String path, boolean calendar) { + var parts = bindingParts(path); + if (calendar) parts.add(RESERVED_LEAF); + parts(parts); + } + + public static void resource(String path, String name) { + var parts = bindingParts(path); + parts.add(name); + parts(parts); + } + + /** Call after authorization and the collection lock, before modifying a resource. */ + static void resourceBindings( + org.springframework.jdbc.core.JdbcTemplate jdbc, java.util.UUID collection, String name) { + jdbc.query( + connection -> { + var statement = + connection.prepareStatement( + "SELECT path FROM dav_bindings WHERE collection_id=? ORDER BY home_id,path"); + statement.setObject(1, collection); + statement.setFetchSize(128); + return statement; + }, + (org.springframework.jdbc.core.RowCallbackHandler) rs -> resource(rs.getString(1), name)); + } + + private static ArrayList bindingParts(String path) { + if (path == null) throw new IllegalArgumentException("Invalid DAV path"); + var result = new ArrayList<>(List.of("calendars", PRINCIPAL)); + if (!path.isEmpty()) result.addAll(List.of(path.split("/", -1))); + return result; + } + + private static int encodedLength(String text) { + int length = 0; + for (byte item : text.getBytes(StandardCharsets.UTF_8)) { + int c = item & 255; + length += + (c >= 'a' && c <= 'z' + || c >= 'A' && c <= 'Z' + || c >= '0' && c <= '9' + || c == '-' + || c == '.' + || c == '_' + || c == '~') + ? 1 + : 3; + } + return length; + } +} diff --git a/src/main/java/org/busystack/caldav/storage/DavStore.java b/src/main/java/org/busystack/caldav/storage/DavStore.java new file mode 100644 index 0000000..07831a8 --- /dev/null +++ b/src/main/java/org/busystack/caldav/storage/DavStore.java @@ -0,0 +1,1317 @@ +package org.busystack.caldav.storage; + +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.UUID; +import javax.xml.namespace.QName; +import org.busystack.caldav.calendar.CalendarContent; +import org.busystack.caldav.xml.XmlCharacters; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Isolation; +import org.springframework.transaction.annotation.Transactional; + +/** URI bindings and DAV metadata surround the authoritative calendar resource store. */ +@Service +public class DavStore { + public record Binding( + UUID home, String path, UUID collectionId, String displayName, boolean calendar) {} + + public record Entry(Binding binding, CalendarStore.Collection collection, String permission) {} + + public record Member(UUID account, String permission, boolean group) { + public Member(UUID account, String permission) { + this(account, permission, false); + } + } + + public record Property(String namespace, String name, String xml) {} + + public record Principal(UUID id, String username, String displayName, boolean group, UUID owner) { + public Principal(UUID id, String username, String displayName) { + this(id, username, displayName, false, id); + } + } + + public record Snapshot( + Entry entry, + List resources, + List properties, + String syncToken) { + public Snapshot { + resources = List.copyOf(resources); + properties = List.copyOf(properties); + } + } + + public record PutResult(CalendarStore.Resource resource, boolean created, boolean transformed) {} + + private static final int MAX_ROWS = 10000; + private static final QName DISPLAY_NAME = new QName("DAV:", "displayname"); + private final JdbcTemplate jdbc; + private final CalendarStore calendars; + + public DavStore(JdbcTemplate jdbc, CalendarStore calendars) { + this.jdbc = jdbc; + this.calendars = calendars; + } + + public Entry resolve(UUID actor, UUID home, String path) { + requireEnabled(actor); + checkPath(path, true); + if (path.isEmpty()) { + if (!actor.equals(home)) forbidden(); + return new Entry(new Binding(home, "", null, principalName(home), false), null, "OWNER"); + } + var bindings = + jdbc.query( + "SELECT * FROM dav_bindings WHERE home_id=? AND path=?", + (rs, row) -> + new Binding( + home, + rs.getString("path"), + rs.getObject("collection_id", UUID.class), + rs.getString("display_name"), + rs.getObject("collection_id") != null), + home, + path); + if (bindings.isEmpty()) missing(); + Binding binding = bindings.getFirst(); + if (!binding.calendar()) { + if (!actor.equals(home)) forbidden(); + return new Entry(binding, null, "OWNER"); + } + var collection = calendars.collection(actor, binding.collectionId()); + return new Entry(binding, collection, permission(actor, collection.id())); + } + + public List children(UUID actor, UUID home, String path) { + Entry parent = resolve(actor, home, path); + if (parent.binding().calendar()) return List.of(); + List paths = + jdbc.queryForList( + "SELECT path FROM dav_bindings WHERE home_id=? AND left(path,?)=? AND position('/' in substring(path from ?))=0 ORDER BY path LIMIT ?", + String.class, + home, + characters(prefix(path)), + prefix(path), + characters(prefix(path)) + 1, + MAX_ROWS + 1); + bounded(paths.size()); + return paths.stream().map(child -> resolve(actor, home, child)).toList(); + } + + @Transactional(readOnly = true, isolation = Isolation.REPEATABLE_READ) + public Snapshot snapshot(UUID actor, UUID home, String path) { + Entry entry = resolve(actor, home, path); + if (!entry.binding().calendar()) + return new Snapshot(entry, List.of(), properties(folderProperties(home, path)), null); + UUID id = entry.collection().id(); + var resources = calendars.metadataBatch(actor, id, "", MAX_ROWS + 1); + bounded(resources.size()); + String token = calendars.currentSyncToken(actor, id); + return new Snapshot(entry, resources, properties(deadProperties(actor, id, null)), token); + } + + @Transactional + public Entry create( + UUID actor, + UUID home, + String path, + String displayName, + String component, + Map properties) { + org.busystack.caldav.storage.DavMutationLock.shared(jdbc); + writableHome(actor, home); + checkPath(path, false); + DavNamespace.binding(path, component != null); + checkDisplayName(displayName); + lockHomes(home); + if (component != null && !Set.of("VEVENT", "VTODO").contains(component)) + throw new IllegalArgumentException("Unsupported calendar component"); + requireFolder(actor, home, parent(path)); + if (bindingExists(home, path)) throw new StoreException(StoreException.Kind.EXISTS); + validateProperties(properties, Set.of()); + if (component == null) { + jdbc.update( + "INSERT INTO dav_bindings(home_id,path,display_name) VALUES (?,?,?)", + home, + path, + displayName); + } else { + UUID id = calendars.createCollection(actor, displayName, component); + jdbc.update( + "UPDATE dav_bindings SET path=? WHERE home_id=? AND collection_id=?", path, home, id); + } + patch(actor, home, path, null, properties, Set.of()); + return resolve(actor, home, path); + } + + @Transactional + public PutResult putResource( + UUID actor, UUID collection, String name, byte[] content, WriteCondition condition) { + org.busystack.caldav.storage.DavMutationLock.shared(jdbc); + if (content == null || content.length == 0) + throw new org.busystack.caldav.calendar.CalendarValidationException( + org.busystack.caldav.calendar.CalendarValidationException.Reason.INVALID_DATA); + if (content.length > 1048576) limit(); + lockCollections(collection); + requireWrite(actor, collection); + var current = calendars.get(actor, collection, name); + var written = calendars.putAdmitted(actor, collection, name, null, content, condition); + return new PutResult(written.resource(), current == null, written.transformed()); + } + + public String permission(UUID actor, UUID collection) { + var stored = calendars.collection(actor, collection); + if (stored.owner().equals(actor)) return "OWNER"; + var permissions = + jdbc.queryForList( + "SELECT permission FROM collection_effective_members WHERE collection_id=? AND account_id=?", + String.class, + collection, + actor); + if (permissions.isEmpty()) + throw new StoreException( + StoreException.Kind.FORBIDDEN, StoreException.RequiredAccess.READ, collection); + return permissions.getFirst(); + } + + public List members(UUID actor, UUID collection) { + requireOwner(actor, collection); + var acl = + jdbc.query( + "SELECT account_id AS principal_id,permission,false AS is_group FROM collection_members WHERE collection_id=? UNION ALL SELECT group_id,permission,true FROM collection_group_members WHERE collection_id=? ORDER BY principal_id", + (rs, row) -> new Member(rs.getObject(1, UUID.class), rs.getString(2), rs.getBoolean(3)), + collection, + collection); + if (acl.size() > 1000) limit(); + return List.copyOf(acl); + } + + @Transactional + public void replaceAcl(UUID actor, UUID collection, List members) { + org.busystack.caldav.storage.DavMutationLock.shared(jdbc); + lockCollections(collection); + requireOwner(actor, collection); + if (members.size() > 1000) limit(); + Set distinct = new LinkedHashSet<>(); + for (Member member : members) { + if (member.account().equals(actor) + || !distinct.add(member.account()) + || !Set.of("READ", "WRITE").contains(member.permission())) + throw new IllegalArgumentException("Invalid collection ACL"); + if (member.group()) { + if (!isGroupPrincipal(member.account())) + throw new IllegalArgumentException("Unknown sharing group"); + } else requireEnabled(member.account()); + } + var existing = members(actor, collection); + if (Set.copyOf(existing).equals(Set.copyOf(members))) return; + jdbc.query("SELECT set_config('busystack.defer_dav_bindings','on',true)", rs -> {}); + for (Member old : existing) + if (members.stream() + .noneMatch( + value -> value.account().equals(old.account()) && value.group() == old.group())) + jdbc.update( + "DELETE FROM " + + (old.group() + ? "collection_group_members WHERE group_id=?" + : "collection_members WHERE account_id=?") + + " AND collection_id=?", + old.account(), + collection); + for (Member member : members) { + String table = member.group() ? "collection_group_members" : "collection_members"; + String key = member.group() ? "group_id" : "account_id"; + jdbc.update( + "INSERT INTO " + + table + + "(collection_id," + + key + + ",permission) VALUES (?,?,?) ON CONFLICT(collection_id," + + key + + ") DO UPDATE SET permission=EXCLUDED.permission WHERE " + + table + + ".permission IS DISTINCT FROM EXCLUDED.permission", + collection, + member.account(), + member.permission()); + } + jdbc.query("SELECT set_config('busystack.defer_dav_bindings','off',true)", rs -> {}); + jdbc.query("SELECT dav_reconcile_memberships(?)", rs -> {}, collection); + } + + private static final String VISIBLE_GROUPS = + "SELECT g.* FROM sharing_groups g WHERE (EXISTS(SELECT 1 FROM accounts administrator WHERE administrator.id=? AND administrator.enabled AND administrator.administrator) OR EXISTS(SELECT 1 FROM sharing_group_members gm WHERE gm.group_id=g.id AND gm.account_id=?) OR EXISTS(SELECT 1 FROM collection_group_members cg JOIN collections c ON c.id=cg.collection_id WHERE cg.group_id=g.id AND c.owner_id=?))"; + + @Transactional(readOnly = true, isolation = Isolation.REPEATABLE_READ) + public List principals(UUID actor) { + requireEnabled(actor); + List visible = new ArrayList<>(accountPrincipals(actor, null)); + var groups = + jdbc.query( + VISIBLE_GROUPS + " ORDER BY g.id LIMIT 1001", + (rs, row) -> + new Principal( + rs.getObject("id", UUID.class), + "group:" + rs.getString("name"), + rs.getString("name"), + true, + rs.getObject("created_by", UUID.class)), + actor, + actor, + actor); + visible.addAll(groups); + bounded(visible.size()); + return List.copyOf(visible); + } + + /** Own-home URI metadata for protocol errors; never enumerates another principal's home. */ + public Binding bindingFor(UUID actor, UUID collection) { + requireEnabled(actor); + return jdbc + .query( + "SELECT home_id,path,collection_id,display_name FROM dav_bindings WHERE home_id=? AND collection_id=?", + (rs, row) -> + new Binding( + rs.getObject("home_id", UUID.class), + rs.getString("path"), + rs.getObject("collection_id", UUID.class), + rs.getString("display_name"), + true), + actor, + collection) + .stream() + .findFirst() + .orElse(null); + } + + public boolean isGroupPrincipal(UUID id) { + return Boolean.TRUE.equals( + jdbc.queryForObject( + "SELECT EXISTS(SELECT 1 FROM sharing_groups WHERE id=?)", Boolean.class, id)); + } + + public boolean recognizedPrincipal(UUID id) { + return Boolean.TRUE.equals( + jdbc.queryForObject( + "SELECT EXISTS(SELECT 1 FROM accounts WHERE id=? UNION ALL SELECT 1 FROM sharing_groups WHERE id=?)", + Boolean.class, + id, + id)); + } + + public Principal principal(UUID actor, UUID id) { + requireEnabled(actor); + var accounts = accountPrincipals(actor, id); + if (!accounts.isEmpty()) return accounts.getFirst(); + var groups = + jdbc.query( + VISIBLE_GROUPS + " AND g.id=?", + (rs, row) -> + new Principal( + rs.getObject("id", UUID.class), + "group:" + rs.getString("name"), + rs.getString("name"), + true, + rs.getObject("created_by", UUID.class)), + actor, + actor, + actor, + id); + if (groups.isEmpty()) missing(); + return groups.getFirst(); + } + + private List accountPrincipals(UUID actor, UUID id) { + String sql = + "WITH visible_groups AS (" + + VISIBLE_GROUPS + + ") SELECT a.id,a.username,a.display_name FROM accounts a WHERE (a.id=? OR EXISTS(SELECT 1 FROM collections c JOIN collection_effective_members m ON m.collection_id=c.id WHERE c.owner_id=a.id AND m.account_id=?) OR EXISTS(SELECT 1 FROM collection_effective_members m JOIN collections c ON c.id=m.collection_id WHERE m.account_id=a.id AND c.owner_id=?) OR EXISTS(SELECT 1 FROM sharing_group_members gm JOIN visible_groups g ON g.id=gm.group_id WHERE gm.account_id=a.id) OR EXISTS(SELECT 1 FROM visible_groups g WHERE g.created_by=a.id))"; + List parameters = new ArrayList<>(List.of(actor, actor, actor, actor, actor, actor)); + if (id != null) { + sql += " AND a.id=?"; + parameters.add(id); + } + return jdbc.query( + sql + " ORDER BY a.id LIMIT 10001", + (rs, row) -> new Principal(rs.getObject(1, UUID.class), rs.getString(2), rs.getString(3)), + parameters.toArray()); + } + + public boolean groupContains(UUID actor, UUID group) { + requireEnabled(actor); + return Boolean.TRUE.equals( + jdbc.queryForObject( + "SELECT EXISTS(SELECT 1 FROM sharing_group_members WHERE group_id=? AND account_id=?)", + Boolean.class, + group, + actor)); + } + + @Transactional(readOnly = true, isolation = Isolation.REPEATABLE_READ) + public List groupMembers(UUID actor, UUID group) { + if (!principal(actor, group).group()) throw new IllegalArgumentException("Not a sharing group"); + var members = + jdbc.query( + "SELECT a.id,a.username,a.display_name FROM accounts a JOIN sharing_group_members m ON m.account_id=a.id WHERE m.group_id=? ORDER BY a.id LIMIT 1001", + (rs, row) -> + new Principal(rs.getObject(1, UUID.class), rs.getString(2), rs.getString(3)), + group); + if (members.size() > 1000) limit(); + return List.copyOf(members); + } + + @Transactional(readOnly = true, isolation = Isolation.REPEATABLE_READ) + public List principalGroups(UUID actor, UUID account) { + // Groups are not nested. Query the visible memberships directly instead of one query per + // visible group for every principal in a directory response. + if (principal(actor, account).group()) return List.of(); + var groups = + jdbc.query( + VISIBLE_GROUPS + + " AND EXISTS(SELECT 1 FROM sharing_group_members member WHERE member.group_id=g.id AND member.account_id=?) ORDER BY g.id LIMIT 1001", + (rs, row) -> + new Principal( + rs.getObject("id", UUID.class), + "group:" + rs.getString("name"), + rs.getString("name"), + true, + rs.getObject("created_by", UUID.class)), + actor, + actor, + actor, + account); + if (groups.size() > 1000) limit(); + return List.copyOf(groups); + } + + public static final long MAX_PROPERTY_BYTES = 1024L * 1024; + + private record PropertyTarget(String table, String predicate, List arguments) {} + + private PropertyTarget propertyTarget(Entry entry, String resource) { + if (!entry.binding().calendar()) + return new PropertyTarget( + "dav_binding_properties", + "home_id=? AND path=?", + List.of(entry.binding().home(), entry.binding().path())); + if (resource == null) + return new PropertyTarget( + "dav_collection_properties", "collection_id=?", List.of(entry.collection().id())); + return new PropertyTarget( + "dav_resource_properties", + "collection_id=? AND resource_name=?", + List.of(entry.collection().id(), resource)); + } + + private void requirePropertyResource(UUID actor, UUID collection, String resourceName) { + calendars.collection(actor, collection); + if (resourceName != null + && !Boolean.TRUE.equals( + jdbc.queryForObject( + "SELECT EXISTS(SELECT 1 FROM calendar_resources WHERE collection_id=? AND name=?)", + Boolean.class, + collection, + resourceName))) missing(); + } + + @Transactional(readOnly = true, isolation = Isolation.REPEATABLE_READ) + public Map deadProperties(UUID actor, UUID collection, String resourceName) { + return deadProperties(actor, collection, resourceName, null); + } + + /** Null selects all values; an explicit set fetches only those names from PostgreSQL. */ + @Transactional(readOnly = true, isolation = Isolation.REPEATABLE_READ) + public Map deadProperties( + UUID actor, UUID collection, String resourceName, Set selected) { + requirePropertyResource(actor, collection, resourceName); + return readProperties( + new PropertyTarget( + resourceName == null ? "dav_collection_properties" : "dav_resource_properties", + resourceName == null ? "collection_id=?" : "collection_id=? AND resource_name=?", + resourceName == null ? List.of(collection) : List.of(collection, resourceName)), + selected); + } + + @Transactional(readOnly = true, isolation = Isolation.REPEATABLE_READ) + public Set deadPropertyNames(UUID actor, UUID collection, String resourceName) { + requirePropertyResource(actor, collection, resourceName); + return readPropertyNames( + new PropertyTarget( + resourceName == null ? "dav_collection_properties" : "dav_resource_properties", + resourceName == null ? "collection_id=?" : "collection_id=? AND resource_name=?", + resourceName == null ? List.of(collection) : List.of(collection, resourceName))); + } + + @Transactional(readOnly = true, isolation = Isolation.REPEATABLE_READ) + public String deadProperty(UUID actor, UUID collection, String resourceName, QName name) { + return deadProperties(actor, collection, resourceName, Set.of(name)).get(name); + } + + @Transactional(readOnly = true, isolation = Isolation.REPEATABLE_READ) + public Map folderProperties( + UUID actor, UUID home, String path, Set selected) { + Entry entry = resolve(actor, home, path); + if (entry.binding().calendar()) throw new IllegalArgumentException("Expected folder binding"); + if (path.isEmpty()) return Map.of(); + return readProperties(propertyTarget(entry, null), selected); + } + + @Transactional(readOnly = true, isolation = Isolation.REPEATABLE_READ) + public Set folderPropertyNames(UUID actor, UUID home, String path) { + Entry entry = resolve(actor, home, path); + if (entry.binding().calendar()) throw new IllegalArgumentException("Expected folder binding"); + if (path.isEmpty()) return Set.of(); + return readPropertyNames(propertyTarget(entry, null)); + } + + private Map readProperties(PropertyTarget target, Set selected) { + if (selected != null && selected.isEmpty()) return Map.of(); + if (selected != null && selected.size() > 1000) limit(); + List arguments = new ArrayList<>(target.arguments()); + String selection = ""; + if (selected != null) { + List clauses = new ArrayList<>(); + for (QName name : selected) { + clauses.add("(namespace=? AND name=?)"); + arguments.add(name.getNamespaceURI()); + arguments.add(name.getLocalPart()); + } + selection = " AND (" + String.join(" OR ", clauses) + ")"; + } + String from = " FROM " + target.table() + " WHERE " + target.predicate() + selection; + long[] size = + jdbc.queryForObject( + "SELECT count(*),COALESCE(sum(octet_length(xml)),0)" + from, + (rs, row) -> new long[] {rs.getLong(1), rs.getLong(2)}, + arguments.toArray()); + if (size[0] > 1000 || size[1] > MAX_PROPERTY_BYTES) limit(); + return propertyMap( + jdbc.query( + "SELECT namespace,name,xml" + from + " ORDER BY namespace,name LIMIT 1001", + (rs, row) -> new Property(rs.getString(1), rs.getString(2), rs.getString(3)), + arguments.toArray())); + } + + private Set readPropertyNames(PropertyTarget target) { + var names = + jdbc.query( + "SELECT namespace,name FROM " + + target.table() + + " WHERE " + + target.predicate() + + " ORDER BY namespace,name LIMIT 1001", + (rs, row) -> new QName(rs.getString(1), rs.getString(2)), + target.arguments().toArray()); + if (names.size() > 1000) limit(); + return java.util.Collections.unmodifiableSet(new LinkedHashSet<>(names)); + } + + /** The caller holds the binding home and, for shared resources, the collection row lock. */ + private List> checkPropertyBudget( + Entry entry, String resource, Map set, Set remove) { + PropertyTarget target = propertyTarget(entry, resource); + Map lengths = new LinkedHashMap<>(); + jdbc.query( + "SELECT namespace,name,octet_length(xml) FROM " + + target.table() + + " WHERE " + + target.predicate(), + (org.springframework.jdbc.core.RowCallbackHandler) + rs -> lengths.put(new QName(rs.getString(1), rs.getString(2)), rs.getInt(3)), + target.arguments().toArray()); + Map original = Map.copyOf(lengths); + long previous = lengths.values().stream().mapToLong(Integer::longValue).sum(); + int previousCount = lengths.size(); + remove.forEach(lengths::remove); + set.forEach((name, xml) -> lengths.put(name, xml.getBytes(StandardCharsets.UTF_8).length)); + long next = lengths.values().stream().mapToLong(Integer::longValue).sum(); + // Previously accepted oversized targets remain repairable by removal or a non-increasing + // update. + if (next > MAX_PROPERTY_BYTES && next > previous + || lengths.size() > 1000 && lengths.size() > previousCount) limit(); + return set.entrySet().stream() + .sorted( + java.util.Comparator.comparingLong( + item -> + item.getValue().getBytes(StandardCharsets.UTF_8).length + - original.getOrDefault(item.getKey(), 0))) + .toList(); + } + + @Transactional + public void patch( + UUID actor, + UUID home, + String path, + String resourceName, + Map set, + Set remove) { + patch(actor, home, path, resourceName, set, remove, WriteCondition.none()); + } + + @Transactional + public void patch( + UUID actor, + UUID home, + String path, + String resourceName, + Map set, + Set remove, + WriteCondition condition) { + org.busystack.caldav.storage.DavMutationLock.shared(jdbc); + writableHome(actor, home); + checkPath(path, true); + lockHomes(home); + Entry entry = resolve(actor, home, path); + if (entry.binding().calendar()) { + lockCollections(entry.collection().id()); + requireWrite(actor, entry.collection().id()); + } else if (resourceName != null) throw new StoreException(StoreException.Kind.CONFLICT); + CalendarStore.Metadata resource = + resourceName == null + ? null + : calendars.metadata(actor, entry.collection().id(), resourceName); + if (resourceName != null && resource == null) missing(); + condition.check( + resource == null ? null : resource.etag(), + true, + resource == null ? null : resource.modifiedAt()); + validateProperties(set, remove); + var orderedProperties = checkPropertyBudget(entry, resourceName, set, remove); + if (remove.contains(DISPLAY_NAME) && resourceName == null) + throw new StoreException(StoreException.Kind.INVALID_PROPERTY); + if (set.containsKey(DISPLAY_NAME)) { + String display = propertyText(DISPLAY_NAME, set.get(DISPLAY_NAME)); + checkDisplayName(display); + if (resourceName == null) { + if (entry.binding().calendar()) + jdbc.update("UPDATE collections SET name=? WHERE id=?", display, entry.collection().id()); + else if (!path.isEmpty()) + jdbc.update( + "UPDATE dav_bindings SET display_name=? WHERE home_id=? AND path=?", + display, + home, + path); + else throw new StoreException(StoreException.Kind.INVALID_PROPERTY); + } + } + if (path.isEmpty() && (!set.isEmpty() || !remove.isEmpty())) + throw new StoreException(StoreException.Kind.INVALID_PROPERTY); + for (QName name : remove) deleteProperty(entry, resourceName, name); + for (var item : orderedProperties) + putProperty(entry, resourceName, item.getKey(), item.getValue()); + QName timezoneProperty = new QName("urn:ietf:params:xml:ns:caldav", "calendar-timezone"); + if (resourceName == null + && entry.binding().calendar() + && (set.containsKey(timezoneProperty) || remove.contains(timezoneProperty))) + jdbc.update( + "UPDATE collections SET generation=gen_random_uuid() WHERE id=?", + entry.collection().id()); + if (resourceName != null && (!set.isEmpty() || !remove.isEmpty())) { + long revision = changed(entry.collection().id(), resourceName, false); + jdbc.update( + "UPDATE calendar_resources SET revision=? WHERE collection_id=? AND name=?", + revision, + entry.collection().id(), + resourceName); + } + } + + @Transactional + public void renameDisplayName(UUID actor, UUID home, String path, String name) { + org.busystack.caldav.storage.DavMutationLock.shared(jdbc); + checkDisplayName(name); + writableHome(actor, home); + lockHomes(home); + Entry entry = resolve(actor, home, path); + if (entry.binding().calendar()) { + lockCollections(entry.collection().id()); + requireWrite(actor, entry.collection().id()); + jdbc.update("UPDATE collections SET name=? WHERE id=?", name, entry.collection().id()); + } else if (!path.isEmpty()) + jdbc.update( + "UPDATE dav_bindings SET display_name=? WHERE home_id=? AND path=?", name, home, path); + else throw new StoreException(StoreException.Kind.INVALID_PROPERTY); + } + + @Transactional + public void deleteBinding(UUID actor, UUID home, String path, WriteCondition condition) { + org.busystack.caldav.storage.DavMutationLock.shared(jdbc); + writableHome(actor, home); + checkPath(path, false); + lockHomes(home); + Entry entry = resolve(actor, home, path); + condition.check(bindingEtag(entry), true); + List tree = tree(home, path); + lockCollections( + tree.stream().filter(Binding::calendar).map(Binding::collectionId).toArray(UUID[]::new)); + for (Binding binding : tree) + if (binding.calendar()) requireOwner(actor, binding.collectionId()); + for (Binding binding : tree) + if (binding.calendar()) + jdbc.update("DELETE FROM collections WHERE id=?", binding.collectionId()); + jdbc.update( + "DELETE FROM dav_bindings WHERE home_id=? AND (path=? OR left(path,?)=?)", + home, + path, + characters(prefix(path)), + prefix(path)); + } + + @Transactional + public boolean transferResource( + UUID actor, + UUID sourceCollection, + String sourceName, + UUID destinationCollection, + String destinationName, + boolean move, + boolean overwrite, + WriteCondition condition) { + org.busystack.caldav.storage.DavMutationLock.shared(jdbc); + checkSegment(sourceName); + checkSegment(destinationName); + lockCollections(sourceCollection, destinationCollection); + calendars.collection(actor, sourceCollection); + requireWrite(actor, destinationCollection); + DavNamespace.resourceBindings(jdbc, destinationCollection, destinationName); + if (move) requireWrite(actor, sourceCollection); + if (sourceCollection.equals(destinationCollection) && sourceName.equals(destinationName)) + forbidden(); + var source = calendars.get(actor, sourceCollection, sourceName); + if (source == null) missing(); + if (Boolean.TRUE.equals( + jdbc.queryForObject( + "SELECT count(*)>1000 OR COALESCE(sum(octet_length(xml)),0)>1048576 FROM dav_resource_properties WHERE collection_id=? AND resource_name=?", + Boolean.class, + sourceCollection, + sourceName))) limit(); + condition.check(source.etag(), true, source.modifiedAt()); + var destination = calendars.get(actor, destinationCollection, destinationName); + if (destination != null && !overwrite) + throw new StoreException(StoreException.Kind.PRECONDITION); + if (destination != null && !destination.uid().equals(source.uid())) + throw new StoreException(StoreException.Kind.UID_CONFLICT); + CalendarContent.parse( + source.content(), calendars.collection(actor, destinationCollection).component()); + var duplicate = + jdbc.queryForList( + "SELECT name FROM calendar_resources WHERE collection_id=? AND uid=? AND name<>?", + String.class, + destinationCollection, + source.uid(), + destinationName); + if (duplicate.stream() + .anyMatch( + name -> + !move + || !sourceCollection.equals(destinationCollection) + || !name.equals(sourceName))) + throw new StoreException(StoreException.Kind.UID_CONFLICT); + java.sql.Timestamp unchangedDate = + destination != null && java.util.Arrays.equals(source.content(), destination.content()) + ? java.sql.Timestamp.from(destination.modifiedAt()) + : null; + if (destination != null) + jdbc.update( + "DELETE FROM calendar_resources WHERE collection_id=? AND name=?", + destinationCollection, + destinationName); + if (move) { + changed(sourceCollection, sourceName, true); + long revision = changed(destinationCollection, destinationName, false); + jdbc.update( + "UPDATE calendar_resources SET collection_id=?,name=?,revision=?,updated_at=COALESCE(?::timestamptz,clock_timestamp()) WHERE collection_id=? AND name=?", + destinationCollection, + destinationName, + revision, + unchangedDate, + sourceCollection, + sourceName); + } else { + long revision = changed(destinationCollection, destinationName, false); + jdbc.update( + "INSERT INTO calendar_resources(collection_id,name,uid,content,etag,revision,updated_at) VALUES (?,?,?,?,?,?,COALESCE(?::timestamptz,clock_timestamp()))", + destinationCollection, + destinationName, + source.uid(), + source.content(), + source.etag(), + revision, + unchangedDate); + jdbc.update( + "INSERT INTO dav_resource_properties(collection_id,resource_name,namespace,name,xml) SELECT ?,?,namespace,name,xml FROM dav_resource_properties WHERE collection_id=? AND resource_name=?", + destinationCollection, + destinationName, + sourceCollection, + sourceName); + } + return destination == null; + } + + public static String bindingEtag(Entry entry) { + return entry.collection() == null + ? null + : "\"collection-" + + entry.collection().generation() + + "-" + + entry.collection().revision() + + "\""; + } + + @Transactional + public boolean transfer( + UUID actor, + UUID sourceHome, + String sourcePath, + UUID destinationHome, + String destinationPath, + boolean move, + boolean overwrite, + WriteCondition condition) { + org.busystack.caldav.storage.DavMutationLock.shared(jdbc); + return transfer( + actor, + sourceHome, + sourcePath, + destinationHome, + destinationPath, + move, + overwrite, + condition, + true); + } + + @Transactional + public boolean transfer( + UUID actor, + UUID sourceHome, + String sourcePath, + UUID destinationHome, + String destinationPath, + boolean move, + boolean overwrite, + WriteCondition condition, + boolean deep) { + org.busystack.caldav.storage.DavMutationLock.shared(jdbc); + writableHome(actor, destinationHome); + if (move) writableHome(actor, sourceHome); + checkPath(sourcePath, false); + checkPath(destinationPath, false); + lockHomes(sourceHome, destinationHome); + if (sourceHome.equals(destinationHome) + && (sourcePath.equals(destinationPath) + || destinationPath.startsWith(prefix(sourcePath)) + || sourcePath.startsWith(prefix(destinationPath)))) forbidden(); + Entry source = resolve(actor, sourceHome, sourcePath); + requireFolder(actor, destinationHome, parent(destinationPath)); + List selected = deep ? tree(sourceHome, sourcePath) : List.of(source.binding()); + boolean existed = bindingExists(destinationHome, destinationPath); + if (existed && !overwrite) throw new StoreException(StoreException.Kind.PRECONDITION); + List replaced = existed ? tree(destinationHome, destinationPath) : List.of(); + List ids = new ArrayList<>(); + selected.stream().filter(Binding::calendar).map(Binding::collectionId).forEach(ids::add); + replaced.stream().filter(Binding::calendar).map(Binding::collectionId).forEach(ids::add); + lockCollections(ids.toArray(UUID[]::new)); + source = resolve(actor, sourceHome, sourcePath); + condition.check(bindingEtag(source), true); + for (Binding item : selected) { + calendarsIfVisible(actor, item); + if (move && item.calendar()) requireOwner(actor, item.collectionId()); + } + for (Binding item : replaced) if (item.calendar()) requireOwner(actor, item.collectionId()); + // Validate the entire resulting namespace before deleting an overwritten destination. + for (Binding item : selected) { + String target = destinationPath + item.path().substring(sourcePath.length()); + DavNamespace.binding(target, item.calendar()); + propertyCopyBudget(item, deep); + if (item.calendar() && deep) { + String component = calendars.collection(actor, item.collectionId()).component(); + jdbc.query( + connection -> { + var statement = + connection.prepareStatement( + "SELECT name,content FROM calendar_resources WHERE collection_id=? ORDER BY name"); + statement.setObject(1, item.collectionId()); + statement.setFetchSize(1); + return statement; + }, + (org.springframework.jdbc.core.RowCallbackHandler) + rs -> { + DavNamespace.resource(target, rs.getString(1)); + // Previously accepted invalid content remains retrievable, but cannot be + // admitted at another URI by bypassing the current write-time constraints. + org.busystack.caldav.calendar.CalendarContent.parse(rs.getBytes(2), component); + }); + } + } + if (move && !deep) throw new IllegalArgumentException("MOVE requires complete subtree"); + if (existed) deleteBinding(actor, destinationHome, destinationPath, WriteCondition.none()); + if (move) { + jdbc.update( + "UPDATE dav_bindings SET path=?||substring(path from ?) WHERE home_id=? AND (path=? OR left(path,?)=?)", + destinationPath, + characters(sourcePath) + 1, + sourceHome, + sourcePath, + characters(prefix(sourcePath)), + prefix(sourcePath)); + // Moving a namespace changes every contained resource's URI; an overwritten + // destination must never inherit the old source's earlier modification date. + for (Binding item : selected) + if (item.calendar()) { + long previous = calendars.collection(actor, item.collectionId()).revision(); + jdbc.update( + "UPDATE calendar_resources r SET revision=?+n.position,updated_at=clock_timestamp() FROM (SELECT name,row_number() OVER(ORDER BY name COLLATE \"C\") AS position FROM calendar_resources WHERE collection_id=?) n WHERE r.collection_id=? AND r.name=n.name", + previous, + item.collectionId(), + item.collectionId()); + jdbc.update( + "INSERT INTO collection_changes(collection_id,revision,name,deleted) SELECT collection_id,revision,name,false FROM calendar_resources WHERE collection_id=? AND revision>?", + item.collectionId(), + previous); + jdbc.update( + "UPDATE collections SET revision=COALESCE((SELECT max(revision) FROM calendar_resources WHERE collection_id=?),revision) WHERE id=?", + item.collectionId(), + item.collectionId()); + } + } else { + for (Binding item : selected) { + String target = destinationPath + item.path().substring(sourcePath.length()); + Entry created = + create( + actor, + destinationHome, + target, + item.displayName(), + item.calendar() + ? calendars.collection(actor, item.collectionId()).component() + : null, + Map.of()); + if (item.calendar()) { + UUID targetId = created.collection().id(); + jdbc.update( + "INSERT INTO dav_collection_properties(collection_id,namespace,name,xml) SELECT ?,namespace,name,xml FROM dav_collection_properties WHERE collection_id=?", + targetId, + item.collectionId()); + if (deep) { + // The source is already validated and collection-locked. Copy bytes and indexes in + // PostgreSQL instead of accumulating every calendar body in application memory. + jdbc.update( + "INSERT INTO calendar_resources(collection_id,name,uid,content,etag,revision,updated_at) SELECT ?,name,uid,content,etag,row_number() OVER(ORDER BY name COLLATE \"C\"),clock_timestamp() FROM calendar_resources WHERE collection_id=?", + targetId, + item.collectionId()); + jdbc.update( + "INSERT INTO collection_changes(collection_id,revision,name,deleted) SELECT collection_id,revision,name,false FROM calendar_resources WHERE collection_id=?", + targetId); + jdbc.update( + "UPDATE collections SET revision=(SELECT COALESCE(max(revision),0) FROM calendar_resources WHERE collection_id=?) WHERE id=?", + targetId, + targetId); + jdbc.update( + "INSERT INTO dav_resource_properties(collection_id,resource_name,namespace,name,xml) SELECT ?,resource_name,namespace,name,xml FROM dav_resource_properties WHERE collection_id=?", + targetId, + item.collectionId()); + } + } else + jdbc.update( + "INSERT INTO dav_binding_properties(home_id,path,namespace,name,xml) SELECT ?,?,namespace,name,xml FROM dav_binding_properties WHERE home_id=? AND path=?", + destinationHome, + target, + sourceHome, + item.path()); + } + } + return !existed; + } + + private void calendarsIfVisible(UUID actor, Binding item) { + if (item.calendar()) calendars.collection(actor, item.collectionId()); + } + + private void propertyCopyBudget(Binding binding, boolean deep) { + boolean excessive; + if (!binding.calendar()) + excessive = + Boolean.TRUE.equals( + jdbc.queryForObject( + "SELECT count(*)>1000 OR COALESCE(sum(octet_length(xml)),0)>1048576 FROM dav_binding_properties WHERE home_id=? AND path=?", + Boolean.class, + binding.home(), + binding.path())); + else { + excessive = + Boolean.TRUE.equals( + jdbc.queryForObject( + "SELECT count(*)>1000 OR COALESCE(sum(octet_length(xml)),0)>1048576 FROM dav_collection_properties WHERE collection_id=?", + Boolean.class, + binding.collectionId())); + if (deep && !excessive) + excessive = + Boolean.TRUE.equals( + jdbc.queryForObject( + "SELECT EXISTS(SELECT 1 FROM dav_resource_properties WHERE collection_id=? GROUP BY resource_name HAVING count(*)>1000 OR sum(octet_length(xml))>1048576)", + Boolean.class, + binding.collectionId())); + } + if (excessive) limit(); + } + + private Map folderProperties(UUID home, String path) { + if (path.isEmpty()) return Map.of(); + return readProperties( + new PropertyTarget("dav_binding_properties", "home_id=? AND path=?", List.of(home, path)), + null); + } + + private static Map propertyMap(List properties) { + Map result = new LinkedHashMap<>(); + for (Property value : properties) + result.put(new QName(value.namespace(), value.name()), value.xml()); + return Map.copyOf(result); + } + + private static List properties(Map map) { + return map.entrySet().stream() + .map( + item -> + new Property( + item.getKey().getNamespaceURI(), item.getKey().getLocalPart(), item.getValue())) + .toList(); + } + + private void deleteProperty(Entry entry, String resource, QName name) { + if (!entry.binding().calendar()) + jdbc.update( + "DELETE FROM dav_binding_properties WHERE home_id=? AND path=? AND namespace=? AND name=?", + entry.binding().home(), + entry.binding().path(), + name.getNamespaceURI(), + name.getLocalPart()); + else if (resource == null) + jdbc.update( + "DELETE FROM dav_collection_properties WHERE collection_id=? AND namespace=? AND name=?", + entry.collection().id(), + name.getNamespaceURI(), + name.getLocalPart()); + else + jdbc.update( + "DELETE FROM dav_resource_properties WHERE collection_id=? AND resource_name=? AND namespace=? AND name=?", + entry.collection().id(), + resource, + name.getNamespaceURI(), + name.getLocalPart()); + } + + private void putProperty(Entry entry, String resource, QName name, String xml) { + try { + if (!entry.binding().calendar()) + jdbc.update( + "INSERT INTO dav_binding_properties(home_id,path,namespace,name,xml) VALUES (?,?,?,?,?) ON CONFLICT(home_id,path,namespace,name) DO UPDATE SET xml=EXCLUDED.xml", + entry.binding().home(), + entry.binding().path(), + name.getNamespaceURI(), + name.getLocalPart(), + xml); + else if (resource == null) + jdbc.update( + "INSERT INTO dav_collection_properties(collection_id,namespace,name,xml) VALUES (?,?,?,?) ON CONFLICT(collection_id,namespace,name) DO UPDATE SET xml=EXCLUDED.xml", + entry.collection().id(), + name.getNamespaceURI(), + name.getLocalPart(), + xml); + else + jdbc.update( + "INSERT INTO dav_resource_properties(collection_id,resource_name,namespace,name,xml) VALUES (?,?,?,?,?) ON CONFLICT(collection_id,resource_name,namespace,name) DO UPDATE SET xml=EXCLUDED.xml", + entry.collection().id(), + resource, + name.getNamespaceURI(), + name.getLocalPart(), + xml); + } catch (org.springframework.dao.DataIntegrityViolationException failure) { + if (failure.getMostSpecificCause() instanceof java.sql.SQLException sql + && "23514".equals(sql.getSQLState()) + && sql.getMessage().contains("property_storage_budget")) limit(); + throw failure; + } + } + + private static final Set PROTECTED_DAV = + Set.of( + "resourcetype", + "getetag", + "getcontenttype", + "getcontentlength", + "getlastmodified", + "creationdate", + "owner", + "acl", + "acl-restrictions", + "inherited-acl-set", + "principal-collection-set", + "current-user-principal", + "current-user-privilege-set", + "supported-privilege-set", + "supported-report-set", + "supported-method-set", + "sync-token", + "supportedlock", + "lockdiscovery", + "principal-URL", + "group-member-set", + "group-membership"); + private static final Set PROTECTED_CAL = + Set.of( + "calendar-data", + "calendar-home-set", + "calendar-user-address-set", + "supported-calendar-component-set", + "supported-calendar-data", + "max-resource-size", + "min-date-time", + "max-date-time", + "max-instances", + "max-attendees-per-instance", + "schedule-inbox-URL", + "schedule-outbox-URL"); + + private static void validateProperties(Map set, Set remove) { + if (set.size() + remove.size() > 1000) limit(); + Set names = new LinkedHashSet<>(set.keySet()); + names.addAll(remove); + for (QName name : names) { + if (name == null + || name.getNamespaceURI().length() > 1024 + || name.getLocalPart().length() > 200 + || name.getLocalPart().isEmpty() + || (name.getNamespaceURI().equals("DAV:") && PROTECTED_DAV.contains(name.getLocalPart())) + || (name.getNamespaceURI().equals("urn:ietf:params:xml:ns:caldav") + && PROTECTED_CAL.contains(name.getLocalPart()))) + throw new StoreException(StoreException.Kind.INVALID_PROPERTY); + if (set.containsKey(name)) { + String xml = set.get(name); + if (xml == null || xml.getBytes(StandardCharsets.UTF_8).length > 65536) limit(); + propertyText(name, xml); + } + } + } + + private static String propertyText(QName name, String xml) { + try { + var factory = javax.xml.parsers.DocumentBuilderFactory.newInstance(); + factory.setNamespaceAware(true); + factory.setXIncludeAware(false); + factory.setExpandEntityReferences(false); + factory.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, true); + factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); + factory.setFeature("http://xml.org/sax/features/external-general-entities", false); + factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); + factory.setAttribute(javax.xml.XMLConstants.ACCESS_EXTERNAL_DTD, ""); + factory.setAttribute(javax.xml.XMLConstants.ACCESS_EXTERNAL_SCHEMA, ""); + factory.setAttribute("jdk.xml.maxElementDepth", "32"); + factory.setAttribute("jdk.xml.elementAttributeLimit", "100"); + factory.setAttribute("jdk.xml.maxXMLNameLimit", "256"); + var parser = factory.newDocumentBuilder(); + parser.setErrorHandler( + new org.xml.sax.helpers.DefaultHandler() { + @Override + public void error(org.xml.sax.SAXParseException failure) + throws org.xml.sax.SAXException { + throw failure; + } + + @Override + public void fatalError(org.xml.sax.SAXParseException failure) + throws org.xml.sax.SAXException { + throw failure; + } + }); + var element = + parser + .parse(new org.xml.sax.InputSource(new java.io.StringReader(xml))) + .getDocumentElement(); + if (!name.getLocalPart().equals(element.getLocalName()) + || !name.getNamespaceURI() + .equals(element.getNamespaceURI() == null ? "" : element.getNamespaceURI())) + throw new StoreException(StoreException.Kind.INVALID_PROPERTY); + return element.getTextContent(); + } catch (javax.xml.parsers.ParserConfigurationException + | org.xml.sax.SAXException + | java.io.IOException failure) { + throw new StoreException(StoreException.Kind.INVALID_PROPERTY); + } + } + + private void writableHome(UUID actor, UUID home) { + requireEnabled(actor); + if (!actor.equals(home)) forbidden(); + } + + private void requireEnabled(UUID actor) { + if (!Boolean.TRUE.equals( + jdbc.queryForObject( + "SELECT EXISTS(SELECT 1 FROM accounts WHERE id=? AND enabled)", Boolean.class, actor))) + forbidden(); + } + + private void requireOwner(UUID actor, UUID id) { + if (!calendars.collection(actor, id).owner().equals(actor)) + throw new StoreException( + StoreException.Kind.FORBIDDEN, StoreException.RequiredAccess.OWNER, id); + } + + private void requireWrite(UUID actor, UUID id) { + if ("READ".equals(permission(actor, id))) + throw new StoreException( + StoreException.Kind.FORBIDDEN, StoreException.RequiredAccess.WRITE, id); + } + + private String principalName(UUID home) { + var names = + jdbc.queryForList("SELECT display_name FROM accounts WHERE id=?", String.class, home); + if (names.isEmpty()) missing(); + return names.getFirst(); + } + + private void lockHomes(UUID... homes) { + java.util.Arrays.stream(homes) + .distinct() + .sorted() + .forEach( + home -> + jdbc.query( + "SELECT pg_advisory_xact_lock(hashtextextended(?,0))", + rs -> {}, + "busystack.dav.home:" + home)); + } + + private void lockCollections(UUID... ids) { + java.util.Arrays.stream(ids) + .distinct() + .sorted() + .forEach( + id -> { + if (jdbc.queryForList( + "SELECT id FROM collections WHERE id=? FOR UPDATE", UUID.class, id) + .isEmpty()) missing(); + }); + } + + private void requireFolder(UUID actor, UUID home, String path) { + try { + if (resolve(actor, home, path).binding().calendar()) + throw new StoreException(StoreException.Kind.CONFLICT); + } catch (StoreException failure) { + if (failure.kind() == StoreException.Kind.NOT_FOUND) + throw new StoreException(StoreException.Kind.CONFLICT); + throw failure; + } + } + + private boolean bindingExists(UUID home, String path) { + return Boolean.TRUE.equals( + jdbc.queryForObject( + "SELECT EXISTS(SELECT 1 FROM dav_bindings WHERE home_id=? AND path=?)", + Boolean.class, + home, + path)); + } + + private List tree(UUID home, String path) { + var rows = + jdbc.query( + "SELECT * FROM dav_bindings WHERE home_id=? AND (path=? OR left(path,?)=?) ORDER BY length(path),path LIMIT ?", + (rs, row) -> + new Binding( + home, + rs.getString("path"), + rs.getObject("collection_id", UUID.class), + rs.getString("display_name"), + rs.getObject("collection_id") != null), + home, + path, + characters(prefix(path)), + prefix(path), + MAX_ROWS + 1); + bounded(rows.size()); + return rows; + } + + private long changed(UUID id, String name, boolean deleted) { + long revision = + jdbc.queryForObject( + "UPDATE collections SET revision=revision+1 WHERE id=? RETURNING revision", + Long.class, + id); + jdbc.update( + "INSERT INTO collection_changes(collection_id,revision,name,deleted) VALUES (?,?,?,?)", + id, + revision, + name, + deleted); + return revision; + } + + private static CalendarStore.Resource resource(java.sql.ResultSet rs) + throws java.sql.SQLException { + return new CalendarStore.Resource( + rs.getString("name"), + rs.getString("uid"), + rs.getBytes("content"), + rs.getString("etag"), + rs.getLong("revision"), + rs.getTimestamp("updated_at").toInstant()); + } + + private static String prefix(String path) { + return path.isEmpty() ? "" : path + "/"; + } + + private static int characters(String value) { + return value.codePointCount(0, value.length()); + } + + private static String parent(String path) { + int slash = path.lastIndexOf('/'); + return slash < 0 ? "" : path.substring(0, slash); + } + + private static void checkPath(String path, boolean root) { + if (path == null || (!root && path.isEmpty())) + throw new IllegalArgumentException("Invalid DAV path"); + DavNamespace.binding(path, false); + } + + private static void checkSegment(String name) { + DavNamespace.segment(name); + } + + private static void checkDisplayName(String name) { + if (name == null || name.isBlank() || name.length() > 200 || !XmlCharacters.valid(name)) + throw new IllegalArgumentException("Invalid display name"); + } + + private static void bounded(int count) { + if (count > MAX_ROWS) limit(); + } + + private static void limit() { + throw new StoreException(StoreException.Kind.LIMIT); + } + + private static void missing() { + throw new StoreException(StoreException.Kind.NOT_FOUND); + } + + private static void forbidden() { + throw new StoreException(StoreException.Kind.FORBIDDEN); + } +} diff --git a/src/main/java/org/busystack/caldav/storage/ModificationDates.java b/src/main/java/org/busystack/caldav/storage/ModificationDates.java new file mode 100644 index 0000000..67f7e1a --- /dev/null +++ b/src/main/java/org/busystack/caldav/storage/ModificationDates.java @@ -0,0 +1,14 @@ +package org.busystack.caldav.storage; + +import java.time.Instant; +import java.time.temporal.ChronoUnit; + +/** HTTP dates have whole-second precision and cannot be later than response origination. */ +public final class ModificationDates { + private ModificationDates() {} + + public static Instant effective(Instant modifiedAt, Instant origin) { + if (modifiedAt == null) return null; + return (modifiedAt.isAfter(origin) ? origin : modifiedAt).truncatedTo(ChronoUnit.SECONDS); + } +} diff --git a/src/main/java/org/busystack/caldav/storage/StoreException.java b/src/main/java/org/busystack/caldav/storage/StoreException.java new file mode 100644 index 0000000..f6cf156 --- /dev/null +++ b/src/main/java/org/busystack/caldav/storage/StoreException.java @@ -0,0 +1,51 @@ +package org.busystack.caldav.storage; + +/** Service failure; mapped to protocol-specific errors by the protocol adapter. */ +public class StoreException extends RuntimeException { + public enum Kind { + NOT_FOUND, + FORBIDDEN, + PRECONDITION, + UID_CONFLICT, + INVALID_TOKEN, + LIMIT, + CONFLICT, + EXISTS, + INVALID_PROPERTY + } + + public enum RequiredAccess { + READ, + WRITE, + OWNER + } + + private final Kind kind; + private final RequiredAccess requiredAccess; + private final java.util.UUID collection; + + public StoreException(Kind kind) { + this(kind, null, null); + } + + public StoreException(Kind kind, RequiredAccess requiredAccess, java.util.UUID collection) { + super(kind.name()); + if (requiredAccess != null && (kind != Kind.FORBIDDEN || collection == null)) + throw new IllegalArgumentException("Access failures require a collection"); + this.kind = kind; + this.requiredAccess = requiredAccess; + this.collection = collection; + } + + public Kind kind() { + return kind; + } + + public RequiredAccess requiredAccess() { + return requiredAccess; + } + + public java.util.UUID collection() { + return collection; + } +} diff --git a/src/main/java/org/busystack/caldav/storage/SyncContinuationCleanup.java b/src/main/java/org/busystack/caldav/storage/SyncContinuationCleanup.java new file mode 100644 index 0000000..2ae3f73 --- /dev/null +++ b/src/main/java/org/busystack/caldav/storage/SyncContinuationCleanup.java @@ -0,0 +1,24 @@ +package org.busystack.caldav.storage; + +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Isolation; +import org.springframework.transaction.annotation.Transactional; + +/** Expiry never depends on cleanup; keep maintenance writes outside repeatable-read reports. */ +@Component +public class SyncContinuationCleanup { + private final JdbcTemplate jdbc; + + public SyncContinuationCleanup(JdbcTemplate jdbc) { + this.jdbc = jdbc; + } + + @Scheduled(fixedDelay = 60000, initialDelay = 60000) + @Transactional(isolation = Isolation.READ_COMMITTED) + public void pruneExpired() { + jdbc.update( + "DELETE FROM sync_continuations WHERE id IN (SELECT id FROM sync_continuations WHERE expires_at { + var states = + jdbc.query( + "SELECT c.generation,c.revision,c.min_retained_revision,s.recovery_epoch,a.enabled,(c.owner_id=a.id OR EXISTS(SELECT 1 FROM collection_effective_members m WHERE m.collection_id=c.id AND m.account_id=a.id)) AS permitted FROM collections c CROSS JOIN system_state s CROSS JOIN accounts a WHERE c.id=? AND s.id=1 AND a.id=? FOR KEY SHARE OF c,s,a", + (rs, row) -> + new Current( + rs.getObject(1, UUID.class), + rs.getLong(2), + rs.getLong(3), + rs.getObject(4, UUID.class), + rs.getBoolean(5), + rs.getBoolean(6)), + collection.id(), + actor); + if (states.isEmpty()) throw new StoreException(StoreException.Kind.INVALID_TOKEN); + Current current = states.getFirst(); + if (!current.enabled() || !current.permitted()) + throw new StoreException( + StoreException.Kind.FORBIDDEN, StoreException.RequiredAccess.READ, collection.id()); + if (!current.generation().equals(collection.generation()) + || !current.epoch().equals(epoch) + || current.minimum() > floor + || current.revision() < upper) + throw new StoreException(StoreException.Kind.INVALID_TOKEN); + UUID id = UUID.randomUUID(); + jdbc.update( + "INSERT INTO sync_continuations(id,collection_id,account_id,generation,recovery_epoch,lower_revision,upper_revision,retention_floor,initial_sync,last_name) VALUES (?,?,?,?,?,?,?,?,?,?)", + id, + collection.id(), + actor, + collection.generation(), + epoch, + lower, + upper, + floor, + initial, + lastName); + return id; + }); + } +} diff --git a/src/main/java/org/busystack/caldav/storage/WriteCondition.java b/src/main/java/org/busystack/caldav/storage/WriteCondition.java new file mode 100644 index 0000000..718224b --- /dev/null +++ b/src/main/java/org/busystack/caldav/storage/WriteCondition.java @@ -0,0 +1,72 @@ +package org.busystack.caldav.storage; + +import java.time.Instant; +import java.util.Set; + +/** HTTP conditions after protocol parsing; evaluation occurs under the mutation lock. */ +public record WriteCondition( + boolean createOnly, + boolean mustExist, + Set matches, + Set excluded, + Instant unmodifiedSince) { + public WriteCondition { + matches = Set.copyOf(matches); + excluded = Set.copyOf(excluded); + if (unmodifiedSince != null) + unmodifiedSince = unmodifiedSince.truncatedTo(java.time.temporal.ChronoUnit.SECONDS); + } + + public WriteCondition( + boolean createOnly, boolean mustExist, Set matches, Set excluded) { + this(createOnly, mustExist, matches, excluded, null); + } + + public WriteCondition(boolean createOnly, boolean mustExist, Set matches) { + this(createOnly, mustExist, matches, Set.of()); + } + + public static WriteCondition none() { + return new WriteCondition(false, false, Set.of()); + } + + public static WriteCondition create() { + return new WriteCondition(true, false, Set.of()); + } + + public static WriteCondition match(String etag) { + return new WriteCondition(false, true, Set.of(etag)); + } + + public void check(String existing) { + check(existing, existing != null); + } + + public void check(String existing, boolean exists) { + check(existing, exists, null); + } + + public void check(String existing, boolean exists, Instant modifiedAt) { + // Evaluate after the caller's locks. Capturing the clock before a lock wait could + // clamp a newly committed mutation to the earlier request's starting time. + Instant effective = ModificationDates.effective(modifiedAt, Instant.now()); + if ((createOnly && exists) + || (mustExist && !exists) + || (!matches.isEmpty() + && (existing == null + || matches.stream() + .noneMatch(tag -> !tag.startsWith("W/") && tag.equals(existing)))) + || (exists + && unmodifiedSince != null + && effective != null + && effective.isAfter(unmodifiedSince)) + || (existing != null + && excluded.stream().anyMatch(tag -> weak(tag).equals(weak(existing))))) { + throw new StoreException(StoreException.Kind.PRECONDITION); + } + } + + private static String weak(String tag) { + return tag.startsWith("W/") ? tag.substring(2) : tag; + } +} diff --git a/src/main/java/org/busystack/caldav/xml/XmlCharacters.java b/src/main/java/org/busystack/caldav/xml/XmlCharacters.java new file mode 100644 index 0000000..61b6010 --- /dev/null +++ b/src/main/java/org/busystack/caldav/xml/XmlCharacters.java @@ -0,0 +1,20 @@ +package org.busystack.caldav.xml; + +/** XML 1.0 section 2.2 character data, independent of application naming policies. */ +public final class XmlCharacters { + private XmlCharacters() {} + + public static boolean valid(String value) { + return value != null + && value + .codePoints() + .allMatch( + codePoint -> + codePoint == 0x9 + || codePoint == 0xA + || codePoint == 0xD + || (codePoint >= 0x20 && codePoint <= 0xD7FF) + || (codePoint >= 0xE000 && codePoint <= 0xFFFD) + || (codePoint >= 0x10000 && codePoint <= 0x10FFFF)); + } +} diff --git a/src/main/resources/META-INF/licenses/ical4j-parser-BSD-3-Clause.txt b/src/main/resources/META-INF/licenses/ical4j-parser-BSD-3-Clause.txt new file mode 100644 index 0000000..cddf29a --- /dev/null +++ b/src/main/resources/META-INF/licenses/ical4j-parser-BSD-3-Clause.txt @@ -0,0 +1,30 @@ +Copyright (c) 2012, Ben Fortuna +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + o Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + + o Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. + + o Neither the name of Ben Fortuna nor the names of any other contributors +may be used to endorse or promote products derived from this software +without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +/ diff --git a/src/main/resources/META-INF/licenses/spring-derived-schemas.txt b/src/main/resources/META-INF/licenses/spring-derived-schemas.txt new file mode 100644 index 0000000..5f9baf3 --- /dev/null +++ b/src/main/resources/META-INF/licenses/spring-derived-schemas.txt @@ -0,0 +1,26 @@ +Spring-derived JDBC schemas (Apache License 2.0) + +Source artifacts remain unchanged and include their Apache license: +org.springframework.security:spring-security-oauth2-authorization-server:7.1.2 +org.springframework.security:spring-security-webauthn:7.1.2 +org.springframework.session:spring-session-jdbc:4.1.1 + +V3__spring_oauth_authorization.sql adapts the upstream registered-client, +authorization and consent SQL schemas. BusyStack changed PostgreSQL blob/text +and timestamp/timestamptz types as instructed by the upstream schema, and adds +application-owned persistent refresh families and their constraints. + +V4__persistent_passkeys.sql adapts the column contract consumed by the Spring +Security JdbcUserCredentialRepository. BusyStack adds stable-account ownership, +foreign keys, UV/counter constraints and application metadata. + +V6__sessions_recovery_protection.sql includes the upstream Spring Session +org/springframework/session/jdbc/schema-postgresql.sql table/index structure, +then adds application-owned recovery, ceremony, throttling and security state. +The original SQL entry does not carry a per-file copyright header; the original +artifact's Apache license is retained. Published Spring Session 4.1.1 POM +metadata was confirmed erroneous by its maintainer in issue #3910. + +These are notices identifying the adaptations without changing the checksums +of already-applied Flyway migrations V1–V9. The original schemas can be read +from the unmodified nested dependency artifacts and their published sources. diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml new file mode 100644 index 0000000..daccb1e --- /dev/null +++ b/src/main/resources/application.yaml @@ -0,0 +1,68 @@ +busystack: + dav: + allow-sync-depth-one: true +spring: + session: + timeout: 30m + jdbc: + initialize-schema: never + autoconfigure: + exclude: org.springframework.boot.security.oauth2.server.authorization.autoconfigure.servlet.OAuth2AuthorizationServerJwtAutoConfiguration + application: + name: busystack-caldav + datasource: + hikari: + maximum-pool-size: 10 + connection-timeout: 10000 + flyway: + validate-on-migrate: true + clean-disabled: true + mvc: + log-request-details: false + dispatch-options-request: true +server: + ssl: + enabled: false + enabled-protocols: TLSv1.2,TLSv1.3 + ciphers: + - TLS_AES_128_GCM_SHA256 + - TLS_AES_256_GCM_SHA384 + - TLS_CHACHA20_POLY1305_SHA256 + - TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 + - TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 + - TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 + - TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 + - TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 + - TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 + compression: + enabled: false + address: 127.0.0.1 + forward-headers-strategy: none + error: + include-message: never + include-stacktrace: never + servlet: + session: + timeout: 30m + cookie: + name: JSESSIONID + secure: true + http-only: true + same-site: lax + tomcat: + max-http-form-post-size: 64KB + max-swallow-size: 1MB + connection-timeout: 10s +management: + endpoints: + web: + exposure: + include: health + endpoint: + health: + show-details: never + probes: + enabled: true + group: + readiness: + include: readinessState,db diff --git a/src/main/resources/db/migration/V10__resumable_sync_history.sql b/src/main/resources/db/migration/V10__resumable_sync_history.sql new file mode 100644 index 0000000..9c101be --- /dev/null +++ b/src/main/resources/db/migration/V10__resumable_sync_history.sql @@ -0,0 +1,104 @@ +-- Immutable representations let separate HTTP pages share a revision watermark without +-- holding a PostgreSQL transaction open between requests. Revisions remain collection-locked. +CREATE TABLE calendar_resource_versions ( + collection_id uuid NOT NULL REFERENCES collections(id) ON DELETE CASCADE, + name text COLLATE "C" NOT NULL, + revision bigint NOT NULL CHECK (revision > 0), + deleted boolean NOT NULL, + uid text, + content bytea, + content_length integer GENERATED ALWAYS AS (octet_length(content)) STORED, + etag text, + updated_at timestamptz, + PRIMARY KEY (collection_id,name,revision), + CHECK ((deleted AND uid IS NULL AND content IS NULL AND etag IS NULL AND updated_at IS NULL) + OR (NOT deleted AND uid IS NOT NULL AND content IS NOT NULL AND etag IS NOT NULL AND updated_at IS NOT NULL)) +); +CREATE INDEX calendar_resource_versions_revision ON calendar_resource_versions(collection_id,revision); +-- Mixed ordering matches DISTINCT ON/keyset traversal and lets PostgreSQL stop at the page +-- boundary instead of sorting the remaining collection. Bodies stay out of this index. +CREATE INDEX calendar_resource_versions_page ON calendar_resource_versions(collection_id,name,revision DESC) INCLUDE(deleted,etag,updated_at,content_length); + +-- Keep historical property values in separate indexed rows. Propname never reads XML, +-- and selecting one property never detoasts the other properties of the resource version. +CREATE TABLE calendar_resource_version_properties ( + collection_id uuid NOT NULL, + resource_name text COLLATE "C" NOT NULL, + revision bigint NOT NULL, + namespace text NOT NULL CHECK(length(namespace)<=1024), + property_name text NOT NULL CHECK(length(property_name) BETWEEN 1 AND 200), + xml text NOT NULL CHECK(octet_length(xml)<=65536), + PRIMARY KEY(collection_id,resource_name,revision,namespace,property_name), + FOREIGN KEY(collection_id,resource_name,revision) + REFERENCES calendar_resource_versions(collection_id,name,revision) ON DELETE CASCADE +); + +INSERT INTO calendar_resource_versions(collection_id,name,revision,deleted,uid,content,etag,updated_at) +SELECT collection_id,name,revision,false,uid,content,etag,updated_at FROM calendar_resources; +INSERT INTO calendar_resource_version_properties(collection_id,resource_name,revision,namespace,property_name,xml) +SELECT r.collection_id,r.name,r.revision,p.namespace,p.name,p.xml FROM calendar_resources r +JOIN dav_resource_properties p ON p.collection_id=r.collection_id AND p.resource_name=r.name; +-- Pre-upgrade stable tokens remain usable; old deletions do not have current rows to backfill. +INSERT INTO calendar_resource_versions(collection_id,name,revision,deleted) +SELECT collection_id,name,revision,true FROM collection_changes WHERE deleted +ON CONFLICT DO NOTHING; + +CREATE FUNCTION capture_calendar_resource_version() RETURNS trigger LANGUAGE plpgsql AS $$ +BEGIN + IF TG_OP='INSERT' OR OLD.revision IS DISTINCT FROM NEW.revision + OR OLD.collection_id IS DISTINCT FROM NEW.collection_id OR OLD.name IS DISTINCT FROM NEW.name THEN + INSERT INTO calendar_resource_versions(collection_id,name,revision,deleted,uid,content,etag,updated_at) + VALUES(NEW.collection_id,NEW.name,NEW.revision,false,NEW.uid,NEW.content,NEW.etag,NEW.updated_at); + INSERT INTO calendar_resource_version_properties(collection_id,resource_name,revision,namespace,property_name,xml) + SELECT NEW.collection_id,NEW.name,NEW.revision,p.namespace,p.name,p.xml + FROM dav_resource_properties p WHERE p.collection_id=NEW.collection_id AND p.resource_name=NEW.name; + END IF; + RETURN NEW; +END $$; +CREATE TRIGGER calendar_resource_version_write AFTER INSERT OR UPDATE ON calendar_resources +FOR EACH ROW EXECUTE FUNCTION capture_calendar_resource_version(); + +CREATE FUNCTION capture_calendar_resource_tombstone() RETURNS trigger LANGUAGE plpgsql AS $$ +BEGIN + IF NEW.deleted THEN + INSERT INTO calendar_resource_versions(collection_id,name,revision,deleted) + VALUES(NEW.collection_id,NEW.name,NEW.revision,true); + END IF; + RETURN NEW; +END $$; +CREATE TRIGGER calendar_resource_version_delete AFTER INSERT ON collection_changes +FOR EACH ROW EXECUTE FUNCTION capture_calendar_resource_tombstone(); + +-- COPY writes properties after inserting the resource. At commit, fill only an empty final +-- current version; a later PROPPATCH must never overwrite a previous version's properties. +CREATE FUNCTION capture_calendar_resource_version_properties() RETURNS trigger LANGUAGE plpgsql AS $$ +DECLARE target_collection uuid; target_name text; +BEGIN + IF TG_OP='DELETE' THEN target_collection:=OLD.collection_id; target_name:=OLD.resource_name; + ELSE target_collection:=NEW.collection_id; target_name:=NEW.resource_name; END IF; + INSERT INTO calendar_resource_version_properties(collection_id,resource_name,revision,namespace,property_name,xml) + SELECT r.collection_id,r.name,r.revision,p.namespace,p.name,p.xml FROM calendar_resources r + JOIN dav_resource_properties p ON p.collection_id=r.collection_id AND p.resource_name=r.name + WHERE r.collection_id=target_collection AND r.name=target_name + AND NOT EXISTS(SELECT 1 FROM calendar_resource_version_properties v + WHERE v.collection_id=r.collection_id AND v.resource_name=r.name AND v.revision=r.revision) + ON CONFLICT DO NOTHING; + RETURN NULL; +END $$; +CREATE CONSTRAINT TRIGGER calendar_resource_version_property_write AFTER INSERT OR UPDATE OR DELETE ON dav_resource_properties +DEFERRABLE INITIALLY DEFERRED FOR EACH ROW EXECUTE FUNCTION capture_calendar_resource_version_properties(); + +CREATE TABLE sync_continuations ( + id uuid PRIMARY KEY, + collection_id uuid NOT NULL REFERENCES collections(id) ON DELETE CASCADE, + account_id uuid NOT NULL REFERENCES accounts(id) ON DELETE CASCADE, + generation uuid NOT NULL, + recovery_epoch uuid NOT NULL, + lower_revision bigint NOT NULL CHECK(lower_revision>=0), + upper_revision bigint NOT NULL CHECK(upper_revision>=lower_revision), + retention_floor bigint NOT NULL CHECK(retention_floor>=0), + initial_sync boolean NOT NULL, + last_name text COLLATE "C" NOT NULL, + expires_at timestamptz NOT NULL DEFAULT (now()+interval '24 hours') +); +CREATE INDEX sync_continuations_expiry ON sync_continuations(expires_at); diff --git a/src/main/resources/db/migration/V11__property_storage_budgets.sql b/src/main/resources/db/migration/V11__property_storage_budgets.sql new file mode 100644 index 0000000..8797f9c --- /dev/null +++ b/src/main/resources/db/migration/V11__property_storage_budgets.sql @@ -0,0 +1,48 @@ +-- Preserve existing values. Oversized legacy targets may be reduced or removed; no new +-- target may grow beyond 1 MiB / 1,000 values. A no-op parent UPDATE serializes +-- READ COMMITTED writers and forces stale REPEATABLE READ writers to abort (40001). +-- These columns deliberately do not fire indexed display-name/property-removal triggers. +CREATE FUNCTION dav_property_storage_budget() RETURNS trigger LANGUAGE plpgsql AS $$ +DECLARE + previous_bytes bigint; + previous_count bigint; + resulting_bytes bigint; + resulting_count bigint; +BEGIN + IF TG_TABLE_NAME='dav_binding_properties' THEN + UPDATE dav_bindings SET home_id=home_id WHERE home_id=NEW.home_id AND path=NEW.path; + SELECT COALESCE(sum(octet_length(xml)),0),count(*) INTO previous_bytes,previous_count + FROM dav_binding_properties WHERE home_id=NEW.home_id AND path=NEW.path; + SELECT COALESCE(sum(octet_length(xml)),0),count(*) INTO resulting_bytes,resulting_count + FROM dav_binding_properties WHERE home_id=NEW.home_id AND path=NEW.path + AND NOT(namespace=NEW.namespace AND name=NEW.name); + ELSIF TG_TABLE_NAME='dav_collection_properties' THEN + UPDATE collections SET revision=revision WHERE id=NEW.collection_id; + SELECT COALESCE(sum(octet_length(xml)),0),count(*) INTO previous_bytes,previous_count + FROM dav_collection_properties WHERE collection_id=NEW.collection_id; + SELECT COALESCE(sum(octet_length(xml)),0),count(*) INTO resulting_bytes,resulting_count + FROM dav_collection_properties WHERE collection_id=NEW.collection_id + AND NOT(namespace=NEW.namespace AND name=NEW.name); + ELSE + UPDATE collections SET revision=revision WHERE id=NEW.collection_id; + SELECT COALESCE(sum(octet_length(xml)),0),count(*) INTO previous_bytes,previous_count + FROM dav_resource_properties WHERE collection_id=NEW.collection_id AND resource_name=NEW.resource_name; + SELECT COALESCE(sum(octet_length(xml)),0),count(*) INTO resulting_bytes,resulting_count + FROM dav_resource_properties WHERE collection_id=NEW.collection_id AND resource_name=NEW.resource_name + AND NOT(namespace=NEW.namespace AND name=NEW.name); + END IF; + resulting_bytes:=resulting_bytes+octet_length(NEW.xml); + resulting_count:=resulting_count+1; + IF (resulting_bytes>1048576 AND resulting_bytes>previous_bytes) + OR (resulting_count>1000 AND resulting_count>previous_count) THEN + RAISE EXCEPTION 'property_storage_budget exceeded' USING ERRCODE='23514',CONSTRAINT='property_storage_budget'; + END IF; + RETURN NEW; +END $$; + +CREATE TRIGGER dav_binding_property_budget BEFORE INSERT OR UPDATE OF xml ON dav_binding_properties +FOR EACH ROW EXECUTE FUNCTION dav_property_storage_budget(); +CREATE TRIGGER dav_collection_property_budget BEFORE INSERT OR UPDATE OF xml ON dav_collection_properties +FOR EACH ROW EXECUTE FUNCTION dav_property_storage_budget(); +CREATE TRIGGER dav_resource_property_budget BEFORE INSERT OR UPDATE OF xml ON dav_resource_properties +FOR EACH ROW EXECUTE FUNCTION dav_property_storage_budget(); diff --git a/src/main/resources/db/migration/V12__namespace_bounds.sql b/src/main/resources/db/migration/V12__namespace_bounds.sql new file mode 100644 index 0000000..476bcde --- /dev/null +++ b/src/main/resources/db/migration/V12__namespace_bounds.sql @@ -0,0 +1,79 @@ +-- These bounds mirror DavNamespace. Deployment prefixes are validated separately. +CREATE FUNCTION dav_uri_encoded_length(value text) RETURNS integer +LANGUAGE plpgsql IMMUTABLE STRICT AS $$ +DECLARE data bytea:=convert_to(value,'UTF8'); total integer:=0; code integer; i integer; +BEGIN + FOR i IN 0..octet_length(data)-1 LOOP + code:=get_byte(data,i); + total:=total+CASE WHEN code BETWEEN 65 AND 90 OR code BETWEEN 97 AND 122 + OR code BETWEEN 48 AND 57 OR code IN (45,46,95,126) THEN 1 ELSE 3 END; + END LOOP; + RETURN total; +END $$; + +CREATE FUNCTION dav_namespace_valid(path text, calendar boolean, leaf text DEFAULT NULL) +RETURNS boolean LANGUAGE plpgsql IMMUTABLE STRICT AS $$ +DECLARE segments text[]:=string_to_array(path,'/'); part text; units integer; cp integer; + total integer:=46; count integer:=2; i integer; +BEGIN + IF path IS NULL OR calendar IS NULL THEN RETURN false; END IF; + -- A single ASCII unreserved segment has exactly one encoded byte / UTF-16 unit. + -- At most four fixed/path/leaf segments and 2,598 encoded bytes (including the + -- worst permitted future calendar leaf) are possible here. Preserve all limits + -- while avoiding character-by-character work for ordinary UUID share bindings. + IF length(path)<=255 AND path COLLATE "C" ~ '^[A-Za-z0-9._~-]+$' AND path NOT IN ('.','..') + AND (leaf IS NULL OR (length(leaf)<=255 AND leaf COLLATE "C" ~ '^[A-Za-z0-9._~-]+$' AND leaf NOT IN ('.','..'))) THEN + RETURN true; + END IF; + IF leaf IS NOT NULL THEN segments:=array_append(segments,leaf); END IF; + FOREACH part IN ARRAY segments LOOP + IF part='' OR part IN ('.','..') OR position('/' IN part)>0 OR position(';' IN part)>0 OR part ~ '[\\[:cntrl:]]' + OR translate(part,U&'\0020\1680\2000\2001\2002\2003\2004\2005\2006\2008\2009\200A\2028\2029\205F\3000','')='' + THEN RETURN false; END IF; + units:=0; + FOR i IN 1..length(part) LOOP + cp:=ascii(substring(part FROM i FOR 1)); + IF cp BETWEEN 0 AND 31 OR cp BETWEEN 127 AND 159 OR cp IN (8232,8233,65534,65535) THEN RETURN false; END IF; + units:=units+CASE WHEN cp>65535 THEN 2 ELSE 1 END; + END LOOP; + IF units>255 THEN RETURN false; END IF; + count:=count+1; total:=total+dav_uri_encoded_length(part)+1; + END LOOP; + IF leaf IS NULL AND calendar THEN + -- Reserve a complete ordinary resource leaf, including the worst permitted encoding. + count:=count+1; total:=total+2296; + END IF; + RETURN count<=20 AND total<=4096; +END $$; + +-- STRICT cannot be used with the optional NULL leaf argument. +ALTER FUNCTION dav_namespace_valid(text,boolean,text) CALLED ON NULL INPUT; + +CREATE FUNCTION dav_binding_namespace_guard() RETURNS trigger LANGUAGE plpgsql AS $$ +BEGIN + IF NOT dav_namespace_valid(NEW.path,NEW.collection_id IS NOT NULL,NULL) THEN + RAISE EXCEPTION 'DAV namespace bound exceeded' USING ERRCODE='23514'; + END IF; + IF NEW.collection_id IS NOT NULL AND EXISTS( + SELECT 1 FROM calendar_resources r WHERE r.collection_id=NEW.collection_id + AND NOT dav_namespace_valid(NEW.path,false,r.name)) THEN + RAISE EXCEPTION 'DAV descendant namespace bound exceeded' USING ERRCODE='23514'; + END IF; + RETURN NEW; +END $$; +CREATE TRIGGER dav_binding_namespace_guard BEFORE INSERT OR UPDATE OF path,collection_id ON dav_bindings +FOR EACH ROW EXECUTE FUNCTION dav_binding_namespace_guard(); + +CREATE FUNCTION dav_resource_namespace_guard() RETURNS trigger LANGUAGE plpgsql AS $$ +BEGIN + IF NOT dav_namespace_valid(NEW.collection_id::text,false,NEW.name) THEN + RAISE EXCEPTION 'DAV resource segment bound exceeded' USING ERRCODE='23514'; + END IF; + IF EXISTS(SELECT 1 FROM dav_bindings b WHERE b.collection_id=NEW.collection_id + AND NOT dav_namespace_valid(b.path,false,NEW.name)) THEN + RAISE EXCEPTION 'DAV resource namespace bound exceeded' USING ERRCODE='23514'; + END IF; + RETURN NEW; +END $$; +CREATE TRIGGER dav_resource_namespace_guard BEFORE INSERT OR UPDATE OF collection_id,name ON calendar_resources +FOR EACH ROW EXECUTE FUNCTION dav_resource_namespace_guard(); diff --git a/src/main/resources/db/migration/V13__group_reconciliation.sql b/src/main/resources/db/migration/V13__group_reconciliation.sql new file mode 100644 index 0000000..158c979 --- /dev/null +++ b/src/main/resources/db/migration/V13__group_reconciliation.sql @@ -0,0 +1,72 @@ +-- GroupService holds the exclusive DAV mutation lock and affected collection locks in UUID +-- order. Materialize the affected/effective sets once per statement: a correlated predicate +-- over the grouped effective-membership view can otherwise aggregate that view per binding. +-- Group maintenance and namespace preflight start with collection IDs. The existing +-- home-first uniqueness index cannot serve that access path under generic query plans. +CREATE INDEX dav_bindings_collection_home ON dav_bindings(collection_id,home_id) + WHERE collection_id IS NOT NULL; + +CREATE FUNCTION dav_reconcile_group_memberships(target_group uuid) RETURNS void +LANGUAGE plpgsql AS $$ +BEGIN + WITH affected AS MATERIALIZED ( + SELECT c.id,c.owner_id FROM collections c + JOIN collection_group_members grant_row ON grant_row.collection_id=c.id + WHERE grant_row.group_id=target_group + ), effective AS MATERIALIZED ( + -- This is the membership-key projection of collection_effective_members. Read/write + -- precedence remains in that view; binding existence needs only these distinct keys. + SELECT direct.collection_id,direct.account_id FROM affected a + JOIN collection_members direct ON direct.collection_id=a.id + WHERE direct.account_id<>a.owner_id + UNION + SELECT granted.collection_id,member.account_id FROM affected a + JOIN collection_group_members granted ON granted.collection_id=a.id + JOIN sharing_group_members member ON member.group_id=granted.group_id + WHERE member.account_id<>a.owner_id + ), missing AS MATERIALIZED ( + SELECT e.collection_id,e.account_id FROM effective e + EXCEPT + SELECT b.collection_id,b.home_id FROM dav_bindings b JOIN affected a ON a.id=b.collection_id + ) + INSERT INTO dav_bindings(home_id,path,collection_id,display_name) + SELECT m.account_id, + CASE WHEN occupied.home_id IS NOT NULL + THEN c.id::text||'-'||gen_random_uuid()::text ELSE c.id::text END, + c.id,c.name + FROM missing m JOIN collections c ON c.id=m.collection_id + LEFT JOIN dav_bindings occupied + ON occupied.home_id=m.account_id AND occupied.path=c.id::text; + + WITH affected AS MATERIALIZED ( + SELECT c.id,c.owner_id FROM collections c + JOIN collection_group_members grant_row ON grant_row.collection_id=c.id + WHERE grant_row.group_id=target_group + ), effective AS MATERIALIZED ( + -- This is the membership-key projection of collection_effective_members. Read/write + -- precedence remains in that view; binding existence needs only these distinct keys. + SELECT direct.collection_id,direct.account_id FROM affected a + JOIN collection_members direct ON direct.collection_id=a.id + WHERE direct.account_id<>a.owner_id + UNION + SELECT granted.collection_id,member.account_id FROM affected a + JOIN collection_group_members granted ON granted.collection_id=a.id + JOIN sharing_group_members member ON member.group_id=granted.group_id + WHERE member.account_id<>a.owner_id + ), removed AS MATERIALIZED ( + -- Both membership keys are NOT NULL in the underlying grant tables. The uncorrelated + -- NOT IN uses one membership hash; carrying the complete primary key avoids a stale + -- generic plan scanning every binding in a home for each collection being removed. + SELECT b.home_id,b.path FROM dav_bindings b JOIN affected a ON a.id=b.collection_id + WHERE b.home_id<>a.owner_id + AND (b.collection_id,b.home_id) NOT IN ( + SELECT e.collection_id,e.account_id FROM effective e + ) + ) + DELETE FROM dav_bindings b USING removed r + WHERE b.home_id=r.home_id AND b.path=r.path; + + UPDATE collections c SET generation=gen_random_uuid() + FROM collection_group_members affected + WHERE affected.group_id=target_group AND affected.collection_id=c.id; +END $$; diff --git a/src/main/resources/db/migration/V1__durable_core.sql b/src/main/resources/db/migration/V1__durable_core.sql new file mode 100644 index 0000000..59f11cd --- /dev/null +++ b/src/main/resources/db/migration/V1__durable_core.sql @@ -0,0 +1,71 @@ +CREATE TABLE system_state ( + id integer PRIMARY KEY CHECK (id = 1), + recovery_epoch uuid NOT NULL DEFAULT gen_random_uuid() +); +INSERT INTO system_state(id) VALUES (1); + +CREATE TABLE accounts ( + id uuid PRIMARY KEY, + username text NOT NULL UNIQUE CHECK (username ~ '^[a-zA-Z0-9][a-zA-Z0-9._-]{0,63}$'), + display_name text NOT NULL CHECK (length(display_name) BETWEEN 1 AND 200), + password_hash text, + administrator boolean NOT NULL DEFAULT false, + enabled boolean NOT NULL DEFAULT true, + created_at timestamptz NOT NULL DEFAULT now() +); + +CREATE TABLE dav_credentials ( + id uuid PRIMARY KEY, + account_id uuid NOT NULL REFERENCES accounts(id) ON DELETE CASCADE, + label text NOT NULL CHECK (length(label) BETWEEN 1 AND 100), + verifier text NOT NULL, + created_at timestamptz NOT NULL DEFAULT now(), + expires_at timestamptz NOT NULL, + revoked_at timestamptz, + CHECK (expires_at > created_at) +); +CREATE INDEX dav_credentials_account ON dav_credentials(account_id); + +CREATE TABLE collections ( + id uuid PRIMARY KEY, + owner_id uuid NOT NULL REFERENCES accounts(id), + name text NOT NULL CHECK (length(name) BETWEEN 1 AND 200), + component text NOT NULL CHECK (component IN ('VEVENT', 'VTODO')), + generation uuid NOT NULL DEFAULT gen_random_uuid(), + revision bigint NOT NULL DEFAULT 0 CHECK (revision >= 0), + min_retained_revision bigint NOT NULL DEFAULT 0 CHECK (min_retained_revision >= 0 AND min_retained_revision <= revision), + created_at timestamptz NOT NULL DEFAULT now() +); +CREATE INDEX collections_owner ON collections(owner_id); + +CREATE TABLE collection_members ( + collection_id uuid NOT NULL REFERENCES collections(id) ON DELETE CASCADE, + account_id uuid NOT NULL REFERENCES accounts(id) ON DELETE CASCADE, + permission text NOT NULL CHECK (permission IN ('READ', 'WRITE')), + PRIMARY KEY (collection_id, account_id) +); +CREATE INDEX collection_members_account ON collection_members(account_id); + +CREATE TABLE calendar_resources ( + collection_id uuid NOT NULL REFERENCES collections(id) ON DELETE CASCADE, + name text NOT NULL CHECK (length(name) BETWEEN 1 AND 255 AND name NOT IN ('.', '..') AND name !~ '[/\\[:cntrl:]]'), + uid text NOT NULL CHECK (length(uid) BETWEEN 1 AND 1024), + content bytea NOT NULL CHECK (octet_length(content) BETWEEN 1 AND 1048576), + etag text NOT NULL, + revision bigint NOT NULL CHECK (revision > 0), + updated_at timestamptz NOT NULL DEFAULT now(), + PRIMARY KEY (collection_id, name), + UNIQUE (collection_id, uid) +); + +-- The collection row lock serializes revision allocation and remains held through commit. +-- A sequence would allow a late-committing mutation to disappear behind a delivered token. +CREATE TABLE collection_changes ( + collection_id uuid NOT NULL REFERENCES collections(id) ON DELETE CASCADE, + revision bigint NOT NULL CHECK (revision > 0), + name text NOT NULL, + deleted boolean NOT NULL, + changed_at timestamptz NOT NULL DEFAULT now(), + PRIMARY KEY (collection_id, revision) +); +CREATE INDEX collection_changes_resource ON collection_changes(collection_id, name, revision); diff --git a/src/main/resources/db/migration/V2__oauth_refresh_families.sql b/src/main/resources/db/migration/V2__oauth_refresh_families.sql new file mode 100644 index 0000000..ecc249e --- /dev/null +++ b/src/main/resources/db/migration/V2__oauth_refresh_families.sql @@ -0,0 +1,21 @@ +-- Spent token verifiers remain available for replay detection for the family's lifetime. +-- These are SHA-256 digests of framework-generated high-entropy bearer tokens, never passwords. +CREATE TABLE oauth_refresh_families ( + authorization_id text PRIMARY KEY CHECK (length(authorization_id) BETWEEN 1 AND 200), + account_id uuid NOT NULL REFERENCES accounts(id) ON DELETE CASCADE, + client_id text NOT NULL CHECK (length(client_id) BETWEEN 1 AND 200), + recovery_epoch uuid NOT NULL, + created_at timestamptz NOT NULL DEFAULT now(), + expires_at timestamptz NOT NULL, + revoked_at timestamptz, + CHECK (expires_at > created_at) +); +CREATE INDEX oauth_refresh_families_account ON oauth_refresh_families(account_id); + +CREATE TABLE oauth_refresh_tokens ( + token_hash char(64) PRIMARY KEY CHECK (token_hash ~ '^[0-9a-f]{64}$'), + authorization_id text NOT NULL REFERENCES oauth_refresh_families(authorization_id) ON DELETE CASCADE, + expires_at timestamptz NOT NULL, + consumed_at timestamptz +); +CREATE INDEX oauth_refresh_tokens_family ON oauth_refresh_tokens(authorization_id); diff --git a/src/main/resources/db/migration/V3__spring_oauth_authorization.sql b/src/main/resources/db/migration/V3__spring_oauth_authorization.sql new file mode 100644 index 0000000..9eb542d --- /dev/null +++ b/src/main/resources/db/migration/V3__spring_oauth_authorization.sql @@ -0,0 +1,69 @@ +-- Adapted from Spring Security 7.1.2 JDBC schemas (Apache-2.0). +-- PostgreSQL text/timestamptz substitutions follow the upstream schema instructions. + +CREATE TABLE oauth2_registered_client ( + id varchar(100) NOT NULL, + client_id varchar(100) NOT NULL, + client_id_issued_at timestamptz DEFAULT CURRENT_TIMESTAMP NOT NULL, + client_secret varchar(200) DEFAULT NULL, + client_secret_expires_at timestamptz DEFAULT NULL, + client_name varchar(200) NOT NULL, + client_authentication_methods varchar(1000) NOT NULL, + authorization_grant_types varchar(1000) NOT NULL, + redirect_uris varchar(1000) DEFAULT NULL, + post_logout_redirect_uris varchar(1000) DEFAULT NULL, + scopes varchar(1000) NOT NULL, + client_settings varchar(2000) NOT NULL, + token_settings varchar(2000) NOT NULL, + PRIMARY KEY (id) +); + +CREATE TABLE oauth2_authorization ( + id varchar(100) NOT NULL, + registered_client_id varchar(100) NOT NULL, + principal_name varchar(200) NOT NULL, + authorization_grant_type varchar(100) NOT NULL, + authorized_scopes varchar(1000) DEFAULT NULL, + attributes text DEFAULT NULL, + state varchar(500) DEFAULT NULL, + authorization_code_value text DEFAULT NULL, + authorization_code_issued_at timestamptz DEFAULT NULL, + authorization_code_expires_at timestamptz DEFAULT NULL, + authorization_code_metadata text DEFAULT NULL, + access_token_value text DEFAULT NULL, + access_token_issued_at timestamptz DEFAULT NULL, + access_token_expires_at timestamptz DEFAULT NULL, + access_token_metadata text DEFAULT NULL, + access_token_type varchar(100) DEFAULT NULL, + access_token_scopes varchar(1000) DEFAULT NULL, + oidc_id_token_value text DEFAULT NULL, + oidc_id_token_issued_at timestamptz DEFAULT NULL, + oidc_id_token_expires_at timestamptz DEFAULT NULL, + oidc_id_token_metadata text DEFAULT NULL, + refresh_token_value text DEFAULT NULL, + refresh_token_issued_at timestamptz DEFAULT NULL, + refresh_token_expires_at timestamptz DEFAULT NULL, + refresh_token_metadata text DEFAULT NULL, + user_code_value text DEFAULT NULL, + user_code_issued_at timestamptz DEFAULT NULL, + user_code_expires_at timestamptz DEFAULT NULL, + user_code_metadata text DEFAULT NULL, + device_code_value text DEFAULT NULL, + device_code_issued_at timestamptz DEFAULT NULL, + device_code_expires_at timestamptz DEFAULT NULL, + device_code_metadata text DEFAULT NULL, + PRIMARY KEY (id) +); + +CREATE TABLE oauth2_authorization_consent ( + registered_client_id varchar(100) NOT NULL, + principal_name varchar(200) NOT NULL, + authorities varchar(1000) NOT NULL, + PRIMARY KEY (registered_client_id, principal_name) +); + +CREATE UNIQUE INDEX oauth2_registered_client_client_id ON oauth2_registered_client(client_id); +CREATE INDEX oauth2_authorization_principal ON oauth2_authorization(principal_name); +CREATE UNIQUE INDEX oauth2_authorization_code_value ON oauth2_authorization(authorization_code_value) WHERE authorization_code_value IS NOT NULL; +CREATE UNIQUE INDEX oauth2_authorization_access_token ON oauth2_authorization(access_token_value) WHERE access_token_value IS NOT NULL; +CREATE UNIQUE INDEX oauth2_authorization_refresh_token ON oauth2_authorization(refresh_token_value) WHERE refresh_token_value IS NOT NULL; diff --git a/src/main/resources/db/migration/V4__persistent_passkeys.sql b/src/main/resources/db/migration/V4__persistent_passkeys.sql new file mode 100644 index 0000000..3b3f3bc --- /dev/null +++ b/src/main/resources/db/migration/V4__persistent_passkeys.sql @@ -0,0 +1,30 @@ +-- Columns consumed by Spring Security 7.1.2 JdbcUserCredentialRepository; +-- extra foreign keys and ownership metadata keep passkeys tied to stable account IDs. +CREATE TABLE user_entities ( + id varchar(1000) PRIMARY KEY, + account_id uuid NOT NULL UNIQUE REFERENCES accounts(id) ON DELETE CASCADE, + name varchar(200) NOT NULL, + display_name varchar(200) +); +CREATE TABLE user_credentials ( + credential_id varchar(1000) PRIMARY KEY, + user_entity_user_id varchar(1000) NOT NULL REFERENCES user_entities(id) ON DELETE CASCADE, + public_key bytea NOT NULL, + signature_count bigint CHECK (signature_count >= 0), + uv_initialized boolean, + backup_eligible boolean NOT NULL, + authenticator_transports varchar(1000), + public_key_credential_type varchar(100), + backup_state boolean NOT NULL, + attestation_object bytea, + attestation_client_data_json bytea, + created timestamp, + last_used timestamp, + label varchar(1000) NOT NULL +); +CREATE INDEX user_credentials_user ON user_credentials(user_entity_user_id); +-- Tombstones prevent an assertion read before removal from reinserting a deleted key. +CREATE TABLE passkey_revocations ( + credential_id varchar(1000) PRIMARY KEY, + revoked_at timestamptz NOT NULL DEFAULT now() +); diff --git a/src/main/resources/db/migration/V5__upstream_oidc_identities.sql b/src/main/resources/db/migration/V5__upstream_oidc_identities.sql new file mode 100644 index 0000000..a6c9015 --- /dev/null +++ b/src/main/resources/db/migration/V5__upstream_oidc_identities.sql @@ -0,0 +1,9 @@ +CREATE TABLE external_identities ( + issuer text COLLATE "C" NOT NULL CHECK (length(issuer) BETWEEN 1 AND 2048), + subject text COLLATE "C" NOT NULL CHECK (length(subject) BETWEEN 1 AND 255), + account_id uuid NOT NULL UNIQUE REFERENCES accounts(id) ON DELETE CASCADE, + source text NOT NULL CHECK (source IN ('ALLOWLIST','LINKED')), + last_verified_at timestamptz NOT NULL, + linked_at timestamptz NOT NULL DEFAULT now(), + PRIMARY KEY (issuer,subject) +); diff --git a/src/main/resources/db/migration/V6__sessions_recovery_protection.sql b/src/main/resources/db/migration/V6__sessions_recovery_protection.sql new file mode 100644 index 0000000..3f2d4c0 --- /dev/null +++ b/src/main/resources/db/migration/V6__sessions_recovery_protection.sql @@ -0,0 +1,75 @@ +-- Standard Spring Session JDBC 4.1.1 PostgreSQL schema (Apache-2.0), managed by Flyway. +CREATE TABLE SPRING_SESSION ( + PRIMARY_ID CHAR(36) NOT NULL, + SESSION_ID CHAR(36) NOT NULL, + CREATION_TIME BIGINT NOT NULL, + LAST_ACCESS_TIME BIGINT NOT NULL, + MAX_INACTIVE_INTERVAL INT NOT NULL, + EXPIRY_TIME BIGINT NOT NULL, + PRINCIPAL_NAME VARCHAR(100), + CONSTRAINT SPRING_SESSION_PK PRIMARY KEY (PRIMARY_ID) +); +CREATE UNIQUE INDEX SPRING_SESSION_IX1 ON SPRING_SESSION (SESSION_ID); +CREATE INDEX SPRING_SESSION_IX2 ON SPRING_SESSION (EXPIRY_TIME); +CREATE INDEX SPRING_SESSION_IX3 ON SPRING_SESSION (PRINCIPAL_NAME); +CREATE TABLE SPRING_SESSION_ATTRIBUTES ( + SESSION_PRIMARY_ID CHAR(36) NOT NULL, + ATTRIBUTE_NAME VARCHAR(200) NOT NULL, + ATTRIBUTE_BYTES BYTEA NOT NULL, + CONSTRAINT SPRING_SESSION_ATTRIBUTES_PK PRIMARY KEY (SESSION_PRIMARY_ID, ATTRIBUTE_NAME), + CONSTRAINT SPRING_SESSION_ATTRIBUTES_FK FOREIGN KEY (SESSION_PRIMARY_ID) REFERENCES SPRING_SESSION(PRIMARY_ID) ON DELETE CASCADE +); + +-- Captured at credential lookup, so a login already validating an old credential cannot +-- create a valid session after disable/recovery changes this generation. +ALTER TABLE accounts ADD COLUMN security_epoch uuid NOT NULL DEFAULT gen_random_uuid(); +-- Existing grants predate captured account generations and require fresh browser authorization. +UPDATE oauth_refresh_families SET revoked_at=COALESCE(revoked_at,now()); + +CREATE TABLE account_recovery_codes ( + id uuid PRIMARY KEY, + account_id uuid NOT NULL REFERENCES accounts(id) ON DELETE CASCADE, + token_hash char(64) NOT NULL CHECK (token_hash ~ '^[0-9a-f]{64}$'), + recovery_epoch uuid NOT NULL, + created_at timestamptz NOT NULL DEFAULT now(), + expires_at timestamptz NOT NULL, + used_at timestamptz, + revoked_at timestamptz, + CHECK (expires_at > created_at) +); +CREATE INDEX account_recovery_codes_account ON account_recovery_codes(account_id); + +CREATE TABLE webauthn_ceremonies ( + id uuid NOT NULL UNIQUE, + session_id char(36) NOT NULL, + purpose varchar(12) NOT NULL CHECK (purpose IN ('AUTHENTICATE','REGISTER')), + account_id uuid REFERENCES accounts(id) ON DELETE CASCADE, + security_epoch uuid, + recovery_epoch uuid NOT NULL, + challenge_hash char(64) NOT NULL CHECK (challenge_hash ~ '^[0-9a-f]{64}$'), + created_at timestamptz NOT NULL DEFAULT now(), + expires_at timestamptz NOT NULL, + PRIMARY KEY (session_id, purpose), + CHECK ((account_id IS NULL) = (security_epoch IS NULL)), + CHECK (expires_at > created_at) +); +CREATE INDEX webauthn_ceremonies_expiry ON webauthn_ceremonies(expires_at); + +CREATE TABLE login_throttle ( + bucket_key char(64) PRIMARY KEY CHECK (bucket_key ~ '^[0-9a-f]{64}$'), + window_started timestamptz NOT NULL, + attempts integer NOT NULL CHECK (attempts BETWEEN 0 AND 1000), + blocked_until timestamptz, + expires_at timestamptz NOT NULL +); +CREATE INDEX login_throttle_expiry ON login_throttle(expires_at); + +CREATE TABLE security_audit ( + id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, + occurred_at timestamptz NOT NULL DEFAULT now(), + account_id uuid REFERENCES accounts(id) ON DELETE SET NULL, + action varchar(40) NOT NULL, + outcome varchar(12) NOT NULL CHECK (outcome IN ('SUCCESS','FAILURE','THROTTLED')), + peer_hash char(64) CHECK (peer_hash IS NULL OR peer_hash ~ '^[0-9a-f]{64}$') +); +CREATE INDEX security_audit_time ON security_audit(occurred_at); diff --git a/src/main/resources/db/migration/V7__oauth_client_integrity.sql b/src/main/resources/db/migration/V7__oauth_client_integrity.sql new file mode 100644 index 0000000..876b03f --- /dev/null +++ b/src/main/resources/db/migration/V7__oauth_client_integrity.sql @@ -0,0 +1,21 @@ +-- A removed registration cannot leave live authorizations or token families behind. +-- Existing orphan state has no usable registered client and is deliberately invalidated. +DELETE FROM oauth2_authorization a +WHERE NOT EXISTS (SELECT 1 FROM oauth2_registered_client c WHERE c.id=a.registered_client_id); +DELETE FROM oauth2_authorization_consent a +WHERE NOT EXISTS (SELECT 1 FROM oauth2_registered_client c WHERE c.id=a.registered_client_id); +DELETE FROM oauth_refresh_families f +WHERE NOT EXISTS (SELECT 1 FROM oauth2_registered_client c WHERE c.client_id=f.client_id); + +ALTER TABLE oauth2_registered_client + ADD CONSTRAINT oauth2_registered_client_client_id_key + UNIQUE USING INDEX oauth2_registered_client_client_id; +ALTER TABLE oauth2_authorization + ADD CONSTRAINT oauth2_authorization_client_fk + FOREIGN KEY (registered_client_id) REFERENCES oauth2_registered_client(id) ON DELETE CASCADE; +ALTER TABLE oauth2_authorization_consent + ADD CONSTRAINT oauth2_authorization_consent_client_fk + FOREIGN KEY (registered_client_id) REFERENCES oauth2_registered_client(id) ON DELETE CASCADE; +ALTER TABLE oauth_refresh_families + ADD CONSTRAINT oauth_refresh_families_client_fk + FOREIGN KEY (client_id) REFERENCES oauth2_registered_client(client_id) ON DELETE CASCADE; diff --git a/src/main/resources/db/migration/V8__dav_bindings_and_properties.sql b/src/main/resources/db/migration/V8__dav_bindings_and_properties.sql new file mode 100644 index 0000000..81e0f11 --- /dev/null +++ b/src/main/resources/db/migration/V8__dav_bindings_and_properties.sql @@ -0,0 +1,77 @@ +CREATE TABLE dav_bindings ( + home_id uuid NOT NULL REFERENCES accounts(id) ON DELETE CASCADE, + path text NOT NULL CHECK (length(path) BETWEEN 1 AND 4096 AND path NOT LIKE '/%' AND path NOT LIKE '%/' AND path NOT LIKE '%//%' AND path !~ '[\\[:cntrl:]]'), + collection_id uuid REFERENCES collections(id) ON DELETE CASCADE, + display_name text NOT NULL CHECK (length(display_name) BETWEEN 1 AND 200), + PRIMARY KEY (home_id,path) +); +CREATE UNIQUE INDEX dav_bindings_one_collection_per_home ON dav_bindings(home_id,collection_id) + WHERE collection_id IS NOT NULL; + +INSERT INTO dav_bindings(home_id,path,collection_id,display_name) +SELECT owner_id,id::text,id,name FROM collections; +INSERT INTO dav_bindings(home_id,path,collection_id,display_name) +SELECT m.account_id,c.id::text,c.id,c.name FROM collection_members m JOIN collections c ON c.id=m.collection_id +ON CONFLICT DO NOTHING; + +CREATE FUNCTION dav_collection_binding() RETURNS trigger LANGUAGE plpgsql AS $$ +BEGIN + IF TG_OP='INSERT' THEN + INSERT INTO dav_bindings(home_id,path,collection_id,display_name) + VALUES (NEW.owner_id,NEW.id::text,NEW.id,NEW.name); + ELSE + UPDATE dav_bindings SET display_name=NEW.name WHERE collection_id=NEW.id; + END IF; + RETURN NEW; +END $$; +CREATE TRIGGER dav_collection_binding AFTER INSERT OR UPDATE OF name ON collections +FOR EACH ROW EXECUTE FUNCTION dav_collection_binding(); + +CREATE FUNCTION dav_member_binding() RETURNS trigger LANGUAGE plpgsql AS $$ +DECLARE title text; destination text; +BEGIN + IF TG_OP='DELETE' THEN + DELETE FROM dav_bindings WHERE home_id=OLD.account_id AND collection_id=OLD.collection_id; + RETURN OLD; + END IF; + IF NOT EXISTS(SELECT 1 FROM dav_bindings WHERE home_id=NEW.account_id AND collection_id=NEW.collection_id) THEN + SELECT name INTO title FROM collections WHERE id=NEW.collection_id; + destination:=NEW.collection_id::text; + IF EXISTS(SELECT 1 FROM dav_bindings WHERE home_id=NEW.account_id AND path=destination) THEN + destination:=NEW.collection_id::text||'-'||gen_random_uuid()::text; + END IF; + INSERT INTO dav_bindings(home_id,path,collection_id,display_name) + VALUES (NEW.account_id,destination,NEW.collection_id,title); + END IF; + RETURN NEW; +END $$; +CREATE TRIGGER dav_member_binding AFTER INSERT OR UPDATE ON collection_members +FOR EACH ROW EXECUTE FUNCTION dav_member_binding(); +CREATE TRIGGER dav_member_unbinding AFTER DELETE ON collection_members +FOR EACH ROW EXECUTE FUNCTION dav_member_binding(); + +CREATE TABLE dav_binding_properties ( + home_id uuid NOT NULL, + path text NOT NULL, + namespace text NOT NULL CHECK (length(namespace)<=1024), + name text NOT NULL CHECK (length(name) BETWEEN 1 AND 200), + xml text NOT NULL CHECK (octet_length(xml)<=65536), + PRIMARY KEY(home_id,path,namespace,name), + FOREIGN KEY(home_id,path) REFERENCES dav_bindings(home_id,path) ON DELETE CASCADE ON UPDATE CASCADE +); +CREATE TABLE dav_collection_properties ( + collection_id uuid NOT NULL REFERENCES collections(id) ON DELETE CASCADE, + namespace text NOT NULL CHECK (length(namespace)<=1024), + name text NOT NULL CHECK (length(name) BETWEEN 1 AND 200), + xml text NOT NULL CHECK (octet_length(xml)<=65536), + PRIMARY KEY(collection_id,namespace,name) +); +CREATE TABLE dav_resource_properties ( + collection_id uuid NOT NULL, + resource_name text NOT NULL, + namespace text NOT NULL CHECK (length(namespace)<=1024), + name text NOT NULL CHECK (length(name) BETWEEN 1 AND 200), + xml text NOT NULL CHECK (octet_length(xml)<=65536), + PRIMARY KEY(collection_id,resource_name,namespace,name), + FOREIGN KEY(collection_id,resource_name) REFERENCES calendar_resources(collection_id,name) ON DELETE CASCADE ON UPDATE CASCADE +); diff --git a/src/main/resources/db/migration/V9__sharing_groups.sql b/src/main/resources/db/migration/V9__sharing_groups.sql new file mode 100644 index 0000000..1411962 --- /dev/null +++ b/src/main/resources/db/migration/V9__sharing_groups.sql @@ -0,0 +1,105 @@ +-- Account and group principal IDs share one durable namespace; retired IDs are never recycled. +CREATE TABLE dav_principals ( + id uuid PRIMARY KEY, + kind text NOT NULL CHECK(kind IN ('ACCOUNT','GROUP')) +); +INSERT INTO dav_principals(id,kind) SELECT id,'ACCOUNT' FROM accounts; +CREATE FUNCTION dav_account_principal() RETURNS trigger LANGUAGE plpgsql AS $$ +BEGIN + INSERT INTO dav_principals(id,kind) VALUES(NEW.id,'ACCOUNT'); + RETURN NEW; +END $$; +CREATE TRIGGER dav_account_principal BEFORE INSERT ON accounts FOR EACH ROW EXECUTE FUNCTION dav_account_principal(); +ALTER TABLE accounts ADD CONSTRAINT accounts_principal_fk FOREIGN KEY(id) REFERENCES dav_principals(id); + +CREATE TABLE sharing_groups ( + id uuid PRIMARY KEY REFERENCES dav_principals(id), + name text NOT NULL UNIQUE CHECK(length(name) BETWEEN 1 AND 200), + created_by uuid NOT NULL REFERENCES accounts(id), + created_at timestamptz NOT NULL DEFAULT now() +); +CREATE FUNCTION dav_group_principal() RETURNS trigger LANGUAGE plpgsql AS $$ +BEGIN + INSERT INTO dav_principals(id,kind) VALUES(NEW.id,'GROUP'); + RETURN NEW; +END $$; +CREATE TRIGGER dav_group_principal BEFORE INSERT ON sharing_groups FOR EACH ROW EXECUTE FUNCTION dav_group_principal(); +CREATE TABLE sharing_group_members ( + group_id uuid NOT NULL REFERENCES sharing_groups(id) ON DELETE CASCADE, + account_id uuid NOT NULL REFERENCES accounts(id) ON DELETE CASCADE, + PRIMARY KEY(group_id,account_id) +); +CREATE INDEX sharing_group_members_account ON sharing_group_members(account_id,group_id); +CREATE TABLE collection_group_members ( + collection_id uuid NOT NULL REFERENCES collections(id) ON DELETE CASCADE, + group_id uuid NOT NULL REFERENCES sharing_groups(id) ON DELETE CASCADE, + permission text NOT NULL CHECK(permission IN ('READ','WRITE')), + PRIMARY KEY(collection_id,group_id) +); +CREATE INDEX collection_group_members_group ON collection_group_members(group_id,collection_id); +CREATE VIEW collection_effective_members AS +SELECT collection_id,account_id,CASE WHEN bool_or(permission='WRITE') THEN 'WRITE' ELSE 'READ' END AS permission +FROM ( + SELECT collection_id,account_id,permission FROM collection_members + UNION ALL + SELECT a.collection_id,m.account_id,a.permission FROM collection_group_members a + JOIN sharing_group_members m ON m.group_id=a.group_id +) grants GROUP BY collection_id,account_id; + +DROP TRIGGER dav_member_binding ON collection_members; +DROP TRIGGER dav_member_unbinding ON collection_members; +DROP FUNCTION dav_member_binding(); +CREATE FUNCTION dav_reconcile_memberships(target uuid) RETURNS void LANGUAGE plpgsql AS $$ +DECLARE calendar_owner uuid; title text; recipient uuid; destination text; +BEGIN + SELECT owner_id,name INTO calendar_owner,title FROM collections WHERE id=target FOR UPDATE; + IF NOT FOUND THEN RETURN; END IF; + INSERT INTO dav_bindings(home_id,path,collection_id,display_name) + SELECT e.account_id, + CASE WHEN EXISTS(SELECT 1 FROM dav_bindings occupied WHERE occupied.home_id=e.account_id AND occupied.path=target::text) + THEN target::text||'-'||gen_random_uuid()::text ELSE target::text END, + target,title + FROM collection_effective_members e + WHERE e.collection_id=target AND e.account_id<>calendar_owner + AND NOT EXISTS(SELECT 1 FROM dav_bindings b WHERE b.home_id=e.account_id AND b.collection_id=target); + DELETE FROM dav_bindings b WHERE b.collection_id=target AND b.home_id<>calendar_owner + AND NOT EXISTS(SELECT 1 FROM collection_effective_members e WHERE e.collection_id=target AND e.account_id=b.home_id); + UPDATE collections SET generation=gen_random_uuid() WHERE id=target; +END $$; +CREATE FUNCTION dav_direct_membership_changed() RETURNS trigger LANGUAGE plpgsql AS $$ +BEGIN + IF current_setting('busystack.defer_dav_bindings',true)='on' THEN + IF TG_OP='DELETE' THEN RETURN OLD; ELSE RETURN NEW; END IF; + END IF; + PERFORM dav_reconcile_memberships(CASE WHEN TG_OP='DELETE' THEN OLD.collection_id ELSE NEW.collection_id END); + IF TG_OP='DELETE' THEN RETURN OLD; ELSE RETURN NEW; END IF; +END $$; +CREATE TRIGGER dav_direct_membership_changed AFTER INSERT OR UPDATE OR DELETE ON collection_members FOR EACH ROW EXECUTE FUNCTION dav_direct_membership_changed(); +CREATE TRIGGER dav_group_acl_changed AFTER INSERT OR UPDATE OR DELETE ON collection_group_members FOR EACH ROW EXECUTE FUNCTION dav_direct_membership_changed(); +CREATE FUNCTION dav_group_membership_changed() RETURNS trigger LANGUAGE plpgsql AS $$ +DECLARE target uuid; affected_group uuid; +BEGIN + IF current_setting('busystack.defer_dav_bindings',true)='on' THEN + IF TG_OP='DELETE' THEN RETURN OLD; ELSE RETURN NEW; END IF; + END IF; + affected_group:=CASE WHEN TG_OP='DELETE' THEN OLD.group_id ELSE NEW.group_id END; + FOR target IN SELECT collection_id FROM collection_group_members WHERE group_id=affected_group ORDER BY collection_id LOOP + PERFORM dav_reconcile_memberships(target); + END LOOP; + IF TG_OP='DELETE' THEN RETURN OLD; ELSE RETURN NEW; END IF; +END $$; +CREATE TRIGGER dav_group_membership_changed AFTER INSERT OR UPDATE OR DELETE ON sharing_group_members FOR EACH ROW EXECUTE FUNCTION dav_group_membership_changed(); + +-- Keep accepted XML language/attribute metadata until an explicit indexed-name replacement. +CREATE FUNCTION dav_collection_name_xml_changed() RETURNS trigger LANGUAGE plpgsql AS $$ +BEGIN + DELETE FROM dav_collection_properties WHERE collection_id=NEW.id AND namespace='DAV:' AND name='displayname'; + RETURN NEW; +END $$; +CREATE TRIGGER dav_collection_name_xml_changed AFTER UPDATE OF name ON collections FOR EACH ROW EXECUTE FUNCTION dav_collection_name_xml_changed(); +CREATE FUNCTION dav_folder_name_xml_changed() RETURNS trigger LANGUAGE plpgsql AS $$ +BEGIN + DELETE FROM dav_binding_properties WHERE home_id=NEW.home_id AND path=NEW.path AND namespace='DAV:' AND name='displayname'; + RETURN NEW; +END $$; +CREATE TRIGGER dav_folder_name_xml_changed AFTER UPDATE OF display_name ON dav_bindings FOR EACH ROW WHEN(NEW.collection_id IS NULL) EXECUTE FUNCTION dav_folder_name_xml_changed(); diff --git a/src/main/resources/ical4j.properties b/src/main/resources/ical4j.properties new file mode 100644 index 0000000..edc4f13 --- /dev/null +++ b/src/main/resources/ical4j.properties @@ -0,0 +1,4 @@ +# Parsing calendar data must never refresh TZURL definitions over the network. +net.fortuna.ical4j.timezone.update.enabled=false +ical4j.parsing.relaxed=false +ical4j.validation.relaxed=false diff --git a/src/main/resources/static/assets/account.css b/src/main/resources/static/assets/account.css new file mode 100644 index 0000000..2f869bf --- /dev/null +++ b/src/main/resources/static/assets/account.css @@ -0,0 +1,3 @@ +:root{color-scheme:light dark;font-family:system-ui,sans-serif;line-height:1.6}body{margin:0}main{max-width:58rem;margin:auto;padding:2rem 1rem}nav{display:flex;gap:1rem;flex-wrap:wrap}label{display:block;margin:.7rem 0}input,select,textarea,button{font:inherit;padding:.4rem .6rem;margin-left:.3rem;max-width:100%;box-sizing:border-box}button{cursor:pointer}form{margin:1.2rem 0;padding:1rem;border:1px solid #888;border-radius:.3rem}li form{display:inline-block;margin:.4rem;padding:0;border:0}code{overflow-wrap:anywhere}a{color:light-dark(#0759b0,#93c5fd)}:focus-visible{outline:3px solid #c87800;outline-offset:3px} + +textarea{display:block;min-height:6rem;resize:vertical}table{border-collapse:collapse;max-width:100%}th,td{padding:.4rem;text-align:left;vertical-align:top;overflow-wrap:anywhere} diff --git a/src/main/resources/static/assets/security.js b/src/main/resources/static/assets/security.js new file mode 100644 index 0000000..4db4965 --- /dev/null +++ b/src/main/resources/static/assets/security.js @@ -0,0 +1,65 @@ +'use strict'; +(() => { + const status = document.getElementById('security-status'); + const csrfToken = document.querySelector('meta[name="csrf-token"]').content; + const csrfHeader = document.querySelector('meta[name="csrf-header"]').content; + function decode(value) { + const raw = atob(value.replace(/-/g, '+').replace(/_/g, '/')); + return Uint8Array.from(raw, character => character.charCodeAt(0)); + } + function encode(value) { + return btoa(String.fromCharCode(...new Uint8Array(value))).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, ''); + } + async function call(base, path, method, body) { + const response = await fetch(base + path, {method, credentials: 'same-origin', + headers: {[csrfHeader]: csrfToken, 'Content-Type': 'application/json'}, + body: body === undefined ? undefined : JSON.stringify(body)}); + if (!response.ok) throw new Error('Request failed (' + response.status + '). Verify your password and existing passkey, then try again.'); + return response.status === 204 ? null : response.json(); + } + function run(button, action) { + button.addEventListener('click', async () => { + button.disabled = true; + status.textContent = 'Waiting for your authenticator…'; + try { await action(button.dataset.context); } + catch (error) { status.textContent = error.message || 'Authentication failed.'; } + finally { button.disabled = false; } + }); + } + const register = document.getElementById('register-passkey'); + if (register) run(register, async base => { + const options = await call(base, 'webauthn/register/options', 'POST'); + options.challenge = decode(options.challenge); options.user.id = decode(options.user.id); + for (const credential of options.excludeCredentials || []) credential.id = decode(credential.id); + const credential = await navigator.credentials.create({publicKey: options}); + const data = {id: credential.id, rawId: encode(credential.rawId), type: credential.type, + response: {attestationObject: encode(credential.response.attestationObject), + clientDataJSON: encode(credential.response.clientDataJSON), transports: credential.response.getTransports()}, + clientExtensionResults: credential.getClientExtensionResults(), authenticatorAttachment: credential.authenticatorAttachment}; + await call(base, 'webauthn/register', 'POST', {publicKey: {credential: data, + label: document.getElementById('passkey-label').value.trim() || 'Passkey'}}); + status.textContent = 'Passkey registered. Verify it below to complete MFA.'; + }); + const authenticate = document.getElementById('authenticate-passkey'); + if (authenticate) run(authenticate, async base => { + const options = await call(base, 'webauthn/authenticate/options', 'POST'); + options.challenge = decode(options.challenge); + for (const credential of options.allowCredentials || []) credential.id = decode(credential.id); + const credential = await navigator.credentials.get({publicKey: options}); + const response = credential.response; + const result = await call(base, 'login/webauthn', 'POST', {id: credential.id, rawId: encode(credential.rawId), type: credential.type, + response: {authenticatorData: encode(response.authenticatorData), clientDataJSON: encode(response.clientDataJSON), + signature: encode(response.signature), userHandle: response.userHandle ? encode(response.userHandle) : null}, + clientExtensionResults: credential.getClientExtensionResults(), authenticatorAttachment: credential.authenticatorAttachment}); + const destination = new URL(result.redirectUrl || base, window.location.href); + const application = new URL(base, window.location.href); + if (destination.origin !== application.origin || !destination.pathname.startsWith(application.pathname)) { + throw new Error('The authentication redirect is outside this application. Return to account management.'); + } + window.location.assign(destination.href); + }); + for (const button of document.querySelectorAll('.remove-passkey')) run(button, async base => { + await call(base, 'webauthn/register/' + encodeURIComponent(button.dataset.id), 'DELETE'); + window.location.reload(); + }); +})(); diff --git a/src/main/resources/templates/account-error.html b/src/main/resources/templates/account-error.html new file mode 100644 index 0000000..36cb948 --- /dev/null +++ b/src/main/resources/templates/account-error.html @@ -0,0 +1 @@ +Check account details · BusyStack

Check the submitted details

The action could not accept one or more values. Review the account, collection, permission and field limits, then try again.

Return to your account

diff --git a/src/main/resources/templates/administration.html b/src/main/resources/templates/administration.html new file mode 100644 index 0000000..54fca7d --- /dev/null +++ b/src/main/resources/templates/administration.html @@ -0,0 +1,2 @@ + +BusyStack administration

Administration

Your account

OAuth client registration

Server diagnostics

Sharing groups

Accounts

  • · · administrator · disabled

Provision an account

diff --git a/src/main/resources/templates/dashboard.html b/src/main/resources/templates/dashboard.html new file mode 100644 index 0000000..8660681 --- /dev/null +++ b/src/main/resources/templates/dashboard.html @@ -0,0 +1,15 @@ + +BusyStack account +

BusyStack account

Signed in as ().

+ +

Manage event and task content in your CalDAV client. Changes to account access and collections require primary sign-in and passkey verification within the last ten minutes.

+

Organization sign-in

Linking an organization identity replaces local password sign-in for this account. Future sensitive operations require organization sign-in and your passkey.

+

Connect a CalDAV client

Server address:
Username:

Use a named DAV credential from this page as your client password. Browser and account passwords do not authenticate DAV. Capable registered applications can instead use browser OAuth authorization.

Strict synchronization depth mode is enabled. The tested Thunderbird and python-caldav versions use Depth: 1 and require the default synchronization accommodation to connect.

Collections

  • · ·
+

Create a collection

+
+

Share an owned collection

+
+

DAV credentials

The account password does not authenticate DAV requests. Each application uses a separate expiring credential.

+
  • · expires · revoked
+

Issue a credential

+
diff --git a/src/main/resources/templates/diagnostics.html b/src/main/resources/templates/diagnostics.html new file mode 100644 index 0000000..df5f75f --- /dev/null +++ b/src/main/resources/templates/diagnostics.html @@ -0,0 +1,8 @@ + +Server diagnostics · BusyStack
+

Server diagnostics

Administration · Account

+

Deployment

Public base URL
OAuth issuer
Java
PostgreSQL
Applied database migration
+

Durable state

Enabled accounts
Collections
Calendar resources
+

HTTP since this application started

Completed requests
Server errors
Mean duration, milliseconds
Recent maximum duration, milliseconds
+

Request logs contain server-generated correlation identifiers, endpoint categories, methods, status codes and duration. Use the response’s X-Request-ID when investigating a failed request. These aggregate counters reset on restart.

+
diff --git a/src/main/resources/templates/error/403.html b/src/main/resources/templates/error/403.html new file mode 100644 index 0000000..49d3d6f --- /dev/null +++ b/src/main/resources/templates/error/403.html @@ -0,0 +1 @@ +Action not allowed · BusyStack

Action not allowed

Sensitive actions require recent sign-in and passkey verification. Sharing and administration also require permission for the affected account or collection.

Sign in again · Verify a passkey · Return to your account

\ No newline at end of file diff --git a/src/main/resources/templates/group-error.html b/src/main/resources/templates/group-error.html new file mode 100644 index 0000000..02b8fc3 --- /dev/null +++ b/src/main/resources/templates/group-error.html @@ -0,0 +1,2 @@ + +Check the group request · BusyStack

Check the group request

diff --git a/src/main/resources/templates/group-sharing.html b/src/main/resources/templates/group-sharing.html new file mode 100644 index 0000000..037f34a --- /dev/null +++ b/src/main/resources/templates/group-sharing.html @@ -0,0 +1,6 @@ +

Group sharing

Administrators manage group membership. Sharing grants the selected access to current and future members. Only the collection owner can change the group grant.

+

Share an owned collection with a group

+

No sharing groups are available.

Manage sharing groups

+
Groups with access to your owned collections
CollectionGroupAccessAction
+ +
diff --git a/src/main/resources/templates/groups.html b/src/main/resources/templates/groups.html new file mode 100644 index 0000000..f0b40a7 --- /dev/null +++ b/src/main/resources/templates/groups.html @@ -0,0 +1,5 @@ + +Sharing groups · BusyStack

Sharing groups

Group administration requires recent primary sign-in and passkey verification. A group grants calendar access only when a collection owner shares with it. Administration alone does not grant access to calendar contents.

+

Create a group

Use up to 1,000 active accounts. An empty group has no members.

+

No sharing groups exist.

members ·

Deleting this group removes its collection grants.

+
diff --git a/src/main/resources/templates/individual-sharing.html b/src/main/resources/templates/individual-sharing.html new file mode 100644 index 0000000..5f5ca28 --- /dev/null +++ b/src/main/resources/templates/individual-sharing.html @@ -0,0 +1 @@ +

Individual sharing

Only direct grants on collections you own appear here. Removing one does not remove access granted through a group.

CollectionAccountAccessAction
(disabled)

No individual grants on this page.

diff --git a/src/main/resources/templates/issued-credential.html b/src/main/resources/templates/issued-credential.html new file mode 100644 index 0000000..8a2a78c --- /dev/null +++ b/src/main/resources/templates/issued-credential.html @@ -0,0 +1,2 @@ + +New DAV credential

New DAV credential

Store this secret in your application's credential settings. It is shown only in this response.

Username
Secret
Expires

Return to your account

diff --git a/src/main/resources/templates/login.html b/src/main/resources/templates/login.html new file mode 100644 index 0000000..6df2fe7 --- /dev/null +++ b/src/main/resources/templates/login.html @@ -0,0 +1,15 @@ + + +Sign in · BusyStack + + +

Sign in to BusyStack

+

Account changes and client authorization require a password and a passkey verified within ten minutes. Complete both steps using the same account.

+

Sign in failed. Check your account details.

+

Sign in with your organization's identity provider

+

+ +

+

After verifying your password, enroll your first passkey. For a lost passkey, contact your server administrator for the documented local recovery operation.

+

Account management

+
diff --git a/src/main/resources/templates/oauth-clients.html b/src/main/resources/templates/oauth-clients.html new file mode 100644 index 0000000..dc50f6a --- /dev/null +++ b/src/main/resources/templates/oauth-clients.html @@ -0,0 +1,9 @@ + + +OAuth clients · BusyStack +

OAuth clients

+

Register a calendar or task application that supports authorization code with S256 PKCE. Native applications use public client identification and have no embedded secret. Generic CalDAV discovery does not register an OAuth client.

+

Registering and removing clients requires password and passkey verification within ten minutes. Removing a client revokes every grant issued to it.

+
Registered applications
Client IDNameRedirectsAction
+

Register a native application

Use application-owned HTTPS URLs, IP loopback HTTP URLs with a port, or reverse-domain application schemes such as org.example.calendar:/callback. Register at most five redirects. Non-loopback redirects use exact matching.

+
diff --git a/src/main/resources/templates/oauth-consent.html b/src/main/resources/templates/oauth-consent.html new file mode 100644 index 0000000..8f73fc0 --- /dev/null +++ b/src/main/resources/templates/oauth-consent.html @@ -0,0 +1,2 @@ + +Authorize application · BusyStack

Authorize application

requests access to your calendars and tasks. Your collection permissions apply to every request.

diff --git a/src/main/resources/templates/oauth-error.html b/src/main/resources/templates/oauth-error.html new file mode 100644 index 0000000..4ac8f54 --- /dev/null +++ b/src/main/resources/templates/oauth-error.html @@ -0,0 +1,2 @@ + +OAuth request · BusyStack

Check the request

diff --git a/src/main/resources/templates/oauth-grants.html b/src/main/resources/templates/oauth-grants.html new file mode 100644 index 0000000..91f6d8d --- /dev/null +++ b/src/main/resources/templates/oauth-grants.html @@ -0,0 +1,7 @@ + + +Client access · BusyStack +

OAuth client access

+

Approved applications can access your calendar and task collections within your existing sharing permissions. Revoking access invalidates that application's current access and refresh credentials immediately.

Revocation requires password and passkey verification within ten minutes. Applications must sign in again after thirty days.

+
Authorization grants
ApplicationAuthorizedExpiresRevokedAction
()

No OAuth applications have been authorized.

+
diff --git a/src/main/resources/templates/passkeys.html b/src/main/resources/templates/passkeys.html new file mode 100644 index 0000000..3e3816f --- /dev/null +++ b/src/main/resources/templates/passkeys.html @@ -0,0 +1,13 @@ + + +Passkeys · BusyStack + +

Passkeys

First enrollment requires a recent password. Adding or removing an existing passkey requires a recent password and passkey. Your authenticator must verify your presence and unlock the passkey.

+ + + +

+ +
Registered passkeys
NameCreatedLast usedAction
+

Verify another factor · Account management

+
diff --git a/src/main/resources/templates/password.html b/src/main/resources/templates/password.html new file mode 100644 index 0000000..a799dca --- /dev/null +++ b/src/main/resources/templates/password.html @@ -0,0 +1,2 @@ + +Password · BusyStack

Change your local password

Password changes require recent primary and passkey verification. They end every session and revoke DAV credentials, OAuth access and recovery codes. Your passkeys remain registered.

If your account uses an external identity provider, change the primary credential there.

Use at least fourteen characters, within seventy-two UTF-8 bytes.

diff --git a/src/main/resources/templates/recovery-codes.html b/src/main/resources/templates/recovery-codes.html new file mode 100644 index 0000000..d3ee169 --- /dev/null +++ b/src/main/resources/templates/recovery-codes.html @@ -0,0 +1,2 @@ + +Save recovery codes · BusyStack

Save your recovery codes

These codes appear once. Store them securely offline. Each code is single use, expires at , and still requires your recent primary sign-in.

Generating another set revokes these codes. The server retains only their verifiers.

Return to recovery settings

diff --git a/src/main/resources/templates/recovery.html b/src/main/resources/templates/recovery.html new file mode 100644 index 0000000..5178790 --- /dev/null +++ b/src/main/resources/templates/recovery.html @@ -0,0 +1,2 @@ + +Recovery · BusyStack

Account recovery

Keep recovery codes offline. A code can replace a lost passkey after you recently verify your primary sign-in. It cannot replace your password or upstream sign-in.

Unused codes: · expires .

Generate new recovery codes

Requires recent primary and passkey verification. Generating a new set revokes every unused code from the previous set. Codes appear once.

Recover from a lost passkey

First verify your primary sign-in. Recovery consumes this code, removes all passkeys, revokes DAV and OAuth access, and ends every browser session. Then sign in again and enroll a new passkey.

diff --git a/src/main/resources/templates/sessions.html b/src/main/resources/templates/sessions.html new file mode 100644 index 0000000..44a115c --- /dev/null +++ b/src/main/resources/templates/sessions.html @@ -0,0 +1,2 @@ + +Sessions · BusyStack

Active browser sessions

Sessions expire after thirty minutes of inactivity. Ending a session requires recent primary and passkey verification. DAV and OAuth access are managed separately.

Your browser sessions
CreatedLast usedExpiresBrowserAction
diff --git a/src/test/java/org/busystack/caldav/calendar/CalendarAdmissionSyntaxTest.java b/src/test/java/org/busystack/caldav/calendar/CalendarAdmissionSyntaxTest.java new file mode 100644 index 0000000..2a45e6a --- /dev/null +++ b/src/test/java/org/busystack/caldav/calendar/CalendarAdmissionSyntaxTest.java @@ -0,0 +1,282 @@ +package org.busystack.caldav.calendar; + +import static org.assertj.core.api.Assertions.*; + +import java.nio.charset.StandardCharsets; +import java.util.List; +import java.util.stream.Stream; +import org.busystack.caldav.calendar.query.FloatingTimezone; +import org.busystack.caldav.calendar.query.QueryException; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; + +/** Lexical counterexamples must be checked before a typed model can normalize them away. */ +class CalendarAdmissionSyntaxTest { + @ParameterizedTest + @ValueSource( + strings = { + "invalid-leading-token", + "invalid-zero-byday", + "invalid-plus-zero-byday", + "invalid-weeks-days" + }) + void originalIndependentResidualInputsAreRejected(String name) throws Exception { + byte[] bytes = fixture(name), original = bytes.clone(); + invalid(bytes, "VEVENT"); + assertThat(bytes).isEqualTo(original); + } + + @ParameterizedTest + @ValueSource( + strings = {"valid-first-weekday", "valid-last-weekday", "valid-one-week", "valid-nine-days"}) + void originalIndependentLegalNeighborsRemainAccepted(String name) throws Exception { + byte[] bytes = fixture(name), original = bytes.clone(); + assertThat(CalendarContent.parse(bytes, "VEVENT").uid()).isEqualTo(name); + assertThat(bytes).isEqualTo(original); + } + + @ParameterizedTest + @ValueSource(strings = {"0MO", "+0MO", "-0MO", "00MO", "+00MO", "-00MO", "MO,0TU"}) + void explicitZeroWeekdayOrdinalsAreRejectedForEventsTasksAndTypedRules(String byday) { + for (String component : List.of("VEVENT", "VTODO")) + invalid(content(component, "RRULE:FREQ=MONTHLY;BYDAY=" + byday + ";COUNT=2\r\n"), component); + invalid( + content("VEVENT", "X-RULE;VALUE=RECUR:FREQ=MONTHLY;BYDAY=" + byday + ";COUNT=2\r\n"), + "VEVENT"); + assertThatThrownBy(() -> FloatingTimezone.parse(timezone(byday))) + .isInstanceOfSatisfying( + QueryException.class, + error -> assertThat(error.kind()).isEqualTo(QueryException.Kind.INVALID_DATA)); + } + + @ParameterizedTest + @ValueSource(strings = {"MO", "MO,TU", "1MO", "+1MO", "-1MO", "01MO", "+01MO", "-01MO"}) + void omittedAndPermittedWeekdayOrdinalsRemainAccepted(String byday) { + for (String component : List.of("VEVENT", "VTODO")) { + byte[] bytes = content(component, "RRULE:FREQ=MONTHLY;BYDAY=" + byday + ";COUNT=2\r\n"); + assertThat(CalendarContent.parse(bytes, component).uid()).isEqualTo("lexical"); + } + assertThat(FloatingTimezone.parse(timezone(byday))).isNotNull(); + } + + @ParameterizedTest + @ValueSource(strings = {"0MO", "+0MO", "-0MO"}) + void alreadyAcceptedLegacyRecurrencePropertiesUseTheSameLexicalGuard(String byday) { + invalid(content("VEVENT", "EXRULE:FREQ=MONTHLY;BYDAY=" + byday + "\r\n"), "VEVENT"); + } + + @ParameterizedTest + @ValueSource(strings = {"MO", "+1MO", "-1MO"}) + void legacyPropertyPreservationDoesNotAddRecurrenceExecution(String byday) { + byte[] bytes = content("VEVENT", "EXRULE:FREQ=MONTHLY;BYDAY=" + byday + "\r\n"), + original = bytes.clone(); + assertThat(CalendarContent.parse(bytes, "VEVENT").uid()).isEqualTo("lexical"); + assertThat(bytes).isEqualTo(original); + } + + static Stream durationContexts() { + return Stream.of( + Arguments.of("event", "VEVENT", "DURATION:%s\r\n"), + Arguments.of("task", "VTODO", "DURATION:%s\r\n"), + Arguments.of( + "alarm repeat", + "VEVENT", + "BEGIN:VALARM\r\nACTION:DISPLAY\r\nDESCRIPTION:Repeat\r\nTRIGGER:-PT5M\r\nREPEAT:2\r\nDURATION:%s\r\nEND:VALARM\r\n"), + Arguments.of( + "relative trigger", + "VEVENT", + "BEGIN:VALARM\r\nACTION:DISPLAY\r\nDESCRIPTION:Reminder\r\nTRIGGER:-%s\r\nEND:VALARM\r\n"), + Arguments.of( + "typed relative trigger", + "VTODO", + "BEGIN:VALARM\r\nACTION:DISPLAY\r\nDESCRIPTION:Reminder\r\nTRIGGER;VALUE=DURATION:-%s\r\nEND:VALARM\r\n"), + Arguments.of("RDATE period", "VEVENT", "RDATE;VALUE=PERIOD:20260106T100000Z/%s\r\n"), + Arguments.of("typed extension duration", "VEVENT", "X-INTERVAL;VALUE=DURATION:%s\r\n"), + Arguments.of( + "typed extension period", "VEVENT", "X-PERIOD;VALUE=PERIOD:20260106T100000Z/%s\r\n")); + } + + @ParameterizedTest(name = "{0}") + @MethodSource("durationContexts") + void mixedWeekFormsAreRejectedWhereDurationValuesAreAccepted( + String label, String component, String fields) { + for (String value : List.of("P1W2D", "P1WT2H", "P1W2DT3H", "P1D2W")) + invalid(content(component, fields.formatted(value)), component); + } + + @ParameterizedTest(name = "{0}") + @MethodSource("durationContexts") + void separateWeekAndDayTimeAlternativesRemainAccepted( + String label, String component, String fields) { + for (String value : List.of("P1W", "P9D", "P1DT2H3M4S", "PT2H", "PT3M4S", "PT4S")) { + byte[] bytes = content(component, fields.formatted(value)); + assertThat(CalendarContent.parse(bytes, component).uid()) + .as(label + " " + value) + .isEqualTo("lexical"); + } + } + + @ParameterizedTest + @ValueSource( + strings = { + "XBEGIN:VCALENDAR", + "BEGINX:VCALENDAR", + "prefixBEGIN:VCALENDAR", + "\uFEFFXBEGIN:VCALENDAR", + "beg\u0131n:vcalendar", + "\uFEFFbeg\u0131n:vcalendar", + "BEGIN:VCALENDAR-suffix" + }) + void initialCalendarBoundaryMustBeTheActualStructure(String boundary) { + byte[] bytes = + new String(content("VEVENT", ""), StandardCharsets.UTF_8) + .replaceFirst("BEGIN:VCALENDAR", boundary) + .getBytes(StandardCharsets.UTF_8); + invalid(bytes, "VEVENT"); + assertThatThrownBy( + () -> FloatingTimezone.parse(timezone("MO").replaceFirst("BEGIN:VCALENDAR", boundary))) + .isInstanceOfSatisfying( + QueryException.class, + error -> assertThat(error.kind()).isEqualTo(QueryException.Kind.INVALID_DATA)); + } + + @ParameterizedTest + @ValueSource( + strings = { + "BEGIN:VCALENDAR", + "begin:vcalendar", + "bEgIn:vCaLeNdAr", + "\uFEFFBEGIN:VCALENDAR", + "\uFEFFbEgIn:vCaLeNdAr" + }) + void caseVariationsAndOneLeadingBomRetainAuthoritativeBytes(String boundary) { + byte[] + bytes = + new String(content("VEVENT", "X-OPAQUE:caseSensitiveValue\r\n"), StandardCharsets.UTF_8) + .replaceFirst("BEGIN:VCALENDAR", boundary) + .getBytes(StandardCharsets.UTF_8), + original = bytes.clone(); + assertThat(CalendarContent.parse(bytes, "VEVENT").uid()).isEqualTo("lexical"); + assertThat(bytes).isEqualTo(original); + assertThat(FloatingTimezone.parse(timezone("MO").replaceFirst("BEGIN:VCALENDAR", boundary))) + .isNotNull(); + } + + @Test + void otherRecognizedDurationPropertiesShareTheLexicalGuard() { + for (String parameter : List.of("", ";VALUE=DURATION")) { + byte[] invalid = + new String(content("VEVENT", ""), StandardCharsets.UTF_8) + .replace( + "VERSION:2.0\r\n", "VERSION:2.0\r\nREFRESH-INTERVAL" + parameter + ":P1W2D\r\n") + .getBytes(StandardCharsets.UTF_8); + invalid(invalid, "VEVENT"); + } + // RFC7986 §5.7 requires VALUE=DURATION; a factory's missing-parameter tolerance is + // not itself a positive normative control or a claim of full optional-property coverage. + byte[] valid = + new String(content("VEVENT", ""), StandardCharsets.UTF_8) + .replace("VERSION:2.0\r\n", "VERSION:2.0\r\nREFRESH-INTERVAL;VALUE=DURATION:P1W\r\n") + .getBytes(StandardCharsets.UTF_8); + assertThat(CalendarContent.parse(valid, "VEVENT").uid()).isEqualTo("lexical"); + } + + @ParameterizedTest + @ValueSource( + strings = { + "beg\u0131n:VEVENT", + "beg\u0131n:VTODO", + "beg\u0131n:VALARM", + "beg\u0131n:VTIMEZONE" + }) + void structuralPrefixMatchingDoesNotUseUnicodeCaseEquivalence(String boundary) { + String component = boundary.substring(boundary.indexOf(':') + 1); + String text = + new String( + content(component.equals("VTODO") ? "VTODO" : "VEVENT", ""), StandardCharsets.UTF_8); + if (component.equals("VALARM")) + text = + text.replace( + "END:VEVENT", + "BEGIN:VALARM\r\nACTION:DISPLAY\r\nDESCRIPTION:Control\r\nTRIGGER:-PT5M\r\nEND:VALARM\r\nEND:VEVENT"); + else if (component.equals("VTIMEZONE")) { + String zone = timezone("MO"); + assertThatThrownBy(() -> FloatingTimezone.parse(zone.replace("BEGIN:VTIMEZONE", boundary))) + .isInstanceOfSatisfying( + QueryException.class, + error -> assertThat(error.kind()).isEqualTo(QueryException.Kind.INVALID_DATA)); + return; + } + byte[] bytes = text.replace("BEGIN:" + component, boundary).getBytes(StandardCharsets.UTF_8); + assertThatThrownBy( + () -> CalendarContent.parse(bytes, component.equals("VTODO") ? "VTODO" : "VEVENT")) + .isInstanceOfSatisfying( + QueryException.class, + error -> assertThat(error.kind()).isEqualTo(QueryException.Kind.INVALID_DATA)); + } + + @Test + void asciiStructuralChecksPreserveUnicodeUidAndTextValues() { + String uid = "lexical-\u0131-\u0130", summary = "MiXeD-\u0131-\u0130"; + byte[] + bytes = + new String(content("VEVENT", "SUMMARY:" + summary + "\r\n"), StandardCharsets.UTF_8) + .replace("UID:lexical\r\n", "UID:" + uid + "\r\n") + .getBytes(StandardCharsets.UTF_8), + original = bytes.clone(); + var parsed = CalendarContent.parse(bytes, "VEVENT"); + assertThat(parsed.uid()).isEqualTo(uid); + assertThat( + parsed.calendar().getComponents().getFirst().getRequiredProperty("SUMMARY").getValue()) + .isEqualTo(summary); + assertThat(bytes).isEqualTo(original); + } + + @Test + void untypedOpaqueExtensionsDoNotAcquireInventedDurationOrRecurrenceSemantics() { + byte[] bytes = content("VEVENT", "X-OPAQUE:BYDAY=0MO;P1W2D;XBEGIN:VCALENDAR\r\n"), + original = bytes.clone(); + assertThat(CalendarContent.parse(bytes, "VEVENT").uid()).isEqualTo("lexical"); + assertThat(bytes).isEqualTo(original); + } + + private static void invalid(byte[] bytes, String component) { + assertThatThrownBy(() -> CalendarContent.parse(bytes, component)) + .isInstanceOfSatisfying( + CalendarValidationException.class, + error -> + assertThat(error.reason()) + .isEqualTo(CalendarValidationException.Reason.INVALID_DATA)); + } + + private static byte[] fixture(String name) throws Exception { + try (var stream = + CalendarAdmissionSyntaxTest.class.getResourceAsStream( + "/calendar/independent-verification/" + name + ".ics")) { + if (stream == null) throw new IllegalStateException("Missing original independent fixture"); + return stream.readAllBytes(); + } + } + + private static byte[] content(String component, String fields) { + return ("BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Lexical boundary regression//EN\r\nBEGIN:" + + component + + "\r\nUID:lexical\r\nDTSTAMP:20260101T000000Z\r\nDTSTART:20260105T100000Z\r\n" + + fields + + "END:" + + component + + "\r\nEND:VCALENDAR\r\n") + .getBytes(StandardCharsets.UTF_8); + } + + private static String timezone(String byday) { + return "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Lexical timezone//EN\r\nBEGIN:VTIMEZONE\r\nTZID:LexicalBoundary\r\n" + + "BEGIN:STANDARD\r\nDTSTART:20260105T100000\r\nTZOFFSETFROM:+0100\r\nTZOFFSETTO:+0100\r\n" + + "RRULE:FREQ=YEARLY;BYMONTH=1;BYDAY=" + + byday + + ";COUNT=2\r\nEND:STANDARD\r\nEND:VTIMEZONE\r\nEND:VCALENDAR\r\n"; + } +} diff --git a/src/test/java/org/busystack/caldav/calendar/CalendarAuditCorrectionsTest.java b/src/test/java/org/busystack/caldav/calendar/CalendarAuditCorrectionsTest.java new file mode 100644 index 0000000..ff9e912 --- /dev/null +++ b/src/test/java/org/busystack/caldav/calendar/CalendarAuditCorrectionsTest.java @@ -0,0 +1,555 @@ +package org.busystack.caldav.calendar; + +import static org.assertj.core.api.Assertions.*; +import static org.busystack.caldav.calendar.CalendarSemanticsTest.*; + +import java.nio.charset.StandardCharsets; +import java.time.*; +import java.util.*; +import java.util.stream.Stream; +import org.busystack.caldav.calendar.query.*; +import org.busystack.caldav.calendar.query.CalendarFilter.*; +import org.busystack.caldav.calendar.recurrence.*; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.*; + +/** + * Original independent fixtures plus standards-derived neighboring cases; no clock-specific oracle. + */ +class CalendarAuditCorrectionsTest { + static Stream utf8FoldPositions() { + return Stream.of( + Arguments.of("€", 1), + Arguments.of("€", 2), + Arguments.of("🙂", 1), + Arguments.of("🙂", 2), + Arguments.of("🙂", 3)); + } + + @ParameterizedTest + @MethodSource("utf8FoldPositions") + void unfoldingRestoresUtf8EvenWhenAProducerFoldsInsideACodePoint(String character, int split) + throws Exception { + var output = new java.io.ByteArrayOutputStream(); + output.write( + ("BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Fold//EN\r\nBEGIN:VEVENT\r\n" + + "UID:fold\r\nDTSTAMP:20260101T000000Z\r\nDTSTART:20260101T100000Z\r\nSUMMARY:before") + .getBytes(StandardCharsets.UTF_8)); + byte[] encoded = character.getBytes(StandardCharsets.UTF_8); + output.write(encoded, 0, split); + output.write("\r\n\t".getBytes(StandardCharsets.US_ASCII)); + output.write(encoded, split, encoded.length - split); + output.write("after\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n".getBytes(StandardCharsets.US_ASCII)); + byte[] bytes = output.toByteArray(), original = bytes.clone(); + var parsed = CalendarContent.parse(bytes, "VEVENT"); + assertThat( + parsed + .calendar() + .getComponents() + .getFirst() + .getRequiredProperty(net.fortuna.ical4j.model.Property.SUMMARY) + .getValue()) + .isEqualTo("before" + character + "after"); + assertThat(CalendarParsingInput.reportText(bytes)) + .contains("SUMMARY:before" + character + "after\r\n") + .doesNotContain("\uFFFD"); + assertThat(bytes).isEqualTo(original); + } + + @ParameterizedTest + @ValueSource(ints = {0x80, 0xC0, 0xC3, 0xFF}) + void unfoldingDoesNotMakeMalformedUtf8Acceptable(int malformed) throws Exception { + byte[] valid = fixture("control-event.ics"); + byte[] input = Arrays.copyOf(valid, valid.length + 1); + input[input.length - 1] = (byte) malformed; + assertThatThrownBy(() -> CalendarContent.parse(input, "VEVENT")) + .isInstanceOf(CalendarValidationException.class); + assertThatThrownBy(() -> CalendarParsingInput.reportText(input)) + .isInstanceOf(CalendarValidationException.class); + } + + @Test + void reportTextPreservesOrdinaryUtf8FoldingAndSourceBytes() { + String original = "SUMMARY:ordinary€\r\n folded🙂\r\n"; + byte[] bytes = original.getBytes(StandardCharsets.UTF_8), before = bytes.clone(); + assertThat(CalendarParsingInput.reportText(bytes)).isEqualTo(original); + assertThat(bytes).isEqualTo(before); + } + + @ParameterizedTest + @ValueSource( + strings = { + "", + "CALSCALE:GREGORIAN\r\n", + "CALSCALE:gReGoRiAn\r\n", + "CALSCALE;X-AUDIT=control:gReGoRiAn\r\n" + }) + void omittedAndExplicitGregorianCalendarScalesRemainAccepted(String scale) throws Exception { + byte[] original = fixture("control-event.ics"); + byte[] bytes = + new String(original, StandardCharsets.UTF_8) + .replace("BEGIN:VCALENDAR\r\n", "BEGIN:VCALENDAR\r\n" + scale) + .getBytes(StandardCharsets.UTF_8); + byte[] before = bytes.clone(); + assertThat(CalendarContent.parse(bytes, "VEVENT").uid()).isNotBlank(); + assertThat(bytes).isEqualTo(before); + } + + @ParameterizedTest + @ValueSource( + strings = { + "CALSCALE:CHINESE\r\n", + "CALSCALE:NOT-REGISTERED\r\n", + "CALSCALE:GREGORIAN,OTHER\r\n", + "CALSCALE:\r\n", + "CALSCALE:GREGORIAN\r\nCALSCALE:GREGORIAN\r\n" + }) + void invalidCalendarScaleGrammarAndRepeatedScaleAreRejected(String scale) throws Exception { + byte[] bytes = + new String(fixture("control-event.ics"), StandardCharsets.UTF_8) + .replace("BEGIN:VCALENDAR\r\n", "BEGIN:VCALENDAR\r\n" + scale) + .getBytes(StandardCharsets.UTF_8); + assertThatThrownBy(() -> CalendarContent.parse(bytes, "VEVENT")) + .isInstanceOfSatisfying( + CalendarValidationException.class, + error -> + assertThat(error.reason()) + .isEqualTo(CalendarValidationException.Reason.INVALID_DATA)); + } + + @ParameterizedTest + @ValueSource(strings = {"20290531T230000", "20290531"}) + void timezoneOnlyParsingRejectsObservanceUntilWithoutUtcForm(String until) throws Exception { + String calendar = timezoneFixture().replace("UNTIL=20290531T230000Z", "UNTIL=" + until); + assertThat(calendar).contains("UNTIL=" + until + "\r\n"); + assertThatThrownBy(() -> FloatingTimezone.parse(calendar)) + .isInstanceOfSatisfying( + QueryException.class, + error -> assertThat(error.kind()).isEqualTo(QueryException.Kind.INVALID_DATA)); + } + + @Test + void timezoneOnlyParsingRetainsValidUtcUntilAndFutureEra() throws Exception { + var timezone = FloatingTimezone.parse(timezoneFixture()); + assertThat(timezone.zoneId().getRules().getOffset(Instant.parse("2040-04-01T10:00:00Z"))) + .isEqualTo(ZoneOffset.ofHours(2)); + } + + private static String timezoneFixture() throws Exception { + String fixture = new String(fixture("future-zone.ics"), StandardCharsets.UTF_8); + int start = fixture.indexOf("BEGIN:VTIMEZONE"), end = fixture.indexOf("END:VTIMEZONE"); + return "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Timezone admission//EN\r\n" + + fixture.substring(start, end + "END:VTIMEZONE".length()) + + "\r\nEND:VCALENDAR\r\n"; + } + + static byte[] fixture(String name) throws Exception { + try (var input = + CalendarAuditCorrectionsTest.class.getResourceAsStream("/calendar/audit/" + name)) { + if (input == null) throw new IllegalStateException("Missing independent fixture " + name); + return input.readAllBytes(); + } + } + + @ParameterizedTest + @ValueSource( + strings = { + "invalid-date-hours-duration.ics", + "invalid-event-end-before-start.ics", + "invalid-event-negative-duration.ics", + "invalid-event-no-start.ics", + "invalid-rule-count-and-until.ics", + "invalid-rule-repeat-count.ics", + "invalid-rule-repeat-freq.ics", + "invalid-rule-until-type.ics", + "invalid-todo-no-start-duration.ics" + }) + void originalInvalidFixturesFailAtTheWriteBoundary(String name) throws Exception { + byte[] bytes = fixture(name), before = bytes.clone(); + assertThatThrownBy( + () -> CalendarContent.parse(bytes, name.contains("todo") ? "VTODO" : "VEVENT")) + .isInstanceOfSatisfying( + CalendarValidationException.class, + error -> + assertThat(error.reason()) + .isEqualTo(CalendarValidationException.Reason.INVALID_DATA)); + assertThat(bytes).isEqualTo(before); + } + + @ParameterizedTest + @ValueSource( + strings = { + "control-event.ics", + "control-last-ordinary-second.ics", + "future-zone.ics", + "valid-lowercase-delimiters.ics", + "valid-rfc9073-location.ics", + "valid-rfc9074-alarm.ics", + "valid-rfc9253-relationship.ics", + "valid-text-list.ics", + "valid-unknown-x-properties.ics" + }) + void originalLegalNeighborsAreAcceptedWithoutChangingInput(String name) throws Exception { + byte[] bytes = fixture(name), before = bytes.clone(); + assertThat( + CalendarContent.parse(bytes, name.contains("relationship") ? "VTODO" : "VEVENT").uid()) + .isNotBlank(); + assertThat(bytes).isEqualTo(before); + } + + @Test + void futureEraWorksForQueryBusyAndExpansionBeyondTheFormerWindow() throws Exception { + byte[] bytes = fixture("future-zone.ics"); + var parsed = CalendarContent.parse(bytes, "VEVENT"); + assertThat(parsed.zoneCoverage()).isEmpty(); + assertThat(point(parsed, "VEVENT", "2040-04-01T10:00:00Z")).isTrue(); + assertThat(point(parsed, "VEVENT", "2040-04-01T11:00:00Z")).isFalse(); + var window = range("2040-04-01T09:00:00Z", "2040-04-01T13:00:00Z"); + assertThat(new CalendarFreeBusy(QueryLimits.defaults()).query(List.of(parsed), window, UTC)) + .containsExactly( + new CalendarFreeBusy.BusyPeriod( + Instant.parse("2040-04-01T10:00:00Z"), + Instant.parse("2040-04-01T11:00:00Z"), + CalendarFreeBusy.BusyType.BUSY)); + assertThat( + new CalendarRecurrence(QueryLimits.defaults()) + .expand(parsed, window, FloatingTimezone.of(UTC)) + .toString()) + .contains("DTSTART:20400401T100000Z", "DTEND:20400401T110000Z"); + assertThat(CalendarContent.parse(bytes, "VEVENT").zoneCoverage()).isEmpty(); + assertThat(bytes).isEqualTo(fixture("future-zone.ics")); + } + + @Test + void chronologicalRulesDoNotDependOnTodaysEraForTasksExceptionsAndDifferentOffsets() + throws Exception { + String original = new String(fixture("future-zone.ics"), StandardCharsets.UTF_8); + String recurring = + original.replace( + "DTSTART;TZID=AuditRules:20400401T120000\r\n", + "DTSTART;TZID=AuditRules:20400401T120000\r\nRRULE:FREQ=DAILY;COUNT=3\r\n"); + String exception = + "BEGIN:VEVENT\r\nUID:future-zone\r\nDTSTAMP:20261027T000000Z\r\n" + + "RECURRENCE-ID;TZID=AuditRules:20400402T120000\r\nDTSTART;TZID=AuditRules:20400402T140000\r\n" + + "DURATION:PT1H\r\nEND:VEVENT\r\n"; + var parsed = + CalendarContent.parse( + recurring + .replace("END:VCALENDAR", exception + "END:VCALENDAR") + .getBytes(StandardCharsets.UTF_8), + "VEVENT"); + assertThat(point(parsed, "VEVENT", "2040-04-02T10:00:00Z")).isFalse(); + assertThat(point(parsed, "VEVENT", "2040-04-02T12:00:00Z")).isTrue(); + var task = + CalendarContent.parse( + original.replace("VEVENT", "VTODO").getBytes(StandardCharsets.UTF_8), "VTODO"); + assertThat(point(task, "VTODO", "2040-04-01T10:30:00Z")).isTrue(); + assertThat(point(task, "VTODO", "2040-04-01T11:30:00Z")).isFalse(); + String different = + original + .replace("+0100", "-0330") + .replace("+0200", "-0230") + .replace("20290531T230000Z", "20290601T033000Z"); + var changed = CalendarContent.parse(different.getBytes(StandardCharsets.UTF_8), "VEVENT"); + assertThat(point(changed, "VEVENT", "2040-04-01T14:30:00Z")).isTrue(); + assertThat(point(changed, "VEVENT", "2040-04-01T13:30:00Z")).isFalse(); + // Future era starts in2030, so the old June onset still determines April2028. + var historical = + CalendarContent.parse( + original.replace("20400401", "20280401").getBytes(StandardCharsets.UTF_8), "VEVENT"); + assertThat(point(historical, "VEVENT", "2028-04-01T11:00:00Z")).isTrue(); + } + + @Test + void mixedCaseStructuralNamesRemainDistinctFromCaseSensitiveValues() { + String zone = + "bEgIn:vTimeZone\r\nTZID:CaseSensitive/Zone\r\nbEgIn:standard\r\n" + + "DTSTART:19700101T000000\r\nTZOFFSETFROM:+0200\r\nTZOFFSETTO:+0200\r\neNd:StAnDaRd\r\neNd:VTIMEZONE\r\n"; + for (String type : List.of("VEVENT", "VTODO")) { + String body = + "bEgIn:vcalendar\r\nVERSION:2.0\r\nPRODID:-//CaseControls//EN\r\n" + + zone + + "bEgIn:" + + type.toLowerCase(Locale.ROOT) + + "\r\nUID:CaseSensitive-Uid\r\nDTSTAMP:20261027T000000Z\r\n" + + "DTSTART;TZID=CaseSensitive/Zone:20400401T120000\r\nSUMMARY:MixedCase Text\r\n" + + "bEgIn:valarm\r\nACTION:DISPLAY\r\nDESCRIPTION:KeepCase\r\nTRIGGER:-PT5M\r\neNd:VaLaRm\r\n" + + "eNd:" + + type + + "\r\neNd:Vcalendar\r\n"; + var parsed = CalendarContent.parse(body.getBytes(StandardCharsets.UTF_8), type); + assertThat(parsed.uid()).isEqualTo("CaseSensitive-Uid"); + assertThat( + parsed + .calendar() + .getComponents(type) + .getFirst() + .getRequiredProperty("SUMMARY") + .getValue()) + .isEqualTo("MixedCase Text"); + assertThatThrownBy( + () -> + CalendarContent.parse( + body.replace("eNd:VaLaRm", "eNd:VTODO").getBytes(StandardCharsets.UTF_8), + type)) + .isInstanceOf(CalendarValidationException.class); + } + } + + @Test + void floatingAndAbsoluteDateTimesUseTheReportsTimezoneRatherThanAGuessedWriteZone() { + var parsed = parse("VEVENT", "DTSTART:20261027T100000\r\nDTEND:20261027T140000Z\r\n"); + var filter = + component( + "VCALENDAR", + null, + List.of(), + List.of( + component( + "VEVENT", + range("2026-10-27T09:00:00Z", "2026-10-27T09:30:00Z"), + List.of(), + List.of()))); + assertThat(new CalendarQuery(QueryLimits.defaults()).matches(parsed, filter, ZoneOffset.UTC)) + .isFalse(); + assertThat( + new CalendarQuery(QueryLimits.defaults()) + .matches(parsed, filter, ZoneOffset.ofHours(2))) + .isTrue(); + } + + @ParameterizedTest + @ValueSource(strings = {"i;octet", "i;ascii-casemap"}) + void listTextIsDecodedOnceBeforePositiveAndNegatedMatching(String collation) { + for (String property : List.of("CATEGORIES", "RESOURCES")) { + String fields = + "DTSTART:20261027T120000Z\r\n" + + property + + ":alpha\\;beta,comma\\,value,back\\\\slash,line\\nfeed,literal\\\\n\r\n" + + property + + ":second\r\nSUMMARY:summary\\;control\r\n"; + var parsed = parse("VEVENT", fields); + for (String expected : + List.of( + "alpha;beta", "comma,value", "back\\slash", "line\nfeed", "literal\\n", "second")) { + assertThat(text(parsed, property, expected, collation, false)) + .as(property + " " + expected) + .isTrue(); + } + assertThat(text(parsed, property, "absent", collation, false)).isFalse(); + assertThat(text(parsed, property, "absent", collation, true)).isTrue(); + assertThat(text(parsed, property, "literal\n", collation, false)).isFalse(); + assertThat(text(parsed, "SUMMARY", "summary;control", collation, false)).isTrue(); + assertThat(CalendarReports.copy(parsed).toString()).contains("literal\\\\n"); + } + var one = parse("VEVENT", "DTSTART:20261027T120000Z\r\nCATEGORIES:alpha\\;beta,other\r\n"); + assertThat(text(one, "CATEGORIES", "alpha;beta", collation, true)).isFalse(); + } + + private static boolean text( + CalendarContent.Parsed parsed, + String property, + String value, + String collation, + boolean negate) { + var filter = + new PropertyFilter( + property, false, null, new TextMatch(value, collation, negate), List.of()); + return new CalendarQuery(QueryLimits.defaults()) + .matches( + parsed, + component( + "VCALENDAR", + null, + List.of(), + List.of(component("VEVENT", null, List.of(filter), List.of()))), + UTC); + } + + static Stream invalidCorpus() { + return Stream.of( + Arguments.of("VEVENT", "DTSTART:20260101T100000Z\r\nRRULE:FREQ=WEEKLY;BYMONTHDAY=1\r\n"), + Arguments.of("VEVENT", "DTSTART:20260101T100000Z\r\nRRULE:FREQ=DAILY;BYYEARDAY=1\r\n"), + Arguments.of("VEVENT", "DTSTART:20260101T100000Z\r\nRRULE:FREQ=MONTHLY;BYWEEKNO=1\r\n"), + Arguments.of("VEVENT", "DTSTART:20260101T100000Z\r\nRRULE:FREQ=DAILY;BYDAY=1MO\r\n"), + Arguments.of( + "VEVENT", "DTSTART:20260101T100000Z\r\nRRULE:FREQ=YEARLY;BYWEEKNO=1;BYDAY=1MO\r\n"), + Arguments.of("VEVENT", "DTSTART:20260101T100000Z\r\nRRULE:FREQ=YEARLY;BYSETPOS=1\r\n"), + Arguments.of("VEVENT", "DTSTART:20260101T100000Z\r\nRRULE:FREQ=DAILY;COUNT=0\r\n"), + Arguments.of("VEVENT", "DTSTART:20260101T100000Z\r\nRRULE:FREQ=DAILY;INTERVAL=0\r\n"), + Arguments.of("VEVENT", "DTSTART;VALUE=DATE:20260101\r\nDTEND:20260102T100000Z\r\n"), + Arguments.of("VTODO", "DTSTART;VALUE=DATE:20260101\r\nDUE:20260102T100000Z\r\n"), + Arguments.of("VTODO", "DTSTART:20260102T100000Z\r\nDUE:20260101T100000Z\r\n"), + Arguments.of("VTODO", "DTSTART:20260101T100000Z\r\nDURATION:-PT1H\r\n"), + Arguments.of("VTODO", "COMPLETED:20260101T100000\r\n"), + Arguments.of("VTODO", "PERCENT-COMPLETE:101\r\n"), + Arguments.of("VTODO", "PRIORITY:10\r\n"), + Arguments.of("VEVENT", "DTSTART:20260101T100000Z\r\nSEQUENCE:-1\r\n"), + Arguments.of( + "VEVENT", + "DTSTART:20260101T100000Z\r\nRDATE;VALUE=PERIOD:20260102T100000Z/20260101T100000Z\r\n"), + Arguments.of("VEVENT", "DTSTART:20260101T100000Z\r\nCATEGORIES:invalid\\q\r\n"), + Arguments.of( + "VEVENT", + "DTSTART:20260101T100000Z\r\nBEGIN:VALARM\r\nACTION:DISPLAY\r\nDESCRIPTION:Repeat\r\nTRIGGER:-PT5M\r\nREPEAT:2\r\nEND:VALARM\r\n"), + Arguments.of( + "VEVENT", + "DTSTART:20260101T100000Z\r\nBEGIN:VALARM\r\nACTION:DISPLAY\r\nDESCRIPTION:Absolute\r\nTRIGGER;VALUE=DATE-TIME:20260101T090000\r\nEND:VALARM\r\n"), + Arguments.of("VEVENT", "DTSTART:20260101T100000Z\r\nATTACH;VALUE=BINARY:SGVsbG8=\r\n"), + Arguments.of("VEVENT", "DTSTART:20260101\r\n"), + Arguments.of("VEVENT", "DTSTART;VALUE=DATE:20260101T100000Z\r\n"), + Arguments.of("VEVENT", "DTSTART;VALUE=DATE-TIME:20260101\r\n"), + Arguments.of("VEVENT", "DTSTART:20260101T100000Z\r\nRDATE:20260102\r\n"), + Arguments.of( + "VEVENT", "DTSTART:20260101T100000Z\r\nEXDATE;VALUE=DATE:20260102T100000Z\r\n"), + Arguments.of( + "VEVENT", "DTSTART:20260101T100000Z\r\nATTACH;VALUE=BINARY;ENCODING=BASE64:!bad\r\n")); + } + + @ParameterizedTest + @MethodSource("invalidCorpus") + void supportedNormativeNegativeCorpus(String type, String fields) { + assertThatThrownBy(() -> parse(type, fields)).isInstanceOf(CalendarValidationException.class); + } + + @Test + void conflictingTimezoneOnsetsFailExplicitlyRatherThanGuessingAnOffset() { + String zone = + "BEGIN:VTIMEZONE\r\nTZID:Conflict\r\nBEGIN:STANDARD\r\nDTSTART:20000101T010000\r\n" + + "TZOFFSETFROM:+0100\r\nTZOFFSETTO:+0200\r\nEND:STANDARD\r\nBEGIN:DAYLIGHT\r\n" + + "DTSTART:20000101T020000\r\nTZOFFSETFROM:+0200\r\nTZOFFSETTO:+0300\r\nEND:DAYLIGHT\r\nEND:VTIMEZONE\r\n"; + assertThatThrownBy( + () -> + FloatingTimezone.parse( + "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Conflict//EN\r\n" + + zone + + "END:VCALENDAR\r\n")) + .isInstanceOfSatisfying( + QueryException.class, + error -> assertThat(error.kind()).isEqualTo(QueryException.Kind.INVALID_DATA)); + } + + static Stream legalCorpus() { + return Stream.of( + Arguments.of("VEVENT", "DTSTART;VALUE=DATE:20260101\r\nDURATION:P2D\r\n"), + Arguments.of("VEVENT", "DTSTART;VALUE=DATE:20260101\r\nDURATION:P1W\r\n"), + Arguments.of("VEVENT", "DTSTART:20260101T100000Z\r\nRRULE:COUNT=2;FREQ=DAILY\r\n"), + Arguments.of( + "VEVENT", "DTSTART:20260101T100000\r\nRRULE:FREQ=DAILY;UNTIL=20260103T100000\r\n"), + Arguments.of( + "VEVENT", "DTSTART;VALUE=DATE:20260101\r\nRRULE:FREQ=YEARLY;UNTIL=20290101\r\n"), + Arguments.of( + "VEVENT", "DTSTART:20260101T100000Z\r\nRRULE:FREQ=YEARLY;BYMONTH=1;BYDAY=1MO\r\n"), + Arguments.of( + "VEVENT", "DTSTART:20260101T100000Z\r\nRRULE:FREQ=MONTHLY;BYDAY=MO;BYSETPOS=-1\r\n"), + Arguments.of( + "VEVENT", + "DTSTART:20260101T100000Z\r\nATTACH;VALUE=BINARY;ENCODING=BASE64:SGVsbG8=\r\n"), + Arguments.of("VTODO", ""), + Arguments.of("VTODO", "DUE:20260101T100000Z\r\n"), + Arguments.of("VTODO", "DTSTART;VALUE=DATE:20260101\r\nDURATION:P2D\r\n"), + Arguments.of("VTODO", "PERCENT-COMPLETE:0\r\nPRIORITY:0\r\nSTATUS:NEEDS-ACTION\r\n"), + Arguments.of( + "VTODO", + "PERCENT-COMPLETE:100\r\nPRIORITY:9\r\nSTATUS:COMPLETED\r\nCOMPLETED:20260101T100000Z\r\n"), + Arguments.of( + "VTODO", + "DUE:20260101T100000Z\r\nBEGIN:VALARM\r\nACTION:DISPLAY\r\nDESCRIPTION:Due-only\r\nTRIGGER;RELATED=END:-PT5M\r\nEND:VALARM\r\n"), + Arguments.of( + "VEVENT", + "DTSTART:20260101T100000Z\r\nBEGIN:VALARM\r\nACTION:DISPLAY\r\nDESCRIPTION:Repeated\r\nTRIGGER:-PT5M\r\nREPEAT:2\r\nDURATION:PT5M\r\nEND:VALARM\r\n")); + } + + @ParameterizedTest + @MethodSource("legalCorpus") + void legalNeighborCorpusRemainsAccepted(String type, String fields) { + assertThat(parse(type, fields).uid()).isEqualTo("semantic"); + } + + @Test + void unadvertisedRecurrenceScaleFailsExplicitlyInsteadOfExecutingGregorianFallback() { + assertThatThrownBy( + () -> + parse("VEVENT", "DTSTART:20260101T100000Z\r\nRRULE:RSCALE=CHINESE;FREQ=YEARLY\r\n")) + .isInstanceOfSatisfying( + CalendarValidationException.class, + error -> + assertThat(error.reason()) + .isEqualTo(CalendarValidationException.Reason.UNSUPPORTED_RSCALE)); + } + + @ParameterizedTest + @ValueSource( + strings = { + "GEO:91;0", + "GEO:0;181", + "COMMENT:bad\\q", + "SUMMARY:unescaped,comma", + "DESCRIPTION:unescaped;semicolon", + "COMPLETED:20260101T000000Z", + "PERCENT-COMPLETE:10", + "TRIGGER:-PT5M", + "X-BOOLEAN;VALUE=BOOLEAN:maybe", + "X-INTEGER;VALUE=INTEGER:2147483648", + "X-FLOAT;VALUE=FLOAT:NaN", + "CLASS:invalid token" + }) + void supplementalCoreTypeAndContextConstraints(String property) { + assertThatThrownBy(() -> parse("VEVENT", "DTSTART:20260101T100000Z\r\n" + property + "\r\n")) + .isInstanceOf(CalendarValidationException.class); + } + + @ParameterizedTest + @ValueSource( + strings = { + "CLASS:X-CUSTOM-2", + "CLASS:OTHER-CLASS", + "CLASS:private", + "GEO:90;180", + "GEO:-90;-180", + "COMMENT:a\\,b", + "SUMMARY:escaped\\;semicolon", + "DESCRIPTION:line\\nother", + "X-BOOLEAN;VALUE=BOOLEAN:true", + "X-INTEGER;VALUE=INTEGER:-2147483648", + "X-FLOAT;VALUE=FLOAT:+3.14", + "RELATED-TO;VALUE=URI:https://example.test/path;a,b", + "X-OPAQUE:preserve;arbitrary,structured\\q" + }) + void supplementalLegalNeighborsRetainOpaqueExtensionData(String property) { + var parsed = parse("VEVENT", "DTSTART:20260101T100000Z\r\n" + property + "\r\n"); + assertThat(parsed.uid()).isEqualTo("semantic"); + } + + @ParameterizedTest + @ValueSource( + strings = { + "ACTION;X-MARK=1:display\r\nTRIGGER:-PT5M\r\n", + "ACTION:X-CUSTOM-2\r\n", + "ACTION:X-CUSTOM-2\r\nTRIGGER:-PT5M\r\nTRIGGER:-PT6M\r\n", + "ACTION:EMAIL\r\nDESCRIPTION:Message\r\nTRIGGER:-PT5M\r\n" + }) + void alarmRequirementsDoNotDependOnActionSpelling(String alarm) { + assertThatThrownBy( + () -> + parse( + "VEVENT", + "DTSTART:20260101T100000Z\r\nBEGIN:VALARM\r\n" + alarm + "END:VALARM\r\n")) + .isInstanceOf(CalendarValidationException.class); + } + + @ParameterizedTest + @ValueSource( + strings = {"ACTION;X-MARK=1:display\r\nDESCRIPTION:Message\r\n", "ACTION:X-CUSTOM-2\r\n"}) + void legalCaseAndExtensionAlarmActionsArePreserved(String action) { + assertThat( + parse( + "VEVENT", + "DTSTART:20260101T100000Z\r\nBEGIN:VALARM\r\n" + + action + + "TRIGGER:-PT5M\r\nEND:VALARM\r\n") + .uid()) + .isEqualTo("semantic"); + } +} diff --git a/src/test/java/org/busystack/caldav/calendar/CalendarCandidateBudgetTest.java b/src/test/java/org/busystack/caldav/calendar/CalendarCandidateBudgetTest.java new file mode 100644 index 0000000..ef82737 --- /dev/null +++ b/src/test/java/org/busystack/caldav/calendar/CalendarCandidateBudgetTest.java @@ -0,0 +1,129 @@ +package org.busystack.caldav.calendar; + +import static org.assertj.core.api.Assertions.*; +import static org.busystack.caldav.calendar.CalendarSemanticsTest.*; + +import java.util.*; +import org.busystack.caldav.calendar.query.*; +import org.busystack.caldav.calendar.query.CalendarFilter.*; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +class CalendarCandidateBudgetTest { + private static final TimeRange WINDOW = range("2026-10-27T00:00:00Z", "2026-10-28T00:00:00Z"); + private static final QueryLimits SMALL = new QueryLimits(2, 900, 500000, 3660, 256); + + private static ComponentFilter root(ComponentFilter child) { + return component("VCALENDAR", null, List.of(), List.of(child)); + } + + private static void selective( + ComponentFilter filter, CalendarContent.Parsed unrelated, CalendarContent.Parsed matching) { + var query = new CalendarQuery(SMALL); + for (int i = 0; i < 5; i++) assertThat(query.matches(unrelated, filter, UTC)).isFalse(); + assertThat(query.matches(matching, filter, UTC)).isTrue(); + } + + @ParameterizedTest + @ValueSource(strings = {"VEVENT", "VTODO"}) + void unrelatedOriginalsUseCandidateWorkRatherThanGeneratedInstanceCapacity(String type) { + selective( + root(component(type, WINDOW, List.of(), List.of())), + parse(type, "DTSTART:20000101T100000Z\r\n"), + parse(type, "DTSTART:20261027T100000Z\r\n")); + } + + @Test + void datePropertyAndAlarmFiltersKeepTheirOwnTimeRangeSemantics() { + selective( + root( + component( + "VEVENT", + null, + List.of(new PropertyFilter("DTSTART", false, WINDOW, null, List.of())), + List.of())), + parse("VEVENT", "DTSTART:20000101T100000Z\r\n"), + parse("VEVENT", "DTSTART:20261027T100000Z\r\n")); + String alarm = + "DTSTART:20000101T100000Z\r\nBEGIN:VALARM\r\nACTION:DISPLAY\r\nDESCRIPTION:Stored reminder\r\nTRIGGER;VALUE=DATE-TIME:%s\r\nEND:VALARM\r\n"; + selective( + root( + component( + "VEVENT", + null, + List.of(), + List.of(component("VALARM", WINDOW, List.of(), List.of())))), + parse("VEVENT", alarm.formatted("20000101T100000Z")), + parse("VEVENT", alarm.formatted("20261027T100000Z"))); + } + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void actualMatchedResourcesHaveOneCumulativeBoundWithOrWithoutATimeRange(boolean ranged) { + var query = new CalendarQuery(SMALL); + var parsed = parse("VEVENT", "DTSTART:20261027T100000Z\r\n"); + var filter = root(component("VEVENT", ranged ? WINDOW : null, List.of(), List.of())); + assertThat(query.matches(parsed, filter, UTC)).isTrue(); + assertThat(query.matches(parsed, filter, UTC)).isTrue(); + assertThatThrownBy(() -> query.matches(parsed, filter, UTC)) + .isInstanceOfSatisfying( + QueryException.class, + error -> assertThat(error.kind()).isEqualTo(QueryException.Kind.LIMIT_EXCEEDED)); + } + + @Test + void nonmatchingOriginalsStillConsumeTheSharedCandidateWorkBound() { + var query = new CalendarQuery(new QueryLimits(100, 900, 10, 3660, 256)); + var parsed = parse("VEVENT", "DTSTART:20000101T100000Z\r\n"); + var filter = root(component("VEVENT", WINDOW, List.of(), List.of())); + assertThatThrownBy( + () -> { + for (int i = 0; i < 20; i++) query.matches(parsed, filter, UTC); + }) + .isInstanceOfSatisfying( + QueryException.class, + error -> assertThat(error.kind()).isEqualTo(QueryException.Kind.LIMIT_EXCEEDED)); + } + + @Test + void generatedRecurrencesRetainTheGlobalInstanceBoundEvenWhenNoTextMatches() { + var query = new CalendarQuery(new QueryLimits(3, 900, 500000, 3660, 256)); + var parsed = + parse( + "VEVENT", + "DTSTART:20261027T100000Z\r\nRRULE:FREQ=DAILY;COUNT=2\r\nSUMMARY:Excluded\r\n"); + var filter = + root( + component( + "VEVENT", + range("2026-10-27T00:00:00Z", "2026-10-30T00:00:00Z"), + List.of( + new PropertyFilter( + "SUMMARY", false, null, new TextMatch("Needle", null, false), List.of())), + List.of())); + assertThat(query.matches(parsed, filter, UTC)).isFalse(); + assertThatThrownBy(() -> query.matches(parsed, filter, UTC)) + .isInstanceOfSatisfying( + QueryException.class, + error -> assertThat(error.kind()).isEqualTo(QueryException.Kind.LIMIT_EXCEEDED)); + } + + @Test + void freeBusyRetainsOnlyMatchingPeriodsWhileBoundingCandidateWork() { + var unrelated = parse("VEVENT", "DTSTART:20000101T100000Z\r\nDURATION:PT1H\r\n"); + var matching = parse("VEVENT", "DTSTART:20261027T100000Z\r\nDURATION:PT1H\r\n"); + var result = + new CalendarFreeBusy(SMALL) + .query( + List.of(unrelated, unrelated, unrelated, unrelated, unrelated, matching), + WINDOW, + UTC); + assertThat(result) + .containsExactly( + new CalendarFreeBusy.BusyPeriod( + java.time.Instant.parse("2026-10-27T10:00:00Z"), + java.time.Instant.parse("2026-10-27T11:00:00Z"), + CalendarFreeBusy.BusyType.BUSY)); + } +} diff --git a/src/test/java/org/busystack/caldav/calendar/CalendarContentTest.java b/src/test/java/org/busystack/caldav/calendar/CalendarContentTest.java new file mode 100644 index 0000000..076b73c --- /dev/null +++ b/src/test/java/org/busystack/caldav/calendar/CalendarContentTest.java @@ -0,0 +1,78 @@ +package org.busystack.caldav.calendar; + +import static org.assertj.core.api.Assertions.*; + +import java.nio.charset.StandardCharsets; +import org.junit.jupiter.api.Test; + +class CalendarContentTest { + private static byte[] calendar(String components) { + return ("BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//BusyStack test//EN\r\n" + + components + + "END:VCALENDAR\r\n") + .getBytes(StandardCharsets.UTF_8); + } + + @Test + void acceptsUndatedCompletedTaskAndPreservesExtensionsInOriginalBytes() { + byte[] bytes = + calendar( + "BEGIN:VTODO\r\nUID:task-one\r\nDTSTAMP:20261027T000000Z\r\nSUMMARY:買い物\r\nSTATUS:COMPLETED\r\nCOMPLETED:20261027T010000Z\r\nPERCENT-COMPLETE:100\r\nPRIORITY:3\r\nRELATED-TO:parent\r\nX-UNKNOWN;X-PARAM=\"a^'b\":retained\r\nATTACH:https://127.0.0.1:1/must-not-fetch\r\nEND:VTODO\r\n"); + assertThat(CalendarContent.parse(bytes, "VTODO").uid()).isEqualTo("task-one"); + assertThat(new String(bytes, StandardCharsets.UTF_8)) + .contains("X-UNKNOWN;X-PARAM=\"a^'b\":retained"); + assertThatThrownBy(() -> CalendarContent.parse(bytes, "VEVENT")) + .isInstanceOf(IllegalArgumentException.class); + } + + @Test + void acceptsUnboundedMasterAndDetachedCancelledExceptionWithoutExpansion() { + byte[] bytes = + calendar( + "BEGIN:VEVENT\r\nUID:series\r\nDTSTAMP:20261027T000000Z\r\nDTSTART;VALUE=DATE:20260228\r\nDTEND;VALUE=DATE:20260301\r\nRRULE:FREQ=DAILY\r\nEXDATE;VALUE=DATE:20260302\r\nEND:VEVENT\r\nBEGIN:VEVENT\r\nUID:series\r\nDTSTAMP:20261027T000000Z\r\nDTSTART;VALUE=DATE:20260303\r\nRECURRENCE-ID;VALUE=DATE:20260303\r\nSTATUS:CANCELLED\r\nEND:VEVENT\r\n"); + assertThat(CalendarContent.parse(bytes, "VEVENT").calendar().getComponents()).hasSize(2); + } + + @Test + void rejectsMixedUidAndCalendarMethodAndMalformedUtf8() { + byte[] mixed = + calendar( + "BEGIN:VTODO\r\nUID:one\r\nDTSTAMP:20261027T000000Z\r\nEND:VTODO\r\nBEGIN:VTODO\r\nUID:two\r\nDTSTAMP:20261027T000000Z\r\nEND:VTODO\r\n"); + assertThatThrownBy(() -> CalendarContent.parse(mixed, "VTODO")) + .isInstanceOf(IllegalArgumentException.class); + assertThatThrownBy(() -> CalendarContent.parse(new byte[] {(byte) 0xc3, 0x28}, "VEVENT")) + .isInstanceOf(IllegalArgumentException.class); + assertThatThrownBy( + () -> + CalendarContent.parse( + calendar( + "METHOD:PUBLISH\r\nBEGIN:VTODO\r\nUID:one\r\nDTSTAMP:20261027T000000Z\r\nEND:VTODO\r\n"), + "VTODO")) + .isInstanceOf(IllegalArgumentException.class); + } + + @Test + void doesNotFetchEmbeddedTimeZoneUrl() throws Exception { + try (var listener = + new java.net.ServerSocket(0, 1, java.net.InetAddress.getLoopbackAddress())) { + listener.setSoTimeout(100); + byte[] data = + calendar( + "BEGIN:VTIMEZONE\r\nTZID:Custom/Zone\r\nTZURL:http://127.0.0.1:" + + listener.getLocalPort() + + "/must-not-fetch\r\nBEGIN:STANDARD\r\nDTSTART:19700101T000000\r\nTZOFFSETFROM:+0530\r\nTZOFFSETTO:+0530\r\nEND:STANDARD\r\nEND:VTIMEZONE\r\nBEGIN:VEVENT\r\nUID:custom-zone\r\nDTSTAMP:20261027T000000Z\r\nDTSTART;TZID=Custom/Zone:20261027T120000\r\nEND:VEVENT\r\n"); + assertThat(CalendarContent.parse(data, "VEVENT").uid()).isEqualTo("custom-zone"); + assertThatThrownBy(listener::accept).isInstanceOf(java.net.SocketTimeoutException.class); + } + } + + @Test + void boundsNestedAndOversizedInput() { + assertThatThrownBy( + () -> CalendarContent.parse(new byte[CalendarContent.MAX_BYTES + 1], "VTODO")) + .isInstanceOf(IllegalArgumentException.class); + String nested = "BEGIN:VTODO\r\n".repeat(100) + "END:VTODO\r\n".repeat(100); + assertThatThrownBy(() -> CalendarContent.parse(calendar(nested), "VTODO")) + .isInstanceOf(IllegalArgumentException.class); + } +} diff --git a/src/test/java/org/busystack/caldav/calendar/CalendarDuplicateRecurrenceTest.java b/src/test/java/org/busystack/caldav/calendar/CalendarDuplicateRecurrenceTest.java new file mode 100644 index 0000000..afeb75d --- /dev/null +++ b/src/test/java/org/busystack/caldav/calendar/CalendarDuplicateRecurrenceTest.java @@ -0,0 +1,36 @@ +package org.busystack.caldav.calendar; + +import static org.assertj.core.api.Assertions.*; +import static org.busystack.caldav.calendar.CalendarSemanticsTest.*; + +import org.junit.jupiter.api.Test; + +class CalendarDuplicateRecurrenceTest { + @Test + void recurrenceRangeParametersDoNotCreateADifferentInstanceIdentity() { + String components = + event(null, "20260101T100000Z", "RRULE:FREQ=DAILY;COUNT=3\r\n") + + event(":20260102T100000Z", "20260102T110000Z", "") + + event(";RANGE=THISANDFUTURE:20260102T100000Z", "20260102T120000Z", ""); + assertThatThrownBy(() -> parseComponents("VEVENT", components)) + .isInstanceOfSatisfying( + CalendarValidationException.class, + error -> + assertThat(error.reason()) + .isEqualTo(CalendarValidationException.Reason.INVALID_OBJECT)); + } + + @Test + void equivalentUtcAndTimezoneIdentifiersCannotDuplicateOneInstance() { + String components = + event(null, "20260101T100000Z", "RRULE:FREQ=DAILY;COUNT=3\r\n") + + event(":20260102T100000Z", "20260102T110000Z", "") + + event(";TZID=America/New_York:20260102T050000", "20260102T120000Z", ""); + assertThatThrownBy(() -> parseComponents("VEVENT", components)) + .isInstanceOfSatisfying( + CalendarValidationException.class, + error -> + assertThat(error.reason()) + .isEqualTo(CalendarValidationException.Reason.INVALID_OBJECT)); + } +} diff --git a/src/test/java/org/busystack/caldav/calendar/CalendarEdgeSemanticsTest.java b/src/test/java/org/busystack/caldav/calendar/CalendarEdgeSemanticsTest.java new file mode 100644 index 0000000..a348d82 --- /dev/null +++ b/src/test/java/org/busystack/caldav/calendar/CalendarEdgeSemanticsTest.java @@ -0,0 +1,196 @@ +package org.busystack.caldav.calendar; + +import static org.assertj.core.api.Assertions.*; +import static org.busystack.caldav.calendar.CalendarSemanticsTest.*; + +import java.time.*; +import java.util.*; +import org.busystack.caldav.calendar.query.*; +import org.busystack.caldav.calendar.query.CalendarFilter.*; +import org.junit.jupiter.api.Test; + +class CalendarEdgeSemanticsTest { + @Test + void relativeAlarmOutsideEventWindowStillMatchesRecurringParent() { + var c = + parse( + "VEVENT", + "DTSTART:20260101T100000Z\r\nRRULE:FREQ=DAILY;COUNT=5\r\nBEGIN:VALARM\r\nTRIGGER:-P2D\r\nACTION:DISPLAY\r\nDESCRIPTION:Stored reminder\r\nEND:VALARM\r\n"); + var alarm = + component( + "VALARM", range("2026-01-02T10:00:00Z", "2026-01-02T10:01:00Z"), List.of(), List.of()); + var filter = + component( + "VCALENDAR", + null, + List.of(), + List.of(component("VEVENT", null, List.of(), List.of(alarm)))); + assertThat(new CalendarQuery(QueryLimits.defaults()).matches(c, filter, UTC)).isTrue(); + } + + @Test + void repeatedAlarmCanReachAfterTheParentsStartQueryWindow() { + var c = + parse( + "VEVENT", + "DTSTART:20260101T100000Z\r\nRRULE:FREQ=DAILY;COUNT=5\r\nBEGIN:VALARM\r\nTRIGGER:PT1H\r\nREPEAT:2\r\nDURATION:P2D\r\nACTION:DISPLAY\r\nDESCRIPTION:Stored reminder\r\nEND:VALARM\r\n"); + var alarm = + component( + "VALARM", range("2026-01-07T11:00:00Z", "2026-01-07T11:01:00Z"), List.of(), List.of()); + var filter = + component( + "VCALENDAR", + null, + List.of(), + List.of(component("VEVENT", null, List.of(), List.of(alarm)))); + assertThat(new CalendarQuery(QueryLimits.defaults()).matches(c, filter, UTC)).isTrue(); + } + + @Test + void periodRdatesDistinguishNominalAndExactDaysAcrossDst() { + String event = + "DTSTART;TZID=America/New_York:20260301T120000\r\nRDATE;TZID=America/New_York;VALUE=PERIOD:20260307T120000/"; + assertThat(point(parse("VEVENT", event + "P1D\r\n"), "VEVENT", "2026-03-08T16:30:00Z")) + .isFalse(); + assertThat(point(parse("VEVENT", event + "PT24H\r\n"), "VEVENT", "2026-03-08T16:30:00Z")) + .isTrue(); + } + + @Test + void openStartQueryIncludesExplicitRdateBeforeDtstart() { + var c = + parse( + "VEVENT", + "DTSTART:20260110T100000Z\r\nRRULE:FREQ=DAILY;COUNT=3\r\nRDATE:20260101T100000Z\r\n"); + assertThat(match(c, "VEVENT", range(null, "2026-01-02T00:00:00Z"))).isTrue(); + } + + @Test + void staticPropertyRangeDoesNotMaterializeAnUnrelatedRecurrenceWindow() { + var c = + parse( + "VEVENT", + "DTSTART:20260101T100000Z\r\nRRULE:FREQ=SECONDLY\r\nCREATED:20200101T000000Z\r\n"); + var property = + new PropertyFilter( + "CREATED", + false, + range("2020-01-01T00:00:00Z", "2020-01-02T00:00:00Z"), + null, + List.of()); + var filter = + component( + "VCALENDAR", + null, + List.of(), + List.of(component("VEVENT", null, List.of(property), List.of()))); + assertThat(new CalendarQuery(QueryLimits.defaults()).matches(c, filter, UTC)).isTrue(); + } + + @Test + void statusTransparencyAndFreebusyTypesAreCaseInsensitive() { + var cancelled = + parse( + "VEVENT", "DTSTART:20260101T090000Z\r\nDTEND:20260101T120000Z\r\nSTATUS:cancelled\r\n"); + var transparent = + parse( + "VEVENT", + "DTSTART:20260101T090000Z\r\nDTEND:20260101T120000Z\r\nTRANSP:transparent\r\n"); + var freebusy = + parse( + "VFREEBUSY", + "DTSTART:20260101T000000Z\r\nDTEND:20260102T000000Z\r\nFREEBUSY;FBTYPE=busy-tentative:20260101T090000Z/20260101T100000Z\r\n"); + var result = + new CalendarFreeBusy(QueryLimits.defaults()) + .query( + List.of(cancelled, transparent, freebusy), + range("2026-01-01T00:00:00Z", "2026-01-02T00:00:00Z"), + UTC); + assertThat(result) + .containsExactly( + new CalendarFreeBusy.BusyPeriod( + Instant.parse("2026-01-01T09:00:00Z"), + Instant.parse("2026-01-01T10:00:00Z"), + CalendarFreeBusy.BusyType.BUSY_TENTATIVE)); + } + + @Test + void rangeAndAlarmRelatedEnumerationsAreCaseInsensitive() { + var c = + parseComponents( + "VEVENT", + event(null, "20260101T100000Z", "RRULE:FREQ=DAILY;COUNT=5\r\nDURATION:PT1H\r\n") + + event( + ";RANGE=thisandfuture:20260103T100000Z", + "20260103T120000Z", + "DURATION:PT2H\r\nBEGIN:VALARM\r\nTRIGGER;RELATED=end:-PT10M\r\nACTION:DISPLAY\r\nDESCRIPTION:Stored reminder\r\nEND:VALARM\r\n")); + var alarm = + component( + "VALARM", range("2026-01-04T13:50:00Z", "2026-01-04T13:51:00Z"), List.of(), List.of()); + var filter = + component( + "VCALENDAR", + null, + List.of(), + List.of(component("VEVENT", null, List.of(), List.of(alarm)))); + assertThat(new CalendarQuery(QueryLimits.defaults()).matches(c, filter, UTC)).isTrue(); + assertThat(new TextMatch("end", "i;octet", false).matches("END")).isFalse(); + } + + @Test + void nominalDurationsUseAConservativeWindowAcrossDifferentDstSeasons() { + var c = + parse( + "VEVENT", + "DTSTART;TZID=America/New_York:20260307T120000\r\nDURATION:P1D\r\nRRULE:FREQ=DAILY\r\n"); + assertThat(point(c, "VEVENT", "2026-11-01T16:30:00Z")).isTrue(); + } + + @Test + void localRecurrenceSearchBoundsDoNotReverseAcrossFallBackOverlap() { + var c = + parse( + "VEVENT", + "DTSTART;TZID=America/New_York:20261031T015955\r\nRRULE:FREQ=DAILY;COUNT=3\r\n"); + assertThat(match(c, "VEVENT", range("2026-11-01T05:59:50Z", "2026-11-01T06:00:10Z"))).isTrue(); + } + + @Test + void smallFiniteSeriesFromLongAgoDoesNotRequireSearchingEveryInterveningDay() { + var parsed = + parse( + "VEVENT", + "DTSTART:20000101T100000Z\r\nRRULE:FREQ=DAILY;COUNT=3\r\nRDATE:20260101T100000Z\r\n"); + assertThat(point(parsed, "VEVENT", "2026-01-01T10:00:00Z")).isTrue(); + assertThat(point(parsed, "VEVENT", "2026-01-02T10:00:00Z")).isFalse(); + } + + @Test + void finiteCountUsesPreGapOffsetEvenWhenTheQueryIsMuchLater() { + var parsed = + parse( + "VEVENT", + "DTSTART;TZID=America/New_York:20060331T023000\r\nRRULE:FREQ=DAILY;COUNT=3\r\n"); + assertThat(point(parsed, "VEVENT", "2006-04-02T07:30:00Z")).isTrue(); + assertThat(point(parsed, "VEVENT", "2006-04-03T06:30:00Z")).isFalse(); + assertThat(point(parsed, "VEVENT", "2026-01-01T10:00:00Z")).isFalse(); + } + + @Test + void expiredUntilSeriesAndFutureAnchorsDoNotCauseUnnecessaryHistoricalSearches() { + var expired = + parse( + "VEVENT", "DTSTART:20000101T100000Z\r\nRRULE:FREQ=SECONDLY;UNTIL=20000101T100003Z\r\n"); + assertThat(point(expired, "VEVENT", "2026-01-01T10:00:00Z")).isFalse(); + var future = parse("VEVENT", "DTSTART:20300101T100000Z\r\nRRULE:FREQ=DAILY;COUNT=3\r\n"); + assertThat(point(future, "VEVENT", "2026-01-01T10:00:00Z")).isFalse(); + } + + @Test + void invalidMonthDatesStillDoNotConsumeCountUnderVerifiedErratum4271() { + var parsed = parse("VEVENT", "DTSTART:20260131T100000Z\r\nRRULE:FREQ=MONTHLY;COUNT=3\r\n"); + assertThat(point(parsed, "VEVENT", "2026-03-31T10:00:00Z")).isTrue(); + assertThat(point(parsed, "VEVENT", "2026-05-31T10:00:00Z")).isTrue(); + assertThat(point(parsed, "VEVENT", "2026-07-31T10:00:00Z")).isFalse(); + } +} diff --git a/src/test/java/org/busystack/caldav/calendar/CalendarErrataTest.java b/src/test/java/org/busystack/caldav/calendar/CalendarErrataTest.java new file mode 100644 index 0000000..f521331 --- /dev/null +++ b/src/test/java/org/busystack/caldav/calendar/CalendarErrataTest.java @@ -0,0 +1,78 @@ +package org.busystack.caldav.calendar; + +import static org.assertj.core.api.Assertions.*; + +import java.time.ZoneOffset; +import java.util.List; +import org.busystack.caldav.calendar.query.CalendarDataProjection; +import org.busystack.caldav.calendar.query.FloatingTimezone; +import org.busystack.caldav.calendar.query.QueryLimits; +import org.junit.jupiter.api.Test; + +/** + * Independent lexical fixtures exercise verified RFC5545 corrections, not library serialization. + */ +class CalendarErrataTest { + @Test + void requestStatusCanOccurMoreThanOnceAndRetainsItsContentLineBoundaries() { + var parsed = + CalendarSemanticsTest.parse( + "VEVENT", + "DTSTART:20261027T100000Z\r\nREQUEST-STATUS:2.0;Success\r\nREQUEST-STATUS:2.1;Fallback\r\n"); + var event = parsed.calendar().getComponents().getFirst(); + assertThat(event.getProperties("REQUEST-STATUS")).hasSize(2); + String output = project(parsed, "REQUEST-STATUS"); + assertThat(output) + .contains("REQUEST-STATUS:2.0;Success\r\n", "REQUEST-STATUS:2.1;Fallback\r\n"); + } + + @Test + void aCommaInAQuotedContactAlternateUriRemainsPartOfTheUri() { + var parsed = + CalendarSemanticsTest.parse( + "VEVENT", + "DTSTART:20261027T100000Z\r\nCONTACT;ALTREP=\"ldap://example.test/o=ABC%20Industries,c=US???(cn=Jim%20Dolittle)\":Jim Dolittle\\, ABC Industries\r\n"); + assertThat( + parsed + .calendar() + .getComponents() + .getFirst() + .getRequiredProperty("CONTACT") + .getRequiredParameter("ALTREP") + .getValue()) + .isEqualTo("ldap://example.test/o=ABC%20Industries,c=US???(cn=Jim%20Dolittle)"); + assertThat(project(parsed, "CONTACT")) + .contains("o=ABC%20Industries,c=US???(cn=Jim%20Dolittle)"); + } + + @Test + void absoluteAlarmTriggerUsesDateTimeAndRepeatRetainsItsElapsedInterval() { + var parsed = + CalendarSemanticsTest.parse( + "VEVENT", + "DTSTART:20261027T100000Z\r\nBEGIN:VALARM\r\nACTION:AUDIO\r\nTRIGGER;VALUE=DATE-TIME:20261026T120000Z\r\nREPEAT:4\r\nDURATION:PT1H\r\nEND:VALARM\r\n"); + String output = parsed.calendar().toString(); + assertThat(output) + .contains( + "TRIGGER;VALUE=DATE-TIME:20261026T120000Z\r\n", "REPEAT:4\r\n", "DURATION:PT1H\r\n"); + } + + private String project(CalendarContent.Parsed parsed, String name) { + var component = + new CalendarDataProjection.ComponentSelection( + "VEVENT", + false, + false, + List.of(new CalendarDataProjection.PropertySelection(name, false)), + List.of()); + var root = + new CalendarDataProjection.ComponentSelection( + "VCALENDAR", false, false, List.of(), List.of(component)); + return new CalendarDataProjection(QueryLimits.defaults()) + .project( + parsed, + new CalendarDataProjection.Selection(root, null, null, null), + FloatingTimezone.of(ZoneOffset.UTC)) + .toString(); + } +} diff --git a/src/test/java/org/busystack/caldav/calendar/CalendarExpansionSemanticsTest.java b/src/test/java/org/busystack/caldav/calendar/CalendarExpansionSemanticsTest.java new file mode 100644 index 0000000..2510273 --- /dev/null +++ b/src/test/java/org/busystack/caldav/calendar/CalendarExpansionSemanticsTest.java @@ -0,0 +1,135 @@ +package org.busystack.caldav.calendar; + +import static org.assertj.core.api.Assertions.*; +import static org.busystack.caldav.calendar.CalendarSemanticsTest.*; + +import java.time.*; +import net.fortuna.ical4j.model.*; +import net.fortuna.ical4j.model.component.*; +import org.busystack.caldav.calendar.query.*; +import org.busystack.caldav.calendar.recurrence.*; +import org.junit.jupiter.api.Test; + +class CalendarExpansionSemanticsTest { + @Test + void expandedUtcTimesRetainExtensionParameters() { + var c = + parse( + "VEVENT", + "DTSTART;TZID=America/New_York;X-ORIGINAL=kept:20260307T120000\r\nDTEND;TZID=America/New_York;X-END=retained:20260307T130000\r\nRRULE:FREQ=DAILY;COUNT=2\r\n"); + var expanded = + new CalendarRecurrence(QueryLimits.defaults()) + .expand( + c, range("2026-03-07T00:00:00Z", "2026-03-10T00:00:00Z"), FloatingTimezone.of(UTC)); + assertThat(expanded.getComponents()).hasSize(2); + for (var event : expanded.getComponents()) { + assertThat( + event + .getRequiredProperty("DTSTART") + .getParameter("X-ORIGINAL") + .orElseThrow() + .getValue()) + .isEqualTo("kept"); + assertThat(event.getRequiredProperty("DTEND").getParameter("X-END").orElseThrow().getValue()) + .isEqualTo("retained"); + assertThat(event.getRequiredProperty("DTSTART").getParameter("TZID")).isEmpty(); + } + assertThat(c.calendar().toString()).contains("TZID=America/New_York"); + } + + @Test + void expandedAlarmInstantsPreserveNominalDayAndRepeatedTriggersAfterUtcConversion() { + var c = + parse( + "VEVENT", + "DTSTART;TZID=America/New_York:20260308T120000\r\nDURATION:PT1H\r\nRRULE:FREQ=DAILY;COUNT=2\r\nBEGIN:VALARM\r\nTRIGGER;X-TRIGGER=kept:-P1D\r\nREPEAT:1\r\nDURATION:PT24H\r\nACTION:DISPLAY\r\nDESCRIPTION:Stored reminder\r\nX-ALARM:preserved\r\nEND:VALARM\r\n"); + var expanded = + new CalendarRecurrence(QueryLimits.defaults()) + .expand( + c, range("2026-03-08T00:00:00Z", "2026-03-10T00:00:00Z"), FloatingTimezone.of(UTC)); + VEvent first = (VEvent) expanded.getComponents().getFirst(); + assertThat(first.getComponents()).hasSize(2); + assertThat(first.toString()) + .contains("20260307T170000Z", "20260308T170000Z", "X-TRIGGER=kept", "X-ALARM:preserved") + .doesNotContain("REPEAT:", "DURATION:", "TZID="); + for (VAlarm alarm : first.getAlarms()) { + var trigger = + (net.fortuna.ical4j.model.property.Trigger) alarm.getRequiredProperty("TRIGGER"); + assertThat(trigger.isAbsolute()).isTrue(); + } + assertThat(CalendarReports.copy(c).toString()) + .contains("TRIGGER;X-TRIGGER=kept:-P1D", "REPEAT:1", "DURATION:PT24H"); + } + + @Test + void explicitDatetimeExtensionsAreNormalizedWithoutDanglingTimezoneReferences() { + var parsed = + parse( + "VEVENT", + "DTSTART;TZID=America/New_York:20260308T120000\r\nX-WHEN;VALUE=DATE-TIME;TZID=America/New_York;X-CUSTOM=retained:20260307T120000,20260308T120000\r\n"); + var expanded = + new CalendarRecurrence(QueryLimits.defaults()) + .expand( + parsed, + range("2026-03-08T00:00:00Z", "2026-03-09T00:00:00Z"), + FloatingTimezone.of(UTC)); + assertThat(expanded.toString()) + .contains("X-WHEN;VALUE=DATE-TIME;X-CUSTOM=retained:20260307T170000Z,20260308T160000Z") + .doesNotContain("TZID="); + assertThat(parsed.calendar().toString()) + .contains("X-WHEN;VALUE=DATE-TIME;TZID=America/New_York"); + } + + @Test + void explicitPeriodExtensionsPreserveDurationMeaningWhileNormalizingTimezone() { + var parsed = + parse( + "VEVENT", + "DTSTART;TZID=America/New_York:20260308T120000\r\nX-PERIOD;VALUE=PERIOD;TZID=America/New_York:20260307T120000/PT24H\r\n"); + var expanded = + new CalendarRecurrence(QueryLimits.defaults()) + .expand( + parsed, + range("2026-03-08T00:00:00Z", "2026-03-09T00:00:00Z"), + FloatingTimezone.of(UTC)); + assertThat(expanded.toString()) + .contains("X-PERIOD;VALUE=PERIOD:20260307T170000Z/20260308T170000Z") + .doesNotContain("TZID="); + } + + @Test + void largeRecurringResourcesAreRejectedBeforeUnboundedCloneAmplification() { + var parsed = + parse( + "VEVENT", + "DTSTART:20260101T100000Z\r\nRRULE:FREQ=DAILY;COUNT=100\r\nX-LARGE:" + + "a".repeat(500000) + + "\r\n"); + assertThatThrownBy( + () -> + new CalendarRecurrence(QueryLimits.defaults()) + .expand( + parsed, + range("2026-01-01T00:00:00Z", "2026-04-11T00:00:00Z"), + FloatingTimezone.of(UTC))) + .isInstanceOfSatisfying( + QueryException.class, + error -> assertThat(error.kind()).isEqualTo(QueryException.Kind.LIMIT_EXCEEDED)); + } + + @Test + void expandedByteReservationIsCumulativeAcrossResourcesInOneReport() { + var parsed = + parse("VEVENT", "DTSTART:20260101T100000Z\r\nX-LARGE:" + "a".repeat(500000) + "\r\n"); + var recurrence = new CalendarRecurrence(QueryLimits.defaults()); + var range = range("2026-01-01T00:00:00Z", "2026-01-02T00:00:00Z"); + assertThatThrownBy( + () -> { + for (int index = 0; index < 10; index++) + recurrence.expand(parsed, range, FloatingTimezone.of(UTC)); + }) + .isInstanceOfSatisfying( + QueryException.class, + error -> assertThat(error.kind()).isEqualTo(QueryException.Kind.LIMIT_EXCEEDED)); + } +} diff --git a/src/test/java/org/busystack/caldav/calendar/CalendarFreeBusySourcesTest.java b/src/test/java/org/busystack/caldav/calendar/CalendarFreeBusySourcesTest.java new file mode 100644 index 0000000..18cfee0 --- /dev/null +++ b/src/test/java/org/busystack/caldav/calendar/CalendarFreeBusySourcesTest.java @@ -0,0 +1,58 @@ +package org.busystack.caldav.calendar; + +import static org.assertj.core.api.Assertions.*; + +import java.time.*; +import java.util.*; +import org.busystack.caldav.calendar.query.*; +import org.junit.jupiter.api.Test; + +class CalendarFreeBusySourcesTest { + @Test + void eachCollectionUsesItsOwnFloatingTimezone() { + var parsed = + CalendarContent.parse( + ("BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Own source fixtures//EN\r\nBEGIN:VEVENT\r\nUID:floating\r\nDTSTAMP:20260101T000000Z\r\nDTSTART:20260101T120000\r\nDURATION:PT1H\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n") + .getBytes(java.nio.charset.StandardCharsets.UTF_8), + "VEVENT"); + var range = + new TimeRange(Instant.parse("2026-01-01T00:00:00Z"), Instant.parse("2026-01-02T00:00:00Z")); + var sources = + List.of( + new CalendarFreeBusy.Source(parsed, FloatingTimezone.of(ZoneOffset.UTC)), + new CalendarFreeBusy.Source(parsed, FloatingTimezone.of(ZoneId.of("Asia/Kolkata")))); + var result = new CalendarFreeBusy(QueryLimits.defaults()).querySources(sources, range); + assertThat(result) + .containsExactly( + new CalendarFreeBusy.BusyPeriod( + Instant.parse("2026-01-01T06:30:00Z"), + Instant.parse("2026-01-01T07:30:00Z"), + CalendarFreeBusy.BusyType.BUSY), + new CalendarFreeBusy.BusyPeriod( + Instant.parse("2026-01-01T12:00:00Z"), + Instant.parse("2026-01-01T13:00:00Z"), + CalendarFreeBusy.BusyType.BUSY)); + } + + @Test + void oneBudgetCoversEverySourceIncludingTasksSkippedForFreeBusy() { + var parsed = + CalendarContent.parse( + ("BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Own source fixtures//EN\r\nBEGIN:VEVENT\r\nUID:event\r\nDTSTAMP:20260101T000000Z\r\nDTSTART:20260101T120000Z\r\nDURATION:PT1H\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n") + .getBytes(java.nio.charset.StandardCharsets.UTF_8), + "VEVENT"); + var source = new CalendarFreeBusy.Source(parsed, FloatingTimezone.of(ZoneOffset.UTC)); + var range = + new TimeRange(Instant.parse("2026-01-01T00:00:00Z"), Instant.parse("2026-01-02T00:00:00Z")); + var limited = new QueryLimits(2, 900, 500000, 3660, 256); + assertThat(new CalendarFreeBusy(limited).querySources(List.of(source), range)).hasSize(1); + assertThatThrownBy( + () -> + new CalendarFreeBusy(limited).querySources(List.of(source, source, source), range)) + .isInstanceOf(QueryException.class); + assertThat( + new CalendarFreeBusy(new QueryLimits(5, 900, 500000, 3660, 256)) + .querySources(List.of(source, source, source), range)) + .hasSize(1); + } +} diff --git a/src/test/java/org/busystack/caldav/calendar/CalendarParameterSemanticsTest.java b/src/test/java/org/busystack/caldav/calendar/CalendarParameterSemanticsTest.java new file mode 100644 index 0000000..8800081 --- /dev/null +++ b/src/test/java/org/busystack/caldav/calendar/CalendarParameterSemanticsTest.java @@ -0,0 +1,155 @@ +package org.busystack.caldav.calendar; + +import static org.assertj.core.api.Assertions.*; + +import java.time.ZoneOffset; +import java.util.List; +import net.fortuna.ical4j.model.parameter.*; +import org.busystack.caldav.calendar.query.*; +import org.busystack.caldav.calendar.query.CalendarFilter.*; +import org.junit.jupiter.api.Test; + +class CalendarParameterSemanticsTest { + private CalendarContent.Parsed parsed(String parameters) { + return CalendarSemanticsTest.parse( + "VEVENT", + "DTSTART:20260101T100000Z\r\nATTENDEE;" + parameters + ":mailto:owner@example.test\r\n"); + } + + @Test + void caretEscapesAreDecodedOnceFromLeftToRight() { + var p = parsed("CN=^^n;X-UNKNOWN=^N"); + assertThat( + p.calendar() + .getComponents() + .getFirst() + .getRequiredProperty("ATTENDEE") + .getRequiredParameter("CN") + .getValue()) + .isEqualTo("^n"); + assertThat( + p.calendar() + .getComponents() + .getFirst() + .getRequiredProperty("ATTENDEE") + .getRequiredParameter("X-UNKNOWN") + .getValue()) + .isEqualTo("^N"); + assertThat(matchesParameter(p, "CN", "^n")).isTrue(); + assertThat(matchesParameter(p, "CN", "\n")).isFalse(); + } + + @Test + void literalQuotePairIsNotMistakenForSyntacticQuoting() { + var p = parsed("CN=^'Babe^'"); + assertThat( + p.calendar() + .getComponents() + .getFirst() + .getRequiredProperty("ATTENDEE") + .getRequiredParameter("CN") + .getValue()) + .isEqualTo("\"Babe\""); + assertThat(matchesParameter(p, "CN", "\"Babe\"")).isTrue(); + } + + @Test + void quotedMemberListRetainsBothUrisIncludingAnEmbeddedComma() { + var p = + parsed( + "MEMBER=\"mailto:first@example.test\",\"mailto:second@example.test?subject=Hi,there\""); + Member member = + p.calendar() + .getComponents() + .getFirst() + .getRequiredProperty("ATTENDEE") + .getRequiredParameter("MEMBER"); + assertThat(member.getGroups().stream().map(java.net.URI::toString)) + .containsExactly( + "mailto:first@example.test", "mailto:second@example.test?subject=Hi,there"); + assertThat(matchesParameter(p, "MEMBER", "second@example.test?subject=Hi,there")).isTrue(); + } + + @Test + void delegationAndUnknownMultiValuedParametersRoundTripWithoutChangingTheirMeaning() { + var p = + parsed( + "DELEGATED-TO=\"mailto:first@example.test\",\"mailto:second@example.test\";X-LIST=^^n,^'quoted^'"); + DelegatedTo to = + p.calendar() + .getComponents() + .getFirst() + .getRequiredProperty("ATTENDEE") + .getRequiredParameter("DELEGATED-TO"); + assertThat(to.getDelegatees()).hasSize(2); + assertThat(matchesParameter(p, "X-LIST", "^n")).isTrue(); + assertThat(matchesParameter(p, "X-LIST", "\"quoted\"")).isTrue(); + String output = p.calendar().copy().toString(); + assertThat(output) + .contains( + "X-LIST=^^n,^'quoted^'", + "DELEGATED-TO=\"mailto:first@example.test\",\"mailto:second@example.test\""); + } + + @Test + void partialProjectionRetainsDecodedParametersAndCorrectWireEncoding() { + var p = parsed("CN=^^n;MEMBER=\"mailto:first@example.test\",\"mailto:second@example.test\""); + var event = + new CalendarDataProjection.ComponentSelection( + "VEVENT", + false, + false, + List.of(new CalendarDataProjection.PropertySelection("ATTENDEE", true)), + List.of()); + var root = + new CalendarDataProjection.ComponentSelection( + "VCALENDAR", false, false, List.of(), List.of(event)); + var selection = new CalendarDataProjection.Selection(root, null, null, null); + var output = + new CalendarDataProjection(QueryLimits.defaults()) + .project(p, selection, FloatingTimezone.of(ZoneOffset.UTC)) + .toString(); + assertThat(output) + .contains( + "ATTENDEE;CN=^^n;MEMBER=\"mailto:first@example.test\",\"mailto:second@example.test\":\r\n"); + } + + @Test + void aSingleValuedParameterCannotBecomeAList() { + assertThatThrownBy(() -> parsed("CN=Alice,Bob")).isInstanceOf(QueryException.class); + } + + @Test + void parameterAdapterKeepsQuotedDelimitersAndPlaceholderLiterals() { + var p = parsed("CN=\"BUSYSTACK;A:B,C=D\";X-LIST=BUSYSTACK,\"a;b:c,d\""); + assertThat(matchesParameter(p, "CN", "BUSYSTACK;A:B,C=D")).isTrue(); + assertThat(matchesParameter(p, "X-LIST", "a;b:c,d")).isTrue(); + } + + @Test + void malformedParameterHeadersNeverBecomeSafePlaceholders() { + for (String value : + List.of("CN=\"unterminated", "CN=\"Alice\"junk", "CN=Ali\"ce", "CN", "=Alice")) + assertThatThrownBy(() -> parsed(value)).isInstanceOf(RuntimeException.class); + } + + @Test + void parameterAdapterBoundsListValuesBeforeModelConstruction() { + assertThatThrownBy( + () -> parsed("X-LIST=" + String.join(",", java.util.Collections.nCopies(1001, "x")))) + .isInstanceOf(RuntimeException.class); + } + + private boolean matchesParameter(CalendarContent.Parsed parsed, String name, String text) { + var property = + new PropertyFilter( + "ATTENDEE", + false, + null, + null, + List.of(new ParameterFilter(name, false, new TextMatch(text, "i;octet", false)))); + var event = new ComponentFilter("VEVENT", false, null, List.of(property), List.of()); + var root = new ComponentFilter("VCALENDAR", false, null, List.of(), List.of(event)); + return new CalendarQuery(QueryLimits.defaults()).matches(parsed, root, ZoneOffset.UTC); + } +} diff --git a/src/test/java/org/busystack/caldav/calendar/CalendarSemanticsTest.java b/src/test/java/org/busystack/caldav/calendar/CalendarSemanticsTest.java new file mode 100644 index 0000000..961b69c --- /dev/null +++ b/src/test/java/org/busystack/caldav/calendar/CalendarSemanticsTest.java @@ -0,0 +1,357 @@ +package org.busystack.caldav.calendar; + +import static org.assertj.core.api.Assertions.*; + +import java.nio.charset.StandardCharsets; +import java.time.*; +import java.util.*; +import java.util.stream.Stream; +import org.busystack.caldav.calendar.query.*; +import org.busystack.caldav.calendar.query.CalendarFilter.*; +import org.busystack.caldav.calendar.recurrence.*; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +class CalendarSemanticsTest { + static final ZoneId UTC = ZoneOffset.UTC; + + static CalendarContent.Parsed parse(String type, String fields) { + return parseComponents( + type, + "BEGIN:" + + type + + "\r\nUID:semantic\r\nDTSTAMP:20260101T000000Z\r\n" + + fields + + "END:" + + type + + "\r\n"); + } + + static CalendarContent.Parsed parseComponents(String type, String components) { + components = withBuiltinTimezones(components); + return CalendarContent.parse( + ("BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//BusyStack original semantic fixtures//EN\r\n" + + components + + "END:VCALENDAR\r\n") + .getBytes(StandardCharsets.UTF_8), + type); + } + + static String withBuiltinTimezones(String components) { + for (String zone : List.of("America/New_York", "Europe/Berlin")) { + if (components.contains("TZID=" + zone) && !components.contains("TZID:" + zone)) { + String path = "/calendar/" + zone.replace('/', '-') + "-ical4j4.4.0.ics"; + try (var resource = CalendarSemanticsTest.class.getResourceAsStream(path)) { + if (resource == null) throw new IllegalStateException("Missing pinned timezone fixture"); + components = new String(resource.readAllBytes(), StandardCharsets.UTF_8) + components; + } catch (java.io.IOException ex) { + throw new java.io.UncheckedIOException(ex); + } + } + } + return components; + } + + static TimeRange range(String first, String last) { + return new TimeRange( + first == null ? null : Instant.parse(first), last == null ? null : Instant.parse(last)); + } + + static ComponentFilter component( + String type, + TimeRange range, + List properties, + List children) { + return new ComponentFilter(type, false, range, properties, children); + } + + static boolean match(CalendarContent.Parsed parsed, String type, TimeRange range) { + return new CalendarQuery(QueryLimits.defaults()) + .matches( + parsed, + component( + "VCALENDAR", + null, + List.of(), + List.of(component(type, range, List.of(), List.of()))), + UTC); + } + + static boolean point(CalendarContent.Parsed parsed, String type, String instant) { + var start = Instant.parse(instant); + return match(parsed, type, new TimeRange(start, start.plusSeconds(1))); + } + + static String event(String rid, String start, String extras) { + return "BEGIN:VEVENT\r\nUID:semantic\r\nDTSTAMP:20260101T000000Z\r\nDTSTART:" + + start + + "\r\n" + + (rid == null ? "" : "RECURRENCE-ID" + rid + "\r\n") + + extras + + "END:VEVENT\r\n"; + } + + static Stream taskCases() { + return Stream.of( + Arguments.of("", "2026-01-01T00:00:00Z", "2026-01-02T00:00:00Z", true), + Arguments.of( + "DTSTART:20260101T100000Z\r\n", "2026-01-01T10:00:00Z", "2026-01-01T10:01:00Z", true), + Arguments.of( + "DTSTART:20260101T100000Z\r\n", "2026-01-01T09:00:00Z", "2026-01-01T10:00:00Z", false), + Arguments.of( + "DUE:20260101T100000Z\r\n", "2026-01-01T09:00:00Z", "2026-01-01T10:00:00Z", true), + Arguments.of( + "DUE:20260101T100000Z\r\n", "2026-01-01T10:00:00Z", "2026-01-01T11:00:00Z", false), + Arguments.of( + "DTSTART:20260101T090000Z\r\nDURATION:PT1H\r\n", + "2026-01-01T10:00:00Z", + "2026-01-01T11:00:00Z", + true), + Arguments.of( + "DTSTART:20260101T090000Z\r\nDUE:20260101T100000Z\r\n", + "2026-01-01T10:00:00Z", + "2026-01-01T11:00:00Z", + false), + Arguments.of( + "COMPLETED:20260101T100000Z\r\n", "2026-01-01T09:00:00Z", "2026-01-01T10:00:00Z", true), + Arguments.of( + "CREATED:20260101T100000Z\r\n", "2026-01-01T09:00:00Z", "2026-01-01T10:00:00Z", false), + Arguments.of( + "CREATED:20260101T100000Z\r\n", "2027-01-01T09:00:00Z", "2027-01-01T10:00:00Z", true), + Arguments.of( + "CREATED:20260101T090000Z\r\nCOMPLETED:20260101T110000Z\r\n", + "2026-01-01T10:00:00Z", + "2026-01-01T10:01:00Z", + true), + Arguments.of( + "CREATED:20260101T090000Z\r\nCOMPLETED:20260101T110000Z\r\n", + "2026-01-01T12:00:00Z", + "2026-01-01T13:00:00Z", + false)); + } + + @ParameterizedTest + @MethodSource("taskCases") + void taskTimeRangesFollowAllRfcRows(String fields, String start, String end, boolean expected) { + assertThat(match(parse("VTODO", fields), "VTODO", range(start, end))).isEqualTo(expected); + } + + @Test + void zeroDurationEventsUsePointSemanticsAndAllDayEndIsExclusive() { + var event = parse("VEVENT", "DTSTART:20260101T100000Z\r\n"); + assertThat(point(event, "VEVENT", "2026-01-01T10:00:00Z")).isTrue(); + assertThat(match(event, "VEVENT", range("2026-01-01T09:00:00Z", "2026-01-01T10:00:00Z"))) + .isFalse(); + var day = parse("VEVENT", "DTSTART;VALUE=DATE:20260228\r\n"); + assertThat(point(day, "VEVENT", "2026-02-28T23:59:59Z")).isTrue(); + assertThat(point(day, "VEVENT", "2026-03-01T00:00:00Z")).isFalse(); + } + + @Test + void nominalAndExactDayDurationsRemainDistinctAcrossDst() { + String base = "DTSTART;TZID=America/New_York:20260307T120000\r\nDURATION:"; + assertThat(point(parse("VEVENT", base + "P1D\r\n"), "VEVENT", "2026-03-08T16:30:00Z")) + .isFalse(); + assertThat(point(parse("VEVENT", base + "PT24H\r\n"), "VEVENT", "2026-03-08T16:30:00Z")) + .isTrue(); + assertThat(point(parse("VEVENT", base + "P1DT1H\r\n"), "VEVENT", "2026-03-08T17:30:00Z")) + .isFalse(); + } + + @Test + void recurrenceUnionExclusionAndFiniteCountDoNotLoseRdates() { + var c = + parse( + "VEVENT", + "DTSTART:20260101T100000Z\r\nRRULE:FREQ=DAILY;COUNT=5\r\nRDATE:20260110T100000Z\r\nEXDATE:20260102T100000Z\r\n"); + assertThat(point(c, "VEVENT", "2026-01-02T10:00:00Z")).isFalse(); + assertThat(point(c, "VEVENT", "2026-01-03T10:00:00Z")).isTrue(); + assertThat(point(c, "VEVENT", "2026-01-06T10:00:00Z")).isFalse(); + assertThat(point(c, "VEVENT", "2026-01-10T10:00:00Z")).isTrue(); + } + + @Test + void detachedMoveSuppressesOriginalAndIncludesMovedTimeOutsideOriginalQuery() { + var c = + parseComponents( + "VEVENT", + event(null, "20260101T100000Z", "RRULE:FREQ=DAILY;COUNT=5\r\n") + + event(":20260103T100000Z", "20260120T100000Z", "")); + assertThat(point(c, "VEVENT", "2026-01-03T10:00:00Z")).isFalse(); + assertThat(point(c, "VEVENT", "2026-01-20T10:00:00Z")).isTrue(); + } + + @Test + void rangeShiftsFutureButLeavesExplicitDetachedInstancesIndependent() { + var c = + parseComponents( + "VEVENT", + event(null, "20260101T100000Z", "RRULE:FREQ=DAILY;COUNT=5\r\nDURATION:PT1H\r\n") + + event( + ";RANGE=THISANDFUTURE:20260103T100000Z", + "20260103T120000Z", + "DURATION:PT2H\r\n") + + event(":20260105T100000Z", "20260105T090000Z", "DURATION:PT1H\r\n")); + assertThat(point(c, "VEVENT", "2026-01-04T10:00:00Z")).isFalse(); + assertThat(point(c, "VEVENT", "2026-01-04T13:00:00Z")).isTrue(); + assertThat(point(c, "VEVENT", "2026-01-05T09:00:00Z")).isTrue(); + assertThat(point(c, "VEVENT", "2026-01-05T12:00:00Z")).isFalse(); + } + + @Test + void nonexistentZonedOccurrencesUsePreGapOffsetAndConsumeCount() { + var c = + parse( + "VEVENT", + "DTSTART;TZID=America/New_York:20260307T023000\r\nRRULE:FREQ=DAILY;COUNT=3\r\n"); + assertThat(point(c, "VEVENT", "2026-03-07T07:30:00Z")).isTrue(); + assertThat(point(c, "VEVENT", "2026-03-08T07:30:00Z")).isTrue(); + assertThat(point(c, "VEVENT", "2026-03-09T06:30:00Z")).isTrue(); + assertThat(point(c, "VEVENT", "2026-03-10T06:30:00Z")).isFalse(); + assertThat(point(c, "VEVENT", "2026-03-11T06:30:00Z")).isFalse(); + } + + @Test + void leapDayRecurrenceUsesCalendarLibraryRules() { + var c = parse("VEVENT", "DTSTART;VALUE=DATE:20240229\r\nRRULE:FREQ=YEARLY\r\n"); + assertThat(point(c, "VEVENT", "2028-02-29T12:00:00Z")).isTrue(); + assertThat(point(c, "VEVENT", "2025-02-28T12:00:00Z")).isFalse(); + } + + @Test + void floatingDatesUseReportTimezoneWithoutChangingZonedData() { + var c = parse("VEVENT", "DTSTART:20260101T120000\r\n"); + var filter = + component( + "VCALENDAR", + null, + List.of(), + List.of( + component( + "VEVENT", + range("2026-01-01T06:30:00Z", "2026-01-01T06:31:00Z"), + List.of(), + List.of()))); + assertThat( + new CalendarQuery(QueryLimits.defaults()).matches(c, filter, ZoneId.of("Asia/Kolkata"))) + .isTrue(); + assertThat(new CalendarQuery(QueryLimits.defaults()).matches(c, filter, UTC)).isFalse(); + } + + @Test + void customEmbeddedTimezoneIsAuthoritative() { + String timezone = + "BEGIN:VTIMEZONE\r\nTZID:Custom/Offset\r\nBEGIN:STANDARD\r\nDTSTART:19700101T000000\r\nTZOFFSETFROM:+0530\r\nTZOFFSETTO:+0530\r\nEND:STANDARD\r\nEND:VTIMEZONE\r\n"; + var c = + parseComponents( + "VEVENT", + timezone + + "BEGIN:VEVENT\r\nUID:semantic\r\nDTSTAMP:20260101T000000Z\r\nDTSTART;TZID=Custom/Offset:20260101T120000\r\nEND:VEVENT\r\n"); + assertThat(point(c, "VEVENT", "2026-01-01T06:30:00Z")).isTrue(); + var floating = + FloatingTimezone.parse( + "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Own timezone fixture//EN\r\n" + + timezone + + "END:VCALENDAR\r\n"); + assertThat(floating.zoneId().getRules().getOffset(Instant.parse("2026-01-01T00:00:00Z"))) + .isEqualTo(ZoneOffset.ofHoursMinutes(5, 30)); + } + + @Test + void propertiesParametersAndNegatedTextUseDeclaredCollations() { + var c = + parse( + "VEVENT", + "DTSTART:20260101T100000Z\r\nSUMMARY:Team Ä meeting\r\nATTENDEE;CN=Alice;PARTSTAT=ACCEPTED:mailto:alice@example.test\r\n"); + var props = + List.of( + new PropertyFilter( + "summary", false, null, new TextMatch("TEAM Ä", null, false), List.of()), + new PropertyFilter( + "ATTENDEE", + false, + null, + new TextMatch("ALICE", null, false), + List.of( + new ParameterFilter( + "PARTSTAT", false, new TextMatch("accepted", null, false))))); + var filter = + component( + "VCALENDAR", null, List.of(), List.of(component("VEVENT", null, props, List.of()))); + assertThat(new CalendarQuery(QueryLimits.defaults()).matches(c, filter, UTC)).isTrue(); + assertThat(new TextMatch("ä", null, false).matches("Ä")).isFalse(); + assertThat(new TextMatch("team", "i;octet", false).matches("Team")).isFalse(); + assertThat(new TextMatch("cancelled", null, true).matches("CONFIRMED")).isTrue(); + assertThatThrownBy(() -> new TextMatch("x", "i;unicode-casemap", false)) + .isInstanceOf(QueryException.class); + } + + @Test + void repeatedNestedAlarmsUseStoredTriggerAndParentOccurrence() { + var c = + parse( + "VEVENT", + "DTSTART:20260101T100000Z\r\nDURATION:PT1H\r\nBEGIN:VALARM\r\nTRIGGER:-PT10M\r\nREPEAT:2\r\nDURATION:PT5M\r\nACTION:DISPLAY\r\nDESCRIPTION:Reminder\r\nEND:VALARM\r\n"); + var alarm = + component( + "VALARM", range("2026-01-01T09:55:00Z", "2026-01-01T09:56:00Z"), List.of(), List.of()); + var filter = + component( + "VCALENDAR", + null, + List.of(), + List.of(component("VEVENT", null, List.of(), List.of(alarm)))); + assertThat(new CalendarQuery(QueryLimits.defaults()).matches(c, filter, UTC)).isTrue(); + } + + @Test + void freeBusyHonorsStatusTransparencyAndCoalescesOnlyEqualTypes() { + var a = parse("VEVENT", "DTSTART:20260101T090000Z\r\nDTEND:20260101T100000Z\r\n"); + var b = parse("VEVENT", "DTSTART:20260101T100000Z\r\nDTEND:20260101T110000Z\r\n"); + var tentative = + parse( + "VEVENT", "DTSTART:20260101T093000Z\r\nDTEND:20260101T103000Z\r\nSTATUS:TENTATIVE\r\n"); + var transparent = + parse( + "VEVENT", + "DTSTART:20260101T090000Z\r\nDTEND:20260101T120000Z\r\nTRANSP:TRANSPARENT\r\n"); + var cancelled = + parse( + "VEVENT", "DTSTART:20260101T090000Z\r\nDTEND:20260101T120000Z\r\nSTATUS:CANCELLED\r\n"); + var periods = + new CalendarFreeBusy(QueryLimits.defaults()) + .query( + List.of(a, b, tentative, transparent, cancelled), + range("2026-01-01T00:00:00Z", "2026-01-02T00:00:00Z"), + UTC); + assertThat(periods) + .containsExactly( + new CalendarFreeBusy.BusyPeriod( + Instant.parse("2026-01-01T09:00:00Z"), + Instant.parse("2026-01-01T11:00:00Z"), + CalendarFreeBusy.BusyType.BUSY), + new CalendarFreeBusy.BusyPeriod( + Instant.parse("2026-01-01T09:30:00Z"), + Instant.parse("2026-01-01T10:30:00Z"), + CalendarFreeBusy.BusyType.BUSY_TENTATIVE)); + } + + @Test + void finiteWorkLimitsFailRatherThanReturnTruncatedAnswers() { + var c = parse("VEVENT", "DTSTART:20260101T100000Z\r\nRRULE:FREQ=SECONDLY\r\n"); + assertThat(point(c, "VEVENT", "2026-01-01T10:00:30Z")).isTrue(); + assertThatThrownBy( + () -> match(c, "VEVENT", range("2026-01-01T00:00:00Z", "2026-01-02T00:00:00Z"))) + .isInstanceOf(QueryException.class); + } + + @Test + void hostileTimezoneRecurrenceIsRejectedBeforeRuleMaterialization() { + String zone = + "BEGIN:VTIMEZONE\r\nTZID:Hostile/Zone\r\nBEGIN:STANDARD\r\nDTSTART:19000101T000000\r\nTZOFFSETFROM:+0100\r\nTZOFFSETTO:+0200\r\nRRULE:FREQ=SECONDLY\r\nEND:STANDARD\r\nEND:VTIMEZONE\r\n"; + assertThatThrownBy(() -> parseComponents("VEVENT", zone + event(null, "20260101T100000Z", ""))) + .isInstanceOf(QueryException.class); + } +} diff --git a/src/test/java/org/busystack/caldav/calendar/CalendarTextProfileTest.java b/src/test/java/org/busystack/caldav/calendar/CalendarTextProfileTest.java new file mode 100644 index 0000000..d8024ec --- /dev/null +++ b/src/test/java/org/busystack/caldav/calendar/CalendarTextProfileTest.java @@ -0,0 +1,30 @@ +package org.busystack.caldav.calendar; + +import static org.assertj.core.api.Assertions.*; + +import java.nio.charset.StandardCharsets; +import org.junit.jupiter.api.Test; + +class CalendarTextProfileTest { + @Test + void unsupportedControlsAndXmlNoncharactersAreRejectedBeforeCalendarParsing() { + for (int invalid : new int[] {0, 1, 8, 11, 12, 31, 127, 0xfffe, 0xffff}) { + String text = + "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Own text fixtures//EN\r\nBEGIN:VTODO\r\nUID:text\r\nDTSTAMP:20260101T000000Z\r\nSUMMARY:a" + + new String(Character.toChars(invalid)) + + "b\r\nEND:VTODO\r\nEND:VCALENDAR\r\n"; + assertThatThrownBy( + () -> CalendarContent.parse(text.getBytes(StandardCharsets.UTF_8), "VTODO")) + .isInstanceOf(IllegalArgumentException.class); + } + } + + @Test + void tabsCrlfFoldsAndSupplementaryUnicodeRemainAcceptedAndUnmodified() { + String text = + "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Own text fixtures//EN\r\nBEGIN:VTODO\r\nUID:text\r\nDTSTAMP:20260101T000000Z\r\nSUMMARY:買い物 🗓️\r\n\tcontinued\r\nEND:VTODO\r\nEND:VCALENDAR\r\n"; + byte[] bytes = text.getBytes(StandardCharsets.UTF_8), original = bytes.clone(); + assertThat(CalendarContent.parse(bytes, "VTODO").uid()).isEqualTo("text"); + assertThat(bytes).isEqualTo(original); + } +} diff --git a/src/test/java/org/busystack/caldav/calendar/CalendarTimezoneCoverageTest.java b/src/test/java/org/busystack/caldav/calendar/CalendarTimezoneCoverageTest.java new file mode 100644 index 0000000..ee7ec20 --- /dev/null +++ b/src/test/java/org/busystack/caldav/calendar/CalendarTimezoneCoverageTest.java @@ -0,0 +1,209 @@ +package org.busystack.caldav.calendar; + +import static org.assertj.core.api.Assertions.*; +import static org.busystack.caldav.calendar.CalendarSemanticsTest.*; + +import java.time.*; +import java.util.*; +import org.busystack.caldav.calendar.query.*; +import org.busystack.caldav.calendar.query.CalendarFilter.*; +import org.busystack.caldav.calendar.recurrence.*; +import org.junit.jupiter.api.Test; + +class CalendarTimezoneCoverageTest { + private static String timezone(String rule) { + return "BEGIN:VTIMEZONE\r\nTZID:Custom/Bounded\r\nBEGIN:STANDARD\r\nDTSTART:20001029T030000\r\nTZOFFSETFROM:+0200\r\nTZOFFSETTO:+0100\r\nRRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU" + + rule + + "\r\nEND:STANDARD\r\nBEGIN:DAYLIGHT\r\nDTSTART:20000326T020000\r\nTZOFFSETFROM:+0100\r\nTZOFFSETTO:+0200\r\nRRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU" + + rule + + "\r\nEND:DAYLIGHT\r\nEND:VTIMEZONE\r\n"; + } + + private static FloatingTimezone floating(String rule) { + return FloatingTimezone.parse( + "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//BusyStack coverage fixture//EN\r\n" + + timezone(rule) + + "END:VCALENDAR\r\n"); + } + + @Test + void unboundedIntervalRuleRetainsItsActualTwoYearCycleWithinCoverage() { + var zone = floating(";INTERVAL=2"); + assertThat(zone.coverageEnd()).isNotNull(); + assertThat(zone.zoneId().getRules().getOffset(Instant.parse("2028-06-01T00:00:00Z"))) + .isEqualTo(ZoneOffset.ofHours(2)); + assertThat(zone.zoneId().getRules().getOffset(Instant.parse("2029-06-01T00:00:00Z"))) + .isEqualTo(ZoneOffset.ofHours(1)); + assertThat(zone.zoneId().getRules().getTransitionRules()).isEmpty(); + } + + @Test + void floatingQueriesFailOutsideCoverageInsteadOfUsingTheFinalOffsetTail() { + var zone = floating(";INTERVAL=2"); + var parsed = parse("VEVENT", "DTSTART:20290601T120000\r\n"); + var filter = + component( + "VCALENDAR", + null, + List.of(), + List.of( + component( + "VEVENT", + range("2029-06-01T11:00:00Z", "2029-06-01T11:01:00Z"), + List.of(), + List.of()))); + assertThat(new CalendarQuery(QueryLimits.defaults()).matches(parsed, filter, zone)).isTrue(); + var far = + component( + "VCALENDAR", + null, + List.of(), + List.of( + component( + "VEVENT", + range("2090-06-01T00:00:00Z", "2090-06-02T00:00:00Z"), + List.of(), + List.of()))); + assertThatThrownBy(() -> new CalendarQuery(QueryLimits.defaults()).matches(parsed, far, zone)) + .isInstanceOfSatisfying( + QueryException.class, + e -> assertThat(e.kind()).isEqualTo(QueryException.Kind.LIMIT_EXCEEDED)); + } + + @Test + void embeddedCoverageSurvivesReparseAndProtectsExpansion() { + String resource = + timezone(";INTERVAL=2") + + "BEGIN:VEVENT\r\nUID:semantic\r\nDTSTAMP:20260101T000000Z\r\nDTSTART;TZID=Custom/Bounded:20290601T120000\r\nEND:VEVENT\r\n"; + var parsed = parseComponents("VEVENT", resource); + var again = parseComponents("VEVENT", resource); + assertThat(parsed.zoneCoverage()).isNotEmpty(); + assertThat(again.zoneCoverage()).isNotEmpty(); + assertThat(point(parsed, "VEVENT", "2029-06-01T11:00:00Z")).isTrue(); + assertThat(CalendarReports.copy(parsed).toString()) + .contains("INTERVAL=2", "TZID:Custom/Bounded"); + assertThatThrownBy( + () -> + new CalendarRecurrence(QueryLimits.defaults()) + .expand( + again, + range("2090-06-01T00:00:00Z", "2090-06-02T00:00:00Z"), + FloatingTimezone.of(UTC))) + .isInstanceOf(QueryException.class); + } + + @Test + void aFarFutureObjectCanBeStoredButCannotBeInterpretedBeyondItsTimezoneCoverage() { + var parsed = + parseComponents( + "VEVENT", + timezone(";INTERVAL=2") + + "BEGIN:VEVENT\r\nUID:semantic\r\nDTSTAMP:20260101T000000Z\r\nDTSTART;TZID=Custom/Bounded:20900601T120000\r\nEND:VEVENT\r\n"); + assertThat(CalendarReports.copy(parsed).toString()).contains("20900601T120000"); + assertThatThrownBy(() -> point(parsed, "VEVENT", "2029-06-01T11:00:00Z")) + .isInstanceOf(QueryException.class); + } + + @Test + void unsupportedAnnualShapesUseBoundedGenerationRatherThanSimplifiedAnnualRules() { + for (String rule : + List.of( + ";BYSETPOS=1", ";BYMONTH=3,4", ";BYDAY=1SU,-1SU", ";BYYEARDAY=85", ";BYWEEKNO=13")) { + String text = timezone(""); + String adjusted = + rule.startsWith(";BYMONTH=") + ? text.replace(";BYMONTH=3", rule) + : rule.startsWith(";BYDAY=") + ? text.replace(";BYDAY=-1SU", rule) + : text.replace(";BYDAY=-1SU", ";BYDAY=-1SU" + rule); + if (rule.equals(";BYWEEKNO=13")) { + // Different observances must not prescribe conflicting offsets at the same UTC onset. + // Keep this a valid unsupported-shape coverage fixture, with separate seasonal weeks. + adjusted = + adjusted.replace( + "BYMONTH=10;BYDAY=-1SU;BYWEEKNO=13", "BYMONTH=10;BYDAY=-1SU;BYWEEKNO=43"); + String invalid = adjusted; + assertThatThrownBy( + () -> + FloatingTimezone.parse( + "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//BusyStack coverage fixture//EN\r\n" + + invalid + + "END:VCALENDAR\r\n")) + .isInstanceOfSatisfying( + QueryException.class, + error -> assertThat(error.kind()).isEqualTo(QueryException.Kind.INVALID_DATA)); + // RFC5545§3.3.10 forbids numeric BYDAY together with YEARLY BYWEEKNO. + // Unnumbered Sundays retain the valid unsupported-shape coverage assertion below. + adjusted = adjusted.replace("BYDAY=-1SU;BYWEEKNO=", "BYDAY=SU;BYWEEKNO="); + } + var zone = + FloatingTimezone.parse( + "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//BusyStack coverage fixture//EN\r\n" + + adjusted + + "END:VCALENDAR\r\n"); + assertThat(zone.coverageEnd()).as(rule).isNotNull(); + assertThat(zone.zoneId().getRules().getTransitionRules()).isEmpty(); + } + } + + @Test + void paddedAlarmSearchCannotRunPastFloatingTimezoneCoverage() { + var zone = floating(";INTERVAL=2"); + var near = zone.coverageEnd().minus(Duration.ofDays(30)); + String start = + new net.fortuna.ical4j.model.TemporalAdapter<>(near.atZone(UTC).toLocalDateTime()) + .toString(); + var parsed = + parse( + "VEVENT", + "DTSTART:" + + start + + "\r\nRRULE:FREQ=DAILY;COUNT=3\r\nBEGIN:VALARM\r\nTRIGGER:-P100D\r\nACTION:DISPLAY\r\nDESCRIPTION:Stored reminder\r\nEND:VALARM\r\n"); + var alarm = component("VALARM", new TimeRange(near, near.plusSeconds(1)), List.of(), List.of()); + var filter = + component( + "VCALENDAR", + null, + List.of(), + List.of(component("VEVENT", null, List.of(), List.of(alarm)))); + assertThatThrownBy( + () -> new CalendarQuery(QueryLimits.defaults()).matches(parsed, filter, zone)) + .isInstanceOfSatisfying( + QueryException.class, + e -> assertThat(e.kind()).isEqualTo(QueryException.Kind.LIMIT_EXCEEDED)); + } + + @Test + void futureRdatesDoNotSuppressInterveningAnnualRuleTransitions() { + var text = + timezone("") + .replace( + "RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU", + "RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU\r\nRDATE:20300331T020000") + .replace( + "RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU", + "RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU\r\nRDATE:20301027T030000"); + var zone = + FloatingTimezone.parse( + "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//BusyStack coverage fixture//EN\r\n" + + text + + "END:VCALENDAR\r\n"); + assertThat(zone.coverageEnd()).isNull(); + assertThat(zone.zoneId().getRules().getOffset(Instant.parse("2029-06-01T00:00:00Z"))) + .isEqualTo(ZoneOffset.ofHours(2)); + assertThat(zone.zoneId().getRules().getOffset(Instant.parse("2031-06-01T00:00:00Z"))) + .isEqualTo(ZoneOffset.ofHours(2)); + } + + @Test + void explicitPeriodDatesAreCheckedBeforeOutOfCoverageMatching() { + var parsed = + parseComponents( + "VEVENT", + timezone(";INTERVAL=2") + + "BEGIN:VEVENT\r\nUID:semantic\r\nDTSTAMP:20260101T000000Z\r\nDTSTART;TZID=Custom/Bounded:20290601T120000\r\nRDATE;TZID=Custom/Bounded;VALUE=PERIOD:20900601T120000/PT24H\r\nEND:VEVENT\r\n"); + assertThat(CalendarReports.copy(parsed).toString()).contains("20900601T120000/PT24H"); + assertThatThrownBy(() -> point(parsed, "VEVENT", "2029-06-01T11:00:00Z")) + .isInstanceOf(QueryException.class); + } +} diff --git a/src/test/java/org/busystack/caldav/calendar/CalendarTimezoneRdateTest.java b/src/test/java/org/busystack/caldav/calendar/CalendarTimezoneRdateTest.java new file mode 100644 index 0000000..5e26d13 --- /dev/null +++ b/src/test/java/org/busystack/caldav/calendar/CalendarTimezoneRdateTest.java @@ -0,0 +1,139 @@ +package org.busystack.caldav.calendar; + +import static org.assertj.core.api.Assertions.*; +import static org.busystack.caldav.calendar.CalendarSemanticsTest.*; + +import java.time.*; +import org.busystack.caldav.calendar.query.*; +import org.busystack.caldav.calendar.recurrence.*; +import org.junit.jupiter.api.Test; + +class CalendarTimezoneRdateTest { + private static final String TIMEZONE = + "BEGIN:VTIMEZONE\r\nTZID:Custom/FutureDates\r\nBEGIN:STANDARD\r\nDTSTART:20001029T030000\r\nTZOFFSETFROM:+0200\r\nTZOFFSETTO:+0100\r\nRDATE:20301027T030000\r\nEND:STANDARD\r\nBEGIN:DAYLIGHT\r\nDTSTART:20000326T020000\r\nTZOFFSETFROM:+0100\r\nTZOFFSETTO:+0200\r\nRDATE:20300331T020000\r\nEND:DAYLIGHT\r\nEND:VTIMEZONE\r\n"; + + @Test + void explicitObservanceDatesRetainHistoricalAndFutureTransitions() { + var timezone = + FloatingTimezone.parse( + "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//BusyStack original timezone fixture//EN\r\n" + + TIMEZONE + + "END:VCALENDAR\r\n"); + var rules = timezone.zoneId().getRules(); + assertThat(rules.getOffset(Instant.parse("2000-06-01T00:00:00Z"))) + .isEqualTo(ZoneOffset.ofHours(2)); + assertThat(rules.getOffset(Instant.parse("2029-06-01T00:00:00Z"))) + .isEqualTo(ZoneOffset.ofHours(1)); + assertThat(rules.getOffset(Instant.parse("2030-06-01T00:00:00Z"))) + .isEqualTo(ZoneOffset.ofHours(2)); + assertThat(rules.getOffset(Instant.parse("2030-12-01T00:00:00Z"))) + .isEqualTo(ZoneOffset.ofHours(1)); + } + + @Test + void embeddedFutureTransitionsDriveQueryAndExpandedUtcRepresentation() { + var parsed = + parseComponents( + "VEVENT", + TIMEZONE + + "BEGIN:VEVENT\r\nUID:semantic\r\nDTSTAMP:20260101T000000Z\r\nDTSTART;TZID=Custom/FutureDates:20300601T120000\r\nDTEND;TZID=Custom/FutureDates:20300601T130000\r\nEND:VEVENT\r\n"); + assertThat(point(parsed, "VEVENT", "2030-06-01T10:30:00Z")).isTrue(); + assertThat(point(parsed, "VEVENT", "2030-06-01T11:30:00Z")).isFalse(); + var expanded = + new CalendarRecurrence(QueryLimits.defaults()) + .expand( + parsed, + range("2030-06-01T00:00:00Z", "2030-06-02T00:00:00Z"), + FloatingTimezone.of(UTC)); + assertThat(expanded.toString()) + .contains("DTSTART:20300601T100000Z", "DTEND:20300601T110000Z") + .doesNotContain("BEGIN:VTIMEZONE"); + } + + private static FloatingTimezone annual(String standardEnd, String daylightEnd) { + var text = + TIMEZONE + .replace( + "RDATE:20301027T030000", "RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU" + standardEnd) + .replace( + "RDATE:20300331T020000", "RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU" + daylightEnd); + return FloatingTimezone.parse( + "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//BusyStack original timezone fixture//EN\r\n" + + text + + "END:VCALENDAR\r\n"); + } + + @Test + void futureUntilRulesStopAfterTheirFinalOnset() { + var zone = annual(";UNTIL=20301027T010000Z", ";UNTIL=20300331T010000Z"); + assertThat(zone.zoneId().getRules().getOffset(Instant.parse("2030-06-01T00:00:00Z"))) + .isEqualTo(ZoneOffset.ofHours(2)); + assertThat(zone.zoneId().getRules().getOffset(Instant.parse("2031-06-01T00:00:00Z"))) + .isEqualTo(ZoneOffset.ofHours(1)); + } + + @Test + void pastUntilRulesDoNotResurrectHistoricalDaylightSaving() { + var zone = annual(";UNTIL=20011028T010000Z", ";UNTIL=20010325T010000Z"); + assertThat(zone.zoneId().getRules().getOffset(Instant.parse("2001-06-01T00:00:00Z"))) + .isEqualTo(ZoneOffset.ofHours(2)); + assertThat(zone.zoneId().getRules().getOffset(Instant.parse("2002-06-01T00:00:00Z"))) + .isEqualTo(ZoneOffset.ofHours(1)); + } + + @Test + void pastCountRulesAreMaterializedOnlyForTheirFiniteInstances() { + var zone = annual(";COUNT=2", ";COUNT=2"); + assertThat(zone.zoneId().getRules().getOffset(Instant.parse("2001-06-01T00:00:00Z"))) + .isEqualTo(ZoneOffset.ofHours(2)); + assertThat(zone.zoneId().getRules().getOffset(Instant.parse("2002-06-01T00:00:00Z"))) + .isEqualTo(ZoneOffset.ofHours(1)); + } + + @Test + void futureCountRulesStopAfterTheirFinalInstance() { + var zone = annual(";COUNT=31", ";COUNT=31"); + assertThat(zone.zoneId().getRules().getOffset(Instant.parse("2030-06-01T00:00:00Z"))) + .isEqualTo(ZoneOffset.ofHours(2)); + assertThat(zone.zoneId().getRules().getOffset(Instant.parse("2031-06-01T00:00:00Z"))) + .isEqualTo(ZoneOffset.ofHours(1)); + } + + @Test + void validUnboundedAnnualRulesRemainUnbounded() { + var zone = annual("", ""); + assertThat(zone.zoneId().getRules().getOffset(Instant.parse("2031-06-01T00:00:00Z"))) + .isEqualTo(ZoneOffset.ofHours(2)); + assertThat(zone.zoneId().getRules().getOffset(Instant.parse("2031-12-01T00:00:00Z"))) + .isEqualTo(ZoneOffset.ofHours(1)); + } + + @Test + void finiteFutureWorkIsCheckedBeforeEitherBuilderMaterializesIt() { + assertThatThrownBy(() -> annual(";UNTIL=99991031T010000Z", ";UNTIL=99990328T010000Z")) + .isInstanceOfSatisfying( + QueryException.class, + error -> assertThat(error.kind()).isEqualTo(QueryException.Kind.LIMIT_EXCEEDED)); + assertThatThrownBy(() -> annual(";COUNT=100000", ";COUNT=100000")) + .isInstanceOfSatisfying( + QueryException.class, + error -> assertThat(error.kind()).isEqualTo(QueryException.Kind.LIMIT_EXCEEDED)); + } + + @Test + void annualRulesWithoutBymonthInheritTheirDtstartMonth() { + var text = + TIMEZONE + .replace("RDATE:20301027T030000", "RRULE:FREQ=YEARLY") + .replace("RDATE:20300331T020000", "RRULE:FREQ=YEARLY"); + var zone = + FloatingTimezone.parse( + "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//BusyStack original timezone fixture//EN\r\n" + + text + + "END:VCALENDAR\r\n"); + assertThat(zone.zoneId().getRules().getOffset(Instant.parse("2031-06-01T00:00:00Z"))) + .isEqualTo(ZoneOffset.ofHours(2)); + assertThat(zone.zoneId().getRules().getOffset(Instant.parse("2031-12-01T00:00:00Z"))) + .isEqualTo(ZoneOffset.ofHours(1)); + } +} diff --git a/src/test/java/org/busystack/caldav/calendar/CalendarTimezoneRequirementTest.java b/src/test/java/org/busystack/caldav/calendar/CalendarTimezoneRequirementTest.java new file mode 100644 index 0000000..856d93a --- /dev/null +++ b/src/test/java/org/busystack/caldav/calendar/CalendarTimezoneRequirementTest.java @@ -0,0 +1,51 @@ +package org.busystack.caldav.calendar; + +import static org.assertj.core.api.Assertions.*; + +import java.nio.charset.StandardCharsets; +import org.junit.jupiter.api.Test; + +class CalendarTimezoneRequirementTest { + private static byte[] resource(String content) { + return ("BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//BusyStack timezone requirement fixture//EN\r\n" + + content + + "END:VCALENDAR\r\n") + .getBytes(StandardCharsets.UTF_8); + } + + @Test + void builtinTimezoneNamesDoNotSubstituteForRequiredEmbeddedDefinitions() { + var bytes = + resource( + "BEGIN:VEVENT\r\nUID:timezone-required\r\nDTSTAMP:20260101T000000Z\r\nDTSTART;TZID=America/New_York:20260308T120000\r\nEND:VEVENT\r\n"); + assertThatThrownBy(() -> CalendarContent.parse(bytes, "VEVENT")) + .isInstanceOfSatisfying( + CalendarValidationException.class, + error -> + assertThat(error.reason()) + .isEqualTo(CalendarValidationException.Reason.INVALID_OBJECT)); + } + + @Test + void explicitDefinitionAfterTheObjectRemainsAcceptedAndAuthoritative() { + var bytes = + resource( + "BEGIN:VEVENT\r\nUID:timezone-required\r\nDTSTAMP:20260101T000000Z\r\nDTSTART;TZID=Custom/Offset:20260308T120000\r\nEND:VEVENT\r\nBEGIN:VTIMEZONE\r\nTZID:Custom/Offset\r\nBEGIN:STANDARD\r\nDTSTART:19700101T000000\r\nTZOFFSETFROM:+0530\r\nTZOFFSETTO:+0530\r\nEND:STANDARD\r\nEND:VTIMEZONE\r\n"); + var parsed = CalendarContent.parse(bytes, "VEVENT"); + assertThat(CalendarSemanticsTest.point(parsed, "VEVENT", "2026-03-08T06:30:00Z")).isTrue(); + assertThat(parsed.calendar().getComponents()).hasSize(2); + } + + @Test + void timezoneReferencesOnNestedAndExtensionPropertiesAreAlsoRequired() { + var bytes = + resource( + "BEGIN:VEVENT\r\nUID:timezone-required\r\nDTSTAMP:20260101T000000Z\r\nDTSTART:20260308T120000Z\r\nBEGIN:VALARM\r\nACTION:DISPLAY\r\nDESCRIPTION:Stored reminder\r\nTRIGGER:-PT15M\r\nX-WHEN;VALUE=DATE-TIME;TZID=America/New_York:20260308T120000\r\nEND:VALARM\r\nEND:VEVENT\r\n"); + assertThatThrownBy(() -> CalendarContent.parse(bytes, "VEVENT")) + .isInstanceOfSatisfying( + CalendarValidationException.class, + error -> + assertThat(error.reason()) + .isEqualTo(CalendarValidationException.Reason.INVALID_OBJECT)); + } +} diff --git a/src/test/java/org/busystack/caldav/calendar/ObservanceUntilRepairTest.java b/src/test/java/org/busystack/caldav/calendar/ObservanceUntilRepairTest.java new file mode 100644 index 0000000..f8d87e5 --- /dev/null +++ b/src/test/java/org/busystack/caldav/calendar/ObservanceUntilRepairTest.java @@ -0,0 +1,296 @@ +package org.busystack.caldav.calendar; + +import static org.assertj.core.api.Assertions.*; + +import java.nio.charset.StandardCharsets; +import java.time.*; +import java.time.format.DateTimeFormatter; +import java.util.*; +import net.fortuna.ical4j.model.Recur; +import org.busystack.caldav.config.DavCompatibilityProperties; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.ValueSource; + +class ObservanceUntilRepairTest { + private final CalendarAdmission admission = + new CalendarAdmission(new DavCompatibilityProperties(true, true)); + private final CalendarAdmission strict = + new CalendarAdmission(new DavCompatibilityProperties(true, false)); + + static byte[] fixture(String name) throws Exception { + try (var stream = + ObservanceUntilRepairTest.class.getResourceAsStream( + "/calendar/observance-until/thunderbird159-" + name + ".ics")) { + return Objects.requireNonNull(stream).readAllBytes(); + } + } + + private static byte[] bytes(String text) { + return text.getBytes(StandardCharsets.UTF_8); + } + + private static String text(byte[] data) { + return new String(data, StandardCharsets.UTF_8); + } + + static String calendar(String offset, String until) { + return "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Observance input fixture//EN\r\n" + + "BEGIN:VTIMEZONE\r\nTZID:Submitted/Zone\r\nBEGIN:STANDARD\r\nDTSTART:20200101T020000\r\n" + + "TZOFFSETFROM:" + + offset + + "\r\nTZOFFSETTO:+0000\r\n" + + "RRULE:FREQ=YEARLY;BYMONTH=1;BYMONTHDAY=1;UNTIL=" + + until + + "\r\n" + + "END:STANDARD\r\nEND:VTIMEZONE\r\nBEGIN:VEVENT\r\nUID:repair-test\r\nDTSTAMP:20261031T000000Z\r\n" + + "DTSTART;TZID=Submitted/Zone:20261109T100000\r\nDTEND;TZID=Submitted/Zone:20261109T110000\r\n" + + "X-OPAQUE;X-ONE=untouched:UNTIL=20260308T020000 ☃\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n"; + } + + @ParameterizedTest + @ValueSource(strings = {"vancouver", "berlin"}) + void capturedUploadsRequireRepairAndAllOtherBytesSurvive(String name) throws Exception { + byte[] input = fixture(name); + assertThatThrownBy(() -> strict.admit(input, "VEVENT")).isInstanceOf(RuntimeException.class); + var result = admission.admit(input, "VEVENT"); + assertThat(result.repairedRules()).isEqualTo(name.equals("vancouver") ? 6 : 7); + // Every changed logical line differs only in its UNTIL value; original evidence is unchanged. + String[] before = text(input).split("\r\n"), after = text(result.content()).split("\r\n"); + assertThat(after).hasSameSizeAs(before); + for (int i = 0; i < before.length; i++) { + if (before[i].startsWith("RRULE:") && before[i].contains("UNTIL=")) + assertThat(after[i].replaceAll("UNTIL=[0-9TZ]+", "UNTIL=")) + .isEqualTo(before[i].replaceAll("UNTIL=[0-9TZ]+", "UNTIL=")); + else assertThat(after[i]).isEqualTo(before[i]); + } + var second = admission.admit(result.content(), "VEVENT"); + assertThat(second.transformed()).isFalse(); + assertThat(second.content()).isEqualTo(result.content()); + assertThat(strict.admit(result.content(), "VEVENT").content()).isEqualTo(result.content()); + } + + @ParameterizedTest + @ValueSource(strings = {"vancouver", "berlin"}) + void eachCapturedBoundaryIncludesFinalLocalOnsetAndExcludesNext(String name) throws Exception { + var date = DateTimeFormatter.ofPattern("uuuuMMdd'T'HHmmss"); + String original = text(fixture(name)); + var parsed = admission.admit(fixture(name), "VEVENT").parsed(); + var zone = + (net.fortuna.ical4j.model.component.VTimeZone) + parsed.calendar().getComponents("VTIMEZONE").getFirst(); + int checked = 0; + for (var observance : zone.getComponents()) { + var property = observance.getProperty("RRULE"); + if (property.isEmpty()) continue; + var corrected = ((net.fortuna.ical4j.model.property.RRule) property.get()).getRecur(); + if (corrected.getUntil() == null) continue; + var offset = + ((net.fortuna.ical4j.model.property.TzOffsetFrom) + observance.getRequiredProperty("TZOFFSETFROM")) + .getOffset(); + var start = + (LocalDateTime) + ((net.fortuna.ical4j.model.property.DtStart) + observance.getRequiredProperty("DTSTART")) + .getDate(); + LocalDateTime cutoff = + org.busystack.caldav.calendar.recurrence.TemporalSupport.instant( + corrected.getUntil(), ZoneOffset.UTC) + .atOffset(offset) + .toLocalDateTime(); + // Locate original local boundary; evaluate the same recurrence in the client's local-onset + // domain and in the corrected fixed-TZOFFSETFROM domain. No host timezone is consulted. + assertThat(original).contains("UNTIL=" + date.format(cutoff)); + String rule = property.get().getValue(); + String unbounded = rule.replaceAll(";UNTIL=[^;]+", ""); + var localRule = new Recur(unbounded); + var neighbors = localRule.getDates(start, cutoff.minusYears(1), cutoff.plusYears(2), 10); + assertThat(neighbors).contains(cutoff); + LocalDateTime next = + neighbors.stream().filter(x -> x.isAfter(cutoff)).findFirst().orElseThrow(); + var localBounded = new Recur(unbounded + ";UNTIL=" + date.format(cutoff)); + var expected = localBounded.getDates(start, cutoff.minusYears(1), next.plusSeconds(1), 10); + var utcBounded = new Recur(rule); + var actual = + utcBounded.getDates( + start.atOffset(offset), + cutoff.minusYears(1).atOffset(offset), + next.plusSeconds(1).atOffset(offset), + 10); + assertThat(actual.stream().map(OffsetDateTime::toLocalDateTime).toList()) + .containsExactlyElementsOf(expected); + assertThat(actual.stream().map(OffsetDateTime::toLocalDateTime).toList()) + .contains(cutoff) + .doesNotContain(next); + var zonedStart = + (java.time.ZonedDateTime) + ((net.fortuna.ical4j.model.property.DtStart) + parsed + .calendar() + .getComponents("VEVENT") + .getFirst() + .getRequiredProperty("DTSTART")) + .getDate(); + Instant finalOnset = cutoff.toInstant(offset); + assertThat(zonedStart.getZone().getRules().getOffset(finalOnset.minusNanos(1))) + .isEqualTo(offset); + assertThat(zonedStart.getZone().getRules().getOffset(finalOnset)) + .isEqualTo( + ((net.fortuna.ical4j.model.property.TzOffsetTo) + observance.getRequiredProperty("TZOFFSETTO")) + .getOffset()); + checked++; + } + assertThat(checked).isEqualTo(name.equals("vancouver") ? 6 : 7); + } + + @ParameterizedTest + @CsvSource({ + "-0800,20260308T020000,20260308T100000Z", + "-0800,20260308t020000,20260308T100000Z", + "+0530,20260101T020000,20251231T203000Z", + "-081228,18840101T000000,18840101T081228Z", + "+005328,18930401T000000,18930331T230632Z", + "-0100,20261231T233000,20270101T003000Z", + "+0000,20260101T020000,20260101T020000Z" + }) + void conversionUsesFromOffsetIncludingSecondsAndRollover( + String offset, String local, String utc) { + String input = calendar(offset, local); + var result = admission.admit(bytes(input), "VEVENT"); + assertThat(result.transformed()).isTrue(); + assertThat(result.content()) + .isEqualTo(bytes(input.replace("UNTIL=" + local + "\r\n", "UNTIL=" + utc + "\r\n"))); + } + + @Test + void foldedRuleAndLowercaseBoundariesKeepUnrelatedFoldedUtf8AndBom() { + String input = + "\ufeff" + + calendar("-0800", "20260308T020000") + .replace("BEGIN:", "begin:") + .replace("END:", "end:") + .replace("UNTIL=20260308", "UN\r\n TIL=20260308") + .replace("☃", "before\r\n\t☃after"); + var result = admission.admit(bytes(input), "VEVENT"); + assertThat(text(result.content())).contains("UNTIL=20260308T100000Z", "before\r\n\t☃after"); + assertThat(text(result.content())).startsWith("\ufeffbegin:VCALENDAR"); + assertThat(admission.admit(result.content(), "VEVENT").content()).isEqualTo(result.content()); + } + + @Test + void changedLongLineIsRefoldedAndValidObjectsAreByteIdentical() { + String input = + calendar("-0800", "20260308T020000") + .replace("RRULE:", "RRULE;X-LONG=" + "a".repeat(80) + ":"); + var result = admission.admit(bytes(input), "VEVENT"); + String changed = text(result.content()); + assertThat(changed).contains("\r\n "); + assertThat(changed.replace("\r\n ", "")).contains("UNTIL=20260308T100000Z"); + assertThat(admission.admit(result.content(), "VEVENT").content()).isEqualTo(result.content()); + String valid = calendar("-0800", "20260308T100000Z"); + assertThat(admission.admit(bytes(valid), "VEVENT").content()).isEqualTo(bytes(valid)); + } + + @ParameterizedTest + @ValueSource( + strings = { + "20260230T020000", + "20260308", + "20261308T020000", + "20260308T240000", + "20260308T020060", + "00000101T020000" + }) + void invalidOrUnrepresentableLocalBoundariesAreRejected(String until) { + assertThatThrownBy(() -> admission.admit(bytes(calendar("-0800", until)), "VEVENT")) + .isInstanceOf(RuntimeException.class); + } + + @Test + void ambiguityAndUnrelatedInvalidDataRemainRejected() throws Exception { + String input = calendar("-0800", "20260308T020000"); + for (String invalid : + List.of( + input.replace("TZOFFSETFROM:-0800\r\n", ""), + input.replace("TZOFFSETFROM:-0800", "TZOFFSETFROM:-0800\r\nTZOFFSETFROM:-0700"), + input.replace("TZOFFSETTO:+0000\r\n", ""), + input.replace("DTSTART:20200101T020000\r\n", ""), + input.replace("TZOFFSETFROM:-0800", "TZOFFSETFROM:-0000"), + input.replace("TZOFFSETFROM:-0800", "TZOFFSETFROM:+0860"), + input.replace("FREQ=YEARLY", "FREQ=YEARLY;FREQ=YEARLY"), + input.replace("FREQ=YEARLY", "FREQ=YEARLY;COUNT=2"), + input.replace("BYMONTHDAY=1", "BYDAY=0MO"), + input.replace("DTEND;TZID=Submitted/Zone:20261109T110000", "DURATION:P1W2D"), + input.replaceFirst("BEGIN:VCALENDAR", "XBEGIN:VCALENDAR"), + input.replace("DTSTAMP:20261031T000000Z\r\n", ""), + input.replace("END:VEVENT", "RRULE:FREQ=DAILY;UNTIL=20261201T100000\r\nEND:VEVENT"))) + assertThatThrownBy(() -> admission.admit(bytes(invalid), "VEVENT")) + .isInstanceOf(RuntimeException.class); + for (String name : + List.of( + "invalid-zero-byday.ics", + "invalid-plus-zero-byday.ics", + "invalid-weeks-days.ics", + "invalid-leading-token.ics")) { + try (var stream = + getClass().getResourceAsStream("/calendar/independent-verification/" + name)) { + byte[] negative = Objects.requireNonNull(stream).readAllBytes(); + assertThatThrownBy(() -> admission.admit(negative, "VEVENT")) + .isInstanceOf(RuntimeException.class); + } + } + } + + @Test + void textExtensionAndOtherComponentRulesAreNeverRepaired() { + String valid = + calendar("-0800", "20260308T100000Z") + .replace( + "END:VEVENT", + "DESCRIPTION:RRULE:UNTIL=20260308T020000\r\nX-RRULE:UNTIL=20260308T020000\r\nEND:VEVENT"); + assertThat(admission.admit(bytes(valid), "VEVENT").content()).isEqualTo(bytes(valid)); + String event = + valid.replace("END:VEVENT", "RRULE:FREQ=DAILY;UNTIL=20261201T100000\r\nEND:VEVENT"); + assertThatThrownBy(() -> admission.admit(bytes(event), "VEVENT")) + .isInstanceOf(RuntimeException.class); + assertThatThrownBy(() -> admission.admit(bytes(event.replace("VEVENT", "VTODO")), "VTODO")) + .isInstanceOf(RuntimeException.class); + } + + @Test + void correctionCannotExceedExistingByteLimit() { + String input = calendar("-0800", "20260308T020000"); + input = + input.replace( + "X-OPAQUE;", + "X-PADDING:" + + "a".repeat(CalendarContent.MAX_BYTES - bytes(input).length - 12) + + "\r\nX-OPAQUE;"); + byte[] boundary = bytes(input); + assertThat(boundary).hasSize(CalendarContent.MAX_BYTES); + assertThatThrownBy(() -> admission.admit(boundary, "VEVENT")) + .isInstanceOf(CalendarValidationException.class); + } + + @Test + void taskAdmissionUsesTheSameAuthoritativeTimezoneAndPreservesOpaqueProperties() + throws Exception { + String input = + text(fixture("vancouver")) + .replace("VEVENT", "VTODO") + .replace("DTEND;", "DUE;") + .replace( + "END:VTODO", + "STATUS:NEEDS-ACTION\r\nPERCENT-COMPLETE:25\r\nX-TASK:kept\r\nEND:VTODO"); + var accepted = admission.admit(bytes(input), "VTODO"); + assertThat(accepted.repairedRules()).isEqualTo(6); + assertThat(text(accepted.content())).contains("X-TASK:kept", "PERCENT-COMPLETE:25"); + assertThat(CalendarSemanticsTest.point(accepted.parsed(), "VTODO", "2026-11-09T17:30:00Z")) + .isTrue(); + assertThat(CalendarSemanticsTest.point(accepted.parsed(), "VTODO", "2026-11-09T18:30:00Z")) + .isFalse(); + } +} diff --git a/src/test/java/org/busystack/caldav/calendar/ThunderbirdCalendarFixturesTest.java b/src/test/java/org/busystack/caldav/calendar/ThunderbirdCalendarFixturesTest.java new file mode 100644 index 0000000..8dad536 --- /dev/null +++ b/src/test/java/org/busystack/caldav/calendar/ThunderbirdCalendarFixturesTest.java @@ -0,0 +1,60 @@ +package org.busystack.caldav.calendar; + +import static org.assertj.core.api.Assertions.*; +import static org.busystack.caldav.calendar.CalendarSemanticsTest.*; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.time.*; +import org.busystack.caldav.calendar.query.*; +import org.busystack.caldav.calendar.recurrence.*; +import org.junit.jupiter.api.Test; + +class ThunderbirdCalendarFixturesTest { + private static byte[] fixture(String name) throws IOException { + try (var resource = + ThunderbirdCalendarFixturesTest.class.getResourceAsStream("/calendar/clients/" + name)) { + assertThat(resource).isNotNull(); + return resource.readAllBytes(); + } + } + + @Test + void nativeRecurringEventRetainsAlarmAndUnknownProperties() throws IOException { + byte[] bytes = fixture("thunderbird-event.ics"); + String text = new String(bytes, StandardCharsets.UTF_8); + assertThat(text.replace("\r\n", "")).doesNotContain("\n"); + var parsed = CalendarContent.parse(bytes, "VEVENT"); + var expanded = + new CalendarRecurrence(QueryLimits.defaults()) + .expand( + parsed, + range("2026-10-27T00:00:00Z", "2026-10-31T00:00:00Z"), + FloatingTimezone.of(UTC)); + assertThat(expanded.getComponents()).hasSize(3); + assertThat(expanded.toString()) + .contains( + "X-NATIVE-CLIENT:preserved", + "X-MOZ-GENERATION:1", + "TRIGGER;VALUE=DATE-TIME:20261027T094500Z", + "BEGIN:VALARM"); + assertThat(point(parsed, "VEVENT", "2026-10-29T10:30:00Z")).isTrue(); + assertThat(point(parsed, "VEVENT", "2026-10-30T10:30:00Z")).isFalse(); + assertThat(CalendarReports.copy(parsed).toString()) + .contains("PRODID:-//Mozilla.org/NONSGML Mozilla Calendar V1.1//EN"); + } + + @Test + void nativeCompletedUndatedTaskUsesTaskCompletionRangeSemantics() throws IOException { + var parsed = CalendarContent.parse(fixture("thunderbird-task.ics"), "VTODO"); + var completed = + (net.fortuna.ical4j.model.property.DateProperty) + parsed.calendar().getComponents().getFirst().getRequiredProperty("COMPLETED"); + Instant at = TemporalSupport.instant(completed.getDate(), UTC); + assertThat(match(parsed, "VTODO", new TimeRange(at.minusSeconds(1), at))).isTrue(); + assertThat(match(parsed, "VTODO", new TimeRange(at.plusSeconds(1), at.plusSeconds(2)))) + .isFalse(); + assertThat(CalendarReports.copy(parsed).toString()) + .contains("STATUS:COMPLETED", "PERCENT-COMPLETE:100", "PRIORITY:2", "X-MOZ-GENERATION:1"); + } +} diff --git a/src/test/java/org/busystack/caldav/calendar/query/CalendarDataProjectionTest.java b/src/test/java/org/busystack/caldav/calendar/query/CalendarDataProjectionTest.java new file mode 100644 index 0000000..b3302cb --- /dev/null +++ b/src/test/java/org/busystack/caldav/calendar/query/CalendarDataProjectionTest.java @@ -0,0 +1,365 @@ +package org.busystack.caldav.calendar.query; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.nio.charset.StandardCharsets; +import java.time.Instant; +import java.time.ZoneOffset; +import java.util.List; +import net.fortuna.ical4j.model.Component; +import net.fortuna.ical4j.model.ComponentContainer; +import net.fortuna.ical4j.model.Property; +import org.busystack.caldav.calendar.CalendarContent; +import org.busystack.caldav.calendar.query.CalendarDataProjection.ComponentSelection; +import org.busystack.caldav.calendar.query.CalendarDataProjection.PropertySelection; +import org.busystack.caldav.calendar.query.CalendarDataProjection.Selection; +import org.junit.jupiter.api.Test; + +class CalendarDataProjectionTest { + private static final FloatingTimezone UTC = FloatingTimezone.of(ZoneOffset.UTC); + private final CalendarDataProjection projection = + new CalendarDataProjection(QueryLimits.defaults()); + + @Test + void wholeReportIsAnIndependentModelCopyAndDoesNotRewriteAuthoritativeData() { + var parsed = fixture(); + String before = parsed.calendar().toString(); + var report = projection.project(parsed, null, UTC); + report + .getComponents() + .getFirst() + .setPropertyList( + report.getComponents().getFirst().getPropertyList().removeAll(Property.SUMMARY)); + assertThat(parsed.calendar().toString()).isEqualTo(before); + assertThat(report.toString()).doesNotContain("SUMMARY:Projection fixture"); + } + + @Test + void selectedNestedComponentsAndPropertiesPreserveParametersWithNoValue() { + var event = + selected( + "VEVENT", + List.of( + property("UID", false), + property("DTSTART", true), + property("ATTENDEE", true), + property("X-CLIENT-DATA", false)), + List.of(selected("VALARM", List.of(property("DESCRIPTION", true)), List.of()))); + var root = selected("VCALENDAR", List.of(property("VERSION", false)), List.of(event)); + var parsed = fixture(); + String before = parsed.calendar().toString(); + var report = projection.project(parsed, new Selection(root, null, null, null), UTC); + assertThat(report.getPropertyList().getAll()) + .extracting(Property::getName) + .containsExactly("VERSION"); + var returned = report.getComponents().getFirst(); + assertThat(returned.getPropertyList().getAll()) + .extracting(Property::getName) + .containsExactly("UID", "DTSTART", "ATTENDEE", "ATTENDEE", "X-CLIENT-DATA"); + assertThat(returned.getProperty(Property.DTSTART).orElseThrow().toString()) + .isEqualTo("DTSTART;X-ORIGIN=client:\r\n"); + List attendees = returned.getProperties(Property.ATTENDEE); + assertThat(attendees) + .allSatisfy( + property -> { + assertThat(property.getValue()).isEmpty(); + assertThat(property.getParameter("CN")).isPresent(); + }); + assertThat(returned.getProperty("X-CLIENT-DATA").orElseThrow().getValue()) + .isEqualTo("opaque client data"); + var alarm = ((ComponentContainer) returned).getComponents().getFirst(); + assertThat(alarm.getPropertyList().getAll()) + .extracting(Property::getName) + .containsExactly(Property.DESCRIPTION); + assertThat(alarm.getProperty(Property.DESCRIPTION).orElseThrow().getValue()).isEmpty(); + assertThat(parsed.calendar().toString()).isEqualTo(before); + } + + @Test + void duplicateSelectorsFormAUnionAndARequestedValueWinsOverNoValue() { + var first = selected("VEVENT", List.of(property("UID", true)), List.of()); + var second = + selected("vevent", List.of(property("UID", false), property("SUMMARY", false)), List.of()); + var root = new ComponentSelection("vcalendar", true, false, List.of(), List.of(first, second)); + var report = projection.project(fixture(), new Selection(root, null, null, null), UTC); + var event = report.getComponents().getFirst(); + assertThat(event.getPropertyList().getAll()) + .extracting(Property::getName) + .containsExactly("UID", "SUMMARY"); + assertThat(event.getProperty(Property.UID).orElseThrow().getValue()) + .isEqualTo("projection-fixture"); + assertThat(((ComponentContainer) event).getComponents()).isEmpty(); + assertThat(report.getProperty(Property.PRODID)).isPresent(); + } + + @Test + void emptyAndExplicitAllSelectorsHaveDifferentMeaningAndUnknownNamesAreAllowed() { + var empty = selected("VCALENDAR", List.of(), List.of()); + var none = projection.project(fixture(), new Selection(empty, null, null, null), UTC); + assertThat(none.getProperties()).isEmpty(); + assertThat(none.getComponents()).isEmpty(); + var all = new ComponentSelection("VCALENDAR", true, true, List.of(), List.of()); + assertThat(projection.project(fixture(), new Selection(all, null, null, null), UTC).toString()) + .isEqualTo(fixture().calendar().toString()); + var unknown = + selected( + "VCALENDAR", + List.of(property("X-ABSENT", false)), + List.of(selected("X-UNKNOWN", List.of(), List.of()))); + assertThat( + projection + .project(fixture(), new Selection(unknown, null, null, null), UTC) + .getComponents()) + .isEmpty(); + } + + @Test + void malformedAndExcessiveSelectionsFailBeforeReturningAReport() { + assertThatThrownBy(() -> property("UID\r\nINJECTED", false)).isInstanceOf(QueryException.class); + assertThatThrownBy( + () -> + new ComponentSelection( + "VEVENT", true, false, List.of(property("UID", false)), List.of())) + .isInstanceOf(QueryException.class); + var wrongRoot = selected("VEVENT", List.of(), List.of()); + assertThatThrownBy( + () -> projection.project(fixture(), new Selection(wrongRoot, null, null, null), UTC)) + .isInstanceOf(QueryException.class); + var root = + selected( + "VCALENDAR", + List.of(property("VERSION", false)), + List.of(selected("VEVENT", List.of(property("UID", false)), List.of()))); + var bounded = new CalendarDataProjection(new QueryLimits(10, 10, 100, 10, 3)); + assertThatThrownBy(() -> bounded.project(fixture(), new Selection(root, null, null, null), UTC)) + .isInstanceOfSatisfying( + QueryException.class, + error -> assertThat(error.kind()).isEqualTo(QueryException.Kind.LIMIT_EXCEEDED)); + var range = + new TimeRange(Instant.parse("2026-10-01T00:00:00Z"), Instant.parse("2026-11-01T00:00:00Z")); + assertThatThrownBy(() -> new Selection(null, range, range, null)) + .isInstanceOf(QueryException.class); + assertThatThrownBy(() -> new Selection(null, new TimeRange(range.start(), null), null, null)) + .isInstanceOf(QueryException.class); + } + + @Test + void freeBusyLimitingRetainsWholeIntersectingPeriodsAndTheirType() { + var parsed = + CalendarContent.parse( + ("BEGIN:VCALENDAR\r\n" + + "VERSION:2.0\r\n" + + "PRODID:-//Freebusy textual fixture//EN\r\n" + + "BEGIN:VFREEBUSY\r\n" + + "UID:busy-projection\r\n" + + "DTSTAMP:20261027T000000Z\r\n" + + "DTSTART:20261001T000000Z\r\n" + + "DTEND:20261101T000000Z\r\n" + + "FREEBUSY;FBTYPE=BUSY-TENTATIVE:20261027T090000Z/20261027T110000Z,20261027T130000Z/PT1H\r\n" + + "FREEBUSY;FBTYPE=FREE:20261027T120000Z/PT30M\r\n" + + "END:VFREEBUSY\r\n" + + "END:VCALENDAR\r\n") + .getBytes(StandardCharsets.UTF_8), + Component.VFREEBUSY); + String original = parsed.calendar().toString(); + var range = + new TimeRange(Instant.parse("2026-10-27T10:00:00Z"), Instant.parse("2026-10-27T12:00:00Z")); + var report = projection.project(parsed, new Selection(null, null, null, range), UTC); + var values = report.getComponents().getFirst().getProperties(Property.FREEBUSY); + assertThat(values).hasSize(1); + assertThat(values.getFirst().getParameter("FBTYPE").orElseThrow().getValue()) + .isEqualTo("BUSY-TENTATIVE"); + var intervals = ((net.fortuna.ical4j.model.property.FreeBusy) values.getFirst()).getIntervals(); + assertThat(intervals).hasSize(1); + assertThat(intervals.getFirst().getStart()).isEqualTo(Instant.parse("2026-10-27T09:00:00Z")); + assertThat(intervals.getFirst().getEnd()).isEqualTo(Instant.parse("2026-10-27T11:00:00Z")); + assertThat(parsed.calendar().toString()).isEqualTo(original); + } + + @Test + void oneReportBudgetCoversMultipleResourceProjections() { + var limited = new CalendarDataProjection(new QueryLimits(3, 10, 1000, 10, 50)); + limited.project(fixture(), null, UTC); + assertThatThrownBy(() -> limited.project(fixture(), null, UTC)) + .isInstanceOfSatisfying( + QueryException.class, + error -> assertThat(error.kind()).isEqualTo(QueryException.Kind.LIMIT_EXCEEDED)); + var work = new CalendarDataProjection(new QueryLimits(100, 10, 25, 10, 50)); + work.project(fixture(), null, UTC); + assertThatThrownBy(() -> work.project(fixture(), null, UTC)) + .isInstanceOfSatisfying( + QueryException.class, + error -> assertThat(error.kind()).isEqualTo(QueryException.Kind.LIMIT_EXCEEDED)); + } + + @Test + void temporalExpansionRunsBeforeRequestedPropertyProjection() { + var parsed = + CalendarContent.parse( + ("BEGIN:VCALENDAR\r\n" + + "VERSION:2.0\r\n" + + "PRODID:-//Expansion projection fixture//EN\r\n" + + "BEGIN:VEVENT\r\n" + + "UID:expanded-projection\r\n" + + "DTSTAMP:20261027T000000Z\r\n" + + "DTSTART:20261028T100000Z\r\n" + + "DTEND:20261028T110000Z\r\n" + + "RRULE:FREQ=DAILY;COUNT=3\r\n" + + "SUMMARY:Preserved source\r\n" + + "END:VEVENT\r\n" + + "END:VCALENDAR\r\n") + .getBytes(StandardCharsets.UTF_8), + Component.VEVENT); + var root = + new ComponentSelection( + "VCALENDAR", + true, + false, + List.of(), + List.of( + selected( + "VEVENT", + List.of( + property("UID", false), + property("RECURRENCE-ID", false), + property("DTSTART", false)), + List.of()))); + var range = + new TimeRange(Instant.parse("2026-10-28T09:00:00Z"), Instant.parse("2026-10-30T11:00:00Z")); + var result = projection.project(parsed, new Selection(root, range, null, null), UTC); + List expanded = result.getComponents(); + assertThat(expanded).hasSize(3); + assertThat(expanded) + .allSatisfy( + component -> { + assertThat(component.getProperty(Property.RRULE)).isEmpty(); + assertThat(component.getProperty(Property.DTEND)).isEmpty(); + assertThat(component.getProperty(Property.SUMMARY)).isEmpty(); + }); + assertThat( + expanded.stream() + .map(component -> component.getProperty(Property.DTSTART).orElseThrow().getValue())) + .containsExactly("20261028T100000Z", "20261029T100000Z", "20261030T100000Z"); + assertThat(parsed.calendar().getComponents().getFirst().getProperty(Property.RRULE)) + .isPresent(); + } + + @Test + void fullAndSelectedReportCopiesRetainNominalDurationSpellingAcrossDst() { + var parsed = + CalendarContent.parse( + ("BEGIN:VCALENDAR\r\n" + + "VERSION:2.0\r\n" + + "PRODID:-//Nominal duration textual fixture//EN\r\n" + + "BEGIN:VTIMEZONE\r\n" + + "TZID:Europe/Berlin\r\n" + + "BEGIN:STANDARD\r\n" + + "DTSTART:20251026T030000\r\n" + + "TZOFFSETFROM:+0200\r\n" + + "TZOFFSETTO:+0100\r\n" + + "RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU\r\n" + + "END:STANDARD\r\n" + + "BEGIN:DAYLIGHT\r\n" + + "DTSTART:20250330T020000\r\n" + + "TZOFFSETFROM:+0100\r\n" + + "TZOFFSETTO:+0200\r\n" + + "RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU\r\n" + + "END:DAYLIGHT\r\n" + + "END:VTIMEZONE\r\n" + + "BEGIN:VEVENT\r\n" + + "UID:nominal-copy\r\n" + + "DTSTAMP:20261027T000000Z\r\n" + + "DTSTART;TZID=Europe/Berlin:20260328T120000\r\n" + + "DURATION:PT24H\r\n" + + "RRULE:FREQ=DAILY;COUNT=2\r\n" + + "RDATE;VALUE=PERIOD;TZID=Europe/Berlin:20260330T120000/PT24H\r\n" + + "BEGIN:VALARM\r\n" + + "TRIGGER;RELATED=END:-PT24H\r\n" + + "ACTION:DISPLAY\r\n" + + "DESCRIPTION:Nominal alarm\r\n" + + "END:VALARM\r\n" + + "END:VEVENT\r\n" + + "END:VCALENDAR\r\n") + .getBytes(StandardCharsets.UTF_8), + Component.VEVENT); + var selection = + new ComponentSelection( + "VCALENDAR", + true, + false, + List.of(), + List.of(new ComponentSelection("VEVENT", true, true, List.of(), List.of()))); + for (var report : + List.of( + projection.project(parsed, null, UTC), + projection.project(parsed, new Selection(selection, null, null, null), UTC))) { + var event = report.getComponents(Component.VEVENT).getFirst(); + assertThat(event.getProperty(Property.DURATION).orElseThrow().getValue()).isEqualTo("PT24H"); + assertThat(event.getProperty(Property.RDATE).orElseThrow().getValue()).endsWith("/PT24H"); + var alarm = ((ComponentContainer) event).getComponents().getFirst(); + assertThat(alarm.getProperty(Property.TRIGGER).orElseThrow().getValue()).isEqualTo("-PT24H"); + } + } + + @Test + void repeatedLargeResourcesShareAByteBudgetBeforeReportCopies() { + String description = "x".repeat(900000); + var text = + new StringBuilder( + "BEGIN:VCALENDAR\r\n" + + "VERSION:2.0\r\n" + + "PRODID:-//Bounded report fixture//EN\r\n" + + "BEGIN:VEVENT\r\n" + + "UID:large-report\r\n" + + "DTSTAMP:20261027T000000Z\r\n" + + "DTSTART:20261028T100000Z\r\n" + + "DESCRIPTION:"); + for (int offset = 0; offset < description.length(); offset += 60) { + if (offset != 0) text.append("\r\n "); + text.append(description, offset, Math.min(offset + 60, description.length())); + } + text.append("\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n"); + var parsed = + CalendarContent.parse(text.toString().getBytes(StandardCharsets.UTF_8), Component.VEVENT); + var bounded = new CalendarDataProjection(QueryLimits.defaults()); + assertThat(bounded.project(parsed, null, UTC).getComponents()).hasSize(1); + assertThatThrownBy(() -> bounded.project(parsed, null, UTC)) + .isInstanceOfSatisfying( + QueryException.class, + error -> assertThat(error.kind()).isEqualTo(QueryException.Kind.LIMIT_EXCEEDED)); + } + + private static ComponentSelection selected( + String name, List properties, List components) { + return new ComponentSelection(name, false, false, properties, components); + } + + private static PropertySelection property(String name, boolean noValue) { + return new PropertySelection(name, noValue); + } + + private static CalendarContent.Parsed fixture() { + return CalendarContent.parse( + ("BEGIN:VCALENDAR\r\n" + + "VERSION:2.0\r\n" + + "PRODID:-//Independent textual fixture//EN\r\n" + + "BEGIN:VEVENT\r\n" + + "UID:projection-fixture\r\n" + + "DTSTAMP:20261027T000000Z\r\n" + + "DTSTART;X-ORIGIN=client:20261028T100000Z\r\n" + + "SUMMARY:Projection fixture\r\n" + + "ATTENDEE;CN=First Client:mailto:first@example.test\r\n" + + "ATTENDEE;CN=Second Client:mailto:second@example.test\r\n" + + "X-CLIENT-DATA;X-FORMAT=legacy:opaque client data\r\n" + + "BEGIN:VALARM\r\n" + + "TRIGGER:-PT5M\r\n" + + "ACTION:DISPLAY\r\n" + + "DESCRIPTION:Alarm fixture\r\n" + + "END:VALARM\r\n" + + "END:VEVENT\r\n" + + "END:VCALENDAR\r\n") + .getBytes(StandardCharsets.UTF_8), + Component.VEVENT); + } +} diff --git a/src/test/java/org/busystack/caldav/config/DavCompatibilityPropertiesTest.java b/src/test/java/org/busystack/caldav/config/DavCompatibilityPropertiesTest.java new file mode 100644 index 0000000..d771a73 --- /dev/null +++ b/src/test/java/org/busystack/caldav/config/DavCompatibilityPropertiesTest.java @@ -0,0 +1,41 @@ +package org.busystack.caldav.config; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Map; +import org.junit.jupiter.api.Test; +import org.springframework.boot.context.properties.bind.Bindable; +import org.springframework.boot.context.properties.bind.Binder; +import org.springframework.boot.context.properties.source.MapConfigurationPropertySource; + +class DavCompatibilityPropertiesTest { + @Test + void omittedConfigurationEnablesNarrowDepthOneAccommodation() { + assertThat(bind(Map.of()).allowSyncDepthOne()).isTrue(); + assertThat(bind(Map.of()).repairLocalObservanceUntil()).isTrue(); + } + + @Test + void observanceRepairCanBeDisabledIndependently() { + var configured = bind(Map.of("busystack.dav.repair-local-observance-until", "false")); + assertThat(configured.repairLocalObservanceUntil()).isFalse(); + assertThat(configured.allowSyncDepthOne()).isTrue(); + } + + @Test + void explicitFalseSelectsStrictMode() { + assertThat(bind(Map.of("busystack.dav.allow-sync-depth-one", "false")).allowSyncDepthOne()) + .isFalse(); + } + + @Test + void explicitTruePreservesCompatibilityMode() { + assertThat(bind(Map.of("busystack.dav.allow-sync-depth-one", "true")).allowSyncDepthOne()) + .isTrue(); + } + + private DavCompatibilityProperties bind(Map values) { + return new Binder(new MapConfigurationPropertySource(values)) + .bindOrCreate("busystack.dav", Bindable.of(DavCompatibilityProperties.class)); + } +} diff --git a/src/test/java/org/busystack/caldav/config/DeploymentPropertiesTest.java b/src/test/java/org/busystack/caldav/config/DeploymentPropertiesTest.java new file mode 100644 index 0000000..cd690dc --- /dev/null +++ b/src/test/java/org/busystack/caldav/config/DeploymentPropertiesTest.java @@ -0,0 +1,52 @@ +package org.busystack.caldav.config; + +import static org.assertj.core.api.Assertions.*; + +import java.net.URI; +import org.junit.jupiter.api.Test; + +class DeploymentPropertiesTest { + @Test + void usesConfiguredCanonicalSubpath() { + URI uri = URI.create("https://calendar.example/team"); + assertThat(new DeploymentProperties(uri, uri, false).davRoot()) + .isEqualTo("https://calendar.example/team/dav/"); + } + + @Test + void rejectsInsecureAndAmbiguousConfiguration() { + for (String value : + new String[] { + "http://calendar.example", + "https://calendar.example/", + "https://user@calendar.example", + "https://calendar.example:0", + "https://calendar.example:65536", + "https://calendar.example/team;parameter", + "https://calendar.example/%2e%2e", + "https://calendar.example/équipe", + "https://calendar.example/%C3%A9quipe", + "https://calendar.example/a/../b", + "https://calendar.example?evil=1", + "https://calendar.example/#fragment" + }) { + URI uri = URI.create(value); + assertThatThrownBy(() -> new DeploymentProperties(uri, uri, false)) + .isInstanceOf(IllegalArgumentException.class); + } + assertThatThrownBy(() -> new DeploymentProperties(null, null, false)) + .isInstanceOf(IllegalArgumentException.class); + assertThatThrownBy( + () -> + new DeploymentProperties( + URI.create("http://example.com"), URI.create("http://example.com"), true)) + .isInstanceOf(IllegalArgumentException.class); + } + + @Test + void explicitlyPermitsLoopbackDevelopment() { + URI uri = URI.create("http://127.0.0.1:8080"); + assertThat(new DeploymentProperties(uri, uri, true).davRoot()) + .isEqualTo("http://127.0.0.1:8080/dav/"); + } +} diff --git a/src/test/java/org/busystack/caldav/protocol/DavHttpIT.java b/src/test/java/org/busystack/caldav/protocol/DavHttpIT.java new file mode 100644 index 0000000..87eb8ae --- /dev/null +++ b/src/test/java/org/busystack/caldav/protocol/DavHttpIT.java @@ -0,0 +1,3661 @@ +package org.busystack.caldav.protocol; + +import static org.assertj.core.api.Assertions.*; + +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.nio.charset.StandardCharsets; +import java.time.Instant; +import java.util.Base64; +import java.util.List; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; +import org.busystack.caldav.account.AccountService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.server.LocalServerPort; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; +import org.testcontainers.postgresql.PostgreSQLContainer; +import org.w3c.dom.Element; + +@SpringBootTest( + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, + properties = { + "busystack.public-base-url=https://calendar.example/team", + "busystack.issuer=https://calendar.example/team" + }) +@org.springframework.test.annotation.DirtiesContext( + classMode = org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_CLASS) +@Testcontainers +class DavHttpIT { + @Container + static final PostgreSQLContainer POSTGRES = + org.busystack.caldav.testing.LoopbackContainers.postgres(); + + @DynamicPropertySource + static void database(DynamicPropertyRegistry registry) { + registry.add("spring.datasource.url", POSTGRES::getJdbcUrl); + registry.add("spring.datasource.username", POSTGRES::getUsername); + registry.add("spring.datasource.password", POSTGRES::getPassword); + registry.add("server.ssl.enabled", () -> true); + registry.add( + "server.ssl.key-store", () -> org.busystack.caldav.testing.TestTls.instance().keyStore()); + registry.add( + "server.ssl.key-store-password", + () -> org.busystack.caldav.testing.TestTls.instance().password()); + } + + @LocalServerPort int port; + @Autowired AccountService accounts; + @Autowired JdbcTemplate jdbc; + @Autowired org.springframework.transaction.PlatformTransactionManager transactions; + UUID alice, bob; + String aliceAuth, bobAuth, home; + final HttpClient http = + HttpClient.newBuilder() + .sslContext(org.busystack.caldav.testing.TestTls.instance().context()) + .followRedirects(HttpClient.Redirect.NEVER) + .build(); + + @BeforeEach + void prepare() { + org.busystack.caldav.testing.FixtureDatabase.clearAccounts(jdbc); + alice = accounts.provision("alice", "Alice & Calendar", "a secure Alice password", false); + bob = accounts.provision("bob", "Bob Tasks", "a secure Bob password", false); + aliceAuth = + basic( + "alice", + accounts.issueDavCredential(alice, "client", Instant.now().plusSeconds(3600)).secret()); + bobAuth = + basic( + "bob", + accounts.issueDavCredential(bob, "client", Instant.now().plusSeconds(3600)).secret()); + home = "/calendars/" + alice + "/"; + } + + private String basic(String user, String secret) { + return "Basic " + + Base64.getEncoder() + .encodeToString((user + ":" + secret).getBytes(StandardCharsets.UTF_8)); + } + + private HttpRequest.Builder request(String path, String auth) { + return HttpRequest.newBuilder(URI.create("https://127.0.0.1:" + port + "/team/dav" + path)) + .header("Authorization", auth); + } + + private HttpResponse sendAs( + String method, String path, String body, String auth, String... headers) throws Exception { + var builder = + request(path, auth) + .method(method, HttpRequest.BodyPublishers.ofString(body, StandardCharsets.UTF_8)); + if (headers.length > 0) builder.headers(headers); + return http.send(builder.build(), HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8)); + } + + private HttpResponse send(String method, String path, String body, String... headers) + throws Exception { + return sendAs(method, path, body, aliceAuth, headers); + } + + private String create(String name, String component) throws Exception { + String path = home + name + "/"; + assertThat( + send( + "MKCALENDAR", + path, + "" + + name + + "") + .statusCode()) + .isEqualTo(201); + return path; + } + + private static String event(String uid, String summary) { + return "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//BusyStack protocol fixture//EN\r\nBEGIN:VEVENT\r\nUID:" + + uid + + "\r\nDTSTAMP:20260101T000000Z\r\nDTSTART:20261027T100000Z\r\nDTEND:20261027T110000Z\r\nSUMMARY:" + + summary + + "\r\nX-CLIENT-PROPERTY;X-PARAM=kept:preserve me\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n"; + } + + private static String query(String filter) { + return "" + + filter + + ""; + } + + private static String sync(String token, int limit) { + return "" + + token + + "1" + + limit + + ""; + } + + private static Element xml(HttpResponse response) { + return DavXml.parse(response.body().getBytes(StandardCharsets.UTF_8)); + } + + private static String text(HttpResponse response, String namespace, String local) { + return xml(response).getElementsByTagNameNS(namespace, local).item(0).getTextContent(); + } + + @Test + void discoveryUsesStablePrincipalAndRealComponentCapabilities() throws Exception { + var root = + send( + "PROPFIND", + "/", + "", + "Depth", + "0"); + assertThat(root.statusCode()).isEqualTo(207); + assertThat(root.body()).contains("https://calendar.example/team/dav/principals/" + alice + "/"); + var principal = + send( + "PROPFIND", + "/principals/" + alice + "/", + "", + "Depth", + "0"); + assertThat(principal.statusCode()).isEqualTo(207); + assertThat(principal.body()) + .contains("Alice & Calendar", "https://calendar.example/team/dav" + home); + String path = create("tasks", "VTODO"); + var props = send("PROPFIND", path, "", "Depth", "0"); + assertThat(props.statusCode()).isEqualTo(207); + assertThat(props.body()) + .contains("VTODO") + .doesNotContain("calendar-schedule", "calendarserver-sharing"); + var options = send("OPTIONS", path, ""); + assertThat(options.statusCode()).isEqualTo(200); + assertThat(options.headers().firstValue("DAV").orElseThrow()) + .doesNotContain("schedule", "2", "extended-mkcol"); + } + + @Test + void acceptedOctetsStrongValidatorsUnicodeAndLiteralPlusRoundTrip() throws Exception { + String path = create("events", "VEVENT") + "a+b-%E9%9B%AA.ics", + data = event("unicode-uid", "Café 雪 & "); + var put = + send( + "PUT", + path, + data, + "Content-Type", + "text/calendar; charset=utf-8", + "If-None-Match", + "*"); + assertThat(put.statusCode()).isEqualTo(201); + String etag = put.headers().firstValue("ETag").orElseThrow(); + assertThat(etag).startsWith("\"").endsWith("\""); + var get = send("GET", path, ""); + assertThat(get.statusCode()).isEqualTo(200); + assertThat(get.body()).isEqualTo(data); + assertThat(get.headers().firstValue("ETag")).hasValue(etag); + var head = send("HEAD", path, ""); + assertThat(head.statusCode()).isEqualTo(200); + assertThat(head.body()).isEmpty(); + assertThat(head.headers().firstValue("Content-Length")) + .hasValue(Integer.toString(data.getBytes(StandardCharsets.UTF_8).length)); + assertThat(send("GET", path, "", "If-None-Match", "W/" + etag).statusCode()).isEqualTo(304); + assertThat(send("PUT", path, data, "If-Match", "W/" + etag).statusCode()).isEqualTo(412); + String report = + "https://calendar.example/team/dav" + + path + + ""; + var multiget = send("REPORT", path.substring(0, path.lastIndexOf('/') + 1), report); + assertThat(multiget.statusCode()).isEqualTo(207); + assertThat(text(multiget, DavXml.CAL, "calendar-data")).isEqualTo(data); + } + + @Test + void createUpdateDeletePreconditionsAreEvaluatedInsideMutation() throws Exception { + String path = create("events", "VEVENT") + "race.ics", data = event("race-uid", "Race"); + HttpRequest first = + request(path, aliceAuth) + .header("If-None-Match", "*") + .PUT(HttpRequest.BodyPublishers.ofString(data)) + .build(); + var a = http.sendAsync(first, HttpResponse.BodyHandlers.ofString()); + var b = http.sendAsync(first, HttpResponse.BodyHandlers.ofString()); + CompletableFuture.allOf(a, b).join(); + assertThat(List.of(a.join().statusCode(), b.join().statusCode())) + .containsExactlyInAnyOrder(201, 412); + String old = send("GET", path, "").headers().firstValue("ETag").orElseThrow(); + assertThat(send("PUT", path, event("race-uid", "Changed"), "If-Match", old).statusCode()) + .isEqualTo(204); + assertThat(send("DELETE", path, "", "If-Match", old).statusCode()).isEqualTo(412); + assertThat(send("GET", path, "").body()).contains("SUMMARY:Changed"); + String current = send("GET", path, "").headers().firstValue("ETag").orElseThrow(); + assertThat(send("PUT", path, data, "If-None-Match", "W/" + current).statusCode()) + .isEqualTo(412); + assertThat(send("DELETE", path, "", "If-Match", current).statusCode()).isEqualTo(204); + assertThat(send("GET", path, "").statusCode()).isEqualTo(404); + } + + @Test + void queryMultigetAndFreeBusyUseProtocolBodiesAndBoundedRanges() throws Exception { + String calendar = create("events", "VEVENT"); + send("PUT", calendar + "one.ics", event("one", "Snow meeting")); + send("PUT", calendar + "two.ics", event("two", "Other")); + String filter = + "snow"; + var query = send("REPORT", calendar, query(filter), "Depth", "1"); + assertThat(query.statusCode()).isEqualTo(207); + assertThat(query.body()).contains("one.ics").doesNotContain("two.ics"); + var busy = + send( + "REPORT", + calendar, + "", + "Depth", + "1"); + assertThat(busy.statusCode()).isEqualTo(200); + assertThat(busy.body()).contains("BEGIN:VFREEBUSY", "FREEBUSY").doesNotContain("Snow meeting"); + var missing = + send( + "REPORT", + calendar, + "https://calendar.example/team/dav" + + calendar + + "absent.ics"); + assertThat(missing.statusCode()).isEqualTo(207); + assertThat(missing.body()).contains("404 Not Found"); + assertThat( + send( + "REPORT", + calendar, + query( + "x"), + "Depth", + "1") + .statusCode()) + .isEqualTo(403); + } + + @Test + void initialIncrementalDeletedExpiredAndLimitedSyncAreExplicit() throws Exception { + String calendar = create("events", "VEVENT"); + assertThat(send("REPORT", calendar, sync("", 100), "Depth", "1").statusCode()).isEqualTo(207); + assertThat( + send( + "REPORT", + calendar, + sync("", 100) + .replace( + "1", + "infinite"), + "Depth", + "1") + .statusCode()) + .isEqualTo(400); + assertThat(send("REPORT", calendar, sync("", 100), "Depth", "infinity").statusCode()) + .isEqualTo(400); + assertThat(send("REPORT", calendar, sync("", 100), "Depth", "0", "Depth", "1").statusCode()) + .isEqualTo(400); + send("PUT", calendar + "one.ics", event("one", "One")); + var initial = send("REPORT", calendar, sync("", 100), "Depth", "0"); + assertThat(initial.statusCode()).isEqualTo(207); + String token = text(initial, DavXml.DAV, "sync-token"); + send("PUT", calendar + "two.ics", event("two", "Two")); + send("DELETE", calendar + "one.ics", ""); + var delta = send("REPORT", calendar, sync(token, 100)); + assertThat(delta.statusCode()).isEqualTo(207); + assertThat(delta.body()).contains("one.ics", "two.ics", "404 Not Found"); + var partial = send("REPORT", calendar, sync(token, 1)); + assertThat(partial.statusCode()).isEqualTo(207); + assertThat(partial.body()).contains("HTTP/1.1 507 Insufficient Storage"); + var remaining = send("REPORT", calendar, sync(text(partial, DavXml.DAV, "sync-token"), 1)); + assertThat(remaining.statusCode()).isEqualTo(207); + assertThat(remaining.body()).doesNotContain("507 Insufficient Storage"); + assertThat(send("REPORT", calendar, sync("urn:invalid", 100)).statusCode()).isEqualTo(403); + String another = create("another", "VEVENT"); + assertThat(send("REPORT", another, sync(token, 100)).statusCode()).isEqualTo(403); + } + + @Test + void propertyUpdateIsAtomicAndUnknownXmlPropertiesRemainNamespaced() throws Exception { + String calendar = create("events", "VEVENT"); + var failed = + send( + "PROPPATCH", + calendar, + "Must not persistinvalid"); + assertThat(failed.statusCode()).isEqualTo(207); + assertThat(failed.body()).contains("403 Forbidden", "424 Failed Dependency"); + assertThat(send("PROPFIND", calendar, "", "Depth", "0").body()) + .contains(">events<") + .doesNotContain("Must not persist"); + var changed = + send( + "PROPPATCH", + calendar, + "New name雪 & ice"); + assertThat(changed.statusCode()).isEqualTo(207); + var props = + send( + "PROPFIND", + calendar, + "", + "Depth", + "0"); + assertThat(props.statusCode()).isEqualTo(207); + assertThat(props.body()).contains("New name", "雪 & ice", "404 Not Found", "urn:client"); + } + + private String acl(String permission) { + return "https://calendar.example/team/dav/principals/" + + bob + + "/" + + (permission.equals("WRITE") ? "" : "") + + ""; + } + + @Test + void sharingUsesOneCollectionAndAllReadWriteAndRevocationPathsCheckAcl() throws Exception { + String calendar = create("shared", "VEVENT"); + send("PUT", calendar + "secret.ics", event("secret", "Shared secret")); + assertThat(send("ACL", calendar, acl("READ")).statusCode()).isEqualTo(200); + UUID id = + jdbc.queryForObject( + "SELECT collection_id FROM dav_bindings WHERE home_id=? AND path='shared'", + UUID.class, + alice); + String shared = "/calendars/" + bob + "/" + id + "/"; + assertThat(sendAs("PROPFIND", "/calendars/" + bob + "/", "", bobAuth, "Depth", "1").body()) + .contains(id.toString()); + assertThat(sendAs("GET", shared + "secret.ics", "", bobAuth).body()).contains("Shared secret"); + assertThat( + sendAs("PUT", shared + "secret.ics", event("secret", "Forbidden"), bobAuth) + .statusCode()) + .isEqualTo(403); + assertThat(sendAs("GET", calendar + "secret.ics", "", bobAuth).statusCode()).isEqualTo(403); + assertThat(send("ACL", calendar, acl("WRITE")).statusCode()).isEqualTo(200); + assertThat( + sendAs("PUT", shared + "secret.ics", event("secret", "Written by Bob"), bobAuth) + .statusCode()) + .isEqualTo(204); + assertThat(send("GET", calendar + "secret.ics", "").body()).contains("Written by Bob"); + String token = text(sendAs("REPORT", shared, sync("", 100), bobAuth), DavXml.DAV, "sync-token"); + assertThat(send("ACL", calendar, "").statusCode()).isEqualTo(200); + assertThat(sendAs("GET", shared + "secret.ics", "", bobAuth).statusCode()).isIn(403, 404); + assertThat(sendAs("REPORT", shared, sync(token, 100), bobAuth).statusCode()).isIn(403, 404); + } + + @Test + void copyMoveResourcesAndCollectionsRespectCollisionsAndSyncHistory() throws Exception { + String source = create("source", "VEVENT"), destination = create("destination", "VEVENT"); + send("PUT", source + "one.ics", event("one", "Original")); + assertThat( + send( + "COPY", + source + "one.ics", + "", + "Destination", + "https://calendar.example/team/dav" + destination + "copy.ics", + "Overwrite", + "F") + .statusCode()) + .isEqualTo(201); + assertThat( + send( + "COPY", + source + "one.ics", + "", + "Destination", + "https://calendar.example/team/dav" + destination + "other.ics", + "Overwrite", + "F") + .statusCode()) + .isEqualTo(403); + assertThat( + send( + "MOVE", + destination + "copy.ics", + "", + "Destination", + "https://calendar.example/team/dav" + destination + "moved.ics") + .statusCode()) + .isEqualTo(201); + assertThat(send("GET", destination + "copy.ics", "").statusCode()).isEqualTo(404); + assertThat( + send( + "COPY", + source, + "", + "Destination", + "https://calendar.example/team/dav" + home + "empty/", + "Depth", + "0") + .statusCode()) + .isEqualTo(201); + assertThat(send("GET", home + "empty/one.ics", "").statusCode()).isEqualTo(404); + assertThat( + send( + "MOVE", + source, + "", + "Destination", + "https://calendar.example/team/dav" + home + "renamed/", + "Depth", + "infinity") + .statusCode()) + .isEqualTo(201); + assertThat(send("GET", home + "renamed/one.ics", "").body()).contains("Original"); + assertThat(send("GET", source + "one.ics", "").statusCode()).isEqualTo(404); + } + + @Test + void foldersAndRequiredPrincipalReportsHaveActualResponseSemantics() throws Exception { + assertThat(send("MKCOL", home + "folder/", "").statusCode()).isEqualTo(201); + String calendar = create("folder/events", "VEVENT"); + var expanded = + send( + "REPORT", + calendar, + ""); + assertThat(expanded.statusCode()).isEqualTo(207); + assertThat(expanded.body()).contains("Alice & Calendar", "D:response"); + var search = + send( + "REPORT", + "/principals/", + "Alice"); + assertThat(search.statusCode()).isEqualTo(207); + assertThat(search.body()).contains("Alice & Calendar").doesNotContain("Bob Tasks"); + var searchable = + send("REPORT", "/principals/", ""); + assertThat(searchable.statusCode()).isEqualTo(200); + assertThat(searchable.body()).contains("principal-search-property"); + var principals = + send( + "REPORT", + calendar, + ""); + assertThat(principals.statusCode()).isEqualTo(207); + assertThat(principals.body()).contains("Alice & Calendar"); + } + + @Test + void undatedTasksAreStoredWithoutInventingEventDates() throws Exception { + String calendar = create("tasks", "VTODO"), + data = + "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Independent raw task fixture//EN\r\nBEGIN:VTODO\r\nUID:task-1\r\nDTSTAMP:20261027T000000Z\r\nSUMMARY:Undated task\r\nPERCENT-COMPLETE:50\r\nPRIORITY:2\r\nEND:VTODO\r\nEND:VCALENDAR\r\n"; + assertThat(send("PUT", calendar + "task.ics", data).statusCode()).isEqualTo(201); + assertThat(send("GET", calendar + "task.ics", "").body()).isEqualTo(data); + assertThat(send("PUT", calendar + "event.ics", event("wrong", "Event")).statusCode()) + .isEqualTo(403); + var report = + send( + "REPORT", + calendar, + query( + ""), + "Depth", + "1"); + assertThat(report.statusCode()).isEqualTo(207); + assertThat(report.body()).contains("task.ics"); + } + + @Test + void metadataAclAndCollectionCreationHonorHttpPreconditions() throws Exception { + String calendar = create("conditions", "VEVENT"), path = calendar + "one.ics"; + var created = send("PUT", path, event("condition-uid", "Before")); + String etag = created.headers().firstValue("ETag").orElseThrow(); + String update = + "accepted"; + assertThat(send("PROPPATCH", path, update, "If-Match", "\"stale\"").statusCode()) + .isEqualTo(412); + assertThat(send("PROPFIND", path, "", "Depth", "0").body()).doesNotContain("accepted"); + assertThat(send("PROPPATCH", path, update, "If-Match", etag).statusCode()).isEqualTo(207); + assertThat(send("ACL", calendar, acl("READ"), "If-None-Match", "*").statusCode()) + .isEqualTo(412); + assertThat(sendAs("PROPFIND", "/calendars/" + bob + "/", "", bobAuth, "Depth", "1").body()) + .doesNotContain("conditions"); + assertThat(send("MKCOL", home + "absent/", "", "If-Match", "*").statusCode()).isEqualTo(412); + assertThat(send("GET", home + "absent/", "").statusCode()).isEqualTo(404); + assertThat(send("MKCALENDAR", home + "new/", "", "If-Match", "*").statusCode()).isEqualTo(412); + } + + @Test + void taggedDavConditionsCheckOtherResourcesWithoutAWindowForBlindWrites() throws Exception { + String calendar = create("tagged", "VEVENT"), + target = calendar + "target.ics", + gate = calendar + "gate.ics"; + send("PUT", target, event("target-uid", "Original")); + String gateTag = + send("PUT", gate, event("gate-uid", "Gate")).headers().firstValue("ETag").orElseThrow(); + String condition = " ([" + gateTag + "])"; + assertThat(send("PUT", target, event("target-uid", "Permitted"), "If", condition).statusCode()) + .isEqualTo(204); + send("PUT", gate, event("gate-uid", "Changed gate")); + assertThat( + send("PUT", target, event("target-uid", "Must not persist"), "If", condition) + .statusCode()) + .isEqualTo(412); + assertThat(send("GET", target, "").body()) + .contains("Permitted") + .doesNotContain("Must not persist"); + } + + @Test + void hostileInputAndCrossOriginDestinationsFailWithoutPartialWrites() throws Exception { + String calendar = create("events", "VEVENT"); + assertThat( + send( + "PROPFIND", + calendar, + "]>&e;") + .statusCode()) + .isEqualTo(400); + assertThat( + send( + "PROPFIND", + calendar, + "" + + "".repeat(40) + + "".repeat(40) + + "") + .statusCode()) + .isEqualTo(400); + assertThat(send("PUT", calendar + "large.ics", "x".repeat(1048577)).statusCode()) + .isEqualTo(413); + var chunked = + request(calendar + "chunked.ics", aliceAuth) + .PUT( + HttpRequest.BodyPublishers.ofInputStream( + () -> new java.io.ByteArrayInputStream(new byte[1048577]))) + .build(); + assertThat(http.send(chunked, HttpResponse.BodyHandlers.ofString()).statusCode()) + .isEqualTo(413); + assertThat(send("PUT", calendar + "bad.ics", "malformed").statusCode()).isEqualTo(403); + assertThat( + send("PUT", calendar + "compressed.ics", "data", "Content-Encoding", "gzip") + .statusCode()) + .isEqualTo(415); + assertThat( + send("COPY", calendar, "", "Destination", "https://attacker.invalid/dav/calendars/") + .statusCode()) + .isEqualTo(403); + assertThat(send("PROPFIND", calendar + "%2e%2e/", "", "Depth", "0").statusCode()) + .isIn(400, 404); + assertThat(send("GET", calendar + "bad.ics", "").statusCode()).isEqualTo(404); + } + + @Test + void principalMatchDepthZeroSearchesMembersRecursivelyAndKeepsRequestScope() throws Exception { + assertThat(send("MKCOL", home + "nested/", "").statusCode()).isEqualTo(201); + String calendar = create("nested/events", "VEVENT"); + assertThat(send("PUT", calendar + "one.ics", event("principal-match", "Member")).statusCode()) + .isEqualTo(201); + String body = + ""; + var matched = send("REPORT", home, body, "Depth", "0"); + assertThat(matched.statusCode()).isEqualTo(207); + assertThat(principalReportHrefs(matched)) + .contains( + "https://calendar.example/team/dav" + home + "nested/", + "https://calendar.example/team/dav" + calendar, + "https://calendar.example/team/dav" + calendar + "one.ics") + .doesNotContain("https://calendar.example/team/dav" + home); + assertThat(send("REPORT", home, body, "Depth", "1").statusCode()).isEqualTo(400); + assertThat(send("REPORT", home, body, "Depth", "infinity").statusCode()).isEqualTo(400); + String self = + ""; + var principals = send("REPORT", "/principals/", self, "Depth", "0"); + assertThat(principals.statusCode()).isEqualTo(207); + assertThat(principalReportHrefs(principals)) + .containsExactly("https://calendar.example/team/dav/principals/" + alice + "/"); + assertThat(principalReportHrefs(send("REPORT", home, self, "Depth", "0"))).isEmpty(); + } + + @Test + void principalSearchAndsAllPropertiesAndUnsearchablePropertiesDoNotMatch() throws Exception { + String positive = + "Alice"; + assertThat(principalReportHrefs(send("REPORT", "/principals/", positive, "Depth", "0"))) + .containsExactly("https://calendar.example/team/dav/principals/" + alice + "/"); + String mixed = + "Alice"; + var unmatched = send("REPORT", "/principals/", mixed, "Depth", "0"); + assertThat(unmatched.statusCode()).isEqualTo(207); + assertThat(principalReportHrefs(unmatched)).isEmpty(); + } + + @Test + void effectivePrivilegesAreSpecificToResourceTypeAndCoarseGrantChildrenAreAbstract() + throws Exception { + String calendar = create("privileges", "VEVENT"); + assertThat(send("PUT", calendar + "one.ics", event("privileges", "One")).statusCode()) + .isEqualTo(201); + String current = + ""; + for (String path : List.of("/", "/principals/", "/principals/" + alice + "/")) { + var response = send("PROPFIND", path, current, "Depth", "0"); + assertThat(response.statusCode()).isEqualTo(207); + assertThat(xml(response).getElementsByTagNameNS("DAV:", "write").getLength()).isZero(); + assertThat(xml(response).getElementsByTagNameNS("DAV:", "write-acl").getLength()).isZero(); + } + var resource = send("PROPFIND", calendar + "one.ics", current, "Depth", "0"); + assertThat(xml(resource).getElementsByTagNameNS("DAV:", "write-content").getLength()).isZero(); + assertThat(xml(resource).getElementsByTagNameNS("DAV:", "write-acl").getLength()).isZero(); + assertThat(xml(resource).getElementsByTagNameNS("DAV:", "bind").getLength()).isZero(); + var collection = send("PROPFIND", calendar, current, "Depth", "0"); + assertThat(xml(collection).getElementsByTagNameNS("DAV:", "write-acl").getLength()) + .isEqualTo(1); + var supported = + send( + "PROPFIND", + calendar, + "", + "Depth", + "0"); + var privileges = xml(supported).getElementsByTagNameNS("DAV:", "supported-privilege"); + boolean checked = false; + for (int index = 0; index < privileges.getLength(); index++) { + var privilege = (Element) privileges.item(index); + if (DavXml.child(DavXml.required(privilege, "DAV:", "privilege"), "DAV:", "write-properties") + != null) { + assertThat(DavXml.child(privilege, "DAV:", "abstract")).isNotNull(); + checked = true; + } + } + assertThat(checked).isTrue(); + } + + private static List principalReportHrefs(HttpResponse response) { + var nodes = xml(response).getElementsByTagNameNS("DAV:", "response"); + return java.util.stream.IntStream.range(0, nodes.getLength()) + .mapToObj( + index -> DavXml.required((Element) nodes.item(index), "DAV:", "href").getTextContent()) + .toList(); + } + + @Autowired org.busystack.caldav.sharing.GroupService groupDirectory; + @Autowired org.busystack.caldav.storage.DavStore propertyStore; + + private org.springframework.security.core.Authentication groupAdministrator() { + var account = accounts.find("group-administrator"); + if (account == null) + account = + accounts.find( + accounts.provision( + "group-administrator", + "Group administrator", + "correct horse battery staple", + true)); + return org.springframework.security.authentication.UsernamePasswordAuthenticationToken + .authenticated( + account.username(), + null, + List.of( + new org.springframework.security.core.authority.SimpleGrantedAuthority( + "ROLE_ADMIN"), + org.busystack.caldav.security.session.SessionSecurity.authority( + account.securityEpoch()), + org.springframework.security.core.authority.FactorGrantedAuthority.fromAuthority( + org.springframework.security.core.authority.FactorGrantedAuthority + .PASSWORD_AUTHORITY), + org.springframework.security.core.authority.FactorGrantedAuthority.fromAuthority( + org.springframework.security.core.authority.FactorGrantedAuthority + .WEBAUTHN_AUTHORITY))); + } + + @Test + void groupAcesUseStandardPrincipalUrlsAndOneSharedCalendarAuthority() throws Exception { + String calendar = create("group-events", "VEVENT"); + var administrator = groupAdministrator(); + UUID group = groupDirectory.create(administrator, "Calendar editors", java.util.Set.of("bob")); + String groupHref = "https://calendar.example/team/dav/principals/" + group + "/"; + String acl = + "" + + groupHref + + ""; + assertThat(send("ACL", calendar, acl).statusCode()).isEqualTo(200); + UUID id = + jdbc.queryForObject( + "SELECT id FROM collections WHERE owner_id=? AND name='group-events'", + UUID.class, + alice); + String shared = "/calendars/" + bob + "/" + id + "/"; + assertThat( + sendAs( + "PUT", + shared + "shared.ics", + event("group-shared", "Written by member"), + bobAuth) + .statusCode()) + .isEqualTo(201); + assertThat(send("GET", calendar + "shared.ics", "").body()).contains("Written by member"); + var principal = + sendAs( + "PROPFIND", + "/principals/" + group + "/", + "", + bobAuth, + "Depth", + "0"); + assertThat(principal.statusCode()).isEqualTo(207); + assertThat(principal.body()) + .contains("Calendar editors", "/principals/" + bob + "/", "404 Not Found"); + var membership = + sendAs( + "PROPFIND", + "/principals/" + bob + "/", + "", + bobAuth, + "Depth", + "0"); + assertThat(membership.body()).contains(groupHref); + String self = + ""; + assertThat( + principalReportHrefs( + sendAs("REPORT", "/principals/" + group + "/", self, bobAuth, "Depth", "0"))) + .containsExactly(groupHref); + assertThat(principalReportHrefs(sendAs("REPORT", "/principals/", self, bobAuth, "Depth", "0"))) + .contains(groupHref, "https://calendar.example/team/dav/principals/" + bob + "/"); + var aclPrincipals = + send( + "REPORT", + calendar, + ""); + assertThat(principalReportHrefs(aclPrincipals)).contains(groupHref); + groupDirectory.update(administrator, group, "Calendar editors", java.util.Set.of()); + assertThat(sendAs("GET", shared + "shared.ics", "", bobAuth).statusCode()).isIn(403, 404); + assertThat(send("GET", calendar + "shared.ics", "").body()).contains("Written by member"); + } + + @Test + void aclPrincipalReportsRequireReadAclAndReturnActualGroupOwner() throws Exception { + String calendar = create("acl-principal-permissions", "VEVENT"); + var administrator = groupAdministrator(); + UUID group = + groupDirectory.create(administrator, "Restricted directory", java.util.Set.of("bob")); + String report = + ""; + var deniedGroupReport = sendAs("REPORT", "/principals/" + group + "/", report, bobAuth); + assertThat(deniedGroupReport.statusCode()).isEqualTo(403); + var need = DavXml.required(xml(deniedGroupReport), "DAV:", "need-privileges"); + var failing = DavXml.required(need, "DAV:", "resource"); + assertThat(DavXml.required(failing, "DAV:", "href").getTextContent()) + .isEqualTo("https://calendar.example/team/dav/principals/" + group + "/"); + assertThat(DavXml.child(DavXml.required(failing, "DAV:", "privilege"), "DAV:", "read-acl")) + .isNotNull(); + assertThat(send("ACL", calendar, acl("READ")).statusCode()).isEqualTo(200); + assertThat(send("REPORT", "/principals/" + bob + "/", report).statusCode()).isEqualTo(403); + UUID calendarId = + jdbc.queryForObject( + "SELECT id FROM collections WHERE owner_id=? AND name='acl-principal-permissions'", + UUID.class, + alice); + String shared = "/calendars/" + bob + "/" + calendarId + "/"; + var deniedWrite = + sendAs("PUT", shared + "not-writable.ics", event("not-writable", "Denied"), bobAuth); + assertThat(deniedWrite.statusCode()).isEqualTo(403); + var writeFailure = + DavXml.required( + DavXml.required(xml(deniedWrite), "DAV:", "need-privileges"), "DAV:", "resource"); + assertThat(DavXml.required(writeFailure, "DAV:", "href").getTextContent()) + .isEqualTo("https://calendar.example/team/dav" + shared); + assertThat(DavXml.child(DavXml.required(writeFailure, "DAV:", "privilege"), "DAV:", "write")) + .isNotNull(); + var deniedOwnership = sendAs("DELETE", shared, "", bobAuth); + assertThat(deniedOwnership.statusCode()).isEqualTo(403); + var ownerFailure = + DavXml.required( + DavXml.required(xml(deniedOwnership), "DAV:", "need-privileges"), "DAV:", "resource"); + assertThat( + DavXml.child(DavXml.required(ownerFailure, "DAV:", "privilege"), "DAV:", "write-acl")) + .isNotNull(); + UUID administratorId = accounts.find(administrator.getName()).id(); + String administratorAuth = + basic( + administrator.getName(), + accounts + .issueDavCredential( + administratorId, "group owner report", Instant.now().plusSeconds(3600)) + .secret()); + var ownerReport = sendAs("REPORT", "/principals/" + group + "/", report, administratorAuth); + assertThat(ownerReport.statusCode()).isEqualTo(207); + assertThat(principalReportHrefs(ownerReport)) + .containsExactly("https://calendar.example/team/dav/principals/" + administratorId + "/"); + assertThat(principalReportHrefs(sendAs("REPORT", "/principals/" + bob + "/", report, bobAuth))) + .containsExactly("https://calendar.example/team/dav/principals/" + bob + "/"); + } + + @Test + void partialGroupWritePrivilegeCannotEscalateReadOnlySharing() throws Exception { + String calendar = create("group-read-only", "VEVENT"); + UUID group = groupDirectory.create(groupAdministrator(), "Readers", java.util.Set.of("bob")); + UUID id = + jdbc.queryForObject( + "SELECT id FROM collections WHERE owner_id=? AND name='group-read-only'", + UUID.class, + alice); + String principal = + "https://calendar.example/team/dav/principals/" + + group + + "/"; + String read = + "" + + principal + + ""; + assertThat(send("ACL", calendar, read).statusCode()).isEqualTo(200); + String partial = + "" + + principal + + ""; + assertThat(send("ACL", calendar, partial).statusCode()).isEqualTo(403); + assertThat( + sendAs( + "PUT", + "/calendars/" + bob + "/" + id + "/forbidden.ics", + event("forbidden", "Forbidden"), + bobAuth) + .statusCode()) + .isEqualTo(403); + } + + @Test + void displayNamePreservesXmlLanguageAndResourceMetadataDoesNotRenameOrRewriteContent() + throws Exception { + String calendar = home + "language/"; + assertThat( + send( + "MKCALENDAR", + calendar, + "Vacances") + .statusCode()) + .isEqualTo(201); + String prop = ""; + var name = + (Element) + xml(send("PROPFIND", calendar, prop, "Depth", "0")) + .getElementsByTagNameNS("DAV:", "displayname") + .item(0); + assertThat(name.getAttributeNS(javax.xml.XMLConstants.XML_NS_URI, "lang")).isEqualTo("fr"); + String content = event("language-event", "Original content"); + var put = send("PUT", calendar + "original.ics", content); + String etag = put.headers().firstValue("ETag").orElseThrow(); + String patch = + "予定"; + assertThat(send("PROPPATCH", calendar + "original.ics", patch).statusCode()).isEqualTo(207); + var resourceName = + (Element) + xml(send("PROPFIND", calendar + "original.ics", prop, "Depth", "0")) + .getElementsByTagNameNS("DAV:", "displayname") + .item(0); + assertThat(resourceName.getTextContent()).isEqualTo("予定"); + assertThat(resourceName.getAttributeNS(javax.xml.XMLConstants.XML_NS_URI, "lang")) + .isEqualTo("ja"); + var get = send("GET", calendar + "original.ics", ""); + assertThat(get.body()).isEqualTo(content); + assertThat(get.headers().firstValue("ETag").orElseThrow()).isEqualTo(etag); + assertThat( + send( + "PROPPATCH", + calendar + "original.ics", + "") + .statusCode()) + .isEqualTo(207); + assertThat( + text( + send("PROPFIND", calendar + "original.ics", prop, "Depth", "0"), + "DAV:", + "displayname")) + .isEqualTo("original.ics"); + propertyStore.renameDisplayName(alice, alice, "language", "Renamed"); + var renamed = + (Element) + xml(send("PROPFIND", calendar, prop, "Depth", "0")) + .getElementsByTagNameNS("DAV:", "displayname") + .item(0); + assertThat(renamed.getTextContent()).isEqualTo("Renamed"); + assertThat(renamed.hasAttributeNS(javax.xml.XMLConstants.XML_NS_URI, "lang")).isFalse(); + } + + @Test + void collectionGetAndHeadEvaluatePreconditionsWithoutInventingValidators() throws Exception { + String calendar = create("collection-conditions", "VEVENT"); + for (String collection : List.of(home, calendar)) { + for (String method : List.of("GET", "HEAD")) { + assertThat(send(method, collection, "", "If-Match", "*").statusCode()).isEqualTo(200); + var none = send(method, collection, "", "If-None-Match", "*"); + assertThat(none.statusCode()).isEqualTo(304); + assertThat(none.body()).isEmpty(); + assertThat(none.headers().firstValue("ETag")).isEmpty(); + assertThat(send(method, collection, "", "If-Match", "\"unknown\"").statusCode()) + .isEqualTo(412); + assertThat(send(method, collection, "", "If-None-Match", "W/\"unknown\"").statusCode()) + .isEqualTo(200); + } + assertThat(send("GET", collection, "", "If-None-Match", "malformed").statusCode()) + .isEqualTo(400); + } + var redirect = + send("GET", calendar.substring(0, calendar.length() - 1), "", "If-None-Match", "*"); + assertThat(redirect.statusCode()).isEqualTo(308); + assertThat(redirect.headers().firstValue("Location")) + .contains("https://calendar.example/team/dav" + calendar); + assertThat(sendAs("GET", calendar, "", bobAuth, "If-None-Match", "*").statusCode()) + .isEqualTo(403); + } + + @Test + void freeBusyReportsAreCollectionOnlyAndConcealAnUnreadableRequestTarget() throws Exception { + String calendar = create("freebusy-contract", "VEVENT"); + assertThat( + send("PUT", calendar + "event.ics", event("freebusy-contract", "Visible")).statusCode()) + .isEqualTo(201); + String report = + ""; + assertThat(send("REPORT", calendar, report, "Depth", "1").statusCode()).isEqualTo(200); + assertThat(send("REPORT", calendar + "event.ics", report).statusCode()).isEqualTo(403); + var supported = + send( + "PROPFIND", + calendar + "event.ics", + "", + "Depth", + "0"); + assertThat(supported.statusCode()).isEqualTo(207); + assertThat(supported.body()) + .contains("calendar-query", "calendar-multiget") + .doesNotContain("free-busy-query"); + assertThat(sendAs("REPORT", calendar, report, bobAuth, "Depth", "1").statusCode()) + .isEqualTo(404); + assertThat(sendAs("REPORT", calendar, query(""), bobAuth, "Depth", "1").statusCode()) + .isEqualTo(403); + } + + @Test + void calendarValidationPreconditionsAreDistinctAndRejectedWritesPreserveBothCollections() + throws Exception { + String events = create("validation-events", "VEVENT"), + tasks = create("validation-tasks", "VTODO"); + String original = event("validation-master", "Original"), resource = events + "original.ics"; + var created = send("PUT", resource, original); + assertThat(created.statusCode()).isEqualTo(201); + String originalEtag = created.headers().firstValue("ETag").orElseThrow(); + String task = + "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Independent validation fixture//EN\r\nBEGIN:VTODO\r\nUID:task-validation\r\nDTSTAMP:20261027T000000Z\r\nSUMMARY:Valid undated task\r\nEND:VTODO\r\nEND:VCALENDAR\r\n"; + assertThat(send("PUT", tasks + "task.ics", task).statusCode()).isEqualTo(201); + String eventToken = text(send("REPORT", events, sync("", 100)), DavXml.DAV, "sync-token"); + String taskToken = text(send("REPORT", tasks, sync("", 100)), DavXml.DAV, "sync-token"); + String method = original.replace("VERSION:2.0\r\n", "VERSION:2.0\r\nMETHOD:PUBLISH\r\n"); + String mixed = + original.replace( + "END:VCALENDAR\r\n", + "BEGIN:VTODO\r\nUID:validation-master\r\nDTSTAMP:20261027T000000Z\r\nEND:VTODO\r\nEND:VCALENDAR\r\n"); + String mixedUid = + original.replace( + "END:VCALENDAR\r\n", + "BEGIN:VEVENT\r\nUID:different-uid\r\nDTSTAMP:20261027T000000Z\r\nRECURRENCE-ID:20261028T100000Z\r\nDTSTART:20261028T100000Z\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n"); + String missingZone = + original + .replace("DTSTART:20261027T100000Z", "DTSTART;TZID=America/New_York:20261027T100000") + .replace("DTEND:20261027T110000Z", "DTEND;TZID=America/New_York:20261027T110000"); + for (String invalid : List.of(method, mixed, mixedUid, missingZone)) { + var rejected = send("PUT", resource, invalid, "If-Match", originalEtag); + assertThat(rejected.statusCode()).isEqualTo(403); + assertThat(DavXml.child(xml(rejected), DavXml.CAL, "valid-calendar-object-resource")) + .isNotNull(); + } + for (String invalid : List.of("not iCalendar", "")) { + var malformed = send("PUT", resource, invalid, "If-Match", originalEtag); + assertThat(malformed.statusCode()).isEqualTo(403); + assertThat(DavXml.child(xml(malformed), DavXml.CAL, "valid-calendar-data")).isNotNull(); + } + var unsupported = send("PUT", resource, task, "If-Match", originalEtag); + assertThat(unsupported.statusCode()).isEqualTo(403); + assertThat(DavXml.child(xml(unsupported), DavXml.CAL, "supported-calendar-component")) + .isNotNull(); + for (String operation : List.of("COPY", "MOVE")) { + var rejected = + send( + operation, + tasks + "task.ics", + "", + "Destination", + "https://calendar.example/team/dav" + events + "task.ics"); + assertThat(rejected.statusCode()).isEqualTo(403); + assertThat(DavXml.child(xml(rejected), DavXml.CAL, "supported-calendar-component")) + .isNotNull(); + assertThat(send("GET", tasks + "task.ics", "").body()).isEqualTo(task); + assertThat(send("GET", events + "task.ics", "").statusCode()).isEqualTo(404); + } + var current = send("GET", resource, ""); + assertThat(current.body()).isEqualTo(original); + assertThat(current.headers().firstValue("ETag")).contains(originalEtag); + assertThat(text(send("REPORT", events, sync("", 100)), DavXml.DAV, "sync-token")) + .isEqualTo(eventToken); + assertThat(text(send("REPORT", tasks, sync("", 100)), DavXml.DAV, "sync-token")) + .isEqualTo(taskToken); + String zoned = + event("embedded-zone", "Embedded zone") + .replace( + "BEGIN:VEVENT\r\n", + "BEGIN:VTIMEZONE\r\nTZID:Custom/Zone\r\nBEGIN:STANDARD\r\nDTSTART:19700101T000000\r\nTZOFFSETFROM:+0200\r\nTZOFFSETTO:+0200\r\nEND:STANDARD\r\nEND:VTIMEZONE\r\nBEGIN:VEVENT\r\n") + .replace("DTSTART:20261027T100000Z", "DTSTART;TZID=Custom/Zone:20261027T100000") + .replace("DTEND:20261027T110000Z", "DTEND;TZID=Custom/Zone:20261027T110000"); + assertThat(send("PUT", events + "embedded-zone.ics", zoned).statusCode()).isEqualTo(201); + assertThat(send("GET", events + "embedded-zone.ics", "").body()).isEqualTo(zoned); + } + + private String currentCollectionToken(String collection) throws Exception { + var response = + send( + "PROPFIND", + collection, + "", + "Depth", + "0"); + assertThat(response.statusCode()).isEqualTo(207); + return text(response, DavXml.DAV, "sync-token"); + } + + @Test + void residualCalendarGrammarFailuresRejectCreationAndConditionalReplacementAtomically() + throws Exception { + for (String name : + List.of( + "invalid-leading-token", + "invalid-unicode-leading-token", + "invalid-unicode-bom-leading-token", + "invalid-zero-byday", + "invalid-plus-zero-byday", + "invalid-minus-zero-byday", + "invalid-weeks-days")) { + String source = + name.equals("invalid-minus-zero-byday") + ? "invalid-zero-byday" + : name.contains("unicode") ? "invalid-leading-token" : name; + String invalid = independentAdmissionFixture(source); + if (name.equals("invalid-minus-zero-byday")) + invalid = invalid.replace("BYDAY=0MO", "BYDAY=-0MO"); + if (name.contains("unicode")) + invalid = + invalid.replace( + "XBEGIN:VCALENDAR", + (name.contains("-bom-") ? "\uFEFF" : "") + "beg\u0131n:vcalendar"); + String uid = + invalid + .lines() + .filter(line -> line.startsWith("UID:")) + .findFirst() + .orElseThrow() + .substring(4); + String calendar = create(name, "VEVENT"), resource = calendar + "resource.ics"; + UUID collection = datedCollection(name); + var empty = admissionHistoryState(collection); + String emptyToken = currentCollectionToken(calendar); + var creation = + send("PUT", resource, invalid, "Content-Type", "text/calendar", "If-None-Match", "*"); + assertThat(creation.statusCode()).as(name + " creation").isEqualTo(403); + assertThat(DavXml.child(xml(creation), DavXml.CAL, "valid-calendar-data")).isNotNull(); + assertThat(send("GET", resource, "").statusCode()).isEqualTo(404); + assertThat(admissionHistoryState(collection)).isEqualTo(empty); + assertThat(empty.get("resources")).isEqualTo(0L); + assertThat(empty.get("versions")).isEqualTo(0L); + assertThat(empty.get("changes")).isEqualTo(0L); + assertThat(currentCollectionToken(calendar)).isEqualTo(emptyToken); + + String previous = event(uid, "Preserved conditional-write control"); + assertThat(send("PUT", resource, previous, "If-None-Match", "*").statusCode()).isEqualTo(201); + var before = + http.send( + request(resource, aliceAuth).GET().build(), HttpResponse.BodyHandlers.ofByteArray()); + assertThat(before.statusCode()).isEqualTo(200); + assertThat(before.body()).isEqualTo(previous.getBytes(StandardCharsets.UTF_8)); + String etag = before.headers().firstValue("ETag").orElseThrow(); + var state = admissionHistoryState(collection); + String token = currentCollectionToken(calendar); + var replacement = + send("PUT", resource, invalid, "Content-Type", "text/calendar", "If-Match", etag); + assertThat(replacement.statusCode()).as(name + " conditional replacement").isEqualTo(403); + assertThat(DavXml.child(xml(replacement), DavXml.CAL, "valid-calendar-data")).isNotNull(); + var after = + http.send( + request(resource, aliceAuth).GET().build(), HttpResponse.BodyHandlers.ofByteArray()); + assertThat(after.statusCode()).isEqualTo(200); + assertThat(after.body()).isEqualTo(before.body()); + assertThat(after.headers().firstValue("ETag")).contains(etag); + assertThat(admissionHistoryState(collection)).isEqualTo(state); + assertThat(currentCollectionToken(calendar)).isEqualTo(token); + var incremental = send("REPORT", calendar, sync(token, 100), "Depth", "0"); + assertThat(incremental.statusCode()).isEqualTo(207); + assertThat(xml(incremental).getElementsByTagNameNS(DavXml.DAV, "response").getLength()) + .isZero(); + } + } + + @Test + void residualCalendarLegalNeighborsAndInitialCaseBomVariantsKeepExactGetBytes() throws Exception { + String calendar = create("lexical-legal-neighbors", "VEVENT"); + for (String name : + List.of("valid-first-weekday", "valid-last-weekday", "valid-one-week", "valid-nine-days")) { + String resource = calendar + name + ".ics", body = independentAdmissionFixture(name); + assertThat(send("PUT", resource, body, "If-None-Match", "*").statusCode()).isEqualTo(201); + var get = + http.send( + request(resource, aliceAuth).GET().build(), HttpResponse.BodyHandlers.ofByteArray()); + assertThat(get.statusCode()).isEqualTo(200); + assertThat(get.body()).isEqualTo(body.getBytes(StandardCharsets.UTF_8)); + } + int ordinal = 0; + for (String boundary : + List.of( + "BEGIN:VCALENDAR", + "begin:vcalendar", + "bEgIn:vCaLeNdAr", + "\uFEFFBEGIN:VCALENDAR", + "\uFEFFbEgIn:vCaLeNdAr")) { + String resource = calendar + "boundary-" + ordinal + ".ics"; + String body = + event("boundary-" + ordinal++, "CaseSensitive TEXT and BOM control") + .replaceFirst("BEGIN:VCALENDAR", boundary); + assertThat(send("PUT", resource, body, "If-None-Match", "*").statusCode()).isEqualTo(201); + var get = + http.send( + request(resource, aliceAuth).GET().build(), HttpResponse.BodyHandlers.ofByteArray()); + assertThat(get.statusCode()).isEqualTo(200); + assertThat(get.body()).isEqualTo(body.getBytes(StandardCharsets.UTF_8)); + } + } + + private static String independentAdmissionFixture(String name) throws Exception { + try (var input = + DavHttpIT.class.getResourceAsStream( + "/calendar/independent-verification/" + name + ".ics")) { + if (input == null) throw new IllegalStateException("Missing independent admission fixture"); + return new String(input.readAllBytes(), StandardCharsets.UTF_8); + } + } + + private java.util.Map admissionHistoryState(UUID collection) { + return jdbc.queryForMap( + "SELECT c.revision, (SELECT count(*) FROM calendar_resources r WHERE r.collection_id=c.id) AS resources," + + " (SELECT coalesce(max(r.revision),0) FROM calendar_resources r WHERE r.collection_id=c.id) AS resource_revision," + + " (SELECT count(*) FROM calendar_resource_versions v WHERE v.collection_id=c.id) AS versions," + + " (SELECT count(*) FROM collection_changes h WHERE h.collection_id=c.id) AS changes" + + " FROM collections c WHERE c.id=?", + collection); + } + + @Test + void syncStateTokensWorkInTaggedAndUntaggedDavIfWithNegationAndCollectionScoping() + throws Exception { + String calendar = create("sync-state", "VEVENT"), other = create("sync-state-other", "VEVENT"); + String href = "https://calendar.example/team/dav" + calendar; + String initial = currentCollectionToken(calendar); + assertThat(send("GET", calendar, "", "If", "(<" + initial + ">)").statusCode()).isEqualTo(200); + String first = calendar + "first.ics"; + assertThat( + send( + "PUT", + first, + event("sync-first", "First"), + "If", + "<" + href + "> (<" + initial + ">)") + .statusCode()) + .isEqualTo(201); + assertThat( + send( + "PUT", + calendar + "stale.ics", + event("sync-stale", "Denied"), + "If", + "<" + href + "> (<" + initial + ">)") + .statusCode()) + .isEqualTo(412); + String current = currentCollectionToken(calendar); + assertThat(current).isNotEqualTo(initial); + assertThat( + send( + "PUT", + calendar + "negated.ics", + event("sync-negated", "Negated"), + "If", + "<" + href + "> (Not <" + initial + ">)") + .statusCode()) + .isEqualTo(201); + current = currentCollectionToken(calendar); + assertThat( + send( + "PUT", + calendar + "denied.ics", + event("sync-denied", "Denied"), + "If", + "<" + href + "> (Not <" + current + ">)") + .statusCode()) + .isEqualTo(412); + assertThat( + send( + "PUT", + calendar + "wrong-collection.ics", + event("sync-wrong", "Denied"), + "If", + "<" + href + "> (<" + currentCollectionToken(other) + ">)") + .statusCode()) + .isEqualTo(412); + String firstEtag = send("GET", first, "").headers().firstValue("ETag").orElseThrow(); + String mixed = + "<" + + href + + "> (<" + + current + + ">) ([\"wrong\"]) ([" + + firstEtag + + "] Not )"; + assertThat(send("GET", first, "", "If", mixed, "If-None-Match", firstEtag).statusCode()) + .isEqualTo(304); + assertThat(send("PUT", first, event("sync-first", "Changed"), "If", mixed).statusCode()) + .isEqualTo(204); + assertThat(send("GET", calendar + "stale.ics", "").statusCode()).isEqualTo(404); + assertThat(send("GET", calendar + "denied.ics", "").statusCode()).isEqualTo(404); + } + + @Test + void syncStateConditionWaitsForAnInFlightMutationBeforeEvaluating() throws Exception { + String calendar = create("sync-state-concurrent", "VEVENT"); + UUID collection = + jdbc.queryForObject( + "SELECT id FROM collections WHERE owner_id=? AND name='sync-state-concurrent'", + UUID.class, + alice); + String before = currentCollectionToken(calendar); + var locked = new java.util.concurrent.CountDownLatch(1); + var finish = new java.util.concurrent.CountDownLatch(1); + var holderPid = new java.util.concurrent.atomic.AtomicInteger(); + try (var workers = java.util.concurrent.Executors.newVirtualThreadPerTaskExecutor()) { + var writer = + workers.submit( + () -> + new org.springframework.transaction.support.TransactionTemplate(transactions) + .executeWithoutResult( + status -> { + org.busystack.caldav.storage.DavMutationLock.shared(jdbc); + holderPid.set( + jdbc.queryForObject("SELECT pg_backend_pid()", Integer.class)); + locked.countDown(); + try { + if (!finish.await(15, java.util.concurrent.TimeUnit.SECONDS)) + throw new AssertionError("Sync condition coordination timed out"); + } catch (InterruptedException interrupted) { + Thread.currentThread().interrupt(); + throw new AssertionError(interrupted); + } + propertyStore.putResource( + alice, + collection, + "committed.ics", + event("sync-committed", "Committed first") + .getBytes(StandardCharsets.UTF_8), + org.busystack.caldav.storage.WriteCondition.create()); + })); + try { + assertThat(locked.await(10, java.util.concurrent.TimeUnit.SECONDS)).isTrue(); + var conditional = + workers.submit( + () -> + send( + "PUT", + calendar + "conditional.ics", + event("sync-conditional", "Must fail"), + "If", + " (<" + before + ">)")); + org.awaitility.Awaitility.await() + .atMost(java.time.Duration.ofSeconds(10)) + .untilAsserted( + () -> + assertThat( + jdbc.queryForObject( + "SELECT EXISTS(SELECT 1 FROM pg_stat_activity WHERE ?=ANY(pg_blocking_pids(pid)))", + Boolean.class, + holderPid.get())) + .isTrue()); + finish.countDown(); + writer.get(15, java.util.concurrent.TimeUnit.SECONDS); + assertThat(conditional.get(15, java.util.concurrent.TimeUnit.SECONDS).statusCode()) + .isEqualTo(412); + } finally { + finish.countDown(); + } + } + assertThat(send("GET", calendar + "committed.ics", "").statusCode()).isEqualTo(200); + assertThat(send("GET", calendar + "conditional.ics", "").statusCode()).isEqualTo(404); + var delta = send("REPORT", calendar, sync(before, 100)); + assertThat(delta.statusCode()).isEqualTo(207); + assertThat(delta.body()).contains("committed.ics").doesNotContain("conditional.ics"); + } + + @Test + void syncTokenOnlyPropertiesAndConditionsDoNotLoadCollectionBodies() throws Exception { + String calendar = create("metadata-token", "VEVENT"); + UUID collection = + jdbc.queryForObject( + "SELECT id FROM collections WHERE owner_id=? AND name='metadata-token'", + UUID.class, + alice); + // Deliberately oversized database-only bodies isolate the metadata/report budget distinction. + jdbc.update( + "INSERT INTO calendar_resources(collection_id,name,uid,content,etag,revision) SELECT ?,value::text||'.ics',value::text,decode(repeat('41',1048576),'hex'),'\"budget-fixture\"',value FROM generate_series(1,33) value", + collection); + jdbc.update("UPDATE collections SET revision=33 WHERE id=?", collection); + String token = currentCollectionToken(calendar); + var metadata = send("REPORT", calendar, sync("", 100)); + assertThat(metadata.statusCode()).isEqualTo(207); + assertThat(xml(metadata).getElementsByTagNameNS("DAV:", "getetag").getLength()).isEqualTo(33); + assertThat( + send( + "PUT", + calendar + "small.ics", + event("metadata-small", "Small"), + "If", + " (<" + token + ">)") + .statusCode()) + .isEqualTo(201); + assertThat(currentCollectionToken(calendar)).isNotEqualTo(token); + } + + @Test + void resourceMultigetCardinalityAndUnsignedSyncLimitsHaveDistinctContracts() throws Exception { + String calendar = create("report-contracts", "VEVENT"), resource = calendar + "one.ics"; + assertThat(send("PUT", resource, event("report-one", "One")).statusCode()).isEqualTo(201); + String href = "https://calendar.example/team/dav" + resource + ""; + String prefix = + ""; + assertThat( + send("REPORT", resource, prefix + href + "", "Depth", "infinity") + .statusCode()) + .isEqualTo(207); + assertThat( + send("REPORT", resource, prefix + href + href + "").statusCode()) + .isEqualTo(400); + for (String invalid : List.of("-1", "+1", " 1", "1.0")) + assertThat( + send( + "REPORT", + calendar, + sync("", 1) + .replace( + "1", + "" + invalid + "")) + .statusCode()) + .isEqualTo(400); + var zero = send("REPORT", calendar, sync("", 0)); + assertThat(zero.statusCode()).isEqualTo(507); + assertThat(zero.body()).doesNotContain("sync-token"); + for (String ceiling : List.of("10001", "9".repeat(1000))) { + var accepted = + send( + "REPORT", + calendar, + sync("", 1) + .replace( + "1", "" + ceiling + "")); + assertThat(accepted.statusCode()) + .as("The client ceiling may exceed the server page size") + .isEqualTo(207); + var result = xml(accepted); + assertThat(result.getElementsByTagNameNS("DAV:", "response").getLength()).isEqualTo(1); + assertThat(result.getElementsByTagNameNS("DAV:", "getetag").getLength()).isEqualTo(1); + assertThat(result.getElementsByTagNameNS("DAV:", "sync-token").getLength()).isEqualTo(1); + assertThat(accepted.body()).contains("one.ics").doesNotContain(" 507 "); + } + assertThat( + send( + "REPORT", + calendar, + sync("", 1) + .replace("1", "00001")) + .statusCode()) + .isEqualTo(207); + } + + @Test + void multigetBoundsRepeatedResourceResolutionBeforeBuildingAnUnboundedResponse() + throws Exception { + String calendar = create("multiget-budget", "VEVENT"), resource = calendar + "large.ics"; + String content = + event("large-report", "Large report") + .replace("END:VEVENT\r\n", "X-BULK:" + "a".repeat(1047000) + "\r\nEND:VEVENT\r\n"); + assertThat(send("PUT", resource, content).statusCode()).isEqualTo(201); + String href = "https://calendar.example/team/dav" + resource + ""; + String report = + "" + + href.repeat(34) + + ""; + var metadata = send("REPORT", calendar, report); + assertThat(metadata.statusCode()).isEqualTo(207); + assertThat(xml(metadata).getElementsByTagNameNS("DAV:", "getetag").getLength()).isEqualTo(34); + assertThat(xml(metadata).getElementsByTagNameNS("DAV:", "response").getLength()).isEqualTo(34); + var limited = + send("REPORT", calendar, report.replace("", "")); + assertThat(limited.statusCode()).isEqualTo(507); + assertThat(limited.body()) + .contains("number-of-matches-within-limits") + .doesNotContain("multistatus", "calendar-data"); + assertThat(send("GET", resource, "").body()).isEqualTo(content); + } + + private UUID datedCollection(String display) { + return jdbc.queryForObject( + "SELECT id FROM collections WHERE owner_id=? AND name=?", UUID.class, alice, display); + } + + private void setModificationDate(UUID collection, String name, Instant modifiedAt) { + jdbc.update( + "UPDATE calendar_resources SET updated_at=? WHERE collection_id=? AND name=?", + java.sql.Timestamp.from(modifiedAt), + collection, + name); + } + + @Test + void resourceModificationDatesRoundTripWithWholeSecondHttpPrecision() throws Exception { + String calendar = create("http-dates", "VEVENT"), resource = calendar + "event.ics"; + String content = event("http-dates", "Unchanged representation"); + assertThat(send("PUT", resource, content).statusCode()).isEqualTo(201); + Instant modified = Instant.parse("2020-01-01T12:00:00.987654Z"); + setModificationDate(datedCollection("http-dates"), "event.ics", modified); + String date = HttpDateValues.format(modified); + var get = send("GET", resource, ""); + String etag = get.headers().firstValue("ETag").orElseThrow(); + assertThat(get.statusCode()).isEqualTo(200); + assertThat(get.body()).isEqualTo(content); + assertThat(get.headers().firstValue("Last-Modified")).contains(date); + assertThat(get.headers().firstValue("Date")).isPresent(); + var head = send("HEAD", resource, ""); + assertThat(head.statusCode()).isEqualTo(200); + assertThat(head.body()).isEmpty(); + assertThat(head.headers().firstValue("Last-Modified")).contains(date); + String properties = + ""; + assertThat( + text(send("PROPFIND", resource, properties, "Depth", "0"), "DAV:", "getlastmodified")) + .isEqualTo(date); + for (String method : List.of("GET", "HEAD")) { + var cached = send(method, resource, "", "If-Modified-Since", date); + assertThat(cached.statusCode()).isEqualTo(304); + assertThat(cached.body()).isEmpty(); + assertThat(cached.headers().firstValue("Last-Modified")).contains(date); + assertThat(cached.headers().firstValue("ETag")).contains(etag); + assertThat( + send( + method, + resource, + "", + "If-Modified-Since", + HttpDateValues.format(modified.minusSeconds(1))) + .statusCode()) + .isEqualTo(200); + assertThat( + send( + method, + resource, + "", + "If-Unmodified-Since", + HttpDateValues.format(modified.minusSeconds(1)), + "If-None-Match", + etag) + .statusCode()) + .isEqualTo(412); + assertThat( + send( + method, + resource, + "", + "If-Match", + etag, + "If-Unmodified-Since", + HttpDateValues.format(modified.minusSeconds(1))) + .statusCode()) + .isEqualTo(200); + assertThat( + send( + method, + resource, + "", + "If-None-Match", + "\"different\"", + "If-Modified-Since", + date) + .statusCode()) + .isEqualTo(200); + } + assertThat(send("PUT", resource, content, "If-Unmodified-Since", date).statusCode()) + .isEqualTo(204); + String patch = + "blue"; + assertThat(send("PROPPATCH", resource, patch, "If-Unmodified-Since", date).statusCode()) + .isEqualTo(207); + assertThat(send("GET", resource, "").headers().firstValue("Last-Modified")).contains(date); + assertThat(send("GET", resource, "").body()).isEqualTo(content); + var protectedDate = + send( + "PROPPATCH", + resource, + "" + + date + + ""); + assertThat(protectedDate.statusCode()).isEqualTo(207); + assertThat(protectedDate.body()).contains("403 Forbidden", "cannot-modify-protected-property"); + } + + @Test + void staleDateMutationConditionsLeaveBytesPropertiesRevisionsAndSyncUnchanged() throws Exception { + String calendar = create("date-writes", "VEVENT"), target = create("date-target", "VEVENT"); + String resource = calendar + "event.ics", original = event("date-write", "Original"); + assertThat(send("PUT", resource, original).statusCode()).isEqualTo(201); + Instant modified = Instant.parse("2020-01-01T12:00:00.500Z"); + setModificationDate(datedCollection("date-writes"), "event.ics", modified); + String stale = HttpDateValues.format(modified.minusSeconds(1)); + String token = currentCollectionToken(calendar), targetToken = currentCollectionToken(target); + var before = send("GET", resource, ""); + String etag = before.headers().firstValue("ETag").orElseThrow(); + assertThat( + send("PUT", resource, event("date-write", "Rejected"), "If-Unmodified-Since", stale) + .statusCode()) + .isEqualTo(412); + assertThat(send("DELETE", resource, "", "If-Unmodified-Since", stale).statusCode()) + .isEqualTo(412); + for (String method : List.of("COPY", "MOVE")) + assertThat( + send( + method, + resource, + "", + "If-Unmodified-Since", + stale, + "Destination", + "https://calendar.example/team/dav" + target + "copied.ics") + .statusCode()) + .isEqualTo(412); + String patch = + "rejected"; + assertThat(send("PROPPATCH", resource, patch, "If-Unmodified-Since", stale).statusCode()) + .isEqualTo(412); + assertThat(send("GET", resource, "").body()).isEqualTo(original); + assertThat(send("GET", resource, "").headers().firstValue("ETag")).contains(etag); + assertThat(currentCollectionToken(calendar)).isEqualTo(token); + assertThat(currentCollectionToken(target)).isEqualTo(targetToken); + var color = + send( + "PROPFIND", + resource, + "", + "Depth", + "0"); + assertThat(color.body()).contains("404 Not Found").doesNotContain("rejected"); + assertThat( + send( + "PUT", + resource, + event("date-write", "Accepted"), + "If-Match", + etag, + "If-Unmodified-Since", + stale) + .statusCode()) + .isEqualTo(204); + assertThat( + send( + "DELETE", + resource, + "", + "If-Unmodified-Since", + HttpDateValues.format(Instant.now().plusSeconds(3600))) + .statusCode()) + .isEqualTo(204); + } + + @Test + void legacyAndInvalidHttpDatesHaveTheirSpecifiedWireBehavior() throws Exception { + String calendar = create("legacy-dates", "VEVENT"), resource = calendar + "event.ics"; + String content = event("legacy-date", "Dates"); + assertThat(send("PUT", resource, content).statusCode()).isEqualTo(201); + setModificationDate( + datedCollection("legacy-dates"), "event.ics", Instant.parse("1994-11-06T08:49:37.250Z")); + for (String date : + List.of( + "Sun, 06 Nov 1994 08:49:37 GMT", + "Sunday, 06-Nov-94 08:49:37 GMT", + "Sun Nov 6 08:49:37 1994")) { + assertThat(send("GET", resource, "", "If-Modified-Since", date).statusCode()).isEqualTo(304); + assertThat(send("PUT", resource, content, "If-Unmodified-Since", date).statusCode()) + .isEqualTo(204); + } + for (String invalid : + List.of( + "invalid date", + "Sun, 06 Nov 1994 08:49:37 GMT, Sun, 06 Nov 1994 08:49:37 GMT", + "Sun, 06 Nov 1994 08:49:37 GMT trailing")) { + assertThat(send("GET", resource, "", "If-Modified-Since", invalid).statusCode()) + .isEqualTo(200); + assertThat(send("PUT", resource, content, "If-Unmodified-Since", invalid).statusCode()) + .isEqualTo(204); + } + assertThat( + send( + "GET", + resource, + "", + "If-Modified-Since", + "Sun, 06 Nov 1994 08:49:37 GMT", + "If-Modified-Since", + "Sun, 06 Nov 1994 08:49:37 GMT") + .statusCode()) + .isEqualTo(200); + assertThat( + send( + "PUT", + resource, + content, + "If-Unmodified-Since", + "Sun, 06 Nov 1993 08:49:37 GMT", + "If-Unmodified-Since", + "Sun, 06 Nov 1993 08:49:37 GMT") + .statusCode()) + .isEqualTo(204); + assertThat( + send("PUT", resource, content, "If-Modified-Since", "Sun, 06 Nov 1994 08:49:37 GMT") + .statusCode()) + .isEqualTo(204); + } + + @Test + void futureStoredDatesAreClampedToResponseDateForHeadersPropertiesAndConditions() + throws Exception { + String calendar = create("future-dates", "VEVENT"), resource = calendar + "event.ics"; + String content = event("future-date", "Restored future timestamp"); + assertThat(send("PUT", resource, content).statusCode()).isEqualTo(201); + setModificationDate( + datedCollection("future-dates"), "event.ics", Instant.parse("2100-01-01T00:00:00Z")); + var get = send("GET", resource, ""); + assertThat(get.headers().firstValue("Last-Modified")) + .isEqualTo(get.headers().firstValue("Date")); + var prop = + send( + "PROPFIND", + resource, + "", + "Depth", + "0"); + var responseDate = + java.time.ZonedDateTime.parse( + prop.headers().firstValue("Date").orElseThrow(), + java.time.format.DateTimeFormatter.RFC_1123_DATE_TIME) + .toInstant(); + var propertyDate = + java.time.ZonedDateTime.parse( + text(prop, "DAV:", "getlastmodified"), + java.time.format.DateTimeFormatter.RFC_1123_DATE_TIME) + .toInstant(); + assertThat(propertyDate).isBeforeOrEqualTo(responseDate); + String effectiveUpperBound = HttpDateValues.format(Instant.now().plusSeconds(60)); + assertThat(send("GET", resource, "", "If-Modified-Since", effectiveUpperBound).statusCode()) + .isEqualTo(304); + assertThat( + send("PUT", resource, content, "If-Unmodified-Since", effectiveUpperBound).statusCode()) + .isEqualTo(204); + } + + @Test + void datelessCollectionsIgnoreDateConditionsAndPreserveRedirectAndAuthorizationPrecedence() + throws Exception { + String calendar = create("dateless-collections", "VEVENT"); + String past = "Sun, 06 Nov 1994 08:49:37 GMT", future = "Fri, 01 Jan 2100 00:00:00 GMT"; + for (String path : List.of(home, calendar)) { + var get = send("GET", path, "", "If-Unmodified-Since", past, "If-Modified-Since", future); + assertThat(get.statusCode()).isEqualTo(200); + assertThat(get.headers().firstValue("Last-Modified")).isEmpty(); + var prop = + send( + "PROPFIND", + path, + "", + "Depth", + "0"); + assertThat(prop.body()).contains("404 Not Found"); + } + assertThat( + send( + "GET", + calendar.substring(0, calendar.length() - 1), + "", + "If-Unmodified-Since", + past) + .statusCode()) + .isEqualTo(308); + assertThat(sendAs("GET", calendar, "", bobAuth, "If-Unmodified-Since", past).statusCode()) + .isEqualTo(403); + } + + @Test + void dateConditionalWriteWaitsForTheMutationLockThenChecksCommittedContent() throws Exception { + String calendar = create("date-concurrent", "VEVENT"), resource = calendar + "event.ics"; + UUID collection = datedCollection("date-concurrent"); + String original = event("date-concurrent", "Original"), + committed = event("date-concurrent", "Committed first"); + assertThat(send("PUT", resource, original).statusCode()).isEqualTo(201); + Instant oldDate = Instant.parse("2020-01-01T12:00:00Z"); + setModificationDate(collection, "event.ics", oldDate); + String before = currentCollectionToken(calendar); + var locked = new java.util.concurrent.CountDownLatch(1); + var finish = new java.util.concurrent.CountDownLatch(1); + var holderPid = new java.util.concurrent.atomic.AtomicInteger(); + try (var workers = java.util.concurrent.Executors.newVirtualThreadPerTaskExecutor()) { + var writer = + workers.submit( + () -> + new org.springframework.transaction.support.TransactionTemplate(transactions) + .executeWithoutResult( + status -> { + org.busystack.caldav.storage.DavMutationLock.shared(jdbc); + holderPid.set( + jdbc.queryForObject("SELECT pg_backend_pid()", Integer.class)); + jdbc.queryForObject( + "SELECT id FROM collections WHERE id=? FOR UPDATE", + UUID.class, + collection); + locked.countDown(); + try { + if (!finish.await(15, java.util.concurrent.TimeUnit.SECONDS)) + throw new AssertionError("Date condition coordination timed out"); + } catch (InterruptedException interrupted) { + Thread.currentThread().interrupt(); + throw new AssertionError(interrupted); + } + propertyStore.putResource( + alice, + collection, + "event.ics", + committed.getBytes(StandardCharsets.UTF_8), + org.busystack.caldav.storage.WriteCondition.none()); + })); + try { + assertThat(locked.await(10, java.util.concurrent.TimeUnit.SECONDS)).isTrue(); + var conditional = + workers.submit( + () -> + send( + "PUT", + resource, + event("date-concurrent", "Must fail"), + "If-Unmodified-Since", + HttpDateValues.format(oldDate))); + org.awaitility.Awaitility.await() + .atMost(java.time.Duration.ofSeconds(10)) + .untilAsserted( + () -> + assertThat( + jdbc.queryForObject( + "SELECT EXISTS(SELECT 1 FROM pg_stat_activity WHERE ?=ANY(pg_blocking_pids(pid)))", + Boolean.class, + holderPid.get())) + .isTrue()); + finish.countDown(); + writer.get(15, java.util.concurrent.TimeUnit.SECONDS); + assertThat(conditional.get(15, java.util.concurrent.TimeUnit.SECONDS).statusCode()) + .isEqualTo(412); + } finally { + finish.countDown(); + } + } + assertThat(send("GET", resource, "").body()).isEqualTo(committed); + var delta = send("REPORT", calendar, sync(before, 100)); + assertThat(delta.statusCode()).isEqualTo(207); + assertThat(xml(delta).getElementsByTagNameNS("DAV:", "getetag").getLength()).isEqualTo(1); + } + + @Test + void multigetAtHomePreservesSuccessfulEntriesBesideMissingAncestors() throws Exception { + String calendar = create("mixed", "VEVENT"); + String resource = calendar + "valid.ics"; + assertThat(send("PUT", resource, event("mixed-member", "Valid")).statusCode()).isEqualTo(201); + String report = + "" + + "/team/dav" + + resource + + "" + + "/team/dav" + + home + + "missing/leaf.ics" + + "/team/dav" + + calendar + + "missing.ics"; + var response = send("REPORT", home, report); + assertThat(response.statusCode()).isEqualTo(207); + assertThat(xml(response).getElementsByTagNameNS("DAV:", "response").getLength()).isEqualTo(3); + assertThat(xml(response).getElementsByTagNameNS("DAV:", "getetag").getLength()).isEqualTo(1); + assertThat(response.body()).contains("HTTP/1.1 404 Not Found"); + } + + @Test + void nestedExpansionPreservesAvailablePropertiesBesideMissingAncestors() throws Exception { + String calendar = create("expanded-mixed", "VEVENT"), resource = calendar + "valid.ics"; + assertThat(send("PUT", resource, event("expanded-member", "Valid")).statusCode()) + .isEqualTo(201); + String patch = + "" + + "/team/dav" + + resource + + "/team/dav" + + home + + "absent/leaf.ics"; + assertThat(send("PROPPATCH", calendar, patch).statusCode()).isEqualTo(207); + var response = + send( + "REPORT", + calendar, + "" + + "" + + "", + "Depth", + "0"); + assertThat(response.statusCode()).isEqualTo(207); + assertThat(xml(response).getElementsByTagNameNS("DAV:", "getetag").getLength()).isEqualTo(1); + assertThat(response.body()).contains("HTTP/1.1 404 Not Found"); + } + + @Test + void selectiveQueriesAndFreeBusyScanBodiesInBatchesBeyondTheFormerAggregateLimit() + throws Exception { + String calendar = create("large-selective", "VEVENT"); + UUID id = datedCollection("large-selective"); + String body = + event("{UID}", "Unrelated") + .replace("END:VEVENT\r\n", "X-BULK:" + "a".repeat(1047000) + "\r\nEND:VEVENT\r\n"); + new org.springframework.transaction.support.TransactionTemplate(transactions) + .executeWithoutResult( + status -> { + jdbc.update( + "INSERT INTO calendar_resources(collection_id,name,uid,content,etag,revision) SELECT ?,n::text||'.ics',n::text,convert_to(replace(?,'{UID}',n::text),'UTF8'),'\"large-fixture\"',n FROM generate_series(1,33) n", + id, + body); + jdbc.update("UPDATE collections SET revision=33 WHERE id=?", id); + }); + assertThat(send("PUT", calendar + "needle.ics", event("needle", "Only match")).statusCode()) + .isEqualTo(201); + var result = + send( + "REPORT", + calendar, + query( + "Only match"), + "Depth", + "1"); + assertThat(result.statusCode()).isEqualTo(207); + assertThat(xml(result).getElementsByTagNameNS("DAV:", "response").getLength()).isEqualTo(1); + assertThat(result.body()).contains("needle.ics").doesNotContain("X-BULK"); + var busy = + send( + "REPORT", + calendar, + "", + "Depth", + "1"); + assertThat(busy.statusCode()).isEqualTo(200); + assertThat(busy.body()).contains("BEGIN:VFREEBUSY"); + var metadata = + send( + "PROPFIND", + calendar, + "", + "Depth", + "1"); + assertThat(metadata.statusCode()).isEqualTo(207); + assertThat(xml(metadata).getElementsByTagNameNS("DAV:", "getetag").getLength()).isEqualTo(35); + assertThat(xml(metadata).getElementsByTagNameNS("DAV:", "response").getLength()).isEqualTo(35); + } + + @Test + void selectiveTimeRangesHandleMoreOriginalResourcesThanTheRecurrenceInstanceLimit() + throws Exception { + String calendar = create("many-originals", "VEVENT"); + UUID id = datedCollection("many-originals"); + String body = event("{UID}", "Cardinality needle"); + new org.springframework.transaction.support.TransactionTemplate(transactions) + .executeWithoutResult( + status -> { + jdbc.update( + "INSERT INTO calendar_resources(collection_id,name,uid,content,etag,revision) SELECT ?,n::text||'.ics',n::text,convert_to(replace(CASE WHEN n=5000 THEN ? ELSE replace(?,'20261027','20000101') END,'{UID}',n::text),'UTF8'),'\"cardinality-fixture\"',n FROM generate_series(1,10037) n", + id, + body, + body); + jdbc.update("UPDATE collections SET revision=10037 WHERE id=?", id); + }); + var result = + send( + "REPORT", + calendar, + query( + "") + .replace("", ""), + "Depth", + "1"); + assertThat(result.statusCode()).describedAs(result.body()).isEqualTo(207); + assertThat(xml(result).getElementsByTagNameNS("DAV:", "response").getLength()).isEqualTo(1); + assertThat(result.body()).contains("5000.ics"); + var busy = + send( + "REPORT", + calendar, + "", + "Depth", + "1"); + assertThat(busy.statusCode()).describedAs(busy.body()).isEqualTo(200); + assertThat(busy.body()).contains("20261027T100000Z/20261027T110000Z"); + } + + @Test + void rawSyncPagesFreezeContentAndPropertiesAcrossWritesAndRetries() throws Exception { + String calendar = create("paged-wire", "VEVENT"); + for (String name : List.of("a", "b", "c")) { + assertThat( + send("PUT", calendar + name + ".ics", event("wire-" + name, "original-" + name)) + .statusCode()) + .isEqualTo(201); + assertThat( + send( + "PROPPATCH", + calendar + name + ".ics", + "original-" + + name + + "") + .statusCode()) + .isEqualTo(207); + } + String report = + sync("", 1) + .replace( + "xmlns:D='DAV:'", + "xmlns:D='DAV:' xmlns:C='urn:ietf:params:xml:ns:caldav' xmlns:X='urn:test'") + .replace("", ""); + var first = send("REPORT", calendar, report); + assertThat(first.statusCode()).isEqualTo(207); + assertThat(first.body()).contains("original-a", "507 Insufficient Storage"); + String cursor = text(first, DavXml.DAV, "sync-token"); + assertThat(send("PUT", calendar + "b.ics", event("wire-b", "changed-b")).statusCode()) + .isEqualTo(204); + assertThat( + send( + "PROPPATCH", + calendar + "b.ics", + "changed-b") + .statusCode()) + .isEqualTo(207); + assertThat(send("DELETE", calendar + "c.ics", "").statusCode()).isEqualTo(204); + assertThat( + send("PUT", calendar + "0.ics", event("wire-zero", "new-before-boundary")).statusCode()) + .isEqualTo(201); + String continuation = + report.replace( + "", "" + cursor + ""); + var second = send("REPORT", calendar, continuation); + var retry = send("REPORT", calendar, continuation); + assertThat(second.statusCode()).isEqualTo(207); + assertThat(text(second, DavXml.CAL, "calendar-data")) + .contains("original-b") + .doesNotContain("changed-b"); + assertThat(text(retry, DavXml.CAL, "calendar-data")) + .isEqualTo(text(second, DavXml.CAL, "calendar-data")); + assertThat(text(second, "urn:test", "note")).isEqualTo("original-b"); + var third = + send( + "REPORT", + calendar, + report.replace( + "", + "" + text(second, DavXml.DAV, "sync-token") + "")); + assertThat(third.statusCode()).isEqualTo(207); + assertThat(third.body()).contains("original-c").doesNotContain("507 Insufficient Storage"); + var delta = send("REPORT", calendar, sync(text(third, DavXml.DAV, "sync-token"), 100)); + assertThat(delta.statusCode()).isEqualTo(207); + assertThat(delta.body()).contains("0.ics", "b.ics", "c.ics", "404 Not Found"); + assertThat(send("GET", calendar + "b.ics", "").body()).contains("changed-b"); + } + + @Test + void partialSyncTokensNeverBypassCurrentSharesOrDisabledAccounts() throws Exception { + String calendar = create("paged-share", "VEVENT"); + for (String name : List.of("a", "b")) + send("PUT", calendar + name + ".ics", event("share-" + name, name)); + UUID id = datedCollection("paged-share"); + propertyStore.replaceAcl( + alice, id, List.of(new org.busystack.caldav.storage.DavStore.Member(bob, "READ"))); + String shared = "/calendars/" + bob + "/" + id + "/"; + var first = sendAs("REPORT", shared, sync("", 1), bobAuth); + assertThat(first.statusCode()).isEqualTo(207); + String token = text(first, DavXml.DAV, "sync-token"); + assertThat(send("REPORT", calendar, sync(token, 1)).statusCode()).isEqualTo(403); + propertyStore.replaceAcl(alice, id, List.of()); + assertThat(sendAs("REPORT", shared, sync(token, 1), bobAuth).statusCode()).isIn(403, 404); + propertyStore.replaceAcl( + alice, id, List.of(new org.busystack.caldav.storage.DavStore.Member(bob, "READ"))); + assertThat(sendAs("REPORT", shared, sync(token, 1), bobAuth).statusCode()).isEqualTo(403); + var fresh = sendAs("REPORT", shared, sync("", 1), bobAuth); + assertThat(fresh.statusCode()).isEqualTo(207); + accounts.disable(bob); + assertThat( + sendAs("REPORT", shared, sync(text(fresh, DavXml.DAV, "sync-token"), 1), bobAuth) + .statusCode()) + .isEqualTo(401); + } + + @Test + void boundaryCollectionsKeepUnicodeResourceUrlsAccessibleThroughShallowDeepCopyAndMove() + throws Exception { + String parent = ""; + for (int i = 0; i < 16; i++) { + parent += (parent.isEmpty() ? "" : "/") + "f" + i; + assertThat(send("MKCOL", home + parent + "/", "").statusCode()).isEqualTo(201); + } + String calendar = create(parent + "/boundary", "VEVENT"); + String name = DavPaths.encode("旅行 %🗓.ics"), content = event("boundary-member", "Kept"); + assertThat(send("PUT", calendar + name, content).statusCode()).isEqualTo(201); + assertThat(send("GET", calendar + name, "").body()).isEqualTo(content); + assertThat( + send( + "COPY", + calendar, + "", + "Destination", + "https://calendar.example/team/dav" + home + "shallow/", + "Depth", + "0") + .statusCode()) + .isEqualTo(201); + assertThat(send("GET", home + "shallow/" + name, "").statusCode()).isEqualTo(404); + assertThat( + send( + "COPY", + calendar, + "", + "Destination", + "https://calendar.example/team/dav" + home + "deep/") + .statusCode()) + .isEqualTo(201); + assertThat(send("GET", home + "deep/" + name, "").body()).isEqualTo(content); + assertThat( + send( + "MOVE", + home + "deep/", + "", + "Destination", + "https://calendar.example/team/dav" + calendar, + "Overwrite", + "T") + .statusCode()) + .isEqualTo(204); + assertThat(send("GET", calendar + name, "").body()).isEqualTo(content); + assertThat(send("MKCOL", home + parent + "/folder/", "").statusCode()).isEqualTo(201); + assertThat(send("MKCALENDAR", home + parent + "/folder/too-deep/", "").statusCode()) + .isEqualTo(400); + assertThat(send("GET", home + parent + "/folder/too-deep/", "").statusCode()).isEqualTo(404); + } + + @Test + void concurrentDavWorkAdmissionRejectsBeforeMutationAndRecoversAfterSlotsRelease() + throws Exception { + String calendar = create("bounded-work", "VEVENT"); + UUID id = datedCollection("bounded-work"); + var locked = new java.util.concurrent.CountDownLatch(1); + var release = new java.util.concurrent.CountDownLatch(1); + try (var workers = java.util.concurrent.Executors.newVirtualThreadPerTaskExecutor()) { + var holder = + workers.submit( + () -> + new org.springframework.transaction.support.TransactionTemplate(transactions) + .executeWithoutResult( + status -> { + jdbc.queryForObject( + "SELECT id FROM collections WHERE id=? FOR UPDATE", UUID.class, id); + locked.countDown(); + try { + if (!release.await(30, java.util.concurrent.TimeUnit.SECONDS)) + throw new AssertionError("Work admission coordination timed out"); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + throw new AssertionError(ex); + } + })); + try { + assertThat(locked.await(10, java.util.concurrent.TimeUnit.SECONDS)).isTrue(); + var requests = new java.util.ArrayList>>(); + for (int i = 0; i < 4; i++) { + int n = i; + requests.add( + workers.submit( + () -> send("PUT", calendar + n + ".ics", event("work-" + n, "Bounded")))); + } + org.awaitility.Awaitility.await() + .atMost(java.time.Duration.ofSeconds(15)) + .untilAsserted( + () -> + assertThat( + jdbc.queryForObject( + "SELECT count(*) FROM pg_stat_activity WHERE datname=current_database() AND wait_event_type='Lock' AND query LIKE '%collections%'", + Integer.class)) + .isGreaterThanOrEqualTo(4)); + var busy = send("REPORT", calendar, sync("", 1)); + assertThat(busy.statusCode()).isEqualTo(503); + assertThat(busy.headers().firstValue("Retry-After")).contains("1"); + assertThat(busy.body()).doesNotContain("sync-token", "multistatus"); + release.countDown(); + holder.get(15, java.util.concurrent.TimeUnit.SECONDS); + for (var pending : requests) + assertThat(pending.get(15, java.util.concurrent.TimeUnit.SECONDS).statusCode()) + .isEqualTo(201); + } finally { + release.countDown(); + } + } + assertThat(send("REPORT", calendar, sync("", 10)).statusCode()).isEqualTo(207); + } + + @Test + void copyAndMoveRejectXmlInvalidDestinationSegmentsBeforeMutation() throws Exception { + String calendar = create("xml-namespace-source", "VEVENT"); + String target = create("xml-namespace-target", "VEVENT"); + String resource = calendar + "safe.ics", content = event("xml-namespace-source", "Keep"); + assertThat(send("PUT", resource, content).statusCode()).isEqualTo(201); + String sourceToken = currentCollectionToken(calendar), + targetToken = currentCollectionToken(target); + for (String forbidden : List.of("%EF%BF%BE", "%EF%BF%BF")) { + String invalidResource = target + "bad" + forbidden + "leaf.ics"; + for (String method : List.of("COPY", "MOVE")) + assertThat( + send( + method, + resource, + "", + "Destination", + "https://calendar.example/team/dav" + invalidResource) + .statusCode()) + .as(method + " XML-invalid resource destination " + forbidden) + .isEqualTo(400); + assertThat( + send( + "MOVE", + calendar, + "", + "Destination", + "https://calendar.example/team/dav" + home + "bad" + forbidden + "calendar/") + .statusCode()) + .isEqualTo(400); + assertThat( + send("PUT", invalidResource, event("xml-invalid-" + forbidden, "Rejected")) + .statusCode()) + .isEqualTo(400); + } + assertThat(send("GET", resource, "").body()).isEqualTo(content); + assertThat(currentCollectionToken(calendar)).isEqualTo(sourceToken); + assertThat(currentCollectionToken(target)).isEqualTo(targetToken); + assertThat( + jdbc.queryForObject( + "SELECT count(*) FROM calendar_resources WHERE collection_id=?", + Integer.class, + datedCollection("xml-namespace-target"))) + .isZero(); + for (String permitted : + List.of("valid\uFDD0.ics", "valid" + new String(Character.toChars(0x1FFFE)) + ".ics")) { + String path = target + DavPaths.encode(permitted); + String data = event("xml-permitted-" + permitted.codePointAt(5), "Preserved"); + assertThat(send("PUT", path, data).statusCode()).isEqualTo(201); + assertThat(send("GET", path, "").body()).isEqualTo(data); + var response = send("PROPFIND", path, "", "Depth", "0"); + assertThat(response.statusCode()).isEqualTo(207); + assertThatCode(() -> xml(response)).doesNotThrowAnyException(); + } + } + + @Test + void copyAndMoveRejectFirewallInaccessibleDestinationSegmentsBeforeMutation() throws Exception { + String calendar = create("namespace-segment", "VEVENT"); + String target = create("namespace-target", "VEVENT"); + String resource = calendar + "safe.ics", content = event("namespace-segment", "Keep"); + assertThat(send("PUT", resource, content).statusCode()).isEqualTo(201); + String before = currentCollectionToken(calendar); + for (String forbidden : List.of("%3B", "%E2%80%A8", "%E2%80%A9")) { + assertThat( + send( + "COPY", + resource, + "", + "Destination", + "https://calendar.example/team/dav" + target + "bad" + forbidden + "leaf.ics") + .statusCode()) + .isEqualTo(400); + assertThat( + send( + "MOVE", + calendar, + "", + "Destination", + "https://calendar.example/team/dav" + home + "bad" + forbidden + "calendar/") + .statusCode()) + .isEqualTo(400); + } + assertThat(send("GET", resource, "").body()).isEqualTo(content); + assertThat(currentCollectionToken(calendar)).isEqualTo(before); + assertThat( + jdbc.queryForObject( + "SELECT count(*) FROM calendar_resources WHERE collection_id=?", + Integer.class, + datedCollection("namespace-segment"))) + .isEqualTo(1); + assertThat( + jdbc.queryForObject( + "SELECT count(*) FROM calendar_resources WHERE collection_id=?", + Integer.class, + datedCollection("namespace-target"))) + .isZero(); + } + + @Test + void syncContinuationParentLockWaitFailsBoundedlyWithoutAdvancingToken() throws Exception { + String calendar = create("sync-cursor-lock", "VEVENT"); + UUID collection = datedCollection("sync-cursor-lock"); + assertThat(send("PUT", calendar + "a.ics", event("sync-lock-a", "A")).statusCode()) + .isEqualTo(201); + assertThat(send("PUT", calendar + "b.ics", event("sync-lock-b", "B")).statusCode()) + .isEqualTo(201); + long cursorCount = + jdbc.queryForObject( + "SELECT count(*) FROM sync_continuations WHERE collection_id=?", + Long.class, + collection); + String stableBefore = currentCollectionToken(calendar); + var locked = new java.util.concurrent.CountDownLatch(1); + var release = new java.util.concurrent.CountDownLatch(1); + try (var workers = java.util.concurrent.Executors.newVirtualThreadPerTaskExecutor()) { + var writer = + workers.submit( + () -> + new org.springframework.transaction.support.TransactionTemplate(transactions) + .executeWithoutResult( + status -> { + jdbc.queryForObject( + "SELECT id FROM collections WHERE id=? FOR UPDATE", + UUID.class, + collection); + locked.countDown(); + try { + if (!release.await(30, java.util.concurrent.TimeUnit.SECONDS)) + throw new AssertionError("Cursor parent lock release timed out"); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + throw new AssertionError(ex); + } + })); + try { + assertThat(locked.await(10, java.util.concurrent.TimeUnit.SECONDS)).isTrue(); + var report = workers.submit(() -> send("REPORT", calendar, sync("", 1))); + // The continuation transaction has its own deadline; a proxy/client timeout must not + // leave the servlet and a DAV-work slot waiting indefinitely on the database writer. + var response = report.get(15, java.util.concurrent.TimeUnit.SECONDS); + assertThat(response.statusCode()).isEqualTo(503); + assertThat(response.headers().firstValue("Retry-After")).contains("1"); + assertThat(response.body()).doesNotContain("sync-token"); + assertThat( + jdbc.queryForObject( + "SELECT count(*) FROM sync_continuations WHERE collection_id=?", + Long.class, + collection)) + .isEqualTo(cursorCount); + } finally { + release.countDown(); + writer.get(10, java.util.concurrent.TimeUnit.SECONDS); + } + } + assertThat(currentCollectionToken(calendar)).isEqualTo(stableBefore); + assertThat(send("REPORT", calendar, sync("", 1)).statusCode()).isEqualTo(207); + assertThat(send("GET", calendar + "a.ics", "").statusCode()).isEqualTo(200); + } + + @Test + void syncEmptyPropertySelectionReturnsAnEmptySuccessfulPropstat() throws Exception { + String calendar = create("sync-empty-properties", "VEVENT"); + assertThat(send("PUT", calendar + "one.ics", event("empty-prop-sync", "One")).statusCode()) + .isEqualTo(201); + var report = + send("REPORT", calendar, sync("", 1).replace("", "")); + assertThat(report.statusCode()).isEqualTo(207); + var result = xml(report); + assertThat(result.getElementsByTagNameNS("DAV:", "response").getLength()).isEqualTo(1); + var response = (Element) result.getElementsByTagNameNS("DAV:", "response").item(0); + assertThat(response.getElementsByTagNameNS("DAV:", "propstat").getLength()).isEqualTo(1); + var propstat = (Element) response.getElementsByTagNameNS("DAV:", "propstat").item(0); + var properties = DavXml.required(propstat, "DAV:", "prop"); + assertThat(DavXml.children(properties)).isEmpty(); + assertThat(DavXml.required(propstat, "DAV:", "status").getTextContent()) + .isEqualTo("HTTP/1.1 200 OK"); + assertThat(result.getElementsByTagNameNS("DAV:", "sync-token").getLength()).isEqualTo(1); + } + + private int propertyStatus(HttpResponse response, String namespace, String local) { + for (Element item : DavXml.children(xml(response))) + if (DavXml.is(item, DavXml.DAV, "response")) + for (Element propstat : DavXml.children(item, DavXml.DAV, "propstat")) + if (DavXml.child(DavXml.required(propstat, DavXml.DAV, "prop"), namespace, local) != null) + return Integer.parseInt( + DavXml.required(propstat, DavXml.DAV, "status").getTextContent().split(" ")[1]); + throw new AssertionError("Property status missing: " + namespace + local); + } + + private String propRequest(String body) { + return "" + + body + + ""; + } + + private String aclGrant(UUID principal, String grants) { + return "https://calendar.example/team/dav/principals/" + + principal + + "/" + + grants + + ""; + } + + private String aclBody(String aces) { + return "" + + aces + + ""; + } + + @Test + void commandExtensionsAreIgnoredAsWholeSubtreesWithoutLosingDeadXml() throws Exception { + String calendar = create("xml-extensions", "VEVENT"); + send("PUT", calendar + "one.ics", event("xml-extensions", "One")); + String set = + "beforeafter"; + String normal = + "" + set + ""; + String extension = + normal.replace( + "", + "bad"); + assertThat(send("PROPPATCH", calendar, normal).statusCode()).isEqualTo(207); + assertThat(send("PROPPATCH", calendar, extension).statusCode()).isEqualTo(207); + var values = + send( + "PROPFIND", + calendar, + propRequest(""), + "Depth", + "0"); + assertThat(propertyStatus(values, "urn:audit", "kept")).isEqualTo(200); + assertThat(propertyStatus(values, "urn:audit", "smuggled")).isEqualTo(404); + assertThat(values.body()).contains("before", "after", "xml:lang=\"ja\"", "a=\"v\""); + String base = query(""); + String added = + base.replace( + "", + ""); + assertThat(principalReportHrefs(send("REPORT", calendar, added, "Depth", "1"))) + .isEqualTo(principalReportHrefs(send("REPORT", calendar, base, "Depth", "1"))); + String selector = + ""; + String projection = + base.replace("", "" + selector + ""); + String extraSelector = + selector.replace( + "", + ""); + assertThat( + text( + send("REPORT", calendar, projection.replace(selector, extraSelector), "Depth", "1"), + DavXml.CAL, + "calendar-data")) + .isEqualTo( + text(send("REPORT", calendar, projection, "Depth", "1"), DavXml.CAL, "calendar-data")); + String read = aclGrant(bob, ""); + String ignoredWrite = + "" + + aclGrant( + bob, "") + + ""; + assertThat(send("ACL", calendar, aclBody(read + ignoredWrite)).statusCode()).isEqualTo(200); + UUID id = + jdbc.queryForObject( + "SELECT id FROM collections WHERE owner_id=? AND name='xml-extensions'", + UUID.class, + alice); + assertThat( + sendAs("PUT", "/calendars/" + bob + "/" + id + "/bad.ics", event("bad", "Bad"), bobAuth) + .statusCode()) + .isEqualTo(403); + String search = + ""; + assertThat(send("REPORT", "/principals/", search, "Depth", "0").statusCode()).isEqualTo(200); + assertThat(send("REPORT", "/principals/", search, "Depth", "0", "Depth", "1").statusCode()) + .isEqualTo(400); + + String expanded = + ""; + var expansion = send("REPORT", calendar, expanded, "Depth", "0"); + assertThat(expansion.statusCode()).isEqualTo(207); + assertThat(expansion.body()) + .contains("Alice & Calendar") + .doesNotContain("calendar-data", ""); + var duplicate = send("PROPPATCH", calendar, normal.replace("", "")); + assertThat(duplicate.statusCode()).isEqualTo(400); + } + + @Test + void abstractAclGrantsFailAtomicallyAndEffectivePrivilegesAreNonabstract() throws Exception { + String calendar = create("model", "VEVENT"); + send("PUT", calendar + "one.ics", event("model", "One")); + String read = aclGrant(bob, ""); + assertThat(send("ACL", calendar, aclBody(read)).statusCode()).isEqualTo(200); + String before = + text( + send("PROPFIND", calendar, propRequest(""), "Depth", "0"), + DavXml.DAV, + "sync-token"); + for (String abstractName : + List.of( + "read-current-user-privilege-set", + "write-properties", + "bind", + "unbind", + "C:read-free-busy")) { + String element = abstractName.startsWith("C:") ? abstractName : "D:" + abstractName; + var failure = + send( + "ACL", + calendar, + aclBody( + aclGrant( + bob, + "<" + + element + + "/>"))); + assertThat(failure.statusCode()).isEqualTo(403); + assertThat(xml(failure).getElementsByTagNameNS(DavXml.DAV, "no-abstract").getLength()) + .isEqualTo(1); + } + assertThat( + text( + send("PROPFIND", calendar, propRequest(""), "Depth", "0"), + DavXml.DAV, + "sync-token")) + .isEqualTo(before); + assertThat(send("ACL", calendar, aclBody(read)).statusCode()).isEqualTo(200); + assertThat( + text( + send("PROPFIND", calendar, propRequest(""), "Depth", "0"), + DavXml.DAV, + "sync-token")) + .isEqualTo(before); + assertThat(send("MKCOL", home + "ordinary-model/", "").statusCode()).isEqualTo(201); + for (String path : + List.of( + "/", + "/principals/", + "/principals/" + alice + "/", + home, + home + "ordinary-model/", + calendar, + calendar + "one.ics")) { + var response = + send( + "PROPFIND", + path, + propRequest(""), + "Depth", + "0"); + assertNonabstractEffectivePrivileges(xml(response), true); + } + UUID id = + jdbc.queryForObject( + "SELECT id FROM collections WHERE owner_id=? AND name='model'", UUID.class, alice); + String shared = "/calendars/" + bob + "/" + id + "/"; + assertNonabstractEffectivePrivileges( + xml( + sendAs( + "PROPFIND", + shared, + propRequest(""), + bobAuth, + "Depth", + "0")), + false); + assertThat( + send( + "ACL", + calendar, + aclBody( + aclGrant( + bob, + ""))) + .statusCode()) + .isEqualTo(200); + var writer = + sendAs( + "PROPFIND", + shared + "one.ics", + propRequest(""), + bobAuth, + "Depth", + "0"); + assertNonabstractEffectivePrivileges(xml(writer), false); + assertThat(xml(writer).getElementsByTagNameNS(DavXml.DAV, "write").getLength()) + .isGreaterThan(1); + UUID group = + groupDirectory.create(groupAdministrator(), "ACL model group", java.util.Set.of("bob")); + assertThat( + send("ACL", calendar, aclBody(aclGrant(group, ""))) + .statusCode()) + .isEqualTo(200); + assertNonabstractEffectivePrivileges( + xml( + sendAs( + "PROPFIND", + shared, + propRequest(""), + bobAuth, + "Depth", + "0")), + false); + assertThat( + send( + "ACL", + calendar, + aclBody( + aclGrant( + group, + ""))) + .statusCode()) + .isEqualTo(200); + assertNonabstractEffectivePrivileges( + xml( + sendAs( + "PROPFIND", + shared + "one.ics", + propRequest(""), + bobAuth, + "Depth", + "0")), + false); + } + + private void assertNonabstractEffectivePrivileges(Element root, boolean owner) { + var supported = root.getElementsByTagNameNS(DavXml.DAV, "supported-privilege"); + java.util.Set nonabstract = new java.util.HashSet<>(); + for (int i = 0; i < supported.getLength(); i++) { + Element privilege = (Element) supported.item(i); + if (DavXml.child(privilege, DavXml.DAV, "abstract") == null) + nonabstract.add( + DavXml.name( + DavXml.children(DavXml.required(privilege, DavXml.DAV, "privilege")).getFirst())); + } + Element current = + (Element) root.getElementsByTagNameNS(DavXml.DAV, "current-user-privilege-set").item(0); + java.util.Set actual = new java.util.HashSet<>(); + for (Element privilege : DavXml.children(current, DavXml.DAV, "privilege")) + actual.add(DavXml.name(DavXml.children(privilege).getFirst())); + assertThat(nonabstract).containsAll(actual); + assertThat(actual.contains(DavXml.d("all"))).isEqualTo(owner); + } + + @Test + void propertyInventoriesAndQueryCollationsRespectVisibilityAndReportContext() throws Exception { + String calendar = create("property-inventory", "VEVENT"); + send("PUT", calendar + "one.ics", event("property-inventory", "One")); + assertThat(send("MKCOL", home + "property-folder/", "").statusCode()).isEqualTo(201); + for (String path : List.of(home, home + "property-folder/", calendar, calendar + "one.ics")) { + var response = + send( + "PROPFIND", + path, + propRequest(""), + "Depth", + "0"); + assertThat(propertyStatus(response, DavXml.DAV, "group")).isEqualTo(200); + assertThat(propertyStatus(response, DavXml.CAL, "supported-collation-set")).isEqualTo(200); + assertThat(response.body()).contains("calendar-query", "i;ascii-casemap", "i;octet"); + } + var names = + send( + "PROPFIND", + calendar, + "", + "Depth", + "0"); + assertThat(xml(names).getElementsByTagNameNS(DavXml.DAV, "acl").getLength()).isEqualTo(1); + assertThat(xml(names).getElementsByTagNameNS(DavXml.DAV, "group").getLength()).isEqualTo(1); + assertThat(xml(names).getElementsByTagNameNS(DavXml.CAL, "calendar-data").getLength()).isZero(); + var all = + send( + "PROPFIND", + calendar, + "", + "Depth", + "0"); + assertThat(xml(all).getElementsByTagNameNS(DavXml.DAV, "acl").getLength()).isZero(); + assertThat(xml(all).getElementsByTagNameNS(DavXml.DAV, "sync-token").getLength()).isZero(); + var data = + send("PROPFIND", calendar + "one.ics", propRequest(""), "Depth", "0"); + assertThat(propertyStatus(data, DavXml.CAL, "calendar-data")).isEqualTo(404); + assertThat(data.body()).doesNotContain("BEGIN:VCALENDAR"); + for (String property : List.of("", "")) { + var protectedWrite = + send( + "PROPPATCH", + calendar, + "" + + property + + ""); + assertThat(protectedWrite.body()).contains("cannot-modify-protected-property"); + } + assertThat( + send("ACL", calendar, aclBody(aclGrant(bob, ""))) + .statusCode()) + .isEqualTo(200); + UUID id = + jdbc.queryForObject( + "SELECT id FROM collections WHERE owner_id=? AND name='property-inventory'", + UUID.class, + alice); + String shared = "/calendars/" + bob + "/" + id + "/"; + var readerNames = + sendAs( + "PROPFIND", + shared, + "", + bobAuth, + "Depth", + "0"); + assertThat(xml(readerNames).getElementsByTagNameNS(DavXml.DAV, "acl").getLength()).isZero(); + var readerAcl = sendAs("PROPFIND", shared, propRequest(""), bobAuth, "Depth", "0"); + assertThat(propertyStatus(readerAcl, DavXml.DAV, "acl")).isEqualTo(403); + assertThat(readerAcl.body()) + .contains("need-privileges", "read-acl") + .doesNotContain(""); + assertThat( + send("REPORT", calendar, query(""), "Depth", "1").body()) + .contains("BEGIN:VCALENDAR"); + assertThat(send("REPORT", calendar, sync("", 10), "Depth", "0").statusCode()).isEqualTo(207); + } + + @Test + void resourceOptionsChecksCurrentReadAccessAndReportsSupportedTargetMethods() throws Exception { + String calendar = create("options-access", "VEVENT"); + send("PUT", calendar + "one.ics", event("options-access", "One")); + assertThat( + send("OPTIONS", calendar + "one.ics", "").headers().firstValue("Allow").orElseThrow()) + .contains("PUT") + .doesNotContain("MKCALENDAR", "ACL"); + assertThat(sendAs("OPTIONS", calendar + "one.ics", "", bobAuth).statusCode()).isEqualTo(403); + assertThat(send("OPTIONS", calendar + "missing.ics", "").statusCode()).isEqualTo(404); + assertThat( + send("ACL", calendar, aclBody(aclGrant(bob, ""))) + .statusCode()) + .isEqualTo(200); + UUID id = + jdbc.queryForObject( + "SELECT id FROM collections WHERE owner_id=? AND name='options-access'", + UUID.class, + alice); + String shared = "/calendars/" + bob + "/" + id + "/one.ics"; + assertThat(sendAs("OPTIONS", shared, "", bobAuth).statusCode()).isEqualTo(200); + assertThat( + send( + "ACL", + calendar, + aclBody( + aclGrant( + bob, + ""))) + .statusCode()) + .isEqualTo(200); + assertThat(sendAs("OPTIONS", shared, "", bobAuth).statusCode()).isEqualTo(200); + assertThat(send("ACL", calendar, aclBody("")).statusCode()).isEqualTo(200); + assertThat(sendAs("OPTIONS", shared, "", bobAuth).statusCode()).isIn(403, 404); + } + + @Test + void queryTimezonesAreValidatedBeforeEnumeratingEmptyCalendar() throws Exception { + String calendar = create("empty-query-timezone", "VEVENT"); + String archived; + try (var fixture = getClass().getResourceAsStream("/calendar/audit/future-zone.ics")) { + assertThat(fixture).isNotNull(); + archived = new String(fixture.readAllBytes(), StandardCharsets.UTF_8); + } + String valid = + archived.substring(0, archived.indexOf("BEGIN:VEVENT\r\n")) + "END:VCALENDAR\r\n"; + String template = + query("") + .replace( + "", + ""); + String selected = ""; + var before = send("PROPFIND", calendar, propRequest(selected), "Depth", "0"); + String token = text(before, DavXml.DAV, "sync-token"); + var control = send("REPORT", calendar, template.replace("__TIMEZONE__", valid), "Depth", "1"); + assertThat(control.statusCode()).isEqualTo(207); + assertThat(principalReportHrefs(control)).isEmpty(); + for (String invalid : + List.of( + "not a calendar", + valid.replace("UNTIL=20290531T230000Z", "UNTIL=20290531T230000"), + valid.replace("UNTIL=20290531T230000Z", "UNTIL=20290531"))) { + var failure = + send("REPORT", calendar, template.replace("__TIMEZONE__", invalid), "Depth", "1"); + assertThat(failure.statusCode()).isEqualTo(403); + assertThat(xml(failure).getElementsByTagNameNS(DavXml.CAL, "valid-calendar-data").getLength()) + .isEqualTo(1); + assertThat(xml(failure).getElementsByTagNameNS(DavXml.CAL, "valid-filter").getLength()) + .isZero(); + var after = send("PROPFIND", calendar, propRequest(selected), "Depth", "0"); + assertThat(after.body()).isEqualTo(before.body()); + assertThat(text(after, DavXml.DAV, "sync-token")).isEqualTo(token); + } + String repeated = + template + .replace("__TIMEZONE__", valid) + .replace( + "", + ""); + assertThat(send("REPORT", calendar, repeated, "Depth", "1").statusCode()).isEqualTo(400); + String extension = + template + .replace("__TIMEZONE__", valid) + .replace( + "", + "not a calendar"); + var extended = send("REPORT", calendar, extension, "Depth", "1"); + assertThat(extended.statusCode()).isEqualTo(207); + assertThat(extended.body()).isEqualTo(control.body()); + String bounded = + valid.replace( + "BEGIN:VTIMEZONE\r\n", "X-WORK-LIMIT:" + "a".repeat(65536) + "\r\nBEGIN:VTIMEZONE\r\n"); + var limited = send("REPORT", calendar, template.replace("__TIMEZONE__", bounded), "Depth", "1"); + assertThat(limited.statusCode()).isEqualTo(507); + assertThat( + xml(limited) + .getElementsByTagNameNS(DavXml.DAV, "number-of-matches-within-limits") + .getLength()) + .isEqualTo(1); + assertThat(send("PROPFIND", calendar, propRequest(selected), "Depth", "0").body()) + .isEqualTo(before.body()); + assertThat( + jdbc.queryForObject( + "SELECT count(*) FROM calendar_resources r JOIN collections c ON c.id=r.collection_id WHERE c.owner_id=?", + Integer.class, + alice)) + .isZero(); + assertThat( + jdbc.queryForObject( + "SELECT count(*) FROM collections WHERE owner_id=?", Integer.class, alice)) + .isEqualTo(1); + } + + @Test + void timezonePropertyFailuresKeepCalDavConditionAndAtomicSiblingState() throws Exception { + String calendar = create("timezone-property-errors", "VEVENT"); + String valid = + "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Timezone property//EN\r\nBEGIN:VTIMEZONE\r\nTZID:Fixed\r\nBEGIN:STANDARD\r\nDTSTART:19700101T000000\r\nTZOFFSETFROM:+0100\r\nTZOFFSETTO:+0100\r\nEND:STANDARD\r\nEND:VTIMEZONE\r\nEND:VCALENDAR\r\n"; + String start = + "changed"; + assertThat(send("PROPPATCH", calendar, start + valid + end).statusCode()).isEqualTo(207); + String archived; + try (var fixture = getClass().getResourceAsStream("/calendar/audit/future-zone.ics")) { + assertThat(fixture).isNotNull(); + archived = new String(fixture.readAllBytes(), StandardCharsets.UTF_8); + } + String utcUntil = + archived.substring(0, archived.indexOf("BEGIN:VEVENT\r\n")) + "END:VCALENDAR\r\n"; + var validRecurrence = send("PROPPATCH", calendar, start + utcUntil + end); + assertThat(propertyStatus(validRecurrence, DavXml.CAL, "calendar-timezone")).isEqualTo(200); + assertThat(propertyStatus(validRecurrence, DavXml.DAV, "displayname")).isEqualTo(200); + String selected = ""; + var before = send("PROPFIND", calendar, propRequest(selected), "Depth", "0"); + String syncToken = text(before, DavXml.DAV, "sync-token"); + int index = 0; + for (String invalid : + List.of( + "not a calendar", + event("not-timezone", "Event"), + valid.replace( + "END:VCALENDAR", + valid.substring(valid.indexOf("BEGIN:VTIMEZONE"), valid.indexOf("END:VCALENDAR")) + + "END:VCALENDAR"), + utcUntil.replace("UNTIL=20290531T230000Z", "UNTIL=20290531T230000"), + utcUntil.replace("UNTIL=20290531T230000Z", "UNTIL=20290531"))) { + var failure = + send("PROPPATCH", calendar, start + invalid + end.replace("changed", "not-applied")); + assertThat(propertyStatus(failure, DavXml.CAL, "calendar-timezone")).isEqualTo(409); + assertThat(propertyStatus(failure, DavXml.DAV, "displayname")).isEqualTo(424); + assertThat(xml(failure).getElementsByTagNameNS(DavXml.CAL, "valid-calendar-data").getLength()) + .isEqualTo(1); + var after = send("PROPFIND", calendar, propRequest(selected), "Depth", "0"); + assertThat(after.body()).isEqualTo(before.body()); + assertThat(text(after, DavXml.DAV, "sync-token")).isEqualTo(syncToken); + String rejected = home + "timezone-initialization-" + index++ + "/"; + int collectionCount = + jdbc.queryForObject( + "SELECT count(*) FROM collections WHERE owner_id=?", Integer.class, alice); + String initialization = + "not-created"; + var rejectedCreation = send("MKCALENDAR", rejected, initialization); + assertThat(rejectedCreation.statusCode()).isEqualTo(409); + assertThat( + xml(rejectedCreation) + .getElementsByTagNameNS(DavXml.CAL, "valid-calendar-data") + .getLength()) + .isEqualTo(1); + assertThat(send("GET", rejected, "").statusCode()).isEqualTo(404); + assertThat( + jdbc.queryForObject( + "SELECT count(*) FROM collections WHERE owner_id=?", Integer.class, alice)) + .isEqualTo(collectionCount); + assertThat(send("PROPFIND", calendar, propRequest(selected), "Depth", "0").body()) + .isEqualTo(before.body()); + } + String initialized = home + "timezone-initialization-valid/"; + assertThat( + send( + "MKCALENDAR", + initialized, + "") + .statusCode()) + .isEqualTo(201); + assertThat( + propertyStatus( + send("PROPFIND", initialized, propRequest(""), "Depth", "0"), + DavXml.CAL, + "calendar-timezone")) + .isEqualTo(200); + } + + @Test + void nonemptyMkcalendarRequiresRecognizedSetAndPropWithoutCreatingState() throws Exception { + int index = 0; + for (String content : + List.of( + "", + "", + "", + "", + "")) { + String path = home + "invalid-mkcalendar-" + index++ + "/"; + int before = + jdbc.queryForObject( + "SELECT count(*) FROM collections WHERE owner_id=?", Integer.class, alice); + var result = + send( + "MKCALENDAR", + path, + "" + + content + + ""); + assertThat(result.statusCode()).isEqualTo(400); + assertThat(send("GET", path, "").statusCode()).isEqualTo(404); + assertThat( + jdbc.queryForObject( + "SELECT count(*) FROM collections WHERE owner_id=?", Integer.class, alice)) + .isEqualTo(before); + } + assertThat(send("MKCALENDAR", home + "empty-http-body/", "").statusCode()).isEqualTo(201); + assertThat( + send( + "MKCALENDAR", + home + "valid-empty-prop/", + "") + .statusCode()) + .isEqualTo(201); + } + + @Test + void operationSpecificFailureStatusHeadersAndConditionsArePreserved() throws Exception { + String calendar = create("failure-contracts", "VEVENT"); + for (String target : List.of("/", home, "/principals/" + alice)) { + var result = send("DELETE", target, ""); + assertThat(result.statusCode()).isEqualTo(405); + assertThat(result.headers().firstValue("Allow").orElseThrow()) + .contains("GET", "PROPFIND") + .doesNotContain("DELETE"); + assertThat(xml(result).getElementsByTagNameNS(DavXml.DAV, "need-privileges").getLength()) + .isZero(); + } + for (String method : List.of("MKCOL", "MKCALENDAR")) { + var result = send(method, calendar, ""); + assertThat(result.statusCode()).isEqualTo(405); + assertThat(result.headers().firstValue("Allow").orElseThrow()) + .contains("GET", "PROPFIND", "ACL") + .doesNotContain("MKCALENDAR"); + if (method.equals("MKCALENDAR")) + assertThat( + xml(result).getElementsByTagNameNS(DavXml.DAV, "resource-must-be-null").getLength()) + .isEqualTo(1); + } + var forbidden = + send("PROPFIND", "/calendars/" + bob + "/", propRequest(""), "Depth", "0"); + assertThat(forbidden.statusCode()).isEqualTo(403); + Element privileges = + (Element) xml(forbidden).getElementsByTagNameNS(DavXml.DAV, "need-privileges").item(0); + assertThat(DavXml.child(privileges, DavXml.DAV, "resource")).isNotNull(); + String inaccessibleHome = "/team/dav/calendars/" + bob + "/"; + assertThat( + send( + "PROPPATCH", + calendar, + "" + + inaccessibleHome + + "") + .statusCode()) + .isEqualTo(207); + var expanded = + send( + "REPORT", + calendar, + "", + "Depth", + "0"); + assertThat(expanded.statusCode()).isEqualTo(207); + Element missingPrivileges = + (Element) xml(expanded).getElementsByTagNameNS(DavXml.DAV, "need-privileges").item(0); + Element missingResource = DavXml.required(missingPrivileges, DavXml.DAV, "resource"); + assertThat(DavXml.required(missingResource, DavXml.DAV, "href").getTextContent()) + .endsWith(inaccessibleHome); + assertThat( + DavXml.child( + DavXml.required(missingResource, DavXml.DAV, "privilege"), DavXml.DAV, "read")) + .isNotNull(); + String read = aclGrant(bob, ""); + var count = send("ACL", calendar, aclBody(read.repeat(1001))); + assertThat(count.statusCode()).isEqualTo(403); + assertThat(xml(count).getElementsByTagNameNS(DavXml.DAV, "limited-number-of-aces").getLength()) + .isEqualTo(1); + var duplicate = send("ACL", calendar, aclBody(read.repeat(2))); + assertThat(duplicate.statusCode()).isEqualTo(403); + assertThat(xml(duplicate).getElementsByTagNameNS(DavXml.DAV, "no-ace-conflict").getLength()) + .isEqualTo(1); + assertThat(jdbc.queryForObject("SELECT count(*) FROM collection_members", Integer.class)) + .isZero(); + } + + @Test + void freeBusyGeneratesOneCompleteValidComponentForEmptyAndOccupiedCalendars() throws Exception { + String calendar = create("generated-freebusy", "VEVENT"); + String request = + ""; + for (boolean populated : List.of(false, true)) { + if (populated) send("PUT", calendar + "one.ics", event("generated-freebusy", "One")); + var response = send("REPORT", calendar, request, "Depth", "1"); + assertThat(response.statusCode()).isEqualTo(200); + var parsed = + new net.fortuna.ical4j.data.CalendarBuilder() + .build(new java.io.StringReader(response.body())); + assertThat(parsed.validate().hasErrors()).isFalse(); + assertThat(parsed.getComponents()).hasSize(1); + var busy = parsed.getComponents().getFirst(); + assertThat(busy.getName()).isEqualTo("VFREEBUSY"); + assertThat(busy.getProperties("DTSTAMP")).hasSize(1); + assertThat(busy.getProperties("UID")).hasSize(1); + assertThat(busy.getProperties("DTSTART")).hasSize(1); + assertThat(busy.getProperties("DTEND")).hasSize(1); + assertThat(busy.getProperty("DTSTAMP").orElseThrow().getValue()).endsWith("Z"); + assertThat(busy.getProperties("FREEBUSY").isEmpty()).isEqualTo(!populated); + if (populated) + assertThat(response.body()) + .contains("FREEBUSY;FBTYPE=BUSY:20261027T100000Z/20261027T110000Z"); + } + } + + @Test + void freeBusyUsesCollectionTimezoneAndIgnoresOutOfContextRequestTimezone() throws Exception { + String calendar = create("freebusy-floating-zone", "VEVENT"); + String zone = + "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Floating timezone test//EN\r\nBEGIN:VTIMEZONE\r\nTZID:Fixed\r\nBEGIN:STANDARD\r\nDTSTART:19700101T000000\r\nTZOFFSETFROM:+0100\r\nTZOFFSETTO:+0100\r\nEND:STANDARD\r\nEND:VTIMEZONE\r\nEND:VCALENDAR\r\n"; + String patch = + ""; + assertThat(send("PROPPATCH", calendar, patch).statusCode()).isEqualTo(207); + String floating = + event("freebusy-floating-zone", "Floating") + .replace("20261027T100000Z", "20261027T100000") + .replace("20261027T110000Z", "20261027T110000"); + send("PUT", calendar + "floating.ics", floating); + String report = + ""; + String extra = + report.replace( + "", + ""); + assertThat(send("REPORT", calendar, report, "Depth", "1").body()) + .contains("20261027T090000Z/20261027T100000Z"); + assertThat(send("REPORT", calendar, extra, "Depth", "1").body()) + .contains("20261027T090000Z/20261027T100000Z") + .doesNotContain("20261027T070000Z"); + } + + @Test + void namespaceAliasesRetainResourceIdentityAndCurrentSharedAuthorization() throws Exception { + String calendar = create("alias+calendar", "VEVENT"); + String first = calendar + "a+b-%E9%9B%AA.ics"; + String alias = calendar.replace("+", "%2B") + "a%2Bb-%e9%9b%aa.ics"; + send("PUT", first, event("alias-identity", "Identity")); + var original = send("GET", first, ""); + var alternate = send("GET", alias, ""); + assertThat(alternate.statusCode()).isEqualTo(200); + assertThat(alternate.body()).isEqualTo(original.body()); + assertThat(alternate.headers().firstValue("ETag")) + .isEqualTo(original.headers().firstValue("ETag")); + assertThat( + send("ACL", calendar, aclBody(aclGrant(bob, ""))) + .statusCode()) + .isEqualTo(200); + UUID id = + jdbc.queryForObject( + "SELECT id FROM collections WHERE owner_id=? AND name='alias+calendar'", + UUID.class, + alice); + String shared = "/calendars/" + bob + "/" + id + "/a%2Bb-%E9%9B%AA.ics"; + assertThat(sendAs("GET", shared, "", bobAuth).body()).isEqualTo(original.body()); + var owner = send("PROPFIND", first, propRequest(""), "Depth", "0"); + var sharedOwner = sendAs("PROPFIND", shared, propRequest(""), bobAuth, "Depth", "0"); + assertThat(text(sharedOwner, DavXml.DAV, "owner")).isEqualTo(text(owner, DavXml.DAV, "owner")); + assertThat(sendAs("PUT", shared, event("alias-identity", "Forbidden"), bobAuth).statusCode()) + .isEqualTo(403); + assertThat(send("ACL", calendar, aclBody("")).statusCode()).isEqualTo(200); + assertThat(sendAs("GET", shared, "", bobAuth).statusCode()).isIn(403, 404); + assertThat(sendAs("OPTIONS", shared, "", bobAuth).statusCode()).isIn(403, 404); + } + + @Test + void legacyInvalidResourcesRemainRetrievableButCannotBypassValidationThroughCollectionCopy() + throws Exception { + String calendar = create("legacy-copy", "VEVENT"); + String invalid = event("legacy-copy", "Legacy").replace("DTSTART:20261027T100000Z\r\n", ""); + UUID id = + jdbc.queryForObject( + "SELECT id FROM collections WHERE owner_id=? AND name='legacy-copy'", + UUID.class, + alice); + jdbc.update( + "INSERT INTO calendar_resources(collection_id,name,uid,content,etag,revision) VALUES(?,?,?,?,?,1)", + id, + "legacy.ics", + "legacy-copy", + invalid.getBytes(StandardCharsets.UTF_8), + "\"legacy\""); + jdbc.update("UPDATE collections SET revision=1 WHERE id=?", id); + assertThat(send("GET", calendar + "legacy.ics", "").body()).isEqualTo(invalid); + int before = + jdbc.queryForObject( + "SELECT count(*) FROM collections WHERE owner_id=?", Integer.class, alice); + for (String method : List.of("COPY", "MOVE")) { + String target = home + "legacy-destination-" + method + "/"; + var result = + send(method, calendar, "", "Destination", "https://calendar.example/team/dav" + target); + assertThat(result.statusCode()).isEqualTo(403); + assertThat(result.body()).contains("valid-calendar-data"); + assertThat(send("GET", target, "").statusCode()).isEqualTo(404); + assertThat( + jdbc.queryForObject( + "SELECT count(*) FROM collections WHERE owner_id=?", Integer.class, alice)) + .isEqualTo(before); + assertThat(send("GET", calendar + "legacy.ics", "").body()).isEqualTo(invalid); + } + assertThat(send("DELETE", calendar + "legacy.ics", "").statusCode()).isEqualTo(204); + } + + @Test + void originalFutureTimezoneFixtureProducesValidFreeBusyAndPreservesSourceBytes() + throws Exception { + String calendar = create("future-freebusy", "VEVENT"); + String body; + try (var input = getClass().getResourceAsStream("/calendar/audit/future-zone.ics")) { + assertThat(input).isNotNull(); + body = new String(input.readAllBytes(), StandardCharsets.UTF_8); + } + assertThat(send("PUT", calendar + "future.ics", body).statusCode()).isEqualTo(201); + String request = + ""; + var response = send("REPORT", calendar, request, "Depth", "1"); + assertThat(response.statusCode()).isEqualTo(200); + var parsed = + new net.fortuna.ical4j.data.CalendarBuilder() + .build(new java.io.StringReader(response.body())); + assertThat(parsed.validate().hasErrors()).isFalse(); + assertThat(parsed.getComponents()).hasSize(1); + assertThat(parsed.getComponents().getFirst().getProperties("DTSTAMP")).hasSize(1); + assertThat(response.body()).contains("20400401T100000Z/20400401T110000Z"); + assertThat(send("GET", calendar + "future.ics", "").body()).isEqualTo(body); + } + + @Test + void concurrentPropertyPatchesRetainBothValuesAndCompleteSyncState() throws Exception { + String calendar = create("property-interleaving", "VEVENT"), path = calendar + "one.ics"; + String content = event("property-interleaving", "One"); + send("PUT", path, content); + String token = text(send("REPORT", calendar, sync("", 10)), DavXml.DAV, "sync-token"); + var etag = send("GET", path, "").headers().firstValue("ETag").orElseThrow(); + try (var workers = java.util.concurrent.Executors.newVirtualThreadPerTaskExecutor()) { + var a = + workers.submit( + () -> + send( + "PROPPATCH", + path, + "one")); + var b = + workers.submit( + () -> + send( + "PROPPATCH", + path, + "two")); + assertThat(a.get().statusCode()).isEqualTo(207); + assertThat(b.get().statusCode()).isEqualTo(207); + } + var properties = + send( + "PROPFIND", + path, + propRequest(""), + "Depth", + "0"); + assertThat(propertyStatus(properties, "urn:audit", "first")).isEqualTo(200); + assertThat(propertyStatus(properties, "urn:audit", "second")).isEqualTo(200); + assertThat(text(properties, "urn:audit", "first")).isEqualTo("one"); + assertThat(text(properties, "urn:audit", "second")).isEqualTo("two"); + String request = + sync(token, 10) + .replace( + "", + ""); + var changes = send("REPORT", calendar, request); + assertThat(principalReportHrefs(changes)) + .containsExactly("https://calendar.example/team/dav" + path); + assertThat(text(changes, "urn:audit", "first")).isEqualTo("one"); + assertThat(text(changes, "urn:audit", "second")).isEqualTo("two"); + var original = send("GET", path, ""); + assertThat(original.body()).isEqualTo(content); + assertThat(original.headers().firstValue("ETag").orElseThrow()).isEqualTo(etag); + } + + @Test + void oversizedCalendarPutReportsAdvertisedSizePreconditionWithoutChangingState() + throws Exception { + String calendar = create("size-precondition", "VEVENT"), path = calendar + "one.ics"; + String content = event("size-precondition", "Original"); + var created = send("PUT", path, content); + var maximum = send("PROPFIND", calendar, propRequest(""), "Depth", "0"); + assertThat(text(maximum, DavXml.CAL, "max-resource-size")) + .isEqualTo(Integer.toString(org.busystack.caldav.calendar.CalendarContent.MAX_BYTES)); + String template = + event("size-at-limit", "At limit").replace("END:VEVENT\r\n", "X-BULK:\r\nEND:VEVENT\r\n"); + String atLimit = + template.replace( + "X-BULK:\r\n", + "X-BULK:" + + "a" + .repeat( + org.busystack.caldav.calendar.CalendarContent.MAX_BYTES + - template.getBytes(StandardCharsets.UTF_8).length) + + "\r\n"); + assertThat(atLimit.getBytes(StandardCharsets.UTF_8)) + .hasSize(org.busystack.caldav.calendar.CalendarContent.MAX_BYTES); + assertThat( + send("PUT", calendar + "limit.ics", atLimit, "Content-Type", "text/calendar") + .statusCode()) + .isEqualTo(201); + assertThat(send("GET", calendar + "limit.ics", "").body()).isEqualTo(atLimit); + assertThat( + send( + "PUT", + calendar + "limit.ics", + atLimit.replace("X-BULK:a", "X-BULK:"), + "Content-Type", + "text/calendar") + .statusCode()) + .isEqualTo(204); + String before = + text( + send("PROPFIND", calendar, propRequest(""), "Depth", "0"), + DavXml.DAV, + "sync-token"); + for (String target : List.of(path, calendar + "missing.ics")) { + var response = + send( + "PUT", + target, + "x".repeat(org.busystack.caldav.calendar.CalendarContent.MAX_BYTES + 1), + "Content-Type", + "text/calendar"); + assertThat(response.statusCode()).isEqualTo(413); + assertThat(xml(response).getElementsByTagNameNS(DavXml.CAL, "max-resource-size").getLength()) + .isEqualTo(1); + } + assertThat(send("GET", path, "").body()).isEqualTo(content); + assertThat(send("GET", path, "").headers().firstValue("ETag")) + .isEqualTo(created.headers().firstValue("ETag")); + assertThat(send("GET", calendar + "missing.ics", "").statusCode()).isEqualTo(404); + assertThat( + text( + send("PROPFIND", calendar, propRequest(""), "Depth", "0"), + DavXml.DAV, + "sync-token")) + .isEqualTo(before); + } + + @Test + void semanticListTextQueriesReturnCorrectHrefsOverHttpForEscapesAndCollations() throws Exception { + String calendar = create("semantic-text-http", "VEVENT"); + String original; + String originalQuery; + try (var input = getClass().getResourceAsStream("/calendar/audit/valid-text-list.ics"); + var request = getClass().getResourceAsStream("/calendar/audit/categories-query.xml")) { + assertThat(input).isNotNull(); + assertThat(request).isNotNull(); + original = new String(input.readAllBytes(), StandardCharsets.UTF_8); + originalQuery = new String(request.readAllBytes(), StandardCharsets.UTF_8); + } + String originalPath = calendar + "original.ics"; + assertThat(send("PUT", originalPath, original, "Content-Type", "text/calendar").statusCode()) + .isEqualTo(201); + assertThat(send("GET", originalPath, "").body()).isEqualTo(original); + var originalResult = send("REPORT", calendar, originalQuery, "Depth", "1"); + assertThat(originalResult.statusCode()).isEqualTo(207); + assertThat(principalReportHrefs(originalResult)) + .containsExactly("https://calendar.example/team/dav" + originalPath); + + // Add typed-value neighbors after checking the original counterexample unchanged. + // Columns are resource name, iCalendar encoded TEXT, and decoded semantic TEXT. + String[][] cases = { + {"original", "alpha\\;beta", "alpha;beta"}, + {"comma", "alpha\\,beta", "alpha,beta"}, + {"backslash", "alpha\\\\beta", "alpha\\beta"}, + {"newline", "alpha\\nbeta", "alpha\nbeta"} + }; + java.util.Set all = new java.util.HashSet<>(); + for (String[] value : cases) { + String resource = calendar + value[0] + ".ics"; + String body = + original + .replace("UID:valid-text-list", "UID:semantic-http-" + value[0]) + .replace("CATEGORIES:alpha\\;beta,other", "CATEGORIES:" + value[1] + ",other") + .replace( + "END:VEVENT\r\n", + "RESOURCES:" + value[1] + ",other\r\nSUMMARY:" + value[1] + "\r\nEND:VEVENT\r\n"); + // The original resource's UID remains immutable during its corrective replacement. + if (value[0].equals("original")) + body = body.replace("UID:semantic-http-original", "UID:valid-text-list"); + assertThat(send("PUT", resource, body, "Content-Type", "text/calendar").statusCode()) + .isEqualTo(value[0].equals("original") ? 204 : 201); + assertThat(send("GET", resource, "").body()).isEqualTo(body); + all.add("https://calendar.example/team/dav" + resource); + } + String repeatedPath = calendar + "repeated.ics"; + String repeated = + original + .replace("UID:valid-text-list", "UID:semantic-http-repeated") + .replace( + "CATEGORIES:alpha\\;beta,other", + "CATEGORIES:RepeatedFirst\r\nCATEGORIES:RepeatedSecond") + .replace( + "END:VEVENT\r\n", + "RESOURCES:RepeatedFirst\r\nRESOURCES:RepeatedSecond\r\nSUMMARY:RepeatedSummary\r\nEND:VEVENT\r\n"); + assertThat(send("PUT", repeatedPath, repeated, "Content-Type", "text/calendar").statusCode()) + .isEqualTo(201); + assertThat(send("GET", repeatedPath, "").body()).isEqualTo(repeated); + String repeatedHref = "https://calendar.example/team/dav" + repeatedPath; + all.add(repeatedHref); + + for (String property : List.of("CATEGORIES", "RESOURCES", "SUMMARY")) { + for (String collation : List.of("i;octet", "i;ascii-casemap")) { + for (String[] value : cases) { + String expected = "https://calendar.example/team/dav" + calendar + value[0] + ".ics"; + var positive = + send( + "REPORT", + calendar, + semanticTextQuery(property, value[2], collation, false), + "Depth", + "1"); + assertThat(positive.statusCode()).isEqualTo(207); + assertThat(principalReportHrefs(positive)).containsExactly(expected); + var negative = + send( + "REPORT", + calendar, + semanticTextQuery(property, value[2], collation, true), + "Depth", + "1"); + assertThat(negative.statusCode()).isEqualTo(207); + var otherResources = new java.util.HashSet<>(all); + otherResources.remove(expected); + assertThat(principalReportHrefs(negative)) + .containsExactlyInAnyOrderElementsOf(otherResources); + } + var caseMatch = + send( + "REPORT", + calendar, + semanticTextQuery(property, "ALPHA;BETA", collation, false), + "Depth", + "1"); + assertThat(caseMatch.statusCode()).isEqualTo(207); + if (collation.equals("i;ascii-casemap")) + assertThat(principalReportHrefs(caseMatch)) + .containsExactly("https://calendar.example/team/dav" + originalPath); + else assertThat(principalReportHrefs(caseMatch)).isEmpty(); + if (!property.equals("SUMMARY")) { + for (String value : List.of("RepeatedFirst", "RepeatedSecond")) { + var instance = + send( + "REPORT", + calendar, + semanticTextQuery(property, value, collation, false), + "Depth", + "1"); + assertThat(instance.statusCode()).isEqualTo(207); + assertThat(principalReportHrefs(instance)).containsExactly(repeatedHref); + } + } + } + } + } + + @Test + void reportsRestoreFoldedUtf8CharactersWithoutChangingAuthoritativeBytes() throws Exception { + String calendar = create("split-utf8", "VEVENT"), path = calendar + "folded.ics"; + String summary = "before€ middle🙂after", original = event("split-utf8", summary); + String[] parts = original.split("€|🙂", -1); + byte[] euro = "€".getBytes(StandardCharsets.UTF_8); + byte[] smile = "🙂".getBytes(StandardCharsets.UTF_8); + var folded = new java.io.ByteArrayOutputStream(); + folded.writeBytes(parts[0].getBytes(StandardCharsets.UTF_8)); + folded.write(euro, 0, 1); + folded.writeBytes("\r\n ".getBytes(StandardCharsets.US_ASCII)); + folded.write(euro, 1, euro.length - 1); + folded.writeBytes(parts[1].getBytes(StandardCharsets.UTF_8)); + folded.write(smile, 0, 2); + folded.writeBytes("\r\n\t".getBytes(StandardCharsets.US_ASCII)); + folded.write(smile, 2, smile.length - 2); + folded.writeBytes(parts[2].getBytes(StandardCharsets.UTF_8)); + byte[] raw = folded.toByteArray(); + var put = + http.send( + request(path, aliceAuth) + .header("Content-Type", "text/calendar; charset=utf-8") + .header("If-None-Match", "*") + .PUT(HttpRequest.BodyPublishers.ofByteArray(raw)) + .build(), + HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8)); + assertThat(put.statusCode()).isEqualTo(201); + String etag = put.headers().firstValue("ETag").orElseThrow(); + var before = + http.send(request(path, aliceAuth).GET().build(), HttpResponse.BodyHandlers.ofByteArray()); + assertThat(before.statusCode()).isEqualTo(200); + assertThat(before.body()).isEqualTo(raw); + assertThat(before.headers().firstValue("ETag")).hasValue(etag); + String href = "https://calendar.example/team/dav" + path; + String multiget = + "" + + href + + ""; + String synchronization = + sync("", 10) + .replace( + "", + ""); + var queried = + send( + "REPORT", + calendar, + semanticTextQuery("SUMMARY", "€ middle🙂", "i;octet", false), + "Depth", + "1"); + var fetched = send("REPORT", calendar, multiget); + var synchronizedData = send("REPORT", calendar, synchronization, "Depth", "0"); + for (var report : List.of(queried, fetched, synchronizedData)) { + assertThat(report.statusCode()).isEqualTo(207); + assertThat(principalReportHrefs(report)).containsExactly(href); + assertThat(text(report, DavXml.DAV, "getetag")).isEqualTo(etag); + String content = text(report, DavXml.CAL, "calendar-data"); + assertThat(content).isEqualTo(original).doesNotContain("\uFFFD"); + var parsed = + org.busystack.caldav.calendar.CalendarContent.parse( + content.getBytes(StandardCharsets.UTF_8), "VEVENT"); + var event = parsed.calendar().getComponents().getFirst(); + assertThat(event.getRequiredProperty(net.fortuna.ical4j.model.Property.SUMMARY).getValue()) + .isEqualTo(summary); + assertThat(event.getRequiredProperty("X-CLIENT-PROPERTY").getValue()) + .isEqualTo("preserve me"); + } + String selected = + ""; + var projected = send("REPORT", calendar, multiget.replace("", selected)); + assertThat(projected.statusCode()).isEqualTo(207); + assertThat(principalReportHrefs(projected)).containsExactly(href); + assertThat(text(projected, DavXml.CAL, "calendar-data")) + .contains("SUMMARY:" + summary) + .doesNotContain("\uFFFD", "UID:", "DTSTART:", "X-CLIENT-PROPERTY"); + var after = + http.send(request(path, aliceAuth).GET().build(), HttpResponse.BodyHandlers.ofByteArray()); + assertThat(after.statusCode()).isEqualTo(200); + assertThat(after.body()).isEqualTo(raw); + assertThat(after.headers().firstValue("ETag")).hasValue(etag); + } + + private String semanticTextQuery( + String property, String value, String collation, boolean negate) { + String text = value.replace("&", "&").replace("<", "<").replace(">", ">"); + return query( + "" + + text + + ""); + } + + private static String capturedObservanceFixture(String zone) throws Exception { + try (var input = + DavHttpIT.class.getResourceAsStream( + "/calendar/observance-until/thunderbird159-" + zone + ".ics")) { + return new String( + java.util.Objects.requireNonNull(input).readAllBytes(), StandardCharsets.UTF_8); + } + } + + @Test + void repairedPutOmitsValidatorsButAllReadPathsUseCorrectedAuthoritativeBytes() throws Exception { + for (String zone : List.of("vancouver", "berlin")) { + String calendar = create("observance-http-" + zone, "VEVENT"), path = calendar + "client.ics"; + String input = capturedObservanceFixture(zone); + String before = currentCollectionToken(calendar); + var put = send("PUT", path, input, "Content-Type", "text/calendar", "If-None-Match", "*"); + assertThat(put.statusCode()).isEqualTo(201); + assertThat(put.headers().firstValue("ETag")).isEmpty(); + assertThat(put.headers().firstValue("Last-Modified")).isEmpty(); + var get = send("GET", path, ""); + String stored = get.body(), etag = get.headers().firstValue("ETag").orElseThrow(); + assertThat(stored).isNotEqualTo(input); + assertThat(etag).startsWith("\"").doesNotStartWith("W/"); + org.busystack.caldav.calendar.CalendarContent.parse( + stored.getBytes(StandardCharsets.UTF_8), "VEVENT"); + assertThat( + text( + send("PROPFIND", path, propRequest(""), "Depth", "0"), + DavXml.DAV, + "getetag")) + .isEqualTo(etag); + String multiget = + "https://calendar.example/team/dav" + + path + + ""; + var multi = send("REPORT", calendar, multiget); + assertThat(multi.statusCode()).isEqualTo(207); + assertThat(text(multi, DavXml.CAL, "calendar-data")).isEqualTo(stored); + var sync = + send( + "REPORT", + calendar, + sync(before, 10) + .replace( + "", + ""), + "Depth", + "0"); + assertThat(sync.statusCode()).isEqualTo(207); + assertThat(text(sync, DavXml.CAL, "calendar-data")).isEqualTo(stored); + // These instants follow the captured rules: Vancouver's last 2026 onset leaves UTC-07; + // Berlin's winter 2026 offset is UTC+01. No host future-DST assumption is involved. + String start = zone.equals("vancouver") ? "20261109T170000Z" : "20261109T090000Z"; + String end = zone.equals("vancouver") ? "20261109T180000Z" : "20261109T100000Z"; + String filter = + ""; + var query = + send( + "REPORT", + calendar, + query(filter) + .replace( + "", + ""), + "Depth", + "1"); + assertThat(query.statusCode()).isEqualTo(207); + assertThat(principalReportHrefs(query)) + .containsExactly("https://calendar.example/team/dav" + path); + assertThat(text(query, DavXml.CAL, "calendar-data")) + .contains("DTSTART:" + start, "DTEND:" + end); + var busy = + send( + "REPORT", + calendar, + "", + "Depth", + "1"); + assertThat(busy.statusCode()).isEqualTo(200); + assertThat(busy.body()).contains(start + "/" + end); + // Transformation is relative to this request, including repeated original submissions. + var repeated = send("PUT", path, input, "If-Match", etag); + assertThat(repeated.statusCode()).isEqualTo(204); + assertThat(repeated.headers().firstValue("ETag")).isEmpty(); + assertThat(repeated.headers().firstValue("Last-Modified")).isEmpty(); + assertThat(send("GET", path, "").body()).isEqualTo(stored); + assertThat(send("GET", path, "").headers().firstValue("ETag")).contains(etag); + var valid = send("PUT", path, stored, "If-Match", etag); + assertThat(valid.statusCode()).isEqualTo(204); + assertThat(valid.headers().firstValue("ETag")).contains(etag); + String edited = + stored + .replace("T100000\r\nDTEND", "T120000\r\nDTEND") + .replace("T110000\r\nTRANSP", "T130000\r\nTRANSP"); + assertThat(send("PUT", path, edited, "If-Match", etag).statusCode()).isEqualTo(204); + assertThat(send("GET", path, "").body()).isEqualTo(edited); + } + } + + @Test + void repairFailuresConflictsAndConcurrentWritesPreserveAtomicHistoryAndSync() throws Exception { + String calendar = create("observance-atomic", "VEVENT"), path = calendar + "one.ics"; + String original = capturedObservanceFixture("vancouver"); + var accepted = send("PUT", path, original); + assertThat(accepted.statusCode()).isEqualTo(201); + var stored = send("GET", path, ""); + String etag = stored.headers().firstValue("ETag").orElseThrow(); + UUID collection = + jdbc.queryForObject( + "SELECT collection_id FROM dav_bindings WHERE home_id=? AND path=?", + UUID.class, + alice, + "observance-atomic"); + var state = admissionHistoryState(collection); + String token = currentCollectionToken(calendar); + for (String invalid : + List.of( + original.replace( + "TZOFFSETFROM:-080000", "TZOFFSETFROM:-080000\r\nTZOFFSETFROM:-070000"), + original.replace("UNTIL=19610924T020000", "UNTIL=19610931T020000"), + original.replace("BYDAY=-1SU", "BYDAY=0SU"), + original.replace("FREQ=YEARLY", "FREQ=YEARLY;COUNT=1"), + original.replace("FREQ=YEARLY", "FREQ=YEARLY;FREQ=YEARLY"), + original.replaceFirst("BEGIN:VCALENDAR", "XBEGIN:VCALENDAR"), + original.replace("DTEND;TZID=America/Vancouver:20261109T110000", "DURATION:P1W2D"))) { + for (String target : List.of(path, calendar + "rejected.ics")) { + var rejected = send("PUT", target, invalid, "If-Match", target.equals(path) ? etag : "*"); + assertThat(rejected.statusCode()).isEqualTo(403); + assertThat( + xml(rejected).getElementsByTagNameNS(DavXml.CAL, "valid-calendar-data").getLength()) + .isEqualTo(1); + assertThat(admissionHistoryState(collection)).isEqualTo(state); + assertThat(currentCollectionToken(calendar)).isEqualTo(token); + assertThat(send("GET", path, "").body()).isEqualTo(stored.body()); + assertThat(send("GET", path, "").headers().firstValue("ETag")).contains(etag); + assertThat(send("GET", calendar + "rejected.ics", "").statusCode()).isEqualTo(404); + } + } + assertThat(send("PUT", path, original, "If-Match", "\"stale\"").statusCode()).isEqualTo(412); + assertThat(send("PUT", path, original, "If-None-Match", "*").statusCode()).isEqualTo(412); + assertThat(send("PUT", calendar + "duplicate.ics", original, "If-None-Match", "*").statusCode()) + .isEqualTo(403); + assertThat(admissionHistoryState(collection)).isEqualTo(state); + assertThat(currentCollectionToken(calendar)).isEqualTo(token); + try (var workers = java.util.concurrent.Executors.newFixedThreadPool(2)) { + var first = + workers.submit( + () -> + send( + "PUT", + path, + original.replace("SUMMARY:TB159", "SUMMARY:First TB159"), + "If-Match", + etag)); + var second = + workers.submit( + () -> + send( + "PUT", + path, + original.replace("SUMMARY:TB159", "SUMMARY:Second TB159"), + "If-Match", + etag)); + assertThat(List.of(first.get().statusCode(), second.get().statusCode())) + .containsExactlyInAnyOrder(204, 412); + } + assertThat(((Number) admissionHistoryState(collection).get("revision")).longValue()) + .isEqualTo(((Number) state.get("revision")).longValue() + 1); + } +} diff --git a/src/test/java/org/busystack/caldav/protocol/DavPathsTest.java b/src/test/java/org/busystack/caldav/protocol/DavPathsTest.java new file mode 100644 index 0000000..a7c07be --- /dev/null +++ b/src/test/java/org/busystack/caldav/protocol/DavPathsTest.java @@ -0,0 +1,49 @@ +package org.busystack.caldav.protocol; + +import static org.assertj.core.api.Assertions.*; + +import java.net.URI; +import java.util.List; +import org.busystack.caldav.config.DeploymentProperties; +import org.junit.jupiter.api.Test; + +class DavPathsTest { + final DavPaths paths = + new DavPaths( + new DeploymentProperties( + URI.create("https://calendar.example/team"), + URI.create("https://calendar.example/team"), + false)); + + @Test + void uriEncodingPreservesLiteralPlusAndUnicodeWithoutFormDecoding() { + List segments = List.of("calendars", "user", "雪 + café", "100%.ics"); + assertThat(paths.parse(paths.href(segments, false))).isEqualTo(segments); + assertThat(paths.parse("/team/dav/calendars/user/a+b.ics")).endsWith("a+b.ics"); + assertThat(paths.parse("https://calendar.example:443/team/dav/")).isEmpty(); + for (String permitted : + List.of("valid\uFDD0.ics", "valid" + new String(Character.toChars(0x1FFFE)) + ".ics")) { + var valid = List.of("calendars", "user", permitted); + assertThat(paths.parse(paths.href(valid, false))).isEqualTo(valid); + } + } + + @Test + void traversalWrongAuthorityAndInvalidUtf8AreRejected() { + for (String href : + List.of( + "/team/dav/a//b", + "/team/dav/%2e%2e/", + "/team/dav/a%2fb", + "/team/dav/a%5cb", + "/team/dav/%ff", + "/team/dav/bad%EF%BF%BEleaf", + "/team/dav/bad%EF%BF%BFleaf", + "/team/dav/a?query", + "//attacker.invalid/team/dav/", + "https://attacker.invalid/team/dav/", + "https://calendar.example:444/team/dav/", + "/other/dav/")) + assertThatThrownBy(() -> paths.parse(href)).as(href).isInstanceOf(DavException.class); + } +} diff --git a/src/test/java/org/busystack/caldav/protocol/DavPropertyBoundsIT.java b/src/test/java/org/busystack/caldav/protocol/DavPropertyBoundsIT.java new file mode 100644 index 0000000..be54a9d --- /dev/null +++ b/src/test/java/org/busystack/caldav/protocol/DavPropertyBoundsIT.java @@ -0,0 +1,667 @@ +package org.busystack.caldav.protocol; + +import static org.assertj.core.api.Assertions.*; + +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.nio.charset.StandardCharsets; +import java.time.Instant; +import java.util.Base64; +import java.util.UUID; +import org.busystack.caldav.account.AccountService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.server.LocalServerPort; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; +import org.testcontainers.postgresql.PostgreSQLContainer; +import org.w3c.dom.Element; + +@SpringBootTest( + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, + properties = { + "busystack.public-base-url=https://calendar.example/team", + "busystack.issuer=https://calendar.example/team" + }) +@org.springframework.test.annotation.DirtiesContext( + classMode = org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_CLASS) +@Testcontainers +@org.springframework.context.annotation.Import(DavPropertyBoundsIT.AllocationProbe.class) +class DavPropertyBoundsIT { + @Container + static final PostgreSQLContainer POSTGRES = + org.busystack.caldav.testing.LoopbackContainers.postgres(); + + @DynamicPropertySource + static void database(DynamicPropertyRegistry registry) { + registry.add("spring.datasource.url", POSTGRES::getJdbcUrl); + registry.add("spring.datasource.username", POSTGRES::getUsername); + registry.add("spring.datasource.password", POSTGRES::getPassword); + registry.add("server.ssl.enabled", () -> true); + registry.add( + "server.ssl.key-store", () -> org.busystack.caldav.testing.TestTls.instance().keyStore()); + registry.add( + "server.ssl.key-store-password", + () -> org.busystack.caldav.testing.TestTls.instance().password()); + } + + @LocalServerPort int port; + @Autowired AccountService accounts; + @org.springframework.test.context.bean.override.mockito.MockitoSpyBean JdbcTemplate jdbc; + @Autowired org.busystack.caldav.storage.DavStore store; + @Autowired org.springframework.transaction.PlatformTransactionManager transactions; + UUID alice, bob; + String aliceAuth, bobAuth, home; + final HttpClient http = + HttpClient.newBuilder() + .sslContext(org.busystack.caldav.testing.TestTls.instance().context()) + .followRedirects(HttpClient.Redirect.NEVER) + .build(); + + @BeforeEach + void prepare() { + org.busystack.caldav.testing.FixtureDatabase.clearAccounts(jdbc); + alice = accounts.provision("alice", "Alice & Calendar", "a secure Alice password", false); + bob = accounts.provision("bob", "Bob Tasks", "a secure Bob password", false); + aliceAuth = + basic( + "alice", + accounts.issueDavCredential(alice, "client", Instant.now().plusSeconds(3600)).secret()); + bobAuth = + basic( + "bob", + accounts.issueDavCredential(bob, "client", Instant.now().plusSeconds(3600)).secret()); + home = "/calendars/" + alice + "/"; + } + + private String basic(String user, String secret) { + return "Basic " + + Base64.getEncoder() + .encodeToString((user + ":" + secret).getBytes(StandardCharsets.UTF_8)); + } + + private HttpRequest.Builder request(String path, String auth) { + return HttpRequest.newBuilder(URI.create("https://127.0.0.1:" + port + "/team/dav" + path)) + .header("Authorization", auth); + } + + private HttpResponse sendAs( + String method, String path, String body, String auth, String... headers) throws Exception { + var builder = + request(path, auth) + .method(method, HttpRequest.BodyPublishers.ofString(body, StandardCharsets.UTF_8)); + if (headers.length > 0) builder.headers(headers); + return http.send(builder.build(), HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8)); + } + + private HttpResponse send(String method, String path, String body, String... headers) + throws Exception { + return sendAs(method, path, body, aliceAuth, headers); + } + + @Test + void inheritedLanguageIsStoredWithMixedPropertyContentAcrossTransfer() throws Exception { + String calendar = home + "languages/"; + assertThat( + send( + "MKCALENDAR", + calendar, + " before 雪 after ") + .statusCode()) + .isEqualTo(201); + String selection = + ""; + var read = send("PROPFIND", calendar, selection, "Depth", "0"); + assertThat(read.statusCode()).isEqualTo(207); + var root = DavXml.parse(read.body().getBytes(StandardCharsets.UTF_8)); + var label = (Element) root.getElementsByTagNameNS("urn:lang", "label").item(0); + assertThat(label.getAttributeNS(javax.xml.XMLConstants.XML_NS_URI, "lang")).isEqualTo("fr"); + assertThat(label.getTextContent()).isEqualTo(" before 雪 after "); + assertThat( + ((Element) label.getElementsByTagNameNS("urn:lang", "em").item(0)) + .getAttributeNS(javax.xml.XMLConstants.XML_NS_URI, "lang")) + .isEqualTo("ja"); + String[][] cases = { + {"xml:lang='fr'", "", "", "", "fr"}, + {"xml:lang='fr'", "xml:lang='de'", "", "", "de"}, + {"xml:lang='fr'", "xml:lang='de'", "xml:lang='es'", "", "es"}, + {"xml:lang='fr'", "xml:lang='de'", "xml:lang='es'", "xml:lang='it'", "it"}, + {"xml:lang='fr'", "xml:lang='de'", "xml:lang='es'", "xml:lang=''", ""} + }; + for (String[] item : cases) { + assertThat( + send( + "PROPPATCH", + calendar, + " mixed text ") + .statusCode()) + .isEqualTo(207); + var reread = + DavXml.parse( + send("PROPFIND", calendar, selection, "Depth", "0") + .body() + .getBytes(StandardCharsets.UTF_8)); + var value = (Element) reread.getElementsByTagNameNS("urn:lang", "label").item(0); + assertThat(value.hasAttributeNS(javax.xml.XMLConstants.XML_NS_URI, "lang")).isTrue(); + assertThat(value.getAttributeNS(javax.xml.XMLConstants.XML_NS_URI, "lang")) + .isEqualTo(item[4]); + assertThat(value.getTextContent()).isEqualTo(" mixed text "); + assertThat( + ((Element) value.getElementsByTagNameNS("urn:lang", "em").item(0)) + .getAttributeNS(javax.xml.XMLConstants.XML_NS_URI, "lang")) + .isEqualTo("ja"); + } + assertThat( + send( + "COPY", + calendar, + "", + "Destination", + "https://calendar.example/team/dav" + home + "copy/") + .statusCode()) + .isEqualTo(201); + assertThat( + send( + "MOVE", + home + "copy/", + "", + "Destination", + "https://calendar.example/team/dav" + home + "moved/") + .statusCode()) + .isEqualTo(201); + var transferred = + DavXml.parse( + send("PROPFIND", home + "moved/", selection, "Depth", "0") + .body() + .getBytes(StandardCharsets.UTF_8)); + var value = (Element) transferred.getElementsByTagNameNS("urn:lang", "label").item(0); + assertThat(value.hasAttributeNS(javax.xml.XMLConstants.XML_NS_URI, "lang")).isTrue(); + assertThat(value.getAttributeNS(javax.xml.XMLConstants.XML_NS_URI, "lang")).isEmpty(); + } + + @Test + void repeatedValidUpdatesCannotAccumulateUnboundedProperties() { + store.create(alice, alice, "bounded", "Bounded", "VEVENT", java.util.Map.of()); + int rejected = 0; + for (int i = 0; i < 32; i++) { + var name = new javax.xml.namespace.QName("urn:bounds", "p" + i); + String xml = "" + "x".repeat(60000) + ""; + try { + store.patch(alice, alice, "bounded", null, java.util.Map.of(name, xml), java.util.Set.of()); + } catch (org.busystack.caldav.storage.StoreException ex) { + assertThat(ex.kind()).isEqualTo(org.busystack.caldav.storage.StoreException.Kind.LIMIT); + rejected++; + } + } + assertThat(rejected).isGreaterThan(0); + } + + private java.util.List recordedSql() { + return org.mockito.Mockito.mockingDetails(jdbc).getInvocations().stream() + .filter( + invocation -> + invocation.getArguments().length > 0 && invocation.getArgument(0) instanceof String) + .map(invocation -> (String) invocation.getArgument(0)) + .toList(); + } + + @Test + void selectiveAndNameOnlyRequestsAvoidUnrelatedValuesAndCalendarBodies() throws Exception { + var entry = store.create(alice, alice, "legacy", "Legacy", "VEVENT", java.util.Map.of()); + UUID id = entry.collection().id(); + var small = new javax.xml.namespace.QName("urn:bounds", "small"); + store.patch( + alice, + alice, + "legacy", + null, + java.util.Map.of(small, "ok"), + java.util.Set.of()); + // Model data admitted by V1-V9 in this disposable database. No production bypass is exposed. + jdbc.execute( + "ALTER TABLE dav_collection_properties DISABLE TRIGGER dav_collection_property_budget"); + try { + jdbc.update( + "INSERT INTO dav_collection_properties(collection_id,namespace,name,xml) SELECT ?, 'urn:bounds','large'||i,''||repeat('x',60000)||'' FROM generate_series(1,24) i", + id); + } finally { + jdbc.execute( + "ALTER TABLE dav_collection_properties ENABLE TRIGGER dav_collection_property_budget"); + } + org.mockito.Mockito.clearInvocations(jdbc); + var selected = + send( + "PROPFIND", + home + "legacy/", + "", + "Depth", + "0"); + assertThat(selected.statusCode()).isEqualTo(207); + assertThat(selected.body()).contains(">ok sql.startsWith("SELECT namespace,name,xml"))) + .allMatch(sql -> sql.contains("namespace=? AND name=?")); + assertThat(selectedSql).noneMatch(sql -> sql.contains("SELECT * FROM calendar_resources")); + org.mockito.Mockito.clearInvocations(jdbc); + var names = + send( + "PROPFIND", + home + "legacy/", + "", + "Depth", + "0"); + assertThat(names.statusCode()).isEqualTo(207); + assertThat(names.body()).contains("large24").doesNotContain("xxxx"); + assertThat(recordedSql()) + .noneMatch( + sql -> + sql.startsWith("SELECT namespace,name,xml") + || sql.contains("SELECT * FROM calendar_resources")); + org.mockito.Mockito.clearInvocations(jdbc); + assertThat( + send( + "PROPFIND", + home + "legacy/", + "", + "Depth", + "0") + .statusCode()) + .isEqualTo(507); + assertThat(recordedSql()).noneMatch(sql -> sql.startsWith("SELECT namespace,name,xml")); + store.patch( + alice, + alice, + "legacy", + null, + java.util.Map.of(), + java.util.Set.of(new javax.xml.namespace.QName("urn:bounds", "large24"))); + assertThat( + send( + "PROPFIND", + home + "legacy/", + "", + "Depth", + "0") + .statusCode()) + .isEqualTo(207); + } + + @Test + void concurrentPropertyAdmissionRemainsAtomicAndSmallRequestsStayUsable() throws Exception { + store.create(alice, alice, "concurrent", "Concurrent", "VEVENT", java.util.Map.of()); + for (int i = 0; i < 15; i++) { + String local = "seed" + i; + store.patch( + alice, + alice, + "concurrent", + null, + java.util.Map.of( + new javax.xml.namespace.QName("urn:bounds", local), + "" + "x".repeat(60000) + ""), + java.util.Set.of()); + } + var jobs = new java.util.ArrayList>(); + for (int i = 0; i < 4; i++) { + String local = "race" + i; + jobs.add( + java.util.concurrent.CompletableFuture.supplyAsync( + () -> { + try { + store.patch( + alice, + alice, + "concurrent", + null, + java.util.Map.of( + new javax.xml.namespace.QName("urn:bounds", local), + "" + + "x".repeat(60000) + + ""), + java.util.Set.of()); + return true; + } catch (org.busystack.caldav.storage.StoreException ex) { + assertThat(ex.kind()) + .isEqualTo(org.busystack.caldav.storage.StoreException.Kind.LIMIT); + return false; + } + })); + } + assertThat(jobs.stream().filter(java.util.concurrent.CompletableFuture::join).count()) + .isEqualTo(2); + assertThat( + jdbc.queryForObject( + "SELECT sum(octet_length(xml)) FROM dav_collection_properties WHERE collection_id=(SELECT collection_id FROM dav_bindings WHERE home_id=? AND path='concurrent')", + Long.class, + alice)) + .isLessThanOrEqualTo(org.busystack.caldav.storage.DavStore.MAX_PROPERTY_BYTES); + var reads = new java.util.ArrayList>(); + for (int i = 0; i < 4; i++) + reads.add( + java.util.concurrent.CompletableFuture.supplyAsync( + () -> { + try { + return send( + "PROPFIND", + home + "concurrent/", + "", + "Depth", + "0") + .statusCode(); + } catch (Exception ex) { + throw new RuntimeException(ex); + } + })); + assertThat(reads.stream().map(java.util.concurrent.CompletableFuture::join).toList()) + .containsOnly(207); + } + + @Test + void multiResourcePropertyWorkIsBoundedUnderConcurrentOversizedReports() throws Exception { + store.create(alice, alice, "property-stress", "Property stress", null, java.util.Map.of()); + for (int target = 0; target < 8; target++) { + String path = "property-stress/calendar-" + target; + store.create(alice, alice, path, "Calendar " + target, "VEVENT", java.util.Map.of()); + var values = new java.util.LinkedHashMap(); + values.put( + new javax.xml.namespace.QName("urn:bounds", "small"), + "ok"); + for (int property = 0; property < 15; property++) { + String local = "large" + property; + values.put( + new javax.xml.namespace.QName("urn:bounds", local), + "" + "x".repeat(60000) + ""); + } + store.patch(alice, alice, path, null, values, java.util.Set.of()); + } + String path = home + "property-stress/"; + String selection = + ""; + assertThat(send("PROPFIND", path, selection, "Depth", "1").statusCode()).isEqualTo(207); + org.mockito.Mockito.clearInvocations(jdbc); + assertThat( + send( + "PROPFIND", + path, + "", + "Depth", + "1") + .statusCode()) + .isEqualTo(207); + assertThat(recordedSql()) + .noneMatch( + sql -> + sql.startsWith("SELECT namespace,name,xml") + || sql.contains("SELECT * FROM calendar_resources")); + for (var pool : java.lang.management.ManagementFactory.getMemoryPoolMXBeans()) + if (pool.getType() == java.lang.management.MemoryType.HEAP) pool.resetPeakUsage(); + long start = System.nanoTime(); + org.mockito.Mockito.clearInvocations(jdbc); + var reports = new java.util.ArrayList>(); + for (int i = 0; i < 4; i++) + reports.add( + java.util.concurrent.CompletableFuture.supplyAsync( + () -> { + try { + return send( + "PROPFIND", + path, + "", + "Depth", + "1") + .statusCode(); + } catch (Exception ex) { + throw new RuntimeException(ex); + } + })); + assertThat(reports.stream().map(java.util.concurrent.CompletableFuture::join).toList()) + .containsOnly(507); + long peak = + java.lang.management.ManagementFactory.getMemoryPoolMXBeans().stream() + .filter(pool -> pool.getType() == java.lang.management.MemoryType.HEAP) + .mapToLong(pool -> pool.getPeakUsage().getUsed()) + .sum(); + System.out.printf( + "PROPERTY_REPORT_STRESS targets=8 accepted_xml_bytes=%d parallel_reports=4 result=507 duration_ms=%d max_heap_bytes=%d sum_heap_pool_peak_bytes=%d jdbc_calls=%d%n", + jdbc.queryForObject( + "SELECT sum(octet_length(xml)) FROM dav_collection_properties WHERE collection_id IN(SELECT collection_id FROM dav_bindings WHERE home_id=? AND path LIKE 'property-stress/%')", + Long.class, alice), + java.util.concurrent.TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start), + Runtime.getRuntime().maxMemory(), + peak, + recordedSql().size()); + assertThat(send("PROPFIND", path, selection, "Depth", "1").statusCode()).isEqualTo(207); + assertThat(send("OPTIONS", path, "").statusCode()).isEqualTo(200); + } + + @Test + void databaseBudgetRejectsConcurrentStaleRepeatableReadSnapshots() { + var entry = store.create(alice, alice, "rr-budget", "RR budget", "VEVENT", java.util.Map.of()); + UUID id = entry.collection().id(); + for (int i = 0; i < 15; i++) { + String local = "seed" + i; + store.patch( + alice, + alice, + "rr-budget", + null, + java.util.Map.of( + new javax.xml.namespace.QName("urn:bounds", local), + "" + "x".repeat(60000) + ""), + java.util.Set.of()); + } + var barrier = new java.util.concurrent.CyclicBarrier(2); + var futures = new java.util.ArrayList>(); + for (int i = 0; i < 2; i++) { + String prefix = "tx" + i; + futures.add( + java.util.concurrent.CompletableFuture.supplyAsync( + () -> { + var tx = + new org.springframework.transaction.support.TransactionTemplate(transactions); + tx.setIsolationLevel( + org.springframework.transaction.TransactionDefinition + .ISOLATION_REPEATABLE_READ); + try { + tx.executeWithoutResult( + ignored -> { + jdbc.queryForObject( + "SELECT sum(octet_length(xml)) FROM dav_collection_properties WHERE collection_id=?", + Long.class, + id); + try { + barrier.await(30, java.util.concurrent.TimeUnit.SECONDS); + } catch (Exception failure) { + throw new RuntimeException(failure); + } + jdbc.update( + "INSERT INTO dav_collection_properties(collection_id,namespace,name,xml) SELECT ?, 'urn:bounds',p,''||repeat('x',60000)||'' FROM (SELECT ?||i AS p FROM generate_series(1,2) i) names", + id, + prefix); + }); + return "committed"; + } catch (RuntimeException failure) { + for (Throwable cause = failure; cause != null; cause = cause.getCause()) + if (cause instanceof java.sql.SQLException sql + && "40001".equals(sql.getSQLState())) return "40001"; + throw failure; + } + })); + } + assertThat(futures.stream().map(java.util.concurrent.CompletableFuture::join).toList()) + .containsExactlyInAnyOrder("committed", "40001"); + assertThat( + jdbc.queryForObject( + "SELECT sum(octet_length(xml)) FROM dav_collection_properties WHERE collection_id=?", + Long.class, + id)) + .isLessThanOrEqualTo(org.busystack.caldav.storage.DavStore.MAX_PROPERTY_BYTES); + } + + @Test + void collectionTimezoneChangesInvalidateContinuationAndStableTokens() throws Exception { + store.create(alice, alice, "zone", "Zone", "VEVENT", java.util.Map.of()); + UUID id = store.resolve(alice, alice, "zone").collection().id(); + for (String uid : java.util.List.of("a", "b")) { + String content = + "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Independent property timezone fixture//EN\r\nBEGIN:VEVENT\r\nUID:" + + uid + + "\r\nDTSTAMP:20261027T000000Z\r\nDTSTART:20261027T100000\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n"; + store.putResource( + alice, + id, + uid + ".ics", + content.getBytes(StandardCharsets.UTF_8), + org.busystack.caldav.storage.WriteCondition.create()); + } + String path = home + "zone/"; + String first = + "11"; + var initial = send("REPORT", path, first, "Depth", "0"); + assertThat(initial.statusCode()).isEqualTo(207); + String continuation = + DavXml.parse(initial.body().getBytes(StandardCharsets.UTF_8)) + .getElementsByTagNameNS(DavXml.DAV, "sync-token") + .item(0) + .getTextContent(); + var tokenProperty = + send( + "PROPFIND", + path, + "", + "Depth", + "0"); + assertThat(tokenProperty.statusCode()).isEqualTo(207); + String stable = + DavXml.parse(tokenProperty.body().getBytes(StandardCharsets.UTF_8)) + .getElementsByTagNameNS(DavXml.DAV, "sync-token") + .item(0) + .getTextContent(); + String zone = + "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Independent property timezone fixture//EN\r\nBEGIN:VTIMEZONE\r\nTZID:Custom/Fixed\r\nBEGIN:STANDARD\r\nDTSTART:19700101T000000\r\nTZOFFSETFROM:+0200\r\nTZOFFSETTO:+0200\r\nEND:STANDARD\r\nEND:VTIMEZONE\r\nEND:VCALENDAR\r\n"; + String update = + "" + + zone + + ""; + assertThat(send("PROPPATCH", path, update).statusCode()).isEqualTo(207); + for (String old : java.util.List.of(continuation, stable)) { + var report = + send( + "REPORT", + path, + "" + + old + + "1", + "Depth", + "0"); + assertThat(report.statusCode()).isEqualTo(403); + assertThat(report.body()).contains("valid-sync-token"); + } + assertThat(send("REPORT", path, first, "Depth", "0").statusCode()).isEqualTo(207); + } + + @org.springframework.boot.test.context.TestConfiguration(proxyBeanMethods = false) + static class AllocationProbe { + private static final java.util.concurrent.atomic.AtomicReference< + java.util.concurrent.CompletableFuture> + NEXT = new java.util.concurrent.atomic.AtomicReference<>(); + + static java.util.concurrent.CompletableFuture next() { + var result = new java.util.concurrent.CompletableFuture(); + if (!NEXT.compareAndSet(null, result)) + throw new IllegalStateException("An allocation measurement is already pending"); + return result; + } + + @org.springframework.context.annotation.Bean + org.springframework.boot.web.servlet.FilterRegistrationBean + requestAllocationProbe() { + if (!(java.lang.management.ManagementFactory.getThreadMXBean() + instanceof com.sun.management.ThreadMXBean metrics) + || !metrics.isThreadAllocatedMemorySupported()) + throw new IllegalStateException("Required Temurin allocation measurement is unavailable"); + if (!metrics.isThreadAllocatedMemoryEnabled()) metrics.setThreadAllocatedMemoryEnabled(true); + jakarta.servlet.Filter filter = + (request, response, chain) -> { + var pending = NEXT.getAndSet(null); + long thread = Thread.currentThread().threadId(); + long before = pending == null ? 0 : metrics.getThreadAllocatedBytes(thread); + try { + chain.doFilter(request, response); + } finally { + if (pending != null) + pending.complete(metrics.getThreadAllocatedBytes(thread) - before); + } + }; + var registration = new org.springframework.boot.web.servlet.FilterRegistrationBean<>(filter); + registration.setOrder(org.springframework.core.Ordered.HIGHEST_PRECEDENCE); + return registration; + } + } + + @Test + void smallExplicitPropertyAllocationsDoNotScaleWithUnrelatedStoredValues() throws Exception { + store.create(alice, alice, "allocation", "Allocation", "VEVENT", java.util.Map.of()); + var values = new java.util.LinkedHashMap(); + values.put( + new javax.xml.namespace.QName("urn:bounds", "small"), + "ok"); + for (int i = 0; i < 15; i++) { + String local = "large" + i; + values.put( + new javax.xml.namespace.QName("urn:bounds", local), + "" + "x".repeat(60000) + ""); + } + store.patch(alice, alice, "allocation", null, values, java.util.Set.of()); + String path = home + "allocation/"; + String selection = + ""; + assertThat(send("PROPFIND", path, selection, "Depth", "0").statusCode()).isEqualTo(207); + org.mockito.Mockito.clearInvocations(jdbc); + var smallMeasurement = AllocationProbe.next(); + assertThat(send("PROPFIND", path, selection, "Depth", "0").statusCode()).isEqualTo(207); + long small = smallMeasurement.get(10, java.util.concurrent.TimeUnit.SECONDS); + assertThat(recordedSql().stream().filter(sql -> sql.startsWith("SELECT namespace,name,xml"))) + .allMatch(sql -> sql.contains("namespace=? AND name=?")); + var allMeasurement = AllocationProbe.next(); + assertThat( + send( + "PROPFIND", + path, + "", + "Depth", + "0") + .statusCode()) + .isEqualTo(207); + long all = allMeasurement.get(10, java.util.concurrent.TimeUnit.SECONDS); + assertThat(small).isLessThan(4L * 1024 * 1024); + assertThat(all).isGreaterThan(small + 1024 * 1024); + System.out.printf( + "PROPERTY_REQUEST_ALLOCATIONS stored_xml_bytes=%d selected_property_bytes=%d all_properties_bytes=%d max_heap_bytes=%d%n", + values.values().stream() + .mapToLong(value -> value.getBytes(StandardCharsets.UTF_8).length) + .sum(), + small, + all, + Runtime.getRuntime().maxMemory()); + } +} diff --git a/src/test/java/org/busystack/caldav/protocol/DavXmlConstructionTest.java b/src/test/java/org/busystack/caldav/protocol/DavXmlConstructionTest.java new file mode 100644 index 0000000..82a0709 --- /dev/null +++ b/src/test/java/org/busystack/caldav/protocol/DavXmlConstructionTest.java @@ -0,0 +1,73 @@ +package org.busystack.caldav.protocol; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +class DavXmlConstructionTest { + @Test + void invalidLegacyLabelTextFailsBeforeAttachingAProperty() { + for (String invalid : java.util.List.of("Invalid\uFFFE", "Invalid\uFFFF", "Invalid\uD800")) { + var document = DavXml.document(); + var root = DavXml.add(document, DavXml.d("prop")); + assertThatThrownBy(() -> DavXml.text(root, DavXml.d("displayname"), invalid)) + .isInstanceOf(DavException.class) + .extracting(failure -> ((DavException) failure).status()) + .isEqualTo(500); + assertThat(root.hasChildNodes()).isFalse(); + } + var document = DavXml.document(); + var root = DavXml.add(document, DavXml.d("prop")); + String valid = "Valid\uFDD0" + new String(Character.toChars(0x1FFFE)); + DavXml.text(root, DavXml.d("displayname"), valid); + assertThat(DavXml.parse(DavXml.bytes(document)).getTextContent()).isEqualTo(valid); + } + + @Test + void constructionRejectsOversizedTextBeforeAttachingIt() { + var document = DavXml.document(); + var root = DavXml.add(document, DavXml.d("multistatus")); + assertThatThrownBy( + () -> DavXml.text(root, DavXml.d("value"), "&".repeat(DavXml.MAX_RESPONSE / 5 + 1))) + .isInstanceOf(DavException.class); + assertThat(root.getTextContent()).isEmpty(); + } + + @Test + void copiedSubtreesAreChargedBeforeAllocationAndFailedCopyIsNotAttached() { + var target = DavXml.document(); + var root = DavXml.add(target, DavXml.d("multistatus")); + var source = DavXml.document(); + var value = source.createElementNS("urn:test", "X:property"); + value.setTextContent("&".repeat(DavXml.MAX_RESPONSE / 5 + 1)); + assertThatThrownBy(() -> DavXml.appendCopy(root, value)).isInstanceOf(DavException.class); + assertThat(root.hasChildNodes()).isFalse(); + } + + @Test + void detachedPropertyRetainsEffectiveLanguageNamespacesAndWhitespaceWithoutMutatingInput() { + var request = + DavXml.parse( + (" before 雪 after ") + .getBytes(java.nio.charset.StandardCharsets.UTF_8)); + var value = (org.w3c.dom.Element) request.getElementsByTagNameNS("urn:test", "label").item(0); + var stored = DavXml.parse(DavXml.propertyBytes(value)); + assertThat(stored.getAttributeNS(javax.xml.XMLConstants.XML_NS_URI, "lang")).isEqualTo("de"); + assertThat(stored.lookupNamespaceURI("unused")).isEqualTo("urn:qname"); + assertThat(stored.getTextContent()).isEqualTo(" before 雪 after "); + assertThat(value.hasAttributeNS(javax.xml.XMLConstants.XML_NS_URI, "lang")).isFalse(); + assertThat( + ((org.w3c.dom.Element) stored.getElementsByTagNameNS("urn:test", "em").item(0)) + .getAttributeNS(javax.xml.XMLConstants.XML_NS_URI, "lang")) + .isEqualTo("ja"); + value.setAttributeNS(javax.xml.XMLConstants.XML_NS_URI, "xml:lang", ""); + assertThat( + DavXml.parse(DavXml.propertyBytes(value)) + .hasAttributeNS(javax.xml.XMLConstants.XML_NS_URI, "lang")) + .isTrue(); + assertThat( + DavXml.parse(DavXml.propertyBytes(value)) + .getAttributeNS(javax.xml.XMLConstants.XML_NS_URI, "lang")) + .isEmpty(); + } +} diff --git a/src/test/java/org/busystack/caldav/protocol/DavXmlTest.java b/src/test/java/org/busystack/caldav/protocol/DavXmlTest.java new file mode 100644 index 0000000..513824f --- /dev/null +++ b/src/test/java/org/busystack/caldav/protocol/DavXmlTest.java @@ -0,0 +1,77 @@ +package org.busystack.caldav.protocol; + +import static org.assertj.core.api.Assertions.*; + +import java.nio.charset.StandardCharsets; +import org.junit.jupiter.api.Test; + +class DavXmlTest { + @Test + void namespaceAndNestedXmlArePreservedByEstablishedParserAndSerializer() { + var parsed = + DavXml.parse( + "雪 & ice" + .getBytes(StandardCharsets.UTF_8)); + var reread = DavXml.parse(DavXml.bytes(parsed)); + assertThat(reread.getNamespaceURI()).isEqualTo("urn:extension"); + assertThat(reread.getTextContent()).isEqualTo("雪 & ice"); + assertThat(reread.getAttributeNS(javax.xml.XMLConstants.XML_NS_URI, "lang")).isEqualTo("ja"); + } + + @Test + void dtdExternalEntitiesDeepTreesAndOversizedBodiesFailClosed() { + for (String xml : + new String[] { + "", + "]>&a;", + "".repeat(33) + "".repeat(33), + "", + "x".repeat(DavXml.MAX_REQUEST + 1) + }) + assertThatThrownBy(() -> DavXml.parse(xml.getBytes(StandardCharsets.UTF_8))) + .isInstanceOf(DavException.class); + } + + @Test + void serializationStopsAtTheResponseLimit() { + var document = DavXml.document(); + // Direct DOM mutation deliberately bypasses construction helpers to retain a test of + // the serializer's independent final-output guard. + var element = document.createElementNS(DavXml.DAV, "D:large"); + element.setTextContent("x".repeat(DavXml.MAX_RESPONSE + 1)); + document.appendChild(element); + assertThatThrownBy(() -> DavXml.bytes(document)) + .isInstanceOf(DavException.class) + .extracting(ex -> ((DavException) ex).status()) + .isEqualTo(507); + } + + @Test + void commandSelectorsIgnoreExtensionSubtreesWithoutPromotingTheirDescendants() { + var command = + DavXml.parse( + ("" + + "" + + "") + .getBytes(StandardCharsets.UTF_8)); + var operations = DavXml.children(command, DavXml.DAV, "set", "remove"); + assertThat(operations).hasSize(1); + assertThat(DavXml.children(DavXml.required(operations.getFirst(), DavXml.DAV, "prop"))) + .singleElement() + .extracting(DavXml::name) + .isEqualTo(new javax.xml.namespace.QName("urn:audit", "kept")); + } + + @Test + void scalarValuesIgnoreExtensionTextWhilePropertyPayloadRemainsIntact() { + var scalar = + DavXml.parse( + ("/team/" + + "evil") + .getBytes(StandardCharsets.UTF_8)); + assertThat(DavXml.scalarText(scalar)).isEqualTo("/team/dav/"); + var copy = DavXml.parse(DavXml.propertyBytes(scalar)); + assertThat(copy.getTextContent()).isEqualTo("/team/evildav/"); + assertThat(copy.getElementsByTagNameNS("urn:audit", "ignored").getLength()).isEqualTo(1); + } +} diff --git a/src/test/java/org/busystack/caldav/protocol/HttpConditionsTest.java b/src/test/java/org/busystack/caldav/protocol/HttpConditionsTest.java new file mode 100644 index 0000000..4414894 --- /dev/null +++ b/src/test/java/org/busystack/caldav/protocol/HttpConditionsTest.java @@ -0,0 +1,118 @@ +package org.busystack.caldav.protocol; + +import static org.assertj.core.api.Assertions.*; + +import java.net.URI; +import org.busystack.caldav.config.DeploymentProperties; +import org.busystack.caldav.storage.StoreException; +import org.junit.jupiter.api.Test; +import org.springframework.mock.web.MockHttpServletRequest; + +class HttpConditionsTest { + final DavPaths paths = + new DavPaths( + new DeploymentProperties( + URI.create("https://calendar.example/team"), + URI.create("https://calendar.example/team"), + false)); + + private MockHttpServletRequest request(String name, String value) { + var request = new MockHttpServletRequest(); + request.setRequestURI("/team/dav/calendars/user/events/item.ics"); + request.addHeader(name, value); + return request; + } + + @Test + void strongIfMatchAndWeakIfNoneMatchUseDifferentComparisonFunctions() { + assertThatThrownBy( + () -> HttpConditions.write(request("If-Match", "W/\"a\""), paths).check("\"a\"")) + .isInstanceOf(StoreException.class); + assertThatThrownBy( + () -> + HttpConditions.write(request("If-None-Match", "W/\"a\", \"b\""), paths) + .check("\"a\"")) + .isInstanceOf(StoreException.class); + HttpConditions.write(request("If-Match", "\"b\", \"a\""), paths).check("\"a\""); + var multiple = request("If-Match", "\"a\""); + multiple.addHeader("If-Match", "\"b\""); + HttpConditions.write(multiple, paths).check("\"b\""); + } + + @Test + void davBooleanListsDistinguishStateTokensFromEtags() { + String target = "/team/dav/calendars/user/events/item.ics"; + var disjunction = DavIf.parse("([\"a\"] Not [\"b\"]) ([\"c\"])", target, paths); + assertThat(disjunction.matches("\"a\"")).isTrue(); + assertThat(disjunction.matches("\"c\"")).isTrue(); + assertThat(disjunction.matches("\"b\"")).isFalse(); + var complement = DavIf.parse("(Not [\"a\"]) ([\"b\"])", target, paths); + assertThat(complement.matches("\"c\"")).isTrue(); + assertThat(complement.matches("\"a\"")).isFalse(); + var state = + DavIf.parse( + "() (Not [\"a\"])", target, paths); + assertThat( + state.matchesStates( + parts -> new DavIf.ResourceState(null, "urn:busystack:sync:current"))) + .isTrue(); + assertThat(state.matchesStates(parts -> new DavIf.ResourceState("\"a\"", null))).isTrue(); + assertThat(state.matchesStates(parts -> DavIf.ResourceState.ABSENT)).isFalse(); + assertThat( + DavIf.parse("(Not )", target, paths) + .matchesStates(parts -> DavIf.ResourceState.ABSENT)) + .isTrue(); + } + + @Test + void taggedResourceConditionsAreGroupedAndAllResourcesMustMatch() { + var tagged = + DavIf.parse( + " ([\"a\"]) ([\"b\"])", + "/team/dav/calendars/user/events/item.ics", + paths); + assertThat(tagged.matchesAll(parts -> parts.getLast().equals("other.ics") ? "\"a\"" : "\"b\"")) + .isTrue(); + assertThat(tagged.matchesAll(parts -> "\"a\"")).isFalse(); + for (String malformed : + new String[] { + "()", "([\"a\"]", "Not [\"a\"]", "(garbage)", "([\"a\"]) trailing", "([\"a\"]) " + }) + assertThatThrownBy( + () -> DavIf.parse(malformed, "/team/dav/calendars/user/events/item.ics", paths)) + .isInstanceOf(DavException.class); + } + + @Test + void entityTagConditionsTakePrecedenceOverTheirDateAlternatives() { + var modified = java.time.Instant.parse("2020-01-01T12:00:00Z"); + var match = request("If-Match", "\"current\""); + match.addHeader("If-Unmodified-Since", HttpDateValues.format(modified.minusSeconds(1))); + assertThat(HttpConditions.notModified(match, "\"current\"", modified, paths)).isFalse(); + assertThat(HttpConditions.write(match, paths).unmodifiedSince()).isNull(); + var none = request("If-None-Match", "\"different\""); + none.addHeader("If-Modified-Since", HttpDateValues.format(modified.plusSeconds(1))); + assertThat(HttpConditions.notModified(none, "\"current\"", modified, paths)).isFalse(); + } + + @Test + void unmodifiedPreconditionsPrecedeCacheValidationAndDatelessNodesIgnoreDates() { + var modified = java.time.Instant.parse("2020-01-01T12:00:00Z"); + var stale = request("If-Unmodified-Since", HttpDateValues.format(modified.minusSeconds(1))); + stale.addHeader("If-None-Match", "*"); + assertThatThrownBy(() -> HttpConditions.notModified(stale, "\"current\"", modified, paths)) + .isInstanceOf(DavException.class) + .extracting("status") + .isEqualTo(412); + assertThat( + HttpConditions.notModified( + request("If-Modified-Since", HttpDateValues.format(modified)), + "\"current\"", + modified, + paths)) + .isTrue(); + var dates = request("If-Unmodified-Since", HttpDateValues.format(modified.minusSeconds(1))); + dates.addHeader("If-Modified-Since", HttpDateValues.format(modified.plusSeconds(1))); + assertThat(HttpConditions.notModified(dates, null, null, paths)).isFalse(); + } +} diff --git a/src/test/java/org/busystack/caldav/protocol/HttpDateValuesTest.java b/src/test/java/org/busystack/caldav/protocol/HttpDateValuesTest.java new file mode 100644 index 0000000..5401edb --- /dev/null +++ b/src/test/java/org/busystack/caldav/protocol/HttpDateValuesTest.java @@ -0,0 +1,92 @@ +package org.busystack.caldav.protocol; + +import static org.assertj.core.api.Assertions.*; + +import java.time.Clock; +import java.time.Instant; +import java.time.ZoneOffset; +import java.util.stream.Stream; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; +import org.springframework.mock.web.MockHttpServletRequest; + +class HttpDateValuesTest { + private static final String HEADER = "If-Unmodified-Since"; + private static final Clock CLOCK = + Clock.fixed(Instant.parse("2026-10-27T00:00:00Z"), ZoneOffset.UTC); + private static final Instant DATE = Instant.parse("1994-11-06T08:49:37Z"); + + @ParameterizedTest + @ValueSource( + strings = { + "Sun, 06 Nov 1994 08:49:37 GMT", + "Sunday, 06-Nov-94 08:49:37 GMT", + "Sun Nov 6 08:49:37 1994", + " \tSun, 06 Nov 1994 08:49:37 GMT\t " + }) + void acceptsTheThreeHttpDateFormats(String value) { + var request = new MockHttpServletRequest(); + request.addHeader(HEADER, value); + assertThat(HttpDateValues.parse(request, HEADER, CLOCK)).isEqualTo(DATE); + } + + static Stream invalidFields() { + return Stream.of( + Arguments.of(""), + Arguments.of("not a date"), + Arguments.of("Monday, 06-Nov-94 08:49:37 GMT"), + Arguments.of("Sat, 01 Jan 10000 00:00:00 GMT"), + Arguments.of("Sat, 01 Jan +10000 00:00:00 GMT"), + Arguments.of("Sat Jan 1 00:00:00 10000"), + Arguments.of("Sat Jan 1 00:00:00 +10000"), + Arguments.of("Sun, 06 Nov 1994 08:49:37 GMT, Sun, 06 Nov 1994 08:49:37 GMT"), + Arguments.of("Sun Nov 6 08:49:37 1994, Sun Nov 6 08:49:37 1994"), + Arguments.of("Sun, 06 Nov 1994 08:49:37 GMT; ignored=true"), + Arguments.of("Sun, 06 Nov 1994 08:49:37 GMT trailing"), + Arguments.of("Sun, 31 Feb 1994 08:49:37 GMT"), + Arguments.of("Sun, 06 Nov 1994 08:49:37 GMT\r\n"), + Arguments.of("Sun, 06 Nov 1994 08:49:37 GMT\u0000"), + Arguments.of(" ".repeat(129))); + } + + @ParameterizedTest + @MethodSource("invalidFields") + void ignoresInvalidCombinedOrPartiallyParsedValues(String value) { + var request = new MockHttpServletRequest(); + request.addHeader(HEADER, value); + assertThat(HttpDateValues.parse(request, HEADER, CLOCK)).isNull(); + } + + @Test + void absentAndDuplicateFieldsAreIgnoredEvenWhenTheirValuesAgree() { + var request = new MockHttpServletRequest(); + assertThat(HttpDateValues.parse(request, HEADER, CLOCK)).isNull(); + request.addHeader(HEADER, "Sun, 06 Nov 1994 08:49:37 GMT"); + request.addHeader(HEADER, "Sun, 06 Nov 1994 08:49:37 GMT"); + assertThat(HttpDateValues.parse(request, HEADER, CLOCK)).isNull(); + } + + @Test + void canonicalOutputIsGmtImfFixdateAtWholeSecondPrecision() { + assertThat(HttpDateValues.format(DATE.plusNanos(123456789))) + .isEqualTo("Sun, 06 Nov 1994 08:49:37 GMT"); + assertThat(HttpDateValues.seconds(DATE.plusNanos(123456789))).isEqualTo(DATE); + assertThat(HttpDateValues.seconds(null)).isNull(); + assertThat(HttpDateValues.seconds(Instant.parse("1969-12-31T23:59:59.999Z"))) + .isEqualTo(Instant.parse("1969-12-31T23:59:59Z")); + } + + @Test + void obsoleteYearsUseTheHttpFiftyYearRuleRatherThanTheServletDefaultWindow() { + var request = new MockHttpServletRequest(); + request.addHeader(HEADER, "Wednesday, 01-Jan-70 00:00:00 GMT"); + assertThat(HttpDateValues.parse(request, HEADER, CLOCK)) + .isEqualTo(Instant.parse("2070-01-01T00:00:00Z")); + request = new MockHttpServletRequest(); + request.addHeader(HEADER, "Sunday, 06-Nov-94 08:49:37 GMT"); + assertThat(HttpDateValues.parse(request, HEADER, CLOCK)).isEqualTo(DATE); + } +} diff --git a/src/test/java/org/busystack/caldav/protocol/IndependentClientIT.java b/src/test/java/org/busystack/caldav/protocol/IndependentClientIT.java new file mode 100644 index 0000000..4e31a97 --- /dev/null +++ b/src/test/java/org/busystack/caldav/protocol/IndependentClientIT.java @@ -0,0 +1,138 @@ +package org.busystack.caldav.protocol; + +import static org.assertj.core.api.Assertions.*; + +import java.net.ServerSocket; +import java.net.URI; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.time.Instant; +import java.util.concurrent.TimeUnit; +import org.busystack.caldav.account.AccountService; +import org.busystack.caldav.testing.TestTls; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; +import org.testcontainers.postgresql.PostgreSQLContainer; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) +@org.springframework.test.annotation.DirtiesContext( + classMode = org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_CLASS) +@Testcontainers +class IndependentClientIT { + static final int PORT = port(); + + @Container + static final PostgreSQLContainer POSTGRES = + org.busystack.caldav.testing.LoopbackContainers.postgres(); + + @DynamicPropertySource + static void settings(DynamicPropertyRegistry registry) { + registry.add("spring.datasource.url", POSTGRES::getJdbcUrl); + registry.add("spring.datasource.username", POSTGRES::getUsername); + registry.add("spring.datasource.password", POSTGRES::getPassword); + registry.add("server.port", () -> PORT); + registry.add("busystack.public-base-url", () -> "https://localhost:" + PORT + "/team"); + registry.add("busystack.issuer", () -> "https://localhost:" + PORT + "/team"); + registry.add("server.ssl.enabled", () -> true); + registry.add("server.ssl.key-store", () -> TestTls.instance().keyStore()); + registry.add("server.ssl.key-store-password", () -> TestTls.instance().password()); + } + + @Autowired AccountService accounts; + + @Test + void independentPythonCalendarAndTaskClientUsesActualHttpsPostgresqlServer() throws Exception { + assertThat(System.getenv("BUSYSTACK_DAV_ALLOW_SYNC_DEPTH_ONE")) + .as( + "Independent clients must qualify the unmodified default, without an environment override") + .isNull(); + assertThat(System.getProperty("busystack.dav.allow-sync-depth-one")).isNull(); + var id = + accounts.provision( + "independent", "Independent client", "a secure independent password", false); + var credential = + accounts.issueDavCredential( + id, "python-caldav qualification", Instant.now().plusSeconds(3600)); + Path privateDirectory = Files.createTempDirectory("busystack-independent-"); + Files.setPosixFilePermissions( + privateDirectory, java.nio.file.attribute.PosixFilePermissions.fromString("rwx------")); + Path secret = privateDirectory.resolve("secret"), output = privateDirectory.resolve("result"); + try { + Files.writeString(secret, credential.secret(), StandardCharsets.UTF_8); + Files.setPosixFilePermissions( + secret, java.nio.file.attribute.PosixFilePermissions.fromString("rw-------")); + String python = + System.getProperty( + "busystack.client-python", + System.getenv().getOrDefault("BUSYSTACK_CLIENT_PYTHON", "python3")); + ProcessBuilder builder = new ProcessBuilder(python, "qualification/clients/python_caldav.py"); + builder.environment().put("BUSYSTACK_CLIENT_URL", "https://localhost:" + PORT + "/team/dav/"); + builder.environment().put("BUSYSTACK_CLIENT_USER", credential.username()); + builder + .environment() + .put("BUSYSTACK_CLIENT_SYNC_DEPTH_INPUT", "setting omitted; application default"); + builder.environment().put("BUSYSTACK_CLIENT_SECRET_FILE", secret.toString()); + builder + .environment() + .put( + "BUSYSTACK_CLIENT_CA", + Path.of(URI.create(TestTls.instance().keyStore())) + .getParent() + .resolve("ca.pem") + .toString()); + builder.redirectErrorStream(true).redirectOutput(output.toFile()); + var process = builder.start(); + boolean finished = process.waitFor(180, TimeUnit.SECONDS); + if (!finished) process.destroyForcibly(); + assertThat(finished).as("Independent client timed out").isTrue(); + String result = Files.readString(output); + assertThat(process.exitValue()) + .withFailMessage( + "Independent client failed. Install pinned qualification/clients/requirements.txt; this required test does not skip missing Python/dependencies. Output:%n%s", + result) + .isZero(); + assertThat(result).contains("\"status\": \"passed\"", "\"version\": \"3.6.0\""); + Files.createDirectories(Path.of("target/qualification")); + Files.writeString(Path.of("target/qualification/python-caldav.json"), result); + String desktop = + System.getProperty( + "busystack.thunderbird-binary", System.getenv("BUSYSTACK_THUNDERBIRD_BINARY")); + assertThat(desktop) + .as("Required native Thunderbird prerequisite: configure BUSYSTACK_THUNDERBIRD_BINARY") + .isNotBlank(); + assertThat(Files.isExecutable(Path.of(desktop))) + .as("Configured Thunderbird prerequisite") + .isTrue(); + builder.command(python, "qualification/clients/thunderbird.py"); + builder.environment().put("BUSYSTACK_THUNDERBIRD_BINARY", desktop); + var desktopProcess = builder.start(); + boolean desktopFinished = desktopProcess.waitFor(360, TimeUnit.SECONDS); + if (!desktopFinished) desktopProcess.destroyForcibly(); + assertThat(desktopFinished).as("Desktop-client qualification timed out").isTrue(); + String desktopResult = Files.readString(output); + assertThat(desktopProcess.exitValue()) + .withFailMessage("Desktop-client qualification failed:%n%s", desktopResult) + .isZero(); + assertThat(desktopResult).contains("\"status\": \"passed\"", "\"client\": \"Thunderbird\""); + Files.writeString(Path.of("target/qualification/thunderbird.json"), desktopResult); + } finally { + Files.deleteIfExists(secret); + Files.deleteIfExists(output); + Files.deleteIfExists(privateDirectory); + } + } + + private static int port() { + try (var socket = new ServerSocket(0, 1, java.net.InetAddress.getLoopbackAddress())) { + return socket.getLocalPort(); + } catch (java.io.IOException ex) { + throw new IllegalStateException("No isolated test port", ex); + } + } +} diff --git a/src/test/java/org/busystack/caldav/protocol/ObservanceUntilStrictIT.java b/src/test/java/org/busystack/caldav/protocol/ObservanceUntilStrictIT.java new file mode 100644 index 0000000..1093ef3 --- /dev/null +++ b/src/test/java/org/busystack/caldav/protocol/ObservanceUntilStrictIT.java @@ -0,0 +1,107 @@ +package org.busystack.caldav.protocol; + +import static org.assertj.core.api.Assertions.*; + +import java.net.URI; +import java.net.http.*; +import java.nio.charset.StandardCharsets; +import java.time.Instant; +import java.util.*; +import org.busystack.caldav.account.AccountService; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.server.LocalServerPort; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.*; +import org.testcontainers.junit.jupiter.*; +import org.testcontainers.postgresql.PostgreSQLContainer; + +@SpringBootTest( + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, + properties = { + "busystack.public-base-url=https://localhost", + "busystack.issuer=https://localhost", + "busystack.dav.repair-local-observance-until=false" + }) +@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS) +@Testcontainers +class ObservanceUntilStrictIT { + @Container + static final PostgreSQLContainer POSTGRES = + org.busystack.caldav.testing.LoopbackContainers.postgres(); + + @DynamicPropertySource + static void database(DynamicPropertyRegistry registry) { + registry.add("spring.datasource.url", POSTGRES::getJdbcUrl); + registry.add("spring.datasource.username", POSTGRES::getUsername); + registry.add("spring.datasource.password", POSTGRES::getPassword); + registry.add("server.ssl.enabled", () -> true); + registry.add( + "server.ssl.key-store", () -> org.busystack.caldav.testing.TestTls.instance().keyStore()); + registry.add( + "server.ssl.key-store-password", + () -> org.busystack.caldav.testing.TestTls.instance().password()); + } + + @LocalServerPort int port; + @Autowired AccountService accounts; + @Autowired org.busystack.caldav.storage.CalendarStore calendars; + @Autowired org.springframework.jdbc.core.JdbcTemplate jdbc; + + @Test + void explicitDisableRetainsOriginalRejectionAndAtomicStateOverHttps() throws Exception { + UUID owner = + accounts.provision("strict-repair", "Strict repair", "a disposable strict password", false); + UUID collection = calendars.createCollection(owner, "Strict", "VEVENT"); + String token = calendars.currentSyncToken(owner, collection); + String credential = + accounts.issueDavCredential(owner, "strict", Instant.now().plusSeconds(3600)).secret(); + String authorization = + "Basic " + + Base64.getEncoder() + .encodeToString(("strict-repair:" + credential).getBytes(StandardCharsets.UTF_8)); + var http = + HttpClient.newBuilder() + .sslContext(org.busystack.caldav.testing.TestTls.instance().context()) + .followRedirects(HttpClient.Redirect.NEVER) + .build(); + for (String zone : List.of("vancouver", "berlin")) { + byte[] original; + try (var input = + getClass() + .getResourceAsStream("/calendar/observance-until/thunderbird159-" + zone + ".ics")) { + original = Objects.requireNonNull(input).readAllBytes(); + } + var uri = + URI.create( + "https://localhost:" + + port + + "/dav/calendars/" + + owner + + "/" + + collection + + "/" + + zone + + ".ics"); + var response = + http.send( + HttpRequest.newBuilder(uri) + .header("Authorization", authorization) + .header("Content-Type", "text/calendar") + .PUT(HttpRequest.BodyPublishers.ofByteArray(original)) + .build(), + HttpResponse.BodyHandlers.ofString()); + assertThat(response.statusCode()).isEqualTo(403); + assertThat(response.body()).contains("valid-calendar-data"); + assertThat(calendars.get(owner, collection, zone + ".ics")).isNull(); + assertThat(calendars.currentSyncToken(owner, collection)).isEqualTo(token); + assertThat( + jdbc.queryForObject( + "SELECT count(*) FROM calendar_resource_versions WHERE collection_id=?", + Long.class, + collection)) + .isZero(); + } + } +} diff --git a/src/test/java/org/busystack/caldav/security/DavBearerAuthenticationEntryPointTest.java b/src/test/java/org/busystack/caldav/security/DavBearerAuthenticationEntryPointTest.java new file mode 100644 index 0000000..860b490 --- /dev/null +++ b/src/test/java/org/busystack/caldav/security/DavBearerAuthenticationEntryPointTest.java @@ -0,0 +1,79 @@ +package org.busystack.caldav.security; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.time.Instant; +import java.util.List; +import java.util.Map; +import java.util.Set; +import org.junit.jupiter.api.Test; +import org.springframework.http.HttpStatus; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.security.access.AccessDeniedException; +import org.springframework.security.authentication.BadCredentialsException; +import org.springframework.security.core.authority.SimpleGrantedAuthority; +import org.springframework.security.oauth2.core.DefaultOAuth2AuthenticatedPrincipal; +import org.springframework.security.oauth2.core.OAuth2AccessToken; +import org.springframework.security.oauth2.core.OAuth2AuthenticationException; +import org.springframework.security.oauth2.server.resource.BearerTokenError; +import org.springframework.security.oauth2.server.resource.authentication.BearerTokenAuthentication; +import org.springframework.security.oauth2.server.resource.web.access.BearerTokenAccessDeniedHandler; + +class DavBearerAuthenticationEntryPointTest { + @Test + void statusAndRequiredErrorParametersAreRetainedWithoutUnimplementedOptionalMetadata() + throws Exception { + var response = new MockHttpServletResponse(); + new DavBearerAuthenticationEntryPoint() + .commence( + new MockHttpServletRequest(), + response, + new OAuth2AuthenticationException( + new BearerTokenError( + "invalid_request", + HttpStatus.BAD_REQUEST, + "Duplicate credential", + "https://example.test/error", + "dav"))); + assertThat(response.getStatus()).isEqualTo(400); + assertThat(response.getHeader("WWW-Authenticate")) + .isEqualTo( + "Bearer error=\"invalid_request\", error_description=\"Duplicate credential\", error_uri=\"https://example.test/error\", scope=\"dav\""); + var ordinary = new MockHttpServletResponse(); + new DavBearerAuthenticationEntryPoint() + .commence( + new MockHttpServletRequest(), + ordinary, + new BadCredentialsException("untrusted detail")); + assertThat(ordinary.getStatus()).isEqualTo(401); + assertThat(ordinary.getHeader("WWW-Authenticate")).isEqualTo("Bearer"); + } + + @Test + void exactConfiguredFrameworkAccessDeniedHandlerDoesNotAdvertiseResourceMetadata() + throws Exception { + var request = new MockHttpServletRequest(); + List authorities = + List.of(new SimpleGrantedAuthority("SCOPE_dav")); + var principal = + new DefaultOAuth2AuthenticatedPrincipal("actor", Map.of("sub", "actor"), authorities); + request.setUserPrincipal( + new BearerTokenAuthentication( + principal, + new OAuth2AccessToken( + OAuth2AccessToken.TokenType.BEARER, + "fixture", + Instant.now(), + Instant.now().plusSeconds(60), + Set.of("dav")), + authorities)); + var response = new MockHttpServletResponse(); + new BearerTokenAccessDeniedHandler() + .handle(request, response, new AccessDeniedException("Denied")); + assertThat(response.getStatus()).isEqualTo(403); + assertThat(response.getHeader("WWW-Authenticate")) + .contains("insufficient_scope") + .doesNotContain("resource_metadata"); + } +} diff --git a/src/test/java/org/busystack/caldav/security/DavCredentialIT.java b/src/test/java/org/busystack/caldav/security/DavCredentialIT.java new file mode 100644 index 0000000..df089f1 --- /dev/null +++ b/src/test/java/org/busystack/caldav/security/DavCredentialIT.java @@ -0,0 +1,120 @@ +package org.busystack.caldav.security; + +import static org.assertj.core.api.Assertions.*; +import static org.mockito.Mockito.*; + +import java.time.Instant; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import org.busystack.caldav.account.AccountService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; +import org.springframework.test.context.bean.override.mockito.MockitoSpyBean; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; +import org.testcontainers.postgresql.PostgreSQLContainer; + +@SpringBootTest( + webEnvironment = SpringBootTest.WebEnvironment.NONE, + properties = { + "busystack.public-base-url=https://calendar.example", + "busystack.issuer=https://calendar.example" + }) +@org.springframework.test.annotation.DirtiesContext( + classMode = org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_CLASS) +@Testcontainers +class DavCredentialIT { + @Container + static final PostgreSQLContainer POSTGRES = + org.busystack.caldav.testing.LoopbackContainers.postgres(); + + @DynamicPropertySource + static void database(DynamicPropertyRegistry registry) { + registry.add("spring.datasource.url", POSTGRES::getJdbcUrl); + registry.add("spring.datasource.username", POSTGRES::getUsername); + registry.add("spring.datasource.password", POSTGRES::getPassword); + } + + @Autowired AccountService accounts; + @Autowired JdbcTemplate jdbc; + @MockitoSpyBean PasswordEncoder passwords; + UUID account; + AccountService.IssuedCredential issued; + + @BeforeEach + void prepare() { + org.busystack.caldav.testing.FixtureDatabase.clearAccounts(jdbc); + account = accounts.provision("dav-user", "DAV user", "a secure human account password", false); + issued = accounts.issueDavCredential(account, "Device", Instant.now().plusSeconds(3600)); + } + + private String stored() { + return jdbc.queryForObject( + "SELECT verifier FROM dav_credentials WHERE id=?", String.class, issued.id()); + } + + @Test + void issuanceUsesRandomTokenVerifierWhileHumanPasswordsStayAdaptiveAndRevocationIsImmediate() { + assertThat(accounts.find(account).passwordHash()).startsWith("$2"); + assertThat(stored()) + .startsWith(DavSecretVerifier.PREFIX) + .doesNotContain(issued.secret().substring(37)); + assertThat(accounts.verifyDav("dav-user", issued.secret())).isTrue(); + assertThat(accounts.verifyDav("dav-user", issued.id() + "." + "x".repeat(43))).isFalse(); + accounts.revokeDav(account, issued.id()); + assertThat(accounts.verifyDav("dav-user", issued.secret())).isFalse(); + } + + @Test + void validLegacyBcryptCredentialUpgradesOnlyItsVerifier() { + String legacy = passwords.encode(issued.secret().substring(37)); + jdbc.update("UPDATE dav_credentials SET verifier=? WHERE id=?", legacy, issued.id()); + assertThat(accounts.verifyDav("dav-user", issued.id() + "." + "x".repeat(43))).isFalse(); + assertThat(stored()).isEqualTo(legacy); + assertThat(accounts.verifyDav("dav-user", issued.secret())).isTrue(); + assertThat(stored()).startsWith(DavSecretVerifier.PREFIX); + assertThat(accounts.verifyDav("dav-user", issued.secret())).isTrue(); + } + + @Test + void revocationDuringLegacyVerificationCannotBeUndoneByVerifierUpgrade() throws Exception { + String secret = issued.secret().substring(37), legacy = passwords.encode(secret); + jdbc.update("UPDATE dav_credentials SET verifier=? WHERE id=?", legacy, issued.id()); + var verified = new CountDownLatch(1); + var proceed = new CountDownLatch(1); + doAnswer( + invocation -> { + boolean result = (boolean) invocation.callRealMethod(); + verified.countDown(); + if (!proceed.await(10, TimeUnit.SECONDS)) + throw new AssertionError("Legacy verification barrier timed out"); + return result; + }) + .when(passwords) + .matches(secret, legacy); + var attempt = + CompletableFuture.supplyAsync(() -> accounts.verifyDav("dav-user", issued.secret())); + try { + assertThat(verified.await(10, TimeUnit.SECONDS)).isTrue(); + accounts.revokeDav(account, issued.id()); + } finally { + proceed.countDown(); + } + assertThat(attempt.get(10, TimeUnit.SECONDS)).isFalse(); + assertThat(stored()).isEqualTo(legacy); + assertThat( + jdbc.queryForObject( + "SELECT revoked_at IS NOT NULL FROM dav_credentials WHERE id=?", + Boolean.class, + issued.id())) + .isTrue(); + } +} diff --git a/src/test/java/org/busystack/caldav/security/DavOriginFilterTest.java b/src/test/java/org/busystack/caldav/security/DavOriginFilterTest.java new file mode 100644 index 0000000..c80ad49 --- /dev/null +++ b/src/test/java/org/busystack/caldav/security/DavOriginFilterTest.java @@ -0,0 +1,51 @@ +package org.busystack.caldav.security; + +import static org.assertj.core.api.Assertions.*; + +import java.net.URI; +import org.busystack.caldav.config.DeploymentProperties; +import org.junit.jupiter.api.Test; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpServletResponse; + +class DavOriginFilterTest { + final DavOriginFilter filter = + new DavOriginFilter( + new DeploymentProperties( + URI.create("https://calendar.example/team"), + URI.create("https://calendar.example/team"), + false)); + + private boolean allowed(String origin, String site) throws Exception { + var request = new MockHttpServletRequest("PROPFIND", "/team/dav/"); + if (origin != null) request.addHeader("Origin", origin); + if (site != null) request.addHeader("Sec-Fetch-Site", site); + var response = new MockHttpServletResponse(); + var passed = new java.util.concurrent.atomic.AtomicBoolean(); + filter.doFilter(request, response, (req, res) -> passed.set(true)); + return passed.get(); + } + + @Test + void ordinaryAndNativeSystemRequestsAndConfiguredSameOriginAreAllowed() throws Exception { + assertThat(allowed(null, null)).isTrue(); + assertThat(allowed(null, "none")).isTrue(); + assertThat(allowed("https://calendar.example", "same-origin")).isTrue(); + assertThat(allowed("https://CALENDAR.example:443", null)).isTrue(); + } + + @Test + void foreignOpaqueSiblingOriginsAndCrossSiteMetadataAreRejected() throws Exception { + for (String origin : + new String[] { + "https://attacker.invalid", + "https://sibling.calendar.example", + "https://calendar.example:444", + "http://calendar.example", + "null", + "https://calendar.example/path" + }) assertThat(allowed(origin, "none")).as(origin).isFalse(); + for (String site : new String[] {"same-site", "cross-site", "invalid"}) + assertThat(allowed("https://calendar.example", site)).as(site).isFalse(); + } +} diff --git a/src/test/java/org/busystack/caldav/security/DavSecretVerifierTest.java b/src/test/java/org/busystack/caldav/security/DavSecretVerifierTest.java new file mode 100644 index 0000000..2e08b96 --- /dev/null +++ b/src/test/java/org/busystack/caldav/security/DavSecretVerifierTest.java @@ -0,0 +1,29 @@ +package org.busystack.caldav.security; + +import static org.assertj.core.api.Assertions.*; + +import java.util.UUID; +import org.junit.jupiter.api.Test; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; + +class DavSecretVerifierTest { + final DavSecretVerifier verifier = new DavSecretVerifier(new BCryptPasswordEncoder(12)); + + @Test + void randomSecretVerifiersAreVersionedDomainAndCredentialBoundAndRejectUnknownFormats() { + UUID id = UUID.randomUUID(); + String secret = "test-only-random-secret-256-bits-placeholder"; + String stored = verifier.encode(id, secret); + assertThat(stored).startsWith(DavSecretVerifier.PREFIX).doesNotContain(secret); + assertThat(verifier.matches(id, secret, stored)).isTrue(); + assertThat(verifier.matches(UUID.randomUUID(), secret, stored)).isFalse(); + assertThat(verifier.matches(id, secret + "wrong", stored)).isFalse(); + for (String malformed : + new String[] { + "{dav-sha256-v2}abc", + DavSecretVerifier.PREFIX + "x".repeat(64), + DavSecretVerifier.PREFIX + "abc", + "{noop}" + secret + }) assertThat(verifier.matches(id, secret, malformed)).isFalse(); + } +} diff --git a/src/test/java/org/busystack/caldav/security/RecentAuthenticationTest.java b/src/test/java/org/busystack/caldav/security/RecentAuthenticationTest.java new file mode 100644 index 0000000..bfb67e9 --- /dev/null +++ b/src/test/java/org/busystack/caldav/security/RecentAuthenticationTest.java @@ -0,0 +1,86 @@ +package org.busystack.caldav.security; + +import static org.assertj.core.api.Assertions.*; + +import java.time.Instant; +import java.util.List; +import org.junit.jupiter.api.Test; +import org.springframework.security.access.AccessDeniedException; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.authority.FactorGrantedAuthority; +import org.springframework.security.core.authority.SimpleGrantedAuthority; + +class RecentAuthenticationTest { + @Test + void acceptsOnlyVerifiedUpstreamPrimaryProof() { + var code = + FactorGrantedAuthority.fromAuthority(FactorGrantedAuthority.AUTHORIZATION_CODE_AUTHORITY); + var passkey = FactorGrantedAuthority.fromAuthority(FactorGrantedAuthority.WEBAUTHN_AUTHORITY); + var marker = new SimpleGrantedAuthority("BUSYSTACK_TRUSTED_UPSTREAM"); + assertThatCode( + () -> + RecentAuthentication.requireMfa( + UsernamePasswordAuthenticationToken.authenticated( + "user", null, List.of(code, marker, passkey)))) + .doesNotThrowAnyException(); + assertThatThrownBy( + () -> + RecentAuthentication.requireMfa( + UsernamePasswordAuthenticationToken.authenticated( + "user", null, List.of(code, passkey)))) + .isInstanceOf(AccessDeniedException.class); + assertThatThrownBy( + () -> + RecentAuthentication.requireMfa( + UsernamePasswordAuthenticationToken.authenticated( + "user", + null, + List.of( + new SimpleGrantedAuthority( + FactorGrantedAuthority.AUTHORIZATION_CODE_AUTHORITY), + marker, + passkey)))) + .isInstanceOf(AccessDeniedException.class); + } + + @Test + void requiresBothRealRecentFactors() { + var password = FactorGrantedAuthority.fromAuthority(FactorGrantedAuthority.PASSWORD_AUTHORITY); + var passkey = FactorGrantedAuthority.fromAuthority(FactorGrantedAuthority.WEBAUTHN_AUTHORITY); + var complete = + UsernamePasswordAuthenticationToken.authenticated("user", null, List.of(password, passkey)); + assertThatCode(() -> RecentAuthentication.requireMfa(complete)).doesNotThrowAnyException(); + var passwordOnly = + UsernamePasswordAuthenticationToken.authenticated("user", null, List.of(password)); + assertThatThrownBy(() -> RecentAuthentication.requireMfa(passwordOnly)) + .isInstanceOf(AccessDeniedException.class); + var fake = + UsernamePasswordAuthenticationToken.authenticated( + "user", + null, + List.of( + password, new SimpleGrantedAuthority(FactorGrantedAuthority.WEBAUTHN_AUTHORITY))); + assertThatThrownBy(() -> RecentAuthentication.requireMfa(fake)) + .isInstanceOf(AccessDeniedException.class); + var expired = + FactorGrantedAuthority.withAuthority(FactorGrantedAuthority.WEBAUTHN_AUTHORITY) + .issuedAt(Instant.now().minusSeconds(601)) + .build(); + assertThatThrownBy( + () -> + RecentAuthentication.requireMfa( + UsernamePasswordAuthenticationToken.authenticated( + "user", null, List.of(password, expired)))) + .isInstanceOf(AccessDeniedException.class); + var future = + FactorGrantedAuthority.withAuthority(FactorGrantedAuthority.WEBAUTHN_AUTHORITY) + .issuedAt(Instant.now().plusSeconds(60)) + .build(); + assertThatThrownBy( + () -> + RecentAuthentication.requireMfa( + UsernamePasswordAuthenticationToken.authenticated( + "user", null, List.of(password, future)))) + .isInstanceOf(AccessDeniedException.class); + } +} diff --git a/src/test/java/org/busystack/caldav/security/SecurityBoundaryIT.java b/src/test/java/org/busystack/caldav/security/SecurityBoundaryIT.java new file mode 100644 index 0000000..0a4c527 --- /dev/null +++ b/src/test/java/org/busystack/caldav/security/SecurityBoundaryIT.java @@ -0,0 +1,263 @@ +package org.busystack.caldav.security; + +import static org.assertj.core.api.Assertions.*; + +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.nio.charset.StandardCharsets; +import java.time.Instant; +import java.util.Base64; +import org.busystack.caldav.account.AccountService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.server.LocalServerPort; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; +import org.testcontainers.postgresql.PostgreSQLContainer; + +@SpringBootTest( + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, + properties = { + "busystack.public-base-url=https://calendar.example/team", + "busystack.issuer=https://calendar.example/team" + }) +@org.springframework.test.annotation.DirtiesContext( + classMode = org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_CLASS) +@Testcontainers +class SecurityBoundaryIT { + @Container + static final PostgreSQLContainer POSTGRES = + org.busystack.caldav.testing.LoopbackContainers.postgres(); + + @DynamicPropertySource + static void database(DynamicPropertyRegistry registry) { + registry.add("spring.datasource.url", POSTGRES::getJdbcUrl); + registry.add("spring.datasource.username", POSTGRES::getUsername); + registry.add("spring.datasource.password", POSTGRES::getPassword); + registry.add("server.ssl.enabled", () -> true); + registry.add( + "server.ssl.key-store", () -> org.busystack.caldav.testing.TestTls.instance().keyStore()); + registry.add( + "server.ssl.key-store-password", + () -> org.busystack.caldav.testing.TestTls.instance().password()); + } + + @LocalServerPort int port; + @Autowired AccountService accounts; + @Autowired JdbcTemplate jdbc; + AccountService.IssuedCredential credential; + java.util.UUID accountId; + final HttpClient client = + HttpClient.newBuilder() + .sslContext(org.busystack.caldav.testing.TestTls.instance().context()) + .followRedirects(HttpClient.Redirect.NEVER) + .build(); + + @BeforeEach + void prepare() { + org.busystack.caldav.testing.FixtureDatabase.clearAccounts(jdbc); + accountId = accounts.provision("user", "Test User", "correct horse battery staple", false); + credential = + accounts.issueDavCredential(accountId, "DAV test", Instant.now().plusSeconds(3600)); + } + + private HttpRequest.Builder request(String path) { + return HttpRequest.newBuilder(URI.create("https://127.0.0.1:" + port + "/team" + path)); + } + + private String basic(String secret) { + return "Basic " + + Base64.getEncoder().encodeToString(("user:" + secret).getBytes(StandardCharsets.UTF_8)); + } + + private HttpResponse send(HttpRequest.Builder builder) throws Exception { + return client.send(builder.build(), HttpResponse.BodyHandlers.ofString()); + } + + @Test + void exactDavMethodsPassFirewallAndChallengeWithoutHtmlRedirect() throws Exception { + for (String method : + new String[] { + "OPTIONS", + "PROPFIND", + "PROPPATCH", + "REPORT", + "MKCALENDAR", + "MKCOL", + "COPY", + "MOVE", + "ACL", + "PUT", + "DELETE", + "GET", + "HEAD" + }) { + var response = send(request("/dav/").method(method, HttpRequest.BodyPublishers.noBody())); + assertThat(response.statusCode()).as(method).isEqualTo(401); + assertThat(response.headers().firstValue("WWW-Authenticate")) + .hasValue("Basic realm=\"BusyStack DAV\", charset=\"UTF-8\""); + assertThat(response.headers().firstValue("Location")).isEmpty(); + } + assertThat( + send(request("/dav/").method("TRACE", HttpRequest.BodyPublishers.noBody())) + .statusCode()) + .isEqualTo(405); + } + + @Test + void primaryPasswordCannotAuthenticateDavAndDavCredentialCannotAccessBrowser() throws Exception { + assertThat( + send(request("/dav/missing") + .header("Authorization", basic("correct horse battery staple"))) + .statusCode()) + .isEqualTo(401); + var dav = send(request("/dav/missing").header("Authorization", basic(credential.secret()))); + // No protocol adapter is registered yet. Authentication success reaches normal MVC routing. + assertThat(dav.statusCode()).isEqualTo(404); + assertThat( + send(request("/admin").header("Authorization", basic(credential.secret()))) + .statusCode()) + .isEqualTo(302); + accounts.revokeDav(accountId, credential.id()); + assertThat( + send(request("/dav/missing").header("Authorization", basic(credential.secret()))) + .statusCode()) + .isEqualTo(401); + } + + @Test + void browserCsrfAndDavOriginBoundaryAreEnforced() throws Exception { + assertThat(send(request("/logout").POST(HttpRequest.BodyPublishers.noBody())).statusCode()) + .isEqualTo(403); + assertThat( + send(request("/dav/missing") + .header("Authorization", basic(credential.secret())) + .header("Origin", "https://evil.example") + .PUT(HttpRequest.BodyPublishers.ofString("data"))) + .statusCode()) + .isEqualTo(403); + assertThat( + send(request("/dav/missing") + .header("Authorization", basic(credential.secret())) + .header("Sec-Fetch-Site", "cross-site") + .method("DELETE", HttpRequest.BodyPublishers.noBody())) + .statusCode()) + .isEqualTo(403); + } + + @Test + void configuredDiscoveryIgnoresForwardedHeadersAndDoesNotAdvertiseCapabilities() + throws Exception { + var response = + send( + request("/.well-known/caldav") + .header("Forwarded", "host=evil.example;proto=http") + .header("X-Forwarded-Host", "evil.example") + .header("X-Forwarded-Prefix", "/evil")); + assertThat(response.statusCode()).isEqualTo(308); + assertThat(response.headers().firstValue("Location")) + .hasValue("https://calendar.example/team/dav/"); + assertThat(response.headers().firstValue("DAV")).isEmpty(); + } + + @Test + void bearerErrorsRetainTheirChallengeWithoutAdvertisingUnavailableResourceMetadata() + throws Exception { + var rejected = + send(request("/dav/").header("Authorization", "Bearer independent-invalid-token")); + assertThat(rejected.statusCode()).isEqualTo(401); + assertThat(rejected.headers().firstValue("WWW-Authenticate").orElseThrow()) + .startsWith("Bearer ") + .contains("error=\"invalid_token\"") + .doesNotContain("resource_metadata", "tls_client_certificate_bound_access_tokens"); + assertThat(rejected.headers().firstValue("Location")).isEmpty(); + var malformed = send(request("/dav/").header("Authorization", "Bearer not a permitted token")); + assertThat(malformed.statusCode()).isEqualTo(401); + assertThat(malformed.headers().firstValue("WWW-Authenticate").orElseThrow()) + .startsWith("Bearer ") + .doesNotContain("resource_metadata"); + var metadata = send(request("/.well-known/oauth-authorization-server")); + assertThat(metadata.statusCode()).isEqualTo(200); + assertThat(metadata.body()).contains("https://calendar.example/team", "S256"); + assertThat(metadata.body()) + .doesNotContain( + "protected_resource_metadata", "tls_client_certificate_bound_access_tokens"); + } + + @Test + void savedLoginRedirectUsesConfiguredOriginWithAnUntrustedHostHeader() throws Exception { + String first; + try (var socket = + (javax.net.ssl.SSLSocket) + org.busystack.caldav.testing.TestTls.instance() + .context() + .getSocketFactory() + .createSocket("localhost", port)) { + var parameters = socket.getSSLParameters(); + parameters.setEndpointIdentificationAlgorithm("HTTPS"); + socket.setSSLParameters(parameters); + socket.setSoTimeout(15000); + socket + .getOutputStream() + .write( + ("GET /team/admin HTTP/1.1\r\nHost: attacker.invalid\r\nAccept: text/html\r\nConnection: close\r\n\r\n") + .getBytes(StandardCharsets.US_ASCII)); + first = new String(socket.getInputStream().readAllBytes(), StandardCharsets.UTF_8); + } + assertThat(first) + .startsWith("HTTP/1.1 302") + .doesNotContain("Location: https://attacker.invalid"); + String cookie = + first + .lines() + .filter( + line -> + line.toLowerCase(java.util.Locale.ROOT).startsWith("set-cookie: jsessionid=")) + .findFirst() + .orElseThrow() + .substring("Set-Cookie: ".length()) + .split(";", 2)[0]; + var login = send(request("/login").header("Cookie", cookie)); + var csrf = new java.util.concurrent.atomic.AtomicReference(); + new javax.swing.text.html.parser.ParserDelegator() + .parse( + new java.io.StringReader(login.body()), + new javax.swing.text.html.HTMLEditorKit.ParserCallback() { + @Override + public void handleSimpleTag( + javax.swing.text.html.HTML.Tag tag, + javax.swing.text.MutableAttributeSet attributes, + int position) { + if (tag == javax.swing.text.html.HTML.Tag.INPUT + && "_csrf" + .equals(attributes.getAttribute(javax.swing.text.html.HTML.Attribute.NAME))) + csrf.set( + (String) attributes.getAttribute(javax.swing.text.html.HTML.Attribute.VALUE)); + } + }, + true); + assertThat(csrf.get()).isNotBlank(); + String body = + "username=user&password=" + + java.net.URLEncoder.encode("correct horse battery staple", StandardCharsets.UTF_8) + + "&_csrf=" + + java.net.URLEncoder.encode(csrf.get(), StandardCharsets.UTF_8); + var authenticated = + send( + request("/login") + .header("Cookie", cookie) + .header("Content-Type", "application/x-www-form-urlencoded") + .POST(HttpRequest.BodyPublishers.ofString(body))); + assertThat(authenticated.statusCode()).isEqualTo(302); + assertThat(authenticated.headers().firstValue("Location").orElseThrow()) + .startsWith("https://calendar.example/team/admin") + .doesNotContain("attacker.invalid"); + } +} diff --git a/src/test/java/org/busystack/caldav/security/oauth/OAuthHttpIT.java b/src/test/java/org/busystack/caldav/security/oauth/OAuthHttpIT.java new file mode 100644 index 0000000..f0c7cf5 --- /dev/null +++ b/src/test/java/org/busystack/caldav/security/oauth/OAuthHttpIT.java @@ -0,0 +1,1271 @@ +package org.busystack.caldav.security.oauth; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.net.URI; +import java.net.URLEncoder; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.Principal; +import java.time.Duration; +import java.time.Instant; +import java.util.Base64; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; +import org.busystack.caldav.account.AccountService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.boot.test.web.server.LocalServerPort; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Import; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.authority.FactorGrantedAuthority; +import org.springframework.security.core.authority.SimpleGrantedAuthority; +import org.springframework.security.core.userdetails.User; +import org.springframework.security.crypto.keygen.Base64StringKeyGenerator; +import org.springframework.security.oauth2.core.AuthorizationGrantType; +import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest; +import org.springframework.security.oauth2.server.authorization.OAuth2Authorization; +import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationCode; +import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService; +import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; +import org.testcontainers.postgresql.PostgreSQLContainer; +import tools.jackson.databind.JsonNode; +import tools.jackson.databind.json.JsonMapper; + +/** + * Seeds completed interactive grants; tests actual HTTP token/DAV boundaries, not WebAuthn login. + */ +@SpringBootTest( + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, + properties = { + "busystack.public-base-url=https://calendar.example", + "busystack.issuer=https://calendar.example", + "server.servlet.session.cookie.secure=false" + }) +@Import(OAuthHttpIT.ProbeConfiguration.class) +@org.springframework.test.annotation.DirtiesContext( + classMode = org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_CLASS) +@Testcontainers +class OAuthHttpIT { + @Container + static final PostgreSQLContainer POSTGRES = + org.busystack.caldav.testing.LoopbackContainers.postgres(); + + @DynamicPropertySource + static void database(DynamicPropertyRegistry registry) { + registry.add("spring.datasource.url", POSTGRES::getJdbcUrl); + registry.add("spring.datasource.username", POSTGRES::getUsername); + registry.add("spring.datasource.password", POSTGRES::getPassword); + } + + private static final String CALLBACK = "http://127.0.0.1:34567/callback"; + private static final String VERIFIER = + "valid-code-verifier-for-native-client-01234567890123456789"; + private final HttpClient http = + HttpClient.newBuilder() + .connectTimeout(Duration.ofSeconds(10)) + .followRedirects(HttpClient.Redirect.NEVER) + .build(); + private final JsonMapper json = JsonMapper.builder().build(); + @Autowired AccountService accounts; + @Autowired OAuthClientService clients; + @Autowired RegisteredClientRepository registrations; + @Autowired OAuth2AuthorizationService authorizations; + @Autowired RefreshFamilyStore families; + @Autowired JdbcTemplate jdbc; + @Autowired org.springframework.transaction.PlatformTransactionManager transactions; + @Autowired org.busystack.caldav.security.session.ClientAccessRevocation clientAccess; + @LocalServerPort int port; + UUID administrator, account; + + @BeforeEach + void prepare() { + jdbc.execute( + "TRUNCATE oauth2_authorization_consent,oauth2_authorization,oauth2_registered_client CASCADE"); + org.busystack.caldav.testing.FixtureDatabase.clearAccounts(jdbc); + administrator = + accounts.provision( + "oauth-admin", "OAuth administrator", "correct horse battery staple", true); + account = accounts.provision("oauth-user", "OAuth user", "correct horse battery staple", false); + clients.registerNative(administrator, "native-http", "Native HTTP test", Set.of(CALLBACK)); + } + + @Test + void publicPkceIssuesScopedOpaqueTokensAndRefreshReplayRevokesDescendants() throws Exception { + var initial = exchange(seedCode()); + assertThat(initial.statusCode()).isEqualTo(200); + assertThat(initial.headers().firstValue("Cache-Control").orElseThrow()).contains("no-store"); + assertThat(initial.headers().firstValue("Pragma")).hasValue("no-cache"); + assertThat(initial.headers().firstValue("Content-Type").orElseThrow()) + .startsWith("application/json"); + JsonNode first = json.readTree(initial.body()); + assertThat(first.path("token_type").asString()).isEqualTo("Bearer"); + assertThat(first.path("expires_in").asLong()).isPositive(); + String firstAccess = first.path("access_token").asString(); + String firstRefresh = first.path("refresh_token").asString(); + assertThat(firstAccess).doesNotContain("."); + assertThat(dav(firstAccess).statusCode()).isEqualTo(200); + assertThat(dav(firstAccess).body()).isEqualTo(account.toString()); + var refreshed = refresh(firstRefresh); + assertThat(refreshed.statusCode()).isEqualTo(200); + JsonNode second = json.readTree(refreshed.body()); + String secondRefresh = second.path("refresh_token").asString(); + String secondAccess = second.path("access_token").asString(); + assertThat(secondRefresh).isNotEqualTo(firstRefresh); + assertThat(dav(firstAccess).statusCode()).isEqualTo(401); + assertThat(dav(secondAccess).statusCode()).isEqualTo(200); + var replay = refresh(firstRefresh); + assertThat(replay.statusCode()).isEqualTo(400); + assertThat(replay.body()).contains("invalid_grant"); + assertThat(dav(secondAccess).statusCode()).isEqualTo(401); + assertThat(refresh(secondRefresh).statusCode()).isEqualTo(400); + } + + @Test + void emptyOptionalSecretRefreshesAndRejectsInvalidGrantsWithoutBrowserRedirects() + throws Exception { + var softly = new org.assertj.core.api.SoftAssertions(); + for (String suffix : + List.of( + "", + "&client_secret=", + "&client_secret=&unrecognized-extension=one&unrecognized-extension=two")) { + var issuedResponse = exchange(seedCode()); + assertThat(issuedResponse.statusCode()).isEqualTo(200); + JsonNode issued = json.readTree(issuedResponse.body()); + String oldAccess = issued.path("access_token").asString(); + String oldRefresh = issued.path("refresh_token").asString(); + var refreshed = + post( + "/oauth2/token", + form( + Map.of( + "grant_type", + "refresh_token", + "client_id", + "native-http", + "refresh_token", + oldRefresh)) + + suffix); + softly.assertThat(refreshed.statusCode()).as("valid refresh %s", suffix).isEqualTo(200); + softly.assertThat(refreshed.headers().firstValue("Location")).isEmpty(); + softly + .assertThat(refreshed.headers().firstValue("Content-Type").orElse("")) + .startsWith("application/json"); + if (refreshed.statusCode() == 200) { + JsonNode successor = json.readTree(refreshed.body()); + softly.assertThat(successor.path("token_type").asString()).isEqualTo("Bearer"); + softly.assertThat(successor.path("refresh_token").asString().equals(oldRefresh)).isFalse(); + softly.assertThat(successor.path("refresh_token").asString()).isNotBlank(); + softly.assertThat(successor.path("access_token").asString()).isNotBlank(); + softly.assertThat(dav(oldAccess).statusCode()).isEqualTo(401); + softly + .assertThat(dav(successor.path("access_token").asString()).statusCode()) + .isEqualTo(200); + softly + .assertThat(refreshed.headers().firstValue("Cache-Control").orElse("")) + .contains("no-store"); + softly.assertThat(refreshed.headers().firstValue("Pragma")).hasValue("no-cache"); + } + var invalid = + post( + "/oauth2/token", + form( + Map.of( + "grant_type", + "refresh_token", + "client_id", + "native-http", + "refresh_token", + "independent-invalid-control")) + + suffix); + softly.assertThat(invalid.statusCode()).as("invalid refresh %s", suffix).isEqualTo(400); + softly.assertThat(invalid.headers().firstValue("Location")).isEmpty(); + softly + .assertThat(invalid.headers().firstValue("Content-Type").orElse("")) + .startsWith("application/json"); + if (invalid.statusCode() == 400) + softly + .assertThat(json.readTree(invalid.body()).path("error").asString()) + .isEqualTo("invalid_grant"); + } + softly.assertAll(); + } + + @Test + void nonemptyAndDuplicateSecretsReturnOAuthErrorsWithoutConsumingRefreshGrants() + throws Exception { + for (var control : + Map.ofEntries( + Map.entry("&client_secret=unsupported-secret", "invalid_client"), + Map.entry("&client_secret=%20", "invalid_client"), + Map.entry("&client_secret=&client_secret=", "invalid_request"), + Map.entry("&client_secret=&client_secret=unsupported-secret", "invalid_request"), + Map.entry("&client_secret=unsupported-secret&client_secret=", "invalid_request"), + Map.entry("&client_secret=one&client_secret=two", "invalid_request"), + Map.entry("&client_secret=&client_id=native-http", "invalid_request"), + Map.entry("&client_secret=&grant_type=refresh_token", "invalid_request"), + Map.entry("&client_secret=&refresh_token=duplicate-control", "invalid_request"), + Map.entry("&client_secret=&scope=dav&scope=dav", "invalid_request")) + .entrySet()) { + var issuedResponse = exchange(seedCode()); + assertThat(issuedResponse.statusCode()).isEqualTo(200); + JsonNode issued = json.readTree(issuedResponse.body()); + String token = issued.path("refresh_token").asString(); + for (String candidate : List.of(token, "independent-invalid-control")) { + var rejected = + post( + "/oauth2/token", + form( + Map.of( + "grant_type", + "refresh_token", + "client_id", + "native-http", + "refresh_token", + candidate)) + + control.getKey()); + assertThat(rejected.statusCode()) + .as("refresh control %s", control.getKey()) + .isEqualTo(control.getValue().equals("invalid_client") ? 401 : 400); + assertThat(rejected.headers().firstValue("Location")).isEmpty(); + assertThat(rejected.headers().firstValue("Content-Type").orElseThrow()) + .startsWith("application/json"); + assertThat(json.readTree(rejected.body()).path("error").asString()) + .isEqualTo(control.getValue()); + } + assertThat(dav(issued.path("access_token").asString()).statusCode()).isEqualTo(200); + var refreshed = refresh(token); + assertThat(refreshed.statusCode()).isEqualTo(200); + assertThat(dav(json.readTree(refreshed.body()).path("access_token").asString()).statusCode()) + .isEqualTo(200); + } + } + + @Test + void emptyOptionalSecretRevokesWithoutAdmittingNonemptyOrDuplicateSecrets() throws Exception { + for (String suffix : List.of("", "&client_secret=")) { + var issued = json.readTree(exchange(seedCode()).body()); + var revoked = + post( + "/oauth2/revoke", + form( + Map.of( + "client_id", + "native-http", + "token", + issued.path("refresh_token").asString())) + + suffix); + assertThat(revoked.statusCode()).as("revoke %s", suffix).isEqualTo(200); + assertThat(revoked.headers().firstValue("Location")).isEmpty(); + assertThat(dav(issued.path("access_token").asString()).statusCode()).isEqualTo(401); + } + for (var control : + Map.of( + "&client_secret=unsupported-secret", "invalid_client", + "&client_secret=%20", "invalid_client", + "&client_secret=&client_secret=", "invalid_request", + "&client_secret=&client_secret=unsupported-secret", "invalid_request") + .entrySet()) { + var issued = json.readTree(exchange(seedCode()).body()); + var rejected = + post( + "/oauth2/revoke", + form( + Map.of( + "client_id", + "native-http", + "token", + issued.path("refresh_token").asString())) + + control.getKey()); + assertThat(rejected.statusCode()) + .as("revoke control %s", control.getKey()) + .isEqualTo(control.getValue().equals("invalid_client") ? 401 : 400); + assertThat(rejected.headers().firstValue("Location")).isEmpty(); + assertThat(rejected.headers().firstValue("Content-Type").orElseThrow()) + .startsWith("application/json"); + assertThat(json.readTree(rejected.body()).path("error").asString()) + .isEqualTo(control.getValue()); + assertThat(dav(issued.path("access_token").asString()).statusCode()).isEqualTo(200); + assertThat(refresh(issued.path("refresh_token").asString()).statusCode()).isEqualTo(200); + } + } + + @Test + void rootBearerChallengeAndAuthorizationRequestBoundariesMatchTheSelectedProfile() + throws Exception { + var invalid = + http.send( + HttpRequest.newBuilder(URI.create(base() + "/dav/")) + .header("Authorization", "Bearer invalid-boundary-fixture") + .GET() + .build(), + HttpResponse.BodyHandlers.ofString()); + assertThat(invalid.statusCode()).isEqualTo(401); + assertThat(invalid.headers().firstValue("WWW-Authenticate").orElseThrow()) + .startsWith("Bearer ") + .contains("invalid_token") + .doesNotContain("resource_metadata"); + String common = + form( + Map.of( + "response_type", + "code", + "client_id", + "native-http", + "redirect_uri", + CALLBACK, + "scope", + "dav", + "code_challenge", + "012345678901234567890123456789012345678901234", + "code_challenge_method", + "S256")); + for (String query : + List.of( + common.replace("client_id=native-http", "client_id=unknown-audit-client"), + common.replace( + java.net.URLEncoder.encode(CALLBACK, StandardCharsets.UTF_8), + java.net.URLEncoder.encode( + "https://attacker.invalid/callback", StandardCharsets.UTF_8)), + common + "&client_id=native-http", + common + "&response_type=code")) { + var rejected = + http.send( + HttpRequest.newBuilder(URI.create(base() + "/oauth2/authorize?" + query)) + .GET() + .build(), + HttpResponse.BodyHandlers.ofString()); + assertThat(rejected.statusCode()).as(rejected.body()).isEqualTo(400); + assertThat(rejected.headers().firstValue("Location")).isEmpty(); + } + var ordinary = + http.send( + HttpRequest.newBuilder(URI.create(base() + "/oauth2/authorize?" + common)) + .header("Accept", "text/html") + .GET() + .build(), + HttpResponse.BodyHandlers.ofString()); + var extension = + http.send( + HttpRequest.newBuilder( + URI.create( + base() + + "/oauth2/authorize?" + + common + + "&unrecognized-audit-extension=opaque")) + .header("Accept", "text/html") + .GET() + .build(), + HttpResponse.BodyHandlers.ofString()); + assertThat(ordinary.statusCode()).isEqualTo(302); + assertThat(extension.statusCode()).isEqualTo(ordinary.statusCode()); + assertThat(extension.headers().firstValue("Location")) + .isEqualTo(ordinary.headers().firstValue("Location")); + assertThat(jdbc.queryForObject("SELECT count(*) FROM oauth2_authorization", Long.class)) + .isZero(); + String omittedResponse = + java.util.Arrays.stream(common.split("&")) + .filter(parameter -> !parameter.startsWith("response_type=")) + .collect(java.util.stream.Collectors.joining("&")); + for (String query : + List.of(omittedResponse, common.replace("response_type=code", "response_type="))) { + var rejected = + http.send( + HttpRequest.newBuilder(URI.create(base() + "/oauth2/authorize?" + query)) + .GET() + .build(), + HttpResponse.BodyHandlers.ofString()); + assertThat(rejected.statusCode()).isEqualTo(400); + assertThat(rejected.headers().firstValue("Location")).isEmpty(); + } + for (String body : + List.of( + "grant_type=authorization_code&grant_type=refresh_token&client_id=native-http&code=unused&code_verifier=" + + VERIFIER, + "grant_type=authorization_code&client_id=native-http&client_id=native-http&code=unused&code_verifier=" + + VERIFIER)) { + var rejected = + http.send( + HttpRequest.newBuilder(URI.create(base() + "/oauth2/token")) + .header("Content-Type", "application/x-www-form-urlencoded") + .POST(HttpRequest.BodyPublishers.ofString(body)) + .build(), + HttpResponse.BodyHandlers.ofString()); + assertThat(rejected.statusCode()).as(rejected.body()).isEqualTo(400); + assertThat(rejected.body()).contains("invalid_request"); + assertThat(rejected.body()).doesNotContain("access_token", "refresh_token"); + } + for (var malformed : + Map.of( + "client_id=native-http", + "invalid_request", + "grant_type=&client_id=native-http", + "invalid_request", + "grant_type=authorization_code&client_id=native-http", + "invalid_request", + "grant_type=authorization_code&client_id=native-http&code=unused", + "invalid_grant", + "grant_type=authorization_code&code=unused&code_verifier=" + VERIFIER, + "invalid_request", + "grant_type=unsupported-audit-grant&client_id=native-http", + "unsupported_grant_type") + .entrySet()) { + var rejected = + http.send( + HttpRequest.newBuilder(URI.create(base() + "/oauth2/token")) + .header("Content-Type", "application/x-www-form-urlencoded") + .POST(HttpRequest.BodyPublishers.ofString(malformed.getKey())) + .build(), + HttpResponse.BodyHandlers.ofString()); + assertThat(rejected.statusCode()).as(rejected.body()).isEqualTo(400); + assertThat(rejected.headers().firstValue("Location")).isEmpty(); + assertThat(json.readTree(rejected.body()).path("error").asString()) + .isEqualTo(malformed.getValue()); + assertThat(rejected.body()).doesNotContain("access_token", "refresh_token"); + } + } + + @Test + void codeReplayRevokesIssuedGrantAndWrongVerifierCannotExchange() throws Exception { + String code = seedCode(); + var wrong = + post( + "/oauth2/token", + Map.of( + "grant_type", + "authorization_code", + "client_id", + "native-http", + "redirect_uri", + CALLBACK, + "code", + code, + "code_verifier", + "wrong-code-verifier-012345678901234567890123456789")); + assertThat(wrong.statusCode()).isEqualTo(400); + var issued = exchange(code); + assertThat(issued.statusCode()).isEqualTo(200); + String access = json.readTree(issued.body()).path("access_token").asString(); + assertThat(dav(access).statusCode()).isEqualTo(200); + assertThat(exchange(code).statusCode()).isEqualTo(400); + assertThat(dav(access).statusCode()).isEqualTo(401); + } + + @Test + void unsupportedClientAuthenticationReturnsOAuthErrorsWithoutConsumingTheCode() throws Exception { + String code = seedCode(); + for (boolean basic : List.of(true, false)) { + var parameters = + new java.util.HashMap<>( + Map.of( + "grant_type", + "authorization_code", + "client_id", + "native-http", + "redirect_uri", + CALLBACK, + "code", + code, + "code_verifier", + VERIFIER)); + var request = + HttpRequest.newBuilder(URI.create(base() + "/oauth2/token")) + .timeout(Duration.ofSeconds(15)) + .header("Content-Type", "application/x-www-form-urlencoded"); + if (basic) + request.header( + "Authorization", + "Basic " + + Base64.getEncoder() + .encodeToString( + "native-http:unsupported-public-client-secret" + .getBytes(StandardCharsets.UTF_8))); + else parameters.put("client_secret", "unsupported-public-client-secret"); + var rejected = + http.send( + request.POST(HttpRequest.BodyPublishers.ofString(form(parameters))).build(), + HttpResponse.BodyHandlers.ofString()); + assertThat(rejected.statusCode()).isEqualTo(401); + assertThat(rejected.headers().firstValue("Content-Type").orElseThrow()) + .startsWith("application/json"); + assertThat(rejected.body()).contains("invalid_client"); + } + assertThat(exchange(code).statusCode()).isEqualTo(200); + } + + @Test + void concurrentRefreshCannotCreateTwoUsableSuccessorChains() throws Exception { + JsonNode issued = json.readTree(exchange(seedCode()).body()); + String refresh = issued.path("refresh_token").asString(); + CountDownLatch ready = new CountDownLatch(2); + CountDownLatch start = new CountDownLatch(1); + var first = CompletableFuture.supplyAsync(() -> concurrentRefresh(refresh, ready, start)); + var second = CompletableFuture.supplyAsync(() -> concurrentRefresh(refresh, ready, start)); + assertThat(ready.await(10, TimeUnit.SECONDS)).isTrue(); + start.countDown(); + var a = first.get(15, TimeUnit.SECONDS); + var b = second.get(15, TimeUnit.SECONDS); + assertThat(List.of(a.statusCode(), b.statusCode())).containsExactlyInAnyOrder(200, 400); + JsonNode winner = json.readTree((a.statusCode() == 200 ? a : b).body()); + assertThat(dav(winner.path("access_token").asString()).statusCode()).isEqualTo(401); + assertThat(refresh(winner.path("refresh_token").asString()).statusCode()).isEqualTo(400); + } + + @Test + void revocationAccountDisablingAndRecoveryInvalidateBearerAccess() throws Exception { + JsonNode issued = json.readTree(exchange(seedCode()).body()); + String access = issued.path("access_token").asString(); + var revoked = + post( + "/oauth2/revoke", + Map.of("client_id", "native-http", "token", issued.path("refresh_token").asString())); + assertThat(revoked.statusCode()).isEqualTo(200); + assertThat(dav(access).statusCode()).isEqualTo(401); + issued = json.readTree(exchange(seedCode()).body()); + access = issued.path("access_token").asString(); + accounts.disable(account); + assertThat(dav(access).statusCode()).isEqualTo(401); + assertThat(refresh(issued.path("refresh_token").asString()).statusCode()).isEqualTo(400); + } + + @Test + void revocationAndAccountResetUseOneLockOrderWithoutDeadlockOrRetry() throws Exception { + JsonNode issued = json.readTree(exchange(seedCode()).body()); + String access = issued.path("access_token").asString(); + String refresh = issued.path("refresh_token").asString(); + String authorization = + authorizations + .findByToken( + access, + org.springframework.security.oauth2.server.authorization.OAuth2TokenType + .ACCESS_TOKEN) + .getId(); + CountDownLatch resetHoldingRows = new CountDownLatch(1); + CountDownLatch finishReset = new CountDownLatch(1); + var resetPid = new java.util.concurrent.atomic.AtomicInteger(); + try (var workers = java.util.concurrent.Executors.newVirtualThreadPerTaskExecutor()) { + var reset = + CompletableFuture.runAsync( + () -> + new org.springframework.transaction.support.TransactionTemplate(transactions) + .executeWithoutResult( + status -> { + jdbc.query( + "SELECT id FROM accounts WHERE id=? FOR UPDATE", rs -> {}, account); + jdbc.update( + "UPDATE oauth_refresh_families SET revoked_at=now() WHERE authorization_id=?", + authorization); + resetPid.set( + jdbc.queryForObject("SELECT pg_backend_pid()", Integer.class)); + resetHoldingRows.countDown(); + try { + if (!finishReset.await(10, TimeUnit.SECONDS)) + throw new AssertionError("Reset coordination timed out"); + } catch (InterruptedException failure) { + Thread.currentThread().interrupt(); + throw new AssertionError(failure); + } + clientAccess.revoke(account, "oauth-user"); + }), + workers); + try { + assertThat(resetHoldingRows.await(10, TimeUnit.SECONDS)).isTrue(); + var revoke = + CompletableFuture.supplyAsync( + () -> { + try { + return post( + "/oauth2/revoke", Map.of("client_id", "native-http", "token", refresh)); + } catch (Exception failure) { + throw new AssertionError(failure); + } + }, + workers); + org.awaitility.Awaitility.await() + .atMost(Duration.ofSeconds(10)) + .untilAsserted( + () -> + assertThat( + jdbc.queryForObject( + "SELECT EXISTS(SELECT 1 FROM pg_stat_activity WHERE ?=ANY(pg_blocking_pids(pid)))", + Boolean.class, + resetPid.get())) + .isTrue()); + finishReset.countDown(); + reset.get(15, TimeUnit.SECONDS); + assertThat(revoke.get(15, TimeUnit.SECONDS).statusCode()).isEqualTo(200); + } finally { + finishReset.countDown(); + } + } + assertThat(authorizations.findById(authorization)).isNull(); + assertThat(families.isActive(authorization)).isFalse(); + assertThat(dav(access).statusCode()).isEqualTo(401); + } + + @Test + void expiryRecoveryAndAudienceChecksApplyOnEveryDavRequest() throws Exception { + JsonNode issued = json.readTree(exchange(seedCode()).body()); + String access = issued.path("access_token").asString(); + var grant = + authorizations.findByToken( + access, + org.springframework.security.oauth2.server.authorization.OAuth2TokenType.ACCESS_TOKEN); + var claims = new java.util.HashMap<>(grant.getAccessToken().getClaims()); + claims.put("aud", new java.util.ArrayList<>(List.of("https://attacker.invalid/dav"))); + authorizations.save( + OAuth2Authorization.from(grant) + .token( + grant.getAccessToken().getToken(), + metadata -> metadata.put(OAuth2Authorization.Token.CLAIMS_METADATA_NAME, claims)) + .build()); + assertThat(dav(access).statusCode()).isEqualTo(401); + issued = json.readTree(exchange(seedCode()).body()); + access = issued.path("access_token").asString(); + grant = + authorizations.findByToken( + access, + org.springframework.security.oauth2.server.authorization.OAuth2TokenType.ACCESS_TOKEN); + var wrongIssuer = new java.util.HashMap<>(grant.getAccessToken().getClaims()); + wrongIssuer.put("iss", "https://attacker.invalid"); + authorizations.save( + OAuth2Authorization.from(grant) + .token( + grant.getAccessToken().getToken(), + metadata -> + metadata.put(OAuth2Authorization.Token.CLAIMS_METADATA_NAME, wrongIssuer)) + .build()); + assertThat(dav(access).statusCode()).isEqualTo(401); + issued = json.readTree(exchange(seedCode()).body()); + access = issued.path("access_token").asString(); + jdbc.update( + "UPDATE oauth2_authorization SET access_token_issued_at=now()-interval '2 minutes',access_token_expires_at=now()-interval '1 minute'"); + assertThat(dav(access).statusCode()).isEqualTo(401); + issued = json.readTree(exchange(seedCode()).body()); + access = issued.path("access_token").asString(); + jdbc.update("UPDATE system_state SET recovery_epoch=gen_random_uuid() WHERE id=1"); + assertThat(dav(access).statusCode()).isEqualTo(401); + assertThat(refresh(issued.path("refresh_token").asString()).statusCode()).isEqualTo(400); + } + + @Test + void metadataUsesConfiguredIssuerAndAdministratorControlsRegistration() throws Exception { + var passwordOnly = + UsernamePasswordAuthenticationToken.authenticated( + "oauth-admin", + null, + List.of( + new SimpleGrantedAuthority("ROLE_ADMIN"), + FactorGrantedAuthority.fromAuthority(FactorGrantedAuthority.PASSWORD_AUTHORITY))); + assertThatThrownBy( + () -> clients.registerNative(passwordOnly, "not-yet", "Requires MFA", Set.of(CALLBACK))) + .isInstanceOf(org.springframework.security.access.AccessDeniedException.class); + assertThat(registrations.findByClientId("not-yet")).isNull(); + var metadata = + http.send( + HttpRequest.newBuilder(URI.create(base() + "/.well-known/oauth-authorization-server")) + .header("Forwarded", "proto=http;host=attacker.invalid") + .GET() + .build(), + HttpResponse.BodyHandlers.ofString()); + assertThat(metadata.statusCode()).isEqualTo(200); + assertThat(json.readTree(metadata.body()).path("issuer").asString()) + .isEqualTo("https://calendar.example"); + assertThat(json.readTree(metadata.body()).path("response_modes_supported").toString()) + .isEqualTo("[\"query\"]"); + assertThat(metadata.body()) + .contains("S256", "authorization_code", "refresh_token") + .doesNotContain( + "client_credentials", + "password", + "implicit", + "attacker.invalid", + "jwks_uri", + "tls_client_certificate_bound_access_tokens", + "dpop_signing_alg_values_supported"); + assertThatThrownBy( + () -> clients.registerNative(account, "untrusted", "Untrusted", Set.of(CALLBACK))) + .isInstanceOf(org.springframework.security.access.AccessDeniedException.class); + assertThatThrownBy( + () -> + clients.registerNative( + administrator, + "redirect", + "Bad redirect", + Set.of("https://attacker.invalid/callback#fragment"))) + .isInstanceOf(IllegalArgumentException.class); + assertThat( + registrations + .findByClientId("native-http") + .getClientSettings() + .isRequireAuthorizationConsent()) + .isTrue(); + assertThat( + post( + "/oauth2/token", + Map.of( + "grant_type", + "password", + "client_id", + "native-http", + "username", + "oauth-user", + "password", + "correct horse battery staple")) + .statusCode()) + .isNotEqualTo(200); + } + + @Test + void passwordOnlyBrowserSessionMustCompleteMfaBeforeClientAuthorization() throws Exception { + HttpClient browser = + HttpClient.newBuilder() + .cookieHandler(new java.net.CookieManager(null, java.net.CookiePolicy.ACCEPT_ALL)) + .connectTimeout(Duration.ofSeconds(10)) + .followRedirects(HttpClient.Redirect.NEVER) + .build(); + var login = + browser.send( + HttpRequest.newBuilder(URI.create(base() + "/login")) + .timeout(Duration.ofSeconds(10)) + .GET() + .build(), + HttpResponse.BodyHandlers.ofString()); + assertThat(login.statusCode()).isEqualTo(200); + var csrf = new java.util.concurrent.atomic.AtomicReference(); + new javax.swing.text.html.parser.ParserDelegator() + .parse( + new java.io.StringReader(login.body()), + new javax.swing.text.html.HTMLEditorKit.ParserCallback() { + @Override + public void handleSimpleTag( + javax.swing.text.html.HTML.Tag tag, + javax.swing.text.MutableAttributeSet attributes, + int position) { + if (tag == javax.swing.text.html.HTML.Tag.INPUT + && "_csrf" + .equals(attributes.getAttribute(javax.swing.text.html.HTML.Attribute.NAME))) + csrf.set( + (String) attributes.getAttribute(javax.swing.text.html.HTML.Attribute.VALUE)); + } + }, + true); + assertThat(csrf.get()).isNotBlank(); + String credentials = + form( + Map.of( + "username", + "oauth-user", + "password", + "correct horse battery staple", + "_csrf", + csrf.get())); + var authenticated = + browser.send( + HttpRequest.newBuilder(URI.create(base() + "/login")) + .timeout(Duration.ofSeconds(10)) + .header("Content-Type", "application/x-www-form-urlencoded") + .POST(HttpRequest.BodyPublishers.ofString(credentials)) + .build(), + HttpResponse.BodyHandlers.ofString()); + assertThat(authenticated.statusCode()).isEqualTo(302); + String authorization = + form( + Map.of( + "response_type", + "code", + "client_id", + "native-http", + "redirect_uri", + CALLBACK, + "scope", + "dav", + "state", + UUID.randomUUID().toString(), + "code_challenge", + "unused-because-step-up-is-required", + "code_challenge_method", + "S256")); + var denied = + browser.send( + HttpRequest.newBuilder(URI.create(base() + "/oauth2/authorize?" + authorization)) + .timeout(Duration.ofSeconds(10)) + .header("Accept", "text/html") + .GET() + .build(), + HttpResponse.BodyHandlers.ofString()); + assertThat(denied.statusCode()).isEqualTo(302); + assertThat(denied.headers().firstValue("Location").orElseThrow()) + .contains("/login?factor=required"); + assertThat(jdbc.queryForObject("SELECT count(*) FROM oauth2_authorization", Long.class)) + .isZero(); + } + + @Test + void oldInteractiveProofCannotIssueTokensAfterAccountReset() throws Exception { + UUID captured = + jdbc.queryForObject("SELECT security_epoch FROM accounts WHERE id=?", UUID.class, account); + jdbc.update("UPDATE accounts SET security_epoch=gen_random_uuid() WHERE id=?", account); + String code = seedCode(captured); + var rejected = exchange(code); + assertThat(rejected.statusCode()).isEqualTo(400); + assertThat(rejected.body()).contains("invalid_grant"); + assertThat(jdbc.queryForObject("SELECT count(*) FROM oauth_refresh_families", Long.class)) + .isZero(); + } + + @Test + void concurrentRegistrationHasOneWinnerAndDatabaseUniqueness() throws Exception { + var start = new CountDownLatch(1); + try (var workers = java.util.concurrent.Executors.newVirtualThreadPerTaskExecutor()) { + java.util.concurrent.Callable register = + () -> { + if (!start.await(10, TimeUnit.SECONDS)) + throw new AssertionError("Registration start timed out"); + try { + clients.registerNative( + administrator, "racing-client", "Concurrent client", Set.of(CALLBACK)); + return true; + } catch (IllegalArgumentException duplicate) { + return false; + } + }; + var first = workers.submit(register); + var second = workers.submit(register); + start.countDown(); + assertThat(List.of(first.get(15, TimeUnit.SECONDS), second.get(15, TimeUnit.SECONDS))) + .containsExactlyInAnyOrder(true, false); + } + assertThat( + jdbc.queryForObject( + "SELECT count(*) FROM oauth2_registered_client WHERE client_id='racing-client'", + Long.class)) + .isEqualTo(1); + assertThatThrownBy( + () -> + jdbc.update( + "INSERT INTO oauth2_registered_client(id,client_id,client_name,client_authentication_methods,authorization_grant_types,scopes,client_settings,token_settings) SELECT ?,client_id,client_name,client_authentication_methods,authorization_grant_types,scopes,client_settings,token_settings FROM oauth2_registered_client WHERE client_id=?", + UUID.randomUUID().toString(), + "racing-client")) + .isInstanceOf(org.springframework.dao.DuplicateKeyException.class); + } + + @Test + void lateAuthorizationSaveCannotRecreateRemovedOrReplacedClientState() throws Exception { + String code = seedCode(); + var pending = + authorizations.findByToken( + code, + new org.springframework.security.oauth2.server.authorization.OAuth2TokenType("code")); + clients.remove(administrator, "native-http"); + assertThatThrownBy(() -> authorizations.save(pending)) + .isInstanceOf(org.springframework.security.oauth2.core.OAuth2AuthenticationException.class); + clients.registerNative( + administrator, "native-http", "Replacement registration", Set.of(CALLBACK)); + assertThat(registrations.findByClientId("native-http").getId()) + .isNotEqualTo(pending.getRegisteredClientId()); + assertThatThrownBy(() -> authorizations.save(pending)) + .isInstanceOf(org.springframework.security.oauth2.core.OAuth2AuthenticationException.class); + assertThat(jdbc.queryForObject("SELECT count(*) FROM oauth2_authorization", Long.class)) + .isZero(); + assertThat(exchange(code).statusCode()).isEqualTo(400); + } + + @Test + void clientRemovalSerializesWithAnInFlightCodeExchange() throws Exception { + removalWaitsForGrantMutation(false); + } + + @Test + void clientRemovalSerializesWithAnInFlightRefresh() throws Exception { + removalWaitsForGrantMutation(true); + } + + private void removalWaitsForGrantMutation(boolean refreshGrant) throws Exception { + String code = seedCode(); + String presented = + refreshGrant ? json.readTree(exchange(code).body()).path("refresh_token").asString() : code; + var authorization = + authorizations.findByToken( + presented, + new org.springframework.security.oauth2.server.authorization.OAuth2TokenType( + refreshGrant ? "refresh_token" : "code")); + jdbc.execute( + "CREATE TABLE oauth_mutation_pause(authorization_id text PRIMARY KEY,lock_key bigint NOT NULL)"); + jdbc.update("INSERT INTO oauth_mutation_pause VALUES (?,?)", authorization.getId(), 731092811L); + jdbc.execute( + "CREATE FUNCTION oauth_pause_mutation() RETURNS trigger LANGUAGE plpgsql AS $$ DECLARE gate bigint; BEGIN SELECT lock_key INTO gate FROM oauth_mutation_pause WHERE authorization_id=NEW.id; IF FOUND THEN PERFORM pg_advisory_xact_lock(gate); END IF; RETURN NEW; END $$"); + jdbc.execute( + "CREATE TRIGGER oauth_pause_mutation BEFORE UPDATE ON oauth2_authorization FOR EACH ROW EXECUTE FUNCTION oauth_pause_mutation()"); + CountDownLatch holdingGate = new CountDownLatch(1); + CountDownLatch releaseGate = new CountDownLatch(1); + var gatePid = new java.util.concurrent.atomic.AtomicInteger(); + var removalPid = new java.util.concurrent.atomic.AtomicInteger(); + try (var workers = java.util.concurrent.Executors.newVirtualThreadPerTaskExecutor()) { + var gate = + CompletableFuture.runAsync( + () -> + new org.springframework.transaction.support.TransactionTemplate(transactions) + .executeWithoutResult( + status -> { + jdbc.query("SELECT pg_advisory_xact_lock(?)", rs -> {}, 731092811L); + gatePid.set( + jdbc.queryForObject("SELECT pg_backend_pid()", Integer.class)); + holdingGate.countDown(); + try { + if (!releaseGate.await(10, TimeUnit.SECONDS)) + throw new AssertionError("Mutation gate timed out"); + } catch (InterruptedException failure) { + Thread.currentThread().interrupt(); + throw new AssertionError(failure); + } + }), + workers); + try { + assertThat(holdingGate.await(10, TimeUnit.SECONDS)).isTrue(); + var mutation = + CompletableFuture.supplyAsync( + () -> { + try { + return refreshGrant ? refresh(presented) : exchange(presented); + } catch (Exception failure) { + throw new AssertionError(failure); + } + }, + workers); + org.awaitility.Awaitility.await() + .atMost(Duration.ofSeconds(10)) + .untilAsserted( + () -> + assertThat( + jdbc.queryForObject( + "SELECT EXISTS(SELECT 1 FROM pg_stat_activity WHERE ?=ANY(pg_blocking_pids(pid)))", + Boolean.class, + gatePid.get())) + .isTrue()); + var removal = + CompletableFuture.runAsync( + () -> + new org.springframework.transaction.support.TransactionTemplate(transactions) + .executeWithoutResult( + status -> { + removalPid.set( + jdbc.queryForObject("SELECT pg_backend_pid()", Integer.class)); + clients.remove(administrator, "native-http"); + }), + workers); + org.awaitility.Awaitility.await() + .atMost(Duration.ofSeconds(10)) + .untilAsserted( + () -> + assertThat( + jdbc.queryForObject( + "SELECT cardinality(pg_blocking_pids(?))>0", + Boolean.class, + removalPid.get())) + .isTrue()); + releaseGate.countDown(); + gate.get(15, TimeUnit.SECONDS); + var response = mutation.get(15, TimeUnit.SECONDS); + assertThat(response.statusCode()).isEqualTo(200); + removal.get(15, TimeUnit.SECONDS); + assertThat(registrations.findByClientId("native-http")).isNull(); + assertThat(jdbc.queryForObject("SELECT count(*) FROM oauth2_authorization", Long.class)) + .isZero(); + assertThat(jdbc.queryForObject("SELECT count(*) FROM oauth_refresh_families", Long.class)) + .isZero(); + assertThat(dav(json.readTree(response.body()).path("access_token").asString()).statusCode()) + .isEqualTo(401); + } finally { + releaseGate.countDown(); + } + } finally { + jdbc.execute("DROP TRIGGER oauth_pause_mutation ON oauth2_authorization"); + jdbc.execute("DROP FUNCTION oauth_pause_mutation()"); + jdbc.execute("DROP TABLE oauth_mutation_pause"); + } + } + + private String seedCode() throws Exception { + return seedCode( + jdbc.queryForObject("SELECT security_epoch FROM accounts WHERE id=?", UUID.class, account)); + } + + @Test + void authorizationAndRegistrationReadUseOneSnapshotDuringClientRemoval() throws Exception { + String code = seedCode(); + String access = json.readTree(exchange(code).body()).path("access_token").asString(); + String registration = registrations.findByClientId("native-http").getId(); + var readingRegistration = new CountDownLatch(1); + var continueRead = new CountDownLatch(1); + var interleaving = + new RegisteredClientRepository() { + @Override + public void save( + org.springframework.security.oauth2.server.authorization.client.RegisteredClient + value) { + registrations.save(value); + } + + @Override + public org.springframework.security.oauth2.server.authorization.client.RegisteredClient + findByClientId(String id) { + return registrations.findByClientId(id); + } + + @Override + public org.springframework.security.oauth2.server.authorization.client.RegisteredClient + findById(String id) { + if (id.equals(registration)) { + readingRegistration.countDown(); + try { + if (!continueRead.await(10, TimeUnit.SECONDS)) + throw new AssertionError("Registration read coordination timed out"); + } catch (InterruptedException failure) { + Thread.currentThread().interrupt(); + throw new AssertionError(failure); + } + } + return registrations.findById(id); + } + }; + var reader = + new PrincipalOAuthAuthorizationService( + OAuthJdbcSerialization.service(jdbc, interleaving), + accounts, + new OAuthClientLock(jdbc), + transactions); + try (var workers = java.util.concurrent.Executors.newVirtualThreadPerTaskExecutor()) { + var result = + CompletableFuture.supplyAsync( + () -> + reader.findByToken( + access, + org.springframework.security.oauth2.server.authorization.OAuth2TokenType + .ACCESS_TOKEN), + workers); + try { + assertThat(readingRegistration.await(10, TimeUnit.SECONDS)).isTrue(); + clients.remove(administrator, "native-http"); + assertThat(registrations.findById(registration)).isNull(); + continueRead.countDown(); + assertThat(result.get(15, TimeUnit.SECONDS).getRegisteredClientId()) + .isEqualTo(registration); + } finally { + continueRead.countDown(); + } + } + assertThat( + reader.findByToken( + access, + org.springframework.security.oauth2.server.authorization.OAuth2TokenType + .ACCESS_TOKEN)) + .isNull(); + assertThat(dav(access).statusCode()).isEqualTo(401); + } + + @Test + void frameworkWebAuthnPrincipalPersistsWithoutAllowingUnrelatedPolymorphicTypes() + throws Exception { + String code = seedCode(); + var pending = + authorizations.findByToken( + code, + new org.springframework.security.oauth2.server.authorization.OAuth2TokenType("code")); + org.springframework.security.core.Authentication original = + pending.getAttribute(Principal.class.getName()); + var user = + org.springframework.security.web.webauthn.api.ImmutablePublicKeyCredentialUserEntity + .builder() + .id(org.springframework.security.web.webauthn.api.Bytes.random()) + .name("oauth-user") + .displayName("OAuth user") + .build(); + var webauthn = + new org.springframework.security.web.webauthn.authentication.WebAuthnAuthentication( + user, original.getAuthorities()); + authorizations.save( + OAuth2Authorization.from(pending).attribute(Principal.class.getName(), webauthn).build()); + org.springframework.security.core.Authentication restored = + authorizations.findById(pending.getId()).getAttribute(Principal.class.getName()); + assertThat(restored) + .isInstanceOf( + org.springframework.security.web.webauthn.authentication.WebAuthnAuthentication.class); + assertThat(restored.getName()).isEqualTo("oauth-user"); + assertThat( + new java.util.ArrayList( + restored.getAuthorities())) + .containsExactlyElementsOf(webauthn.getAuthorities()); + org.busystack.caldav.security.RecentAuthentication.requireMfa(restored); + assertThat(exchange(code).statusCode()).isEqualTo(200); + jdbc.update( + "UPDATE oauth2_authorization SET attributes=replace(attributes,?,?) WHERE id=?", + org.springframework.security.web.webauthn.authentication.WebAuthnAuthentication.class + .getName(), + "java.lang.ProcessBuilder", + pending.getId()); + assertThatThrownBy(() -> authorizations.findById(pending.getId())) + .hasRootCauseInstanceOf(tools.jackson.databind.exc.InvalidTypeIdException.class); + } + + private String seedCode(UUID capturedEpoch) throws Exception { + var client = registrations.findByClientId("native-http"); + var user = + User.withUsername("oauth-user") + .password(accounts.find(account).passwordHash()) + .roles("USER") + .build(); + var authentication = + UsernamePasswordAuthenticationToken.authenticated( + user, + null, + List.of( + new SimpleGrantedAuthority("ROLE_USER"), + org.busystack.caldav.security.session.SessionSecurity.authority(capturedEpoch), + FactorGrantedAuthority.fromAuthority(FactorGrantedAuthority.PASSWORD_AUTHORITY), + FactorGrantedAuthority.fromAuthority(FactorGrantedAuthority.WEBAUTHN_AUTHORITY))); + String challenge = + Base64.getUrlEncoder() + .withoutPadding() + .encodeToString( + MessageDigest.getInstance("SHA-256") + .digest(VERIFIER.getBytes(StandardCharsets.US_ASCII))); + var request = + OAuth2AuthorizationRequest.authorizationCode() + .authorizationUri("https://calendar.example/oauth2/authorize") + .clientId("native-http") + .redirectUri(CALLBACK) + .scopes(Set.of("dav")) + .additionalParameters( + Map.of("code_challenge", challenge, "code_challenge_method", "S256")) + .build(); + var code = + new OAuth2AuthorizationCode( + new Base64StringKeyGenerator(Base64.getUrlEncoder().withoutPadding(), 32).generateKey(), + Instant.now(), + Instant.now().plusSeconds(60)); + authorizations.save( + OAuth2Authorization.withRegisteredClient(client) + .principalName("oauth-user") + .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE) + .authorizedScopes(Set.of("dav")) + .attribute(Principal.class.getName(), authentication) + .attribute(OAuth2AuthorizationRequest.class.getName(), request) + .token(code) + .build()); + return code.getTokenValue(); + } + + private HttpResponse exchange(String code) throws Exception { + return post( + "/oauth2/token", + Map.of( + "grant_type", + "authorization_code", + "client_id", + "native-http", + "redirect_uri", + CALLBACK, + "code", + code, + "code_verifier", + VERIFIER)); + } + + private HttpResponse refresh(String value) throws Exception { + return post( + "/oauth2/token", + Map.of("grant_type", "refresh_token", "client_id", "native-http", "refresh_token", value)); + } + + private HttpResponse dav(String value) throws Exception { + return http.send( + HttpRequest.newBuilder(URI.create(base() + "/dav/oauth-security-probe")) + .timeout(Duration.ofSeconds(10)) + .header("Authorization", "Bearer " + value) + .GET() + .build(), + HttpResponse.BodyHandlers.ofString()); + } + + private HttpResponse post(String path, Map parameters) throws Exception { + return post(path, form(parameters)); + } + + private HttpResponse post(String path, String form) throws Exception { + return http.send( + HttpRequest.newBuilder(URI.create(base() + path)) + .timeout(Duration.ofSeconds(15)) + .header("Content-Type", "application/x-www-form-urlencoded") + .POST(HttpRequest.BodyPublishers.ofString(form)) + .build(), + HttpResponse.BodyHandlers.ofString()); + } + + private HttpResponse concurrentRefresh( + String token, CountDownLatch ready, CountDownLatch start) { + ready.countDown(); + try { + if (!start.await(10, TimeUnit.SECONDS)) + throw new AssertionError("Refresh start latch timed out"); + return refresh(token); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + throw new AssertionError(ex); + } catch (Exception ex) { + throw new AssertionError(ex); + } + } + + private static String form(Map parameters) { + return parameters.entrySet().stream() + .map( + e -> + URLEncoder.encode(e.getKey(), StandardCharsets.UTF_8) + + "=" + + URLEncoder.encode(e.getValue(), StandardCharsets.UTF_8)) + .collect(Collectors.joining("&")); + } + + private String base() { + return "http://127.0.0.1:" + port; + } + + @TestConfiguration + static class ProbeConfiguration { + @Bean + ProbeController probe() { + return new ProbeController(); + } + } + + @RestController + static class ProbeController { + @GetMapping("/dav/oauth-security-probe") + String principal(Principal principal) { + return principal.getName(); + } + } +} diff --git a/src/test/java/org/busystack/caldav/security/oauth/RefreshFamilyStoreIT.java b/src/test/java/org/busystack/caldav/security/oauth/RefreshFamilyStoreIT.java new file mode 100644 index 0000000..df2b3fe --- /dev/null +++ b/src/test/java/org/busystack/caldav/security/oauth/RefreshFamilyStoreIT.java @@ -0,0 +1,300 @@ +package org.busystack.caldav.security.oauth; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.time.Duration; +import java.time.Instant; +import java.util.Base64; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import org.busystack.caldav.account.AccountService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.security.crypto.keygen.Base64StringKeyGenerator; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.support.TransactionTemplate; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; +import org.testcontainers.postgresql.PostgreSQLContainer; + +@SpringBootTest( + properties = { + "busystack.public-base-url=https://calendar.example", + "busystack.issuer=https://calendar.example" + }) +@org.springframework.test.annotation.DirtiesContext( + classMode = org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_CLASS) +@Testcontainers +class RefreshFamilyStoreIT { + @Container + static final PostgreSQLContainer POSTGRES = + org.busystack.caldav.testing.LoopbackContainers.postgres(); + + @DynamicPropertySource + static void database(DynamicPropertyRegistry registry) { + registry.add("spring.datasource.url", POSTGRES::getJdbcUrl); + registry.add("spring.datasource.username", POSTGRES::getUsername); + registry.add("spring.datasource.password", POSTGRES::getPassword); + } + + @Autowired RefreshFamilyStore families; + @Autowired AccountService accounts; + @Autowired JdbcTemplate jdbc; + @Autowired PlatformTransactionManager transactions; + @Autowired OAuthClientLock clients; + + @Autowired + org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository + registrations; + + private final Base64StringKeyGenerator tokens = + new Base64StringKeyGenerator(Base64.getUrlEncoder().withoutPadding(), 96); + private UUID account; + private String authorization, initial; + private Instant expiry; + + @BeforeEach + void prepare() { + if (registrations.findByClientId("native-client") == null) + registrations.save( + org.springframework.security.oauth2.server.authorization.client.RegisteredClient.withId( + "ledger-client") + .clientId("native-client") + .clientAuthenticationMethod( + org.springframework.security.oauth2.core.ClientAuthenticationMethod.NONE) + .authorizationGrantType( + org.springframework.security.oauth2.core.AuthorizationGrantType + .AUTHORIZATION_CODE) + .redirectUri("http://127.0.0.1:34567/callback") + .scope("dav") + .build()); + org.busystack.caldav.testing.FixtureDatabase.clearAccounts(jdbc); + account = + accounts.provision("native-user", "Native user", "correct horse battery staple", false); + authorization = UUID.randomUUID().toString(); + initial = tokens.generateKey(); + expiry = Instant.now().plusSeconds(3600); + families.register( + new RefreshFamilyStore.Family(authorization, account, "native-client", expiry), + initial, + expiry, + () -> {}); + } + + @Test + void rotatesAndCommitsFamilyRevocationOnSpentTokenReplay() { + String successor = rotate(initial); + assertThat(successor).isNotEqualTo(initial); + assertThat(families.isActive(authorization)).isTrue(); + assertRejected(() -> rotate(initial), RefreshRejectedException.Reason.REPLAY); + assertThat(families.isActive(authorization)).isFalse(); + assertRejected(() -> rotate(successor), RefreshRejectedException.Reason.REVOKED); + assertThat(jdbc.queryForList("SELECT token_hash FROM oauth_refresh_tokens", String.class)) + .allSatisfy( + hash -> assertThat(hash).hasSize(64).isNotEqualTo(initial).isNotEqualTo(successor)); + } + + @Test + void replayRevocationSurvivesAnOuterTransactionRollbackAndStoreRestart() { + rotate(initial); + new TransactionTemplate(transactions) + .executeWithoutResult( + status -> { + assertRejected(() -> rotate(initial), RefreshRejectedException.Reason.REPLAY); + status.setRollbackOnly(); + }); + var restarted = new RefreshFamilyStore(jdbc, transactions, accounts, clients); + assertThat(restarted.isActive(authorization)).isFalse(); + } + + @Test + void callbackFailureRollsBackConsumptionAndAuthorizationMutation() { + assertThatThrownBy( + () -> + families.refresh( + initial, + "native-client", + family -> { + jdbc.update( + "UPDATE accounts SET display_name='must roll back' WHERE id=?", account); + throw new IllegalStateException("Simulated grant persistence failure"); + })) + .isInstanceOf(IllegalStateException.class); + assertThat(accounts.find(account).displayName()).isEqualTo("Native user"); + assertThat( + jdbc.queryForObject( + "SELECT count(*) FROM oauth_refresh_tokens WHERE consumed_at IS NOT NULL", + Long.class)) + .isZero(); + assertThat(rotate(initial)).isNotBlank(); + } + + @Test + void initialAuthorizationAndFamilyRegistrationAreAtomic() { + String failedId = UUID.randomUUID().toString(); + assertThatThrownBy( + () -> + families.register( + new RefreshFamilyStore.Family(failedId, account, "native-client", expiry), + tokens.generateKey(), + expiry, + () -> { + jdbc.update( + "UPDATE accounts SET display_name='must roll back' WHERE id=?", account); + throw new IllegalStateException("Simulated initial authorization failure"); + })) + .isInstanceOf(IllegalStateException.class); + assertThat(families.isActive(failedId)).isFalse(); + assertThat(accounts.find(account).displayName()).isEqualTo("Native user"); + assertThat(jdbc.queryForObject("SELECT count(*) FROM oauth_refresh_families", Long.class)) + .isEqualTo(1); + } + + @Test + void initialFamilyRegistrationJoinsTheCodeExchangeTransaction() { + String rolledBackId = UUID.randomUUID().toString(); + new TransactionTemplate(transactions) + .executeWithoutResult( + status -> { + families.register( + new RefreshFamilyStore.Family(rolledBackId, account, "native-client", expiry), + tokens.generateKey(), + expiry, + () -> + jdbc.update( + "UPDATE accounts SET display_name='must roll back' WHERE id=?", account)); + status.setRollbackOnly(); + }); + assertThat(families.isActive(rolledBackId)).isFalse(); + assertThat(accounts.find(account).displayName()).isEqualTo("Native user"); + assertThat(jdbc.queryForObject("SELECT count(*) FROM oauth_refresh_families", Long.class)) + .isEqualTo(1); + } + + @Test + void wrongClientAndGuessedTokensCannotRevokeAnExistingFamily() { + AtomicBoolean called = new AtomicBoolean(); + assertRejected( + () -> + families.refresh( + initial, + "another-client", + family -> { + called.set(true); + return new RefreshFamilyStore.Rotation<>( + "unexpected", tokens.generateKey(), expiry); + }), + RefreshRejectedException.Reason.INVALID); + assertRejected(() -> rotate(tokens.generateKey()), RefreshRejectedException.Reason.INVALID); + assertThat(called).isFalse(); + assertThat(families.isActive(authorization)).isTrue(); + assertThat(rotate(initial)).isNotBlank(); + } + + @Test + void enforcesAccountStateRevocationExpiryAndRecoveryEpoch() { + families.revoke(UUID.randomUUID(), authorization); + assertThat(families.isActive(authorization)).isTrue(); + accounts.disable(account); + assertThat(families.isActive(authorization)).isFalse(); + assertRejected(() -> rotate(initial), RefreshRejectedException.Reason.DISABLED); + } + + @Test + void enforcesExpiryAndRecoveryEpoch() { + jdbc.update("UPDATE oauth_refresh_tokens SET expires_at=now()-interval '1 minute'"); + assertRejected(() -> rotate(initial), RefreshRejectedException.Reason.EXPIRED); + jdbc.update("UPDATE oauth_refresh_tokens SET expires_at=now()+interval '1 hour'"); + jdbc.update("UPDATE system_state SET recovery_epoch=gen_random_uuid() WHERE id=1"); + assertThat(families.isActive(authorization)).isFalse(); + assertRejected(() -> rotate(initial), RefreshRejectedException.Reason.REVOKED); + } + + @Test + void concurrentRefreshSerializesAndDetectsTheLosingAttemptAsReplay() throws Exception { + CountDownLatch mutationEntered = new CountDownLatch(1); + CountDownLatch secondStarted = new CountDownLatch(1); + CountDownLatch allowCommit = new CountDownLatch(1); + try (var workers = java.util.concurrent.Executors.newFixedThreadPool(2)) { + CompletableFuture first = + CompletableFuture.supplyAsync( + () -> + families.refresh( + initial, + "native-client", + family -> { + mutationEntered.countDown(); + try { + if (!allowCommit.await(10, TimeUnit.SECONDS)) + throw new AssertionError("Refresh commit latch timed out"); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + throw new AssertionError(ex); + } + String successor = tokens.generateKey(); + return new RefreshFamilyStore.Rotation<>(successor, successor, expiry); + }), + workers); + assertThat(mutationEntered.await(10, TimeUnit.SECONDS)).isTrue(); + CompletableFuture second = + CompletableFuture.supplyAsync( + () -> { + secondStarted.countDown(); + return rotate(initial); + }, + workers); + try { + assertThat(secondStarted.await(10, TimeUnit.SECONDS)).isTrue(); + org.awaitility.Awaitility.await() + .atMost(Duration.ofSeconds(5)) + .untilAsserted( + () -> + assertThat( + jdbc.queryForObject( + "SELECT count(*) FROM pg_locks WHERE locktype='advisory' AND NOT granted", + Long.class)) + .isGreaterThan(0)); + allowCommit.countDown(); + String successor = first.get(10, TimeUnit.SECONDS); + assertThatThrownBy(() -> second.get(10, TimeUnit.SECONDS)) + .isInstanceOf(ExecutionException.class) + .hasCauseInstanceOf(RefreshRejectedException.class); + assertThat(families.isActive(authorization)).isFalse(); + assertRejected(() -> rotate(successor), RefreshRejectedException.Reason.REVOKED); + assertThat(jdbc.queryForObject("SELECT count(*) FROM oauth_refresh_tokens", Long.class)) + .isEqualTo(2); + } finally { + allowCommit.countDown(); + } + } + } + + private String rotate(String token) { + return families.refresh( + token, + "native-client", + family -> { + String successor = tokens.generateKey(); + return new RefreshFamilyStore.Rotation<>(successor, successor, expiry); + }); + } + + private static void assertRejected( + org.assertj.core.api.ThrowableAssert.ThrowingCallable action, + RefreshRejectedException.Reason reason) { + assertThatThrownBy(action) + .isInstanceOfSatisfying( + RefreshRejectedException.class, + failure -> assertThat(failure.reason()).isEqualTo(reason)); + } +} diff --git a/src/test/java/org/busystack/caldav/security/oidc/ExternalIdentityIT.java b/src/test/java/org/busystack/caldav/security/oidc/ExternalIdentityIT.java new file mode 100644 index 0000000..4133a40 --- /dev/null +++ b/src/test/java/org/busystack/caldav/security/oidc/ExternalIdentityIT.java @@ -0,0 +1,241 @@ +package org.busystack.caldav.security.oidc; + +import static org.assertj.core.api.Assertions.*; + +import java.time.Instant; +import java.util.List; +import org.busystack.caldav.account.AccountService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.authority.FactorGrantedAuthority; +import org.springframework.security.oauth2.core.OAuth2AuthenticationException; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; +import org.testcontainers.postgresql.PostgreSQLContainer; + +@SpringBootTest( + webEnvironment = SpringBootTest.WebEnvironment.NONE, + properties = { + "busystack.public-base-url=https://calendar.example", + "busystack.issuer=https://calendar.example", + "busystack.oidc.enabled=true", "busystack.oidc.issuer=https://identity.example/realms/team", + "busystack.oidc.client-id=fixture", "busystack.oidc.client-secret=fixture-only-secret", + "busystack.oidc.allowed-users[0].subject=subject-one", + "busystack.oidc.allowed-users[0].username=allowed", + "busystack.oidc.allowed-users[0].display-name=Allowed user" + }) +@org.springframework.test.annotation.DirtiesContext( + classMode = org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_CLASS) +@Testcontainers +class ExternalIdentityIT { + private static final String ISSUER = "https://identity.example/realms/team"; + + @Container + static final PostgreSQLContainer POSTGRES = + org.busystack.caldav.testing.LoopbackContainers.postgres(); + + @DynamicPropertySource + static void database(DynamicPropertyRegistry registry) { + registry.add("spring.datasource.url", POSTGRES::getJdbcUrl); + registry.add("spring.datasource.username", POSTGRES::getUsername); + registry.add("spring.datasource.password", POSTGRES::getPassword); + } + + @Autowired ExternalIdentityService identities; + @Autowired ExternalAccessPolicy policy; + @Autowired AccountService accounts; + @Autowired JdbcTemplate jdbc; + + @Autowired + org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository + registrations; + + @BeforeEach + void reset() { + org.busystack.caldav.testing.FixtureDatabase.clearAccounts(jdbc); + if (registrations.findById("fixture-client") == null) + registrations.save( + org.springframework.security.oauth2.server.authorization.client.RegisteredClient.withId( + "fixture-client") + .clientId("fixture-client") + .clientAuthenticationMethod( + org.springframework.security.oauth2.core.ClientAuthenticationMethod.NONE) + .authorizationGrantType( + org.springframework.security.oauth2.core.AuthorizationGrantType + .AUTHORIZATION_CODE) + .authorizationGrantType( + org.springframework.security.oauth2.core.AuthorizationGrantType.REFRESH_TOKEN) + .redirectUri("http://127.0.0.1:34567/callback") + .scope("dav") + .clientSettings( + org.springframework.security.oauth2.server.authorization.settings.ClientSettings + .builder() + .requireProofKey(true) + .requireAuthorizationConsent(true) + .build()) + .tokenSettings( + org.springframework.security.oauth2.server.authorization.settings.TokenSettings + .builder() + .accessTokenFormat( + org.springframework.security.oauth2.server.authorization.settings + .OAuth2TokenFormat.REFERENCE) + .reuseRefreshTokens(false) + .build()) + .build()); + } + + @Test + void exactIssuerSubjectProvisioningIsClosedAndNeverAssignsAdministrator() { + var account = identities.verifiedLogin(ISSUER, "subject-one", Instant.now(), null, null); + assertThat(account.username()).isEqualTo("allowed"); + assertThat(account.administrator()).isFalse(); + assertThat(account.passwordHash()).isNull(); + assertThat(identities.verifiedLogin(ISSUER, "subject-one", Instant.now(), null, null).id()) + .isEqualTo(account.id()); + assertThatThrownBy( + () -> + identities.verifiedLogin( + "https://attacker.example/realms/team", + "subject-one", + Instant.now(), + null, + null)) + .isInstanceOf(OAuth2AuthenticationException.class); + assertThatThrownBy(() -> identities.verifiedLogin(ISSUER, "unknown", Instant.now(), null, null)) + .isInstanceOf(OAuth2AuthenticationException.class); + assertThatThrownBy( + () -> + identities.verifiedLogin( + ISSUER, "subject-one", Instant.now().minusSeconds(301), null, null)) + .isInstanceOf(OAuth2AuthenticationException.class); + } + + @Test + void existingAccountNamesRequireExplicitRecentMfaLinking() { + var id = accounts.provision("allowed", "Existing admin", "correct horse battery staple", true); + assertThatThrownBy( + () -> identities.verifiedLogin(ISSUER, "subject-one", Instant.now(), null, null)) + .isInstanceOf(OAuth2AuthenticationException.class); + var auth = mfa("allowed"); + var link = identities.prepareLink(auth); + var linked = identities.verifiedLogin(ISSUER, "subject-one", Instant.now(), link, auth); + assertThat(linked.id()).isEqualTo(id); + assertThat(linked.administrator()).isTrue(); + assertThat(linked.passwordHash()).isNull(); + assertThatThrownBy( + () -> identities.verifiedLogin(ISSUER, "different-subject", Instant.now(), link, auth)) + .isInstanceOf(OAuth2AuthenticationException.class); + assertThatThrownBy( + () -> + identities.verifiedLogin( + ISSUER, + "subject-one", + Instant.now(), + new ExternalIdentityService.LinkIntent( + id, "allowed", Instant.now().minusSeconds(301)), + auth)) + .isInstanceOf(OAuth2AuthenticationException.class); + } + + @Test + void externalFreshnessAndLocalDisablingAreDurable() { + var account = identities.verifiedLogin(ISSUER, "subject-one", Instant.now(), null, null); + assertThat(policy.isCurrent(account.id())).isTrue(); + jdbc.update( + "UPDATE external_identities SET last_verified_at=now()-interval '25 hours' WHERE account_id=?", + account.id()); + assertThat(policy.isCurrent(account.id())).isFalse(); + identities.verifiedLogin(ISSUER, "subject-one", Instant.now(), null, null); + assertThat(policy.isCurrent(account.id())).isTrue(); + accounts.disable(account.id()); + assertThatThrownBy( + () -> identities.verifiedLogin(ISSUER, "subject-one", Instant.now(), null, null)) + .isInstanceOf(OAuth2AuthenticationException.class); + } + + @Test + void externalRecoveryRequiresTheExistingPairAndRevokesRestoredAccessBeforeReauthentication() { + var account = identities.verifiedLogin(ISSUER, "subject-one", Instant.now(), null, null); + var credential = + accounts.issueDavCredential( + account.id(), "Recovery fixture", Instant.now().plusSeconds(3600)); + jdbc.update( + "INSERT INTO oauth2_authorization(id,registered_client_id,principal_name,authorization_grant_type) VALUES ('recovery-fixture','fixture-client',?,'authorization_code')", + account.username()); + jdbc.update( + "INSERT INTO oauth2_authorization_consent(registered_client_id,principal_name,authorities) VALUES ('fixture-client',?,'SCOPE_dav')", + account.username()); + jdbc.update( + "INSERT INTO oauth_refresh_families(authorization_id,account_id,client_id,recovery_epoch,expires_at) SELECT 'recovery-fixture',?,'fixture-client',recovery_epoch,now()+interval '1 hour' FROM system_state WHERE id=1", + account.id()); + jdbc.update( + "INSERT INTO user_entities(id,account_id,name,display_name) VALUES ('cmVjb3ZlcnktdXNlcg',?,?,'Recovery fixture')", + account.id(), + account.username()); + jdbc.update( + "INSERT INTO user_credentials(credential_id,user_entity_user_id,public_key,signature_count,uv_initialized,backup_eligible,backup_state,label) VALUES ('cmVjb3Zlcnkta2V5','cmVjb3ZlcnktdXNlcg',decode('00','hex'),0,true,false,false,'Recovery fixture')"); + accounts.disable(account.id()); + assertThatThrownBy( + () -> accounts.recoverExternalOffline(account.username(), ISSUER, "wrong-subject")) + .isInstanceOf(IllegalArgumentException.class); + assertThat(accounts.find(account.id()).enabled()).isFalse(); + assertThat( + jdbc.queryForObject( + "SELECT count(*) FROM dav_credentials WHERE account_id=?", + Long.class, + account.id())) + .isEqualTo(1); + accounts.recoverExternalOffline(account.username(), ISSUER, "subject-one"); + assertThat(accounts.find(account.id()).enabled()).isTrue(); + assertThat(accounts.find(account.id()).passwordHash()).isNull(); + assertThat(policy.isCurrent(account.id())).isFalse(); + assertThat(accounts.verifyDav(account.username(), credential.secret())).isFalse(); + assertThat( + jdbc.queryForObject( + "SELECT count(*) FROM oauth2_authorization WHERE id='recovery-fixture'", + Long.class)) + .isZero(); + assertThat( + jdbc.queryForObject( + "SELECT count(*) FROM oauth2_authorization_consent WHERE principal_name=?", + Long.class, + account.username())) + .isZero(); + assertThat( + jdbc.queryForObject( + "SELECT count(*) FROM oauth_refresh_families WHERE authorization_id='recovery-fixture'", + Long.class)) + .isZero(); + assertThat( + jdbc.queryForObject( + "SELECT count(*) FROM user_credentials WHERE credential_id='cmVjb3Zlcnkta2V5'", + Long.class)) + .isZero(); + assertThat( + jdbc.queryForObject( + "SELECT count(*) FROM passkey_revocations WHERE credential_id='cmVjb3Zlcnkta2V5'", + Long.class)) + .isEqualTo(1); + assertThat(identities.verifiedLogin(ISSUER, "subject-one", Instant.now(), null, null).id()) + .isEqualTo(account.id()); + assertThat(policy.isCurrent(account.id())).isTrue(); + } + + private Authentication mfa(String username) { + return UsernamePasswordAuthenticationToken.authenticated( + username, + null, + List.of( + FactorGrantedAuthority.fromAuthority(FactorGrantedAuthority.PASSWORD_AUTHORITY), + FactorGrantedAuthority.fromAuthority(FactorGrantedAuthority.WEBAUTHN_AUTHORITY), + org.busystack.caldav.security.session.SessionSecurity.authority( + accounts.find(username).securityEpoch()))); + } +} diff --git a/src/test/java/org/busystack/caldav/security/oidc/KeycloakOidcIT.java b/src/test/java/org/busystack/caldav/security/oidc/KeycloakOidcIT.java new file mode 100644 index 0000000..17bfe5a --- /dev/null +++ b/src/test/java/org/busystack/caldav/security/oidc/KeycloakOidcIT.java @@ -0,0 +1,699 @@ +package org.busystack.caldav.security.oidc; + +import static org.assertj.core.api.Assertions.*; + +import java.io.StringReader; +import java.net.CookieManager; +import java.net.CookiePolicy; +import java.net.ServerSocket; +import java.net.URI; +import java.net.URLEncoder; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.nio.charset.StandardCharsets; +import java.time.Duration; +import java.time.Instant; +import java.util.Base64; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import javax.net.ssl.HttpsURLConnection; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSocketFactory; +import javax.swing.text.MutableAttributeSet; +import javax.swing.text.html.HTML; +import javax.swing.text.html.HTMLEditorKit; +import javax.swing.text.html.parser.ParserDelegator; +import org.busystack.caldav.account.AccountService; +import org.busystack.caldav.testing.TestTls; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Import; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.authority.FactorGrantedAuthority; +import org.springframework.security.core.context.SecurityContextImpl; +import org.springframework.security.core.userdetails.User; +import org.springframework.security.oauth2.core.oidc.user.OidcUser; +import org.springframework.security.web.context.HttpSessionSecurityContextRepository; +import org.springframework.session.Session; +import org.springframework.session.SessionRepository; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.wait.strategy.Wait; +import org.testcontainers.images.builder.ImageFromDockerfile; +import org.testcontainers.images.builder.Transferable; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; +import org.testcontainers.postgresql.PostgreSQLContainer; +import org.testcontainers.utility.MountableFile; +import tools.jackson.databind.json.JsonMapper; + +/** Actual Spring OAuth2 Login code/nonce/PKCE exchange with disposable Keycloak; no mock issuer. */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) +@Import(KeycloakOidcIT.ProbeConfiguration.class) +@org.springframework.test.annotation.DirtiesContext( + classMode = org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_CLASS) +@Testcontainers +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +class KeycloakOidcIT { + private static final int APP_PORT = reservePort(); + private static final String BASE = "https://localhost:" + APP_PORT; + private static final String ALICE = "d0880a2f-bb8e-4f40-84dd-f0707fe08c77"; + private static final String BOB = "4695db38-f030-4724-be1b-6e45a204bc65"; + private static final String USER_PASSWORD = "disposable-keycloak-user-password"; + private static final String ADMIN_PASSWORD = "disposable-keycloak-admin-password"; + private static final SSLContext PREVIOUS_SSL_CONTEXT = defaultSslContext(); + private static final SSLSocketFactory PREVIOUS_SOCKET_FACTORY = + HttpsURLConnection.getDefaultSSLSocketFactory(); + + static { + // Standard OIDC discovery/token/JWK clients use JVM trust. This isolated fork + // trusts only the disposable test CA; hostname verification remains enabled. + SSLContext.setDefault(TestTls.instance().context()); + HttpsURLConnection.setDefaultSSLSocketFactory(TestTls.instance().context().getSocketFactory()); + } + + @Container + static final PostgreSQLContainer POSTGRES = + org.busystack.caldav.testing.LoopbackContainers.postgres(); + + @Container + static final GenericContainer KEYCLOAK = + new GenericContainer<>( + new ImageFromDockerfile("busystack-keycloak-test:26.8.1", false) + .withDockerfile(java.nio.file.Path.of("qualification/keycloak/Dockerfile"))) + .withExposedPorts(8443) + .withCreateContainerCmdModifier( + cmd -> org.busystack.caldav.testing.LoopbackContainers.bind(cmd, 8443)) + .withLabel("org.busystack.caldav.test", "true") + .withEnv("KC_BOOTSTRAP_ADMIN_USERNAME", "fixture-admin") + .withEnv("KC_BOOTSTRAP_ADMIN_PASSWORD", ADMIN_PASSWORD) + .withEnv("JAVA_OPTS_KC_HEAP", "-Xms128m -Xmx512m") + .withCopyToContainer( + Transferable.of(realm()), "/opt/keycloak/data/import/busystack-fixture.json") + .withCopyToContainer( + MountableFile.forHostPath( + java.nio.file.Path.of(URI.create(TestTls.instance().keyStore())), 0444), + "/opt/keycloak/conf/test-server.p12") + .withCommand( + "start-dev", + "--import-realm", + "--hostname-strict=false", + "--https-port=8443", + "--https-key-store-file=/opt/keycloak/conf/test-server.p12", + "--https-key-store-password=" + TestTls.instance().password()) + .waitingFor( + Wait.forHttp("/realms/busystack-fixture/.well-known/openid-configuration") + .forPort(8443) + .usingTls() + .forStatusCode(200)) + // Augmentation and schema import exceeded three minutes on the shared legacy CPU. + // The qualification host needed just over five minutes under build contention. + // Readiness is still probed; this is a bounded deadline, not a fixed sleep. + .withStartupTimeout(Duration.ofMinutes(8)); + + @DynamicPropertySource + static void properties(DynamicPropertyRegistry registry) { + registry.add("spring.datasource.url", POSTGRES::getJdbcUrl); + registry.add("spring.datasource.username", POSTGRES::getUsername); + registry.add("spring.datasource.password", POSTGRES::getPassword); + registry.add("server.port", () -> APP_PORT); + registry.add("server.ssl.enabled", () -> true); + registry.add("server.ssl.key-store", () -> TestTls.instance().keyStore()); + registry.add("server.ssl.key-store-password", () -> TestTls.instance().password()); + registry.add("busystack.public-base-url", () -> BASE); + registry.add("busystack.issuer", () -> BASE); + registry.add("busystack.oidc.enabled", () -> true); + registry.add("busystack.oidc.issuer", KeycloakOidcIT::issuer); + registry.add("busystack.oidc.client-id", () -> "busystack-upstream"); + registry.add("busystack.oidc.client-secret", () -> "disposable-upstream-client-secret"); + registry.add("busystack.oidc.allowed-users[0].subject", () -> ALICE); + registry.add("busystack.oidc.allowed-users[0].username", () -> "internal-alice"); + registry.add("busystack.oidc.allowed-users[0].display-name", () -> "Internal Alice"); + } + + @Autowired AccountService accounts; + @Autowired JdbcTemplate jdbc; + @Autowired SessionRepository sessions; + @Autowired org.busystack.caldav.storage.CalendarStore calendars; + + @Autowired + org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository + registrations; + + private final JsonMapper json = JsonMapper.builder().build(); + private String lastLoginStage; + + @AfterAll + static void restoreJvmTrust() { + SSLContext.setDefault(PREVIOUS_SSL_CONTEXT); + HttpsURLConnection.setDefaultSSLSocketFactory(PREVIOUS_SOCKET_FACTORY); + } + + @BeforeEach + void reset() { + org.busystack.caldav.testing.FixtureDatabase.clearAccounts(jdbc); + if (registrations.findById("fixture-client") == null) + registrations.save( + org.springframework.security.oauth2.server.authorization.client.RegisteredClient.withId( + "fixture-client") + .clientId("fixture-client") + .clientAuthenticationMethod( + org.springframework.security.oauth2.core.ClientAuthenticationMethod.NONE) + .authorizationGrantType( + org.springframework.security.oauth2.core.AuthorizationGrantType + .AUTHORIZATION_CODE) + .authorizationGrantType( + org.springframework.security.oauth2.core.AuthorizationGrantType.REFRESH_TOKEN) + .redirectUri("http://127.0.0.1:34567/callback") + .scope("dav") + .clientSettings( + org.springframework.security.oauth2.server.authorization.settings.ClientSettings + .builder() + .requireProofKey(true) + .requireAuthorizationConsent(true) + .build()) + .tokenSettings( + org.springframework.security.oauth2.server.authorization.settings.TokenSettings + .builder() + .accessTokenFormat( + org.springframework.security.oauth2.server.authorization.settings + .OAuth2TokenFormat.REFERENCE) + .reuseRefreshTokens(false) + .build()) + .build()); + } + + @Test + @Order(1) + void verifiedRealmSubjectMapsToNonAdministratorAndUpstreamTokensCannotAuthenticateDav() + throws Exception { + var client = browser(); + var result = login(client, "realm-alice"); + assertThat(result.statusCode()) + .as("Login failed at %s: %s", lastLoginStage, visibleText(result.body())) + .isEqualTo(302); + assertThat(result.headers().firstValue("Location").orElse("")).doesNotContain("error"); + var probe = client.send(get(BASE + "/probe/oidc"), HttpResponse.BodyHandlers.ofString()); + assertThat(probe.statusCode()).isEqualTo(200); + var principal = json.readTree(probe.body()); + assertThat(principal.path("name").asString()).isEqualTo("internal-alice"); + assertThat(principal.path("authorities").toString()) + .contains("FACTOR_AUTHORIZATION_CODE", "BUSYSTACK_TRUSTED_UPSTREAM") + .doesNotContain("ROLE_ADMIN", "FACTOR_PASSWORD"); + var account = accounts.find("internal-alice"); + assertThat(account.administrator()).isFalse(); + assertThat(account.passwordHash()).isNull(); + var dav = + client.send( + HttpRequest.newBuilder(URI.create(BASE + "/dav/missing")) + .header("Authorization", "Bearer " + principal.path("idToken").asString()) + .GET() + .build(), + HttpResponse.BodyHandlers.ofString()); + assertThat(dav.statusCode()).isEqualTo(401); + var localFallback = browser(); + var page = localFallback.send(get(BASE + "/login"), HttpResponse.BodyHandlers.ofString()); + String csrf = parse(page.body()).hidden().get("_csrf"); + var password = + post( + localFallback, + BASE + "/login", + Map.of("_csrf", csrf, "username", "internal-alice", "password", USER_PASSWORD)); + assertThat(password.statusCode()).isEqualTo(302); + assertThat(password.headers().firstValue("Location").orElse("")).contains("error"); + } + + @Test + @Order(2) + void unknownRealmUserDoesNotAutoLinkAnExistingLocalUsername() throws Exception { + var localId = + accounts.provision("realm-bob", "Local owner", "correct horse battery staple", true); + var result = login(browser(), "realm-bob"); + assertThat(result.statusCode()) + .as("Login failed at %s: %s", lastLoginStage, visibleText(result.body())) + .isEqualTo(302); + assertThat(result.headers().firstValue("Location").orElse("")).contains("error"); + assertThat(accounts.find("realm-bob").id()).isEqualTo(localId); + assertThat(accounts.find("realm-bob").passwordHash()).isNotNull(); + assertThat(jdbc.queryForObject("SELECT count(*) FROM external_identities", Long.class)) + .isZero(); + } + + @Test + @Order(3) + void localDisablingRejectsAnAlreadyAuthenticatedFederatedBrowser() throws Exception { + var client = browser(); + var loggedIn = login(client, "realm-alice"); + assertThat(loggedIn.statusCode()) + .as("Login failed at %s: %s", lastLoginStage, visibleText(loggedIn.body())) + .isEqualTo(302); + assertThat(loggedIn.headers().firstValue("Location").orElse("")).doesNotContain("error"); + var account = accounts.find("internal-alice"); + accounts.disable(account.id()); + var revoked = client.send(get(BASE + "/probe/oidc"), HttpResponse.BodyHandlers.ofString()); + assertThat(revoked.statusCode()).isEqualTo(302); + assertThat(URI.create(BASE).resolve(revoked.headers().firstValue("Location").orElseThrow())) + .isEqualTo(URI.create(BASE + "/login")); + var result = login(browser(), "realm-alice"); + assertThat(result.headers().firstValue("Location").orElse("")).contains("error"); + } + + @Test + @Order(4) + void explicitMfaLinkingKeepsOwnershipAndReplacesTheOldSecurityContext() throws Exception { + // The initial MFA session and old OAuth rows are fixtures, not WebAuthn qualification. + // Linking, CSRF, provider password/code/PKCE, signed ID-token verification, + // callback, and replacement JDBC session exercise the real HTTP application. + var ownerId = + accounts.provision( + "linked-owner", "Existing local administrator", "correct horse battery staple", true); + var original = accounts.find(ownerId); + var calendarId = calendars.createCollection(ownerId, "Owned calendar", "VEVENT"); + var credential = + accounts.issueDavCredential(ownerId, "Before linking", Instant.now().plusSeconds(3600)); + jdbc.update( + "INSERT INTO oauth2_authorization(id,registered_client_id,principal_name,authorization_grant_type) VALUES ('link-fixture','fixture-client',?,'authorization_code')", + original.username()); + jdbc.update( + "INSERT INTO oauth2_authorization_consent(registered_client_id,principal_name,authorities) VALUES ('fixture-client',?,'SCOPE_dav')", + original.username()); + jdbc.update( + "INSERT INTO oauth_refresh_families(authorization_id,account_id,client_id,recovery_epoch,expires_at) SELECT 'link-fixture',?,'fixture-client',recovery_epoch,now()+interval '1 hour' FROM system_state WHERE id=1", + ownerId); + String oldSession = persistMfaSession(sessions, original); + var client = browserFor(oldSession); + var dashboard = client.send(get(BASE + "/"), HttpResponse.BodyHandlers.ofString()); + assertThat(dashboard.statusCode()).isEqualTo(200); + String csrf = parse(dashboard.body()).hidden().get("_csrf"); + assertThat(csrf).isNotBlank(); + assertThat(post(client, BASE + "/security/identities/link", Map.of()).statusCode()) + .isEqualTo(403); + URI unrelatedAuthorization = beginAuthorization(client); + var begin = post(client, BASE + "/security/identities/link", Map.of("_csrf", csrf)); + assertThat(begin.statusCode()).isEqualTo(302); + assertThat(URI.create(BASE).resolve(begin.headers().firstValue("Location").orElseThrow())) + .isEqualTo(URI.create(BASE + "/oauth2/authorization/upstream")); + URI linkAuthorization = beginAuthorization(client); + // Complete an earlier parallel request first. Its callback must leave the account + // untouched and must not consume the linking request's state-bound intent. + var unrelated = completeLogin(client, unrelatedAuthorization, "realm-bob"); + assertThat(unrelated.statusCode()).isEqualTo(302); + assertThat(unrelated.headers().firstValue("Location").orElse("")).contains("error"); + assertThat(accounts.find(ownerId).passwordHash()).isNotNull(); + assertThat(accounts.find(ownerId).securityEpoch()).isEqualTo(original.securityEpoch()); + assertThat(jdbc.queryForObject("SELECT count(*) FROM external_identities", Long.class)) + .isZero(); + assertThat(accounts.verifyDav(original.username(), credential.secret())).isTrue(); + var linked = completeLogin(client, linkAuthorization, "realm-alice"); + assertThat(linked.statusCode()) + .as("Linking failed at %s: %s", lastLoginStage, visibleText(linked.body())) + .isEqualTo(302); + assertThat(linked.headers().firstValue("Location").orElse("")).doesNotContain("error"); + var current = accounts.find(ownerId); + assertThat(current.username()).isEqualTo(original.username()); + assertThat(current.administrator()).isTrue(); + assertThat(current.passwordHash()).isNull(); + assertThat(current.securityEpoch()).isNotEqualTo(original.securityEpoch()); + assertThat(calendars.collection(ownerId, calendarId).owner()).isEqualTo(ownerId); + assertThat(accounts.find("internal-alice")).isNull(); + assertThat(accounts.verifyDav(current.username(), credential.secret())).isFalse(); + assertThat(sessions.findById(oldSession)).isNull(); + var oldBrowser = browserFor(oldSession); + var denied = oldBrowser.send(get(BASE + "/"), HttpResponse.BodyHandlers.ofString()); + assertThat(denied.statusCode()).isEqualTo(302); + assertThat(URI.create(BASE).resolve(denied.headers().firstValue("Location").orElseThrow())) + .isEqualTo(URI.create(BASE + "/login")); + assertThat( + jdbc.queryForObject( + "SELECT count(*) FROM oauth2_authorization WHERE id='link-fixture'", Long.class)) + .isZero(); + assertThat( + jdbc.queryForObject( + "SELECT count(*) FROM oauth2_authorization_consent WHERE principal_name=?", + Long.class, + current.username())) + .isZero(); + assertThat( + jdbc.queryForObject( + "SELECT revoked_at IS NOT NULL FROM oauth_refresh_families WHERE authorization_id='link-fixture'", + Boolean.class)) + .isTrue(); + var probe = client.send(get(BASE + "/probe/oidc"), HttpResponse.BodyHandlers.ofString()); + assertThat(probe.statusCode()).isEqualTo(200); + var principal = json.readTree(probe.body()); + assertThat(principal.path("name").asString()).isEqualTo(original.username()); + assertThat(principal.path("authorities").toString()) + .contains("ROLE_ADMIN", "BUSYSTACK_TRUSTED_UPSTREAM", "FACTOR_AUTHORIZATION_CODE") + .doesNotContain("FACTOR_PASSWORD", "FACTOR_WEBAUTHN"); + } + + @Test + @Order(5) + void providerDisablingAndOutageHaveTheDocumentedBoundedRevocationBoundary() throws Exception { + var client = browser(); + var loggedIn = login(client, "realm-alice"); + assertThat(loggedIn.statusCode()) + .as("Login failed at %s: %s", lastLoginStage, visibleText(loggedIn.body())) + .isEqualTo(302); + assertThat(loggedIn.headers().firstValue("Location").orElse("")).doesNotContain("error"); + var account = accounts.find("internal-alice"); + var credential = + accounts.issueDavCredential( + account.id(), "OIDC boundary fixture", Instant.now().plusSeconds(3600)); + var configured = + KEYCLOAK.execInContainer( + "/opt/keycloak/bin/kcadm.sh", + "config", + "credentials", + "--server", + "http://localhost:8080", + "--realm", + "master", + "--user", + "fixture-admin", + "--password", + ADMIN_PASSWORD); + assertThat(configured.getExitCode()).isZero(); + var disabled = + KEYCLOAK.execInContainer( + "/opt/keycloak/bin/kcadm.sh", + "update", + "users/" + ALICE, + "-r", + "busystack-fixture", + "-s", + "enabled=false"); + assertThat(disabled.getExitCode()).isZero(); + var refused = login(browser(), "realm-alice"); + assertThat(refused.statusCode()).isEqualTo(200); + assertThat(refused.body().toLowerCase(java.util.Locale.ROOT)) + .containsAnyOf("invalid username or password", "account is disabled"); + // OIDC login is not a real-time deprovisioning event for independent credentials. + assertThat(accounts.verifyDav(account.username(), credential.secret())).isTrue(); + KEYCLOAK.stop(); + assertThat(accounts.verifyDav(account.username(), credential.secret())).isTrue(); + jdbc.update( + "UPDATE external_identities SET last_verified_at=now()-interval '25 hours' WHERE account_id=?", + account.id()); + assertThat(accounts.verifyDav(account.username(), credential.secret())).isFalse(); + accounts.provision("local-survivor", "Local survivor", "correct horse battery staple", false); + var local = browser(); + var page = local.send(get(BASE + "/login"), HttpResponse.BodyHandlers.ofString()); + var localLogin = + post( + local, + BASE + "/login", + Map.of( + "_csrf", + parse(page.body()).hidden().get("_csrf"), + "username", + "local-survivor", + "password", + "correct horse battery staple")); + assertThat(localLogin.statusCode()).isEqualTo(302); + assertThat(localLogin.headers().firstValue("Location").orElse("")).doesNotContain("error"); + } + + private HttpResponse login(HttpClient client, String username) throws Exception { + return completeLogin(client, beginAuthorization(client), username); + } + + private URI beginAuthorization(HttpClient client) throws Exception { + var start = + client.send( + get(BASE + "/oauth2/authorization/upstream"), HttpResponse.BodyHandlers.ofString()); + assertThat(start.statusCode()).isEqualTo(302); + var authorization = URI.create(start.headers().firstValue("Location").orElseThrow()); + assertThat(authorization.toString()) + .contains("code_challenge_method=S256", "prompt=login", "max_age=0"); + return authorization; + } + + private HttpResponse completeLogin(HttpClient client, URI authorization, String username) + throws Exception { + var login = client.send(get(authorization.toString()), HttpResponse.BodyHandlers.ofString()); + assertThat(login.statusCode()).isEqualTo(200); + var form = parse(login.body()); + var values = new LinkedHashMap<>(form.hidden()); + values.put("username", username); + values.put("password", USER_PASSWORD); + values.put("credentialId", ""); + lastLoginStage = "Keycloak login form POST"; + var submitted = post(client, form.action(), values); + if (submitted.statusCode() != 302) return submitted; + String callback = submitted.headers().firstValue("Location").orElseThrow(); + assertThat(callback).startsWith(BASE + "/login/oauth2/code/upstream"); + lastLoginStage = "application OIDC callback"; + return client.send(get(callback), HttpResponse.BodyHandlers.ofString()); + } + + private HttpResponse post(HttpClient client, String url, Map values) + throws Exception { + String form = + values.entrySet().stream() + .map( + entry -> + URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8) + + "=" + + URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8)) + .collect(java.util.stream.Collectors.joining("&")); + return client.send( + HttpRequest.newBuilder(URI.create(url)) + .timeout(Duration.ofSeconds(20)) + .header("Content-Type", "application/x-www-form-urlencoded") + .POST(HttpRequest.BodyPublishers.ofString(form)) + .build(), + HttpResponse.BodyHandlers.ofString()); + } + + private HttpClient browser() { + return HttpClient.newBuilder() + .cookieHandler(new CookieManager(null, CookiePolicy.ACCEPT_ALL)) + .sslContext(TestTls.instance().context()) + .followRedirects(HttpClient.Redirect.NEVER) + .connectTimeout(Duration.ofSeconds(10)) + .build(); + } + + private HttpClient browserFor(String sessionId) throws java.io.IOException { + var cookies = new CookieManager(null, CookiePolicy.ACCEPT_ALL); + String encoded = Base64.getEncoder().encodeToString(sessionId.getBytes(StandardCharsets.UTF_8)); + cookies.put( + URI.create(BASE), + Map.of( + "Set-Cookie", + List.of("JSESSIONID=" + encoded + "; Path=/; Secure; HttpOnly; SameSite=Lax"))); + return HttpClient.newBuilder() + .cookieHandler(cookies) + .sslContext(TestTls.instance().context()) + .followRedirects(HttpClient.Redirect.NEVER) + .connectTimeout(Duration.ofSeconds(10)) + .build(); + } + + private static String persistMfaSession( + SessionRepository sessions, AccountService.Account account) { + var principal = + User.withUsername(account.username()) + .password(account.passwordHash()) + .authorities("ROLE_ADMIN") + .build(); + var authentication = + UsernamePasswordAuthenticationToken.authenticated( + principal, + null, + List.of( + new org.springframework.security.core.authority.SimpleGrantedAuthority( + "ROLE_ADMIN"), + org.busystack.caldav.security.session.SessionSecurity.authority( + account.securityEpoch()), + FactorGrantedAuthority.fromAuthority(FactorGrantedAuthority.PASSWORD_AUTHORITY), + FactorGrantedAuthority.fromAuthority(FactorGrantedAuthority.WEBAUTHN_AUTHORITY))); + S session = sessions.createSession(); + session.setAttribute( + HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, + new SecurityContextImpl(authentication)); + sessions.save(session); + return session.getId(); + } + + private HttpRequest get(String uri) { + return HttpRequest.newBuilder(URI.create(uri)).timeout(Duration.ofSeconds(20)).GET().build(); + } + + private record Form(String action, Map hidden) {} + + private String visibleText(String html) throws Exception { + var text = new StringBuilder(); + new ParserDelegator() + .parse( + new StringReader(html), + new HTMLEditorKit.ParserCallback() { + @Override + public void handleText(char[] value, int position) { + if (text.length() < 500) text.append(value).append(' '); + } + }, + true); + return text.substring(0, Math.min(text.length(), 500)); + } + + private Form parse(String html) throws Exception { + var action = new java.util.concurrent.atomic.AtomicReference(); + var fields = new LinkedHashMap(); + new ParserDelegator() + .parse( + new StringReader(html), + new HTMLEditorKit.ParserCallback() { + @Override + public void handleStartTag( + HTML.Tag tag, MutableAttributeSet attributes, int position) { + if (tag == HTML.Tag.FORM + && (action.get() == null + || "kc-form-login".equals(attributes.getAttribute(HTML.Attribute.ID)))) + action.set((String) attributes.getAttribute(HTML.Attribute.ACTION)); + } + + @Override + public void handleSimpleTag( + HTML.Tag tag, MutableAttributeSet attributes, int position) { + if (tag == HTML.Tag.INPUT + && attributes.getAttribute(HTML.Attribute.NAME) != null + && "hidden".equals(attributes.getAttribute(HTML.Attribute.TYPE))) + fields.put( + (String) attributes.getAttribute(HTML.Attribute.NAME), + String.valueOf(attributes.getAttribute(HTML.Attribute.VALUE))); + } + }, + true); + return new Form(action.get(), fields); + } + + private static int reservePort() { + try (var socket = new ServerSocket(0)) { + return socket.getLocalPort(); + } catch (java.io.IOException ex) { + throw new IllegalStateException(ex); + } + } + + private static String issuer() { + return "https://localhost:" + KEYCLOAK.getMappedPort(8443) + "/realms/busystack-fixture"; + } + + private static SSLContext defaultSslContext() { + try { + return SSLContext.getDefault(); + } catch (java.security.NoSuchAlgorithmException ex) { + throw new IllegalStateException(ex); + } + } + + private static byte[] realm() { + Map client = + Map.ofEntries( + Map.entry("clientId", "busystack-upstream"), + Map.entry("secret", "disposable-upstream-client-secret"), + Map.entry("protocol", "openid-connect"), + Map.entry("publicClient", false), + Map.entry("standardFlowEnabled", true), + Map.entry("directAccessGrantsEnabled", false), + Map.entry("implicitFlowEnabled", false), + Map.entry("serviceAccountsEnabled", false), + Map.entry("redirectUris", List.of(BASE + "/login/oauth2/code/upstream")), + Map.entry("webOrigins", List.of(BASE)), + Map.entry("attributes", Map.of("pkce.code.challenge.method", "S256"))); + var users = + List.of( + Map.of( + "id", + ALICE, + "username", + "realm-alice", + "enabled", + true, + "email", + "alice@example.test", + "emailVerified", + true, + "firstName", + "Realm", + "lastName", + "Alice", + "realmRoles", + List.of("admin"), + "credentials", + List.of(Map.of("type", "password", "value", USER_PASSWORD, "temporary", false))), + Map.of( + "id", + BOB, + "username", + "realm-bob", + "enabled", + true, + "email", + "bob@example.test", + "emailVerified", + true, + "firstName", + "Realm", + "lastName", + "Bob", + "credentials", + List.of(Map.of("type", "password", "value", USER_PASSWORD, "temporary", false)))); + return JsonMapper.builder() + .build() + .writeValueAsBytes( + Map.of( + "realm", + "busystack-fixture", + "enabled", + true, + "sslRequired", + "none", + "roles", + Map.of("realm", List.of(Map.of("name", "admin"))), + "clients", + List.of(client), + "users", + users)); + } + + @TestConfiguration + static class ProbeConfiguration { + @Bean + ProbeController oidcProbe() { + return new ProbeController(); + } + } + + @RestController + static class ProbeController { + @GetMapping("/probe/oidc") + Map principal(Authentication authentication) { + var user = (OidcUser) authentication.getPrincipal(); + return Map.of( + "name", + authentication.getName(), + "authorities", + authentication.getAuthorities().stream().map(a -> a.getAuthority()).toList(), + "idToken", + user.getIdToken().getTokenValue()); + } + } +} diff --git a/src/test/java/org/busystack/caldav/security/oidc/LinkingAuthorizationRequestResolverTest.java b/src/test/java/org/busystack/caldav/security/oidc/LinkingAuthorizationRequestResolverTest.java new file mode 100644 index 0000000..d16d669 --- /dev/null +++ b/src/test/java/org/busystack/caldav/security/oidc/LinkingAuthorizationRequestResolverTest.java @@ -0,0 +1,94 @@ +package org.busystack.caldav.security.oidc; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.time.Instant; +import java.util.UUID; +import org.junit.jupiter.api.Test; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpSession; +import org.springframework.security.oauth2.client.registration.ClientRegistration; +import org.springframework.security.oauth2.client.registration.InMemoryClientRegistrationRepository; +import org.springframework.security.oauth2.client.web.DefaultOAuth2AuthorizationRequestResolver; +import org.springframework.security.oauth2.core.AuthorizationGrantType; + +class LinkingAuthorizationRequestResolverTest { + @Test + void aParallelLoginCannotConsumeOrApplyAnotherRequestsLinkingIntent() { + var resolver = resolver(); + var session = new MockHttpSession(); + var parallel = resolver.resolve(start(session)); + var intent = new ExternalIdentityService.LinkIntent(UUID.randomUUID(), "alice", Instant.now()); + session.setAttribute(LinkingAuthorizationRequestResolver.PENDING_LINK, intent); + var linking = resolver.resolve(start(session)); + assertThat(linking.getState()).isNotEqualTo(parallel.getState()); + assertThat(session.getAttribute(LinkingAuthorizationRequestResolver.PENDING_LINK)).isNull(); + + assertThat(LinkingAuthorizationRequestResolver.consume(callback(session, parallel.getState()))) + .isNull(); + assertThat(LinkingAuthorizationRequestResolver.consume(callback(session, "wrong-state"))) + .isNull(); + assertThat(LinkingAuthorizationRequestResolver.consume(callback(session, linking.getState()))) + .isEqualTo(intent); + assertThat(LinkingAuthorizationRequestResolver.consume(callback(session, linking.getState()))) + .isNull(); + } + + @Test + void unrelatedRequestsCannotBindAnIntentAndAnUnstartedIntentCannotLink() { + var resolver = resolver(); + var session = new MockHttpSession(); + var intent = new ExternalIdentityService.LinkIntent(UUID.randomUUID(), "alice", Instant.now()); + session.setAttribute(LinkingAuthorizationRequestResolver.PENDING_LINK, intent); + var unrelated = new MockHttpServletRequest("GET", "/unrelated"); + unrelated.setServletPath("/unrelated"); + unrelated.setSession(session); + assertThat(resolver.resolve(unrelated)).isNull(); + assertThat(session.getAttribute(LinkingAuthorizationRequestResolver.PENDING_LINK)) + .isEqualTo(intent); + assertThat(LinkingAuthorizationRequestResolver.consume(callback(session, "arbitrary-state"))) + .isNull(); + } + + @Test + void explicitRegistrationResolutionUsesTheSameStateBinding() { + var resolver = resolver(); + var session = new MockHttpSession(); + var intent = new ExternalIdentityService.LinkIntent(UUID.randomUUID(), "alice", Instant.now()); + session.setAttribute(LinkingAuthorizationRequestResolver.PENDING_LINK, intent); + var authorization = resolver.resolve(start(session), "upstream"); + assertThat( + LinkingAuthorizationRequestResolver.consume( + callback(session, authorization.getState()))) + .isEqualTo(intent); + } + + private LinkingAuthorizationRequestResolver resolver() { + var client = + ClientRegistration.withRegistrationId("upstream") + .clientId("fixture") + .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE) + .authorizationUri("https://issuer.example/authorize") + .tokenUri("https://issuer.example/token") + .redirectUri("https://calendar.example/login/oauth2/code/upstream") + .scope("openid") + .build(); + return new LinkingAuthorizationRequestResolver( + new DefaultOAuth2AuthorizationRequestResolver( + new InMemoryClientRegistrationRepository(client), "/oauth2/authorization")); + } + + private MockHttpServletRequest start(MockHttpSession session) { + var request = new MockHttpServletRequest("GET", "/oauth2/authorization/upstream"); + request.setServletPath("/oauth2/authorization/upstream"); + request.setSession(session); + return request; + } + + private MockHttpServletRequest callback(MockHttpSession session, String state) { + var request = new MockHttpServletRequest("GET", "/login/oauth2/code/upstream"); + request.setSession(session); + request.addParameter("state", state); + return request; + } +} diff --git a/src/test/java/org/busystack/caldav/security/oidc/OidcBoundaryIT.java b/src/test/java/org/busystack/caldav/security/oidc/OidcBoundaryIT.java new file mode 100644 index 0000000..834a092 --- /dev/null +++ b/src/test/java/org/busystack/caldav/security/oidc/OidcBoundaryIT.java @@ -0,0 +1,395 @@ +package org.busystack.caldav.security.oidc; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import com.nimbusds.jose.JOSEObjectType; +import com.nimbusds.jose.JWSAlgorithm; +import com.nimbusds.jose.JWSHeader; +import com.nimbusds.jose.crypto.MACSigner; +import com.nimbusds.jose.crypto.RSASSASigner; +import com.nimbusds.jose.jwk.JWKSet; +import com.nimbusds.jose.jwk.RSAKey; +import com.nimbusds.jose.jwk.gen.RSAKeyGenerator; +import com.nimbusds.jwt.JWTClaimsSet; +import com.nimbusds.jwt.SignedJWT; +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpsConfigurator; +import com.sun.net.httpserver.HttpsServer; +import java.net.InetSocketAddress; +import java.net.URI; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.security.KeyStore; +import java.time.Instant; +import java.util.Date; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicInteger; +import javax.net.ssl.HttpsURLConnection; +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSocketFactory; +import org.busystack.caldav.account.AccountService; +import org.busystack.caldav.config.DeploymentProperties; +import org.busystack.caldav.testing.SignedPasskeyClient; +import org.busystack.caldav.testing.TestTls; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.server.LocalServerPort; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; +import org.testcontainers.postgresql.PostgreSQLContainer; + +/** + * Production OIDC filters and discovery/JWKS/UserInfo clients against a signed disposable issuer. + */ +@SpringBootTest( + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, + properties = { + "busystack.public-base-url=https://localhost/team", + "busystack.issuer=https://localhost/team", + "busystack.oidc.enabled=true", + "busystack.oidc.client-id=boundary-client", + "busystack.oidc.client-secret=boundary-client-secret", + "busystack.oidc.allowed-users[0].subject=boundary-subject", + "busystack.oidc.allowed-users[0].username=boundary-user", + "busystack.oidc.allowed-users[0].display-name=Boundary User" + }) +@org.springframework.test.annotation.DirtiesContext( + classMode = org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_CLASS) +@Testcontainers +class OidcBoundaryIT { + @Container + static final PostgreSQLContainer POSTGRES = + org.busystack.caldav.testing.LoopbackContainers.postgres(); + + private static final SSLContext PREVIOUS_TLS = defaultTls(); + private static final SSLSocketFactory PREVIOUS_SOCKET = + HttpsURLConnection.getDefaultSSLSocketFactory(); + private static final Issuer PROVIDER = new Issuer(); + + static { + SSLContext.setDefault(TestTls.instance().context()); + HttpsURLConnection.setDefaultSSLSocketFactory(TestTls.instance().context().getSocketFactory()); + } + + @DynamicPropertySource + static void properties(DynamicPropertyRegistry registry) { + registry.add("spring.datasource.url", POSTGRES::getJdbcUrl); + registry.add("spring.datasource.username", POSTGRES::getUsername); + registry.add("spring.datasource.password", POSTGRES::getPassword); + registry.add("server.ssl.enabled", () -> true); + registry.add("server.ssl.key-store", () -> TestTls.instance().keyStore()); + registry.add("server.ssl.key-store-password", () -> TestTls.instance().password()); + registry.add("busystack.oidc.issuer", PROVIDER::issuer); + } + + @AfterAll + static void stop() { + PROVIDER.server.stop(0); + SSLContext.setDefault(PREVIOUS_TLS); + HttpsURLConnection.setDefaultSSLSocketFactory(PREVIOUS_SOCKET); + } + + @Autowired JdbcTemplate jdbc; + @Autowired AccountService accounts; + @Autowired OidcProperties properties; + @Autowired DeploymentProperties deployment; + @LocalServerPort int port; + + @BeforeEach + void clear() { + org.busystack.caldav.testing.FixtureDatabase.clearAccounts(jdbc); + PROVIDER.mode = "correct"; + } + + @Test + void configuredDiscoveryRejectsAnIssuerMismatchOverNormallyVerifiedHttps() { + PROVIDER.mode = "discovery-issuer"; + try { + var registration = new ConfiguredOidcRegistrationRepository(properties, deployment); + assertThatThrownBy(() -> registration.findByRegistrationId("upstream")) + .isInstanceOf(RuntimeException.class); + } finally { + PROVIDER.mode = "correct"; + } + } + + @Test + void signedIssuerAudienceExpiryNonceSignatureAlgorithmAndUserInfoNegativesCannotProvision() + throws Exception { + for (String mode : + List.of( + "issuer", + "audience", + "azp", + "expired", + "future-issued", + "nonce", + "signature", + "unknown-kid", + "hmac-algorithm", + "userinfo-subject")) { + PROVIDER.mode = mode; + var result = login(); + assertThat(result.statusCode()).as(mode + ": " + result.body()).isEqualTo(302); + assertThat(result.headers().firstValue("Location").orElseThrow()).as(mode).contains("error"); + assertThat(accounts.find("boundary-user")).as(mode).isNull(); + assertThat(jdbc.queryForObject("SELECT count(*) FROM external_identities", Long.class)) + .isZero(); + } + assertThat(PROVIDER.userInfos.get()).isPositive(); + assertThat(PROVIDER.jwks.get()).isPositive(); + } + + @Test + void validUpstreamIdentityAndARealJwksKeyRotationKeepTheSameNonAdministratorAccount() + throws Exception { + var first = login(); + assertThat(first.statusCode()).as(first.body()).isEqualTo(302); + assertThat(first.headers().firstValue("Location").orElseThrow()).doesNotContain("error"); + var account = accounts.find("boundary-user"); + assertThat(account).isNotNull(); + assertThat(account.administrator()).isFalse(); + assertThat(account.passwordHash()).isNull(); + PROVIDER.mode = "multi-audience"; + var multi = login(); + assertThat(multi.headers().firstValue("Location").orElseThrow()).doesNotContain("error"); + assertThat(accounts.find("boundary-user").id()).isEqualTo(account.id()); + PROVIDER.rotate(); + var second = login(); + assertThat(second.statusCode()).as(second.body()).isEqualTo(302); + assertThat(second.headers().firstValue("Location").orElseThrow()).doesNotContain("error"); + assertThat(accounts.find("boundary-user").id()).isEqualTo(account.id()); + assertThat(jdbc.queryForObject("SELECT count(*) FROM external_identities", Long.class)) + .isEqualTo(1); + } + + @Test + void wrongStateIsRejectedBeforeAnyTokenRequestOrAccountProvisioning() throws Exception { + try (var transport = new SignedPasskeyClient(port, "/team", "https://localhost")) { + var browser = transport.browser(); + var begin = transport.get(browser, "/oauth2/authorization/upstream"); + String authorization = begin.headers().firstValue("Location").orElseThrow(); + var provider = + browser.send( + java.net.http.HttpRequest.newBuilder(URI.create(authorization)).GET().build(), + java.net.http.HttpResponse.BodyHandlers.ofString()); + String callback = provider.headers().firstValue("Location").orElseThrow(); + var parameters = SignedPasskeyClient.query(callback); + parameters.put("state", "unrelated-state"); + int tokens = PROVIDER.tokens.get(); + var denied = + transport.get( + browser, "/login/oauth2/code/upstream?" + SignedPasskeyClient.form(parameters)); + assertThat(denied.statusCode()).isEqualTo(302); + assertThat(denied.headers().firstValue("Location").orElseThrow()).contains("error"); + assertThat(PROVIDER.tokens.get()).isEqualTo(tokens); + assertThat(accounts.find("boundary-user")).isNull(); + } + } + + private java.net.http.HttpResponse login() throws Exception { + try (var transport = new SignedPasskeyClient(port, "/team", "https://localhost")) { + var browser = transport.browser(); + var begin = transport.get(browser, "/oauth2/authorization/upstream"); + assertThat(begin.statusCode()).isEqualTo(302); + String authorization = begin.headers().firstValue("Location").orElseThrow(); + var parameters = SignedPasskeyClient.query(authorization); + assertThat(parameters.get("code_challenge_method")).isEqualTo("S256"); + assertThat(parameters.get("nonce")).isNotBlank(); + var provider = + browser.send( + java.net.http.HttpRequest.newBuilder(URI.create(authorization)).GET().build(), + java.net.http.HttpResponse.BodyHandlers.ofString()); + assertThat(provider.statusCode()).isEqualTo(302); + String callback = provider.headers().firstValue("Location").orElseThrow(); + return transport.get(browser, transport.localPath(callback)); + } + } + + private static SSLContext defaultTls() { + try { + return SSLContext.getDefault(); + } catch (java.security.NoSuchAlgorithmException failure) { + throw new IllegalStateException(failure); + } + } + + private static final class Issuer { + final HttpsServer server; + final Map> codes = new ConcurrentHashMap<>(); + final AtomicInteger tokens = new AtomicInteger(); + final AtomicInteger jwks = new AtomicInteger(); + final AtomicInteger userInfos = new AtomicInteger(); + volatile RSAKey signingKey; + volatile String mode = "correct"; + + Issuer() { + try { + rotate(); + KeyStore store = KeyStore.getInstance("PKCS12"); + try (var stream = + Files.newInputStream(Path.of(URI.create(TestTls.instance().keyStore())))) { + store.load(stream, TestTls.instance().password().toCharArray()); + } + var managers = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); + managers.init(store, TestTls.instance().password().toCharArray()); + var tls = SSLContext.getInstance("TLS"); + tls.init(managers.getKeyManagers(), null, null); + server = HttpsServer.create(new InetSocketAddress("127.0.0.1", 0), 0); + server.setHttpsConfigurator(new HttpsConfigurator(tls)); + server.createContext("/provider/", this::handle); + server.start(); + } catch (Exception failure) { + throw new IllegalStateException(failure); + } + } + + void rotate() throws Exception { + signingKey = new RSAKeyGenerator(2048).keyID(UUID.randomUUID().toString()).generate(); + } + + String issuer() { + return "https://localhost:" + server.getAddress().getPort() + "/provider"; + } + + void handle(HttpExchange exchange) throws java.io.IOException { + try { + String path = exchange.getRequestURI().getPath(); + if (path.endsWith("/.well-known/openid-configuration")) { + json( + exchange, + 200, + Map.ofEntries( + Map.entry( + "issuer", + mode.equals("discovery-issuer") ? "https://other.invalid" : issuer()), + Map.entry("authorization_endpoint", issuer() + "/authorize"), + Map.entry("token_endpoint", issuer() + "/token"), + Map.entry("jwks_uri", issuer() + "/jwks"), + Map.entry("userinfo_endpoint", issuer() + "/userinfo"), + Map.entry("response_types_supported", List.of("code")), + Map.entry("subject_types_supported", List.of("public")), + Map.entry("id_token_signing_alg_values_supported", List.of("RS256")), + Map.entry( + "token_endpoint_auth_methods_supported", List.of("client_secret_basic")), + Map.entry("code_challenge_methods_supported", List.of("S256")))); + } else if (path.endsWith("/jwks")) { + jwks.incrementAndGet(); + json(exchange, 200, new JWKSet(signingKey.toPublicJWK()).toJSONObject()); + } else if (path.endsWith("/authorize")) { + var query = SignedPasskeyClient.query("https://localhost" + exchange.getRequestURI()); + String code = UUID.randomUUID().toString(); + codes.put(code, query); + exchange + .getResponseHeaders() + .set( + "Location", + query.get("redirect_uri") + + "?" + + SignedPasskeyClient.form( + Map.of("code", code, "state", query.get("state")))); + exchange.sendResponseHeaders(302, -1); + exchange.close(); + } else if (path.endsWith("/token")) { + tokens.incrementAndGet(); + String body = + new String(exchange.getRequestBody().readAllBytes(), StandardCharsets.UTF_8); + var form = SignedPasskeyClient.query("https://localhost/?" + body); + var request = codes.remove(form.get("code")); + if (request == null + || !SignedPasskeyClient.encoded( + SignedPasskeyClient.hash( + form.get("code_verifier").getBytes(StandardCharsets.US_ASCII))) + .equals(request.get("code_challenge"))) { + json(exchange, 400, Map.of("error", "invalid_grant")); + return; + } + Instant now = Instant.now(); + var claims = + new JWTClaimsSet.Builder() + .issuer(mode.equals("issuer") ? "https://other.invalid" : issuer()) + .subject("boundary-subject") + .audience( + mode.equals("audience") + ? List.of("other-client") + : mode.equals("azp") || mode.equals("multi-audience") + ? List.of("boundary-client", "other-client") + : List.of("boundary-client")) + .issueTime(Date.from(mode.equals("future-issued") ? now.plusSeconds(600) : now)) + .expirationTime( + Date.from( + mode.equals("expired") ? now.minusSeconds(600) : now.plusSeconds(600))) + .claim("nonce", mode.equals("nonce") ? "unrelated-nonce" : request.get("nonce")) + .claim("auth_time", now.getEpochSecond()); + if (mode.equals("azp")) claims.claim("azp", "other-client"); + if (mode.equals("multi-audience")) claims.claim("azp", "boundary-client"); + SignedJWT token; + if (mode.equals("hmac-algorithm")) { + token = new SignedJWT(new JWSHeader(JWSAlgorithm.HS256), claims.build()); + token.sign(new MACSigner(new byte[32])); + } else { + token = + new SignedJWT( + new JWSHeader.Builder(JWSAlgorithm.RS256) + .type(JOSEObjectType.JWT) + .keyID(mode.equals("unknown-kid") ? "unknown-key" : signingKey.getKeyID()) + .build(), + claims.build()); + token.sign( + new RSASSASigner( + mode.equals("signature") ? new RSAKeyGenerator(2048).generate() : signingKey)); + } + json( + exchange, + 200, + Map.of( + "access_token", + "fixture-upstream-access", + "token_type", + "Bearer", + "expires_in", + 600, + "scope", + "openid profile", + "id_token", + token.serialize())); + } else if (path.endsWith("/userinfo")) { + userInfos.incrementAndGet(); + json( + exchange, + 200, + Map.of( + "sub", + mode.equals("userinfo-subject") ? "other-subject" : "boundary-subject", + "name", + "Untrusted display name", + "roles", + List.of("admin"))); + } else json(exchange, 404, Map.of("error", "not_found")); + } catch (Exception failure) { + json(exchange, 500, Map.of("error", failure.getClass().getSimpleName())); + } + } + + private static void json(HttpExchange exchange, int status, Object value) + throws java.io.IOException { + byte[] bytes = SignedPasskeyClient.JSON.writeValueAsBytes(value); + exchange.getResponseHeaders().set("Content-Type", "application/json"); + exchange.sendResponseHeaders(status, bytes.length); + exchange.getResponseBody().write(bytes); + exchange.close(); + } + } +} diff --git a/src/test/java/org/busystack/caldav/security/oidc/OidcPropertiesTest.java b/src/test/java/org/busystack/caldav/security/oidc/OidcPropertiesTest.java new file mode 100644 index 0000000..2fb4a53 --- /dev/null +++ b/src/test/java/org/busystack/caldav/security/oidc/OidcPropertiesTest.java @@ -0,0 +1,27 @@ +package org.busystack.caldav.security.oidc; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.util.List; +import org.junit.jupiter.api.Test; + +class OidcPropertiesTest { + @Test + void allowlistDisplayNamesMustBeXmlRepresentableBeforeIdentityProvisioning() { + for (String invalid : + List.of("Invalid\uFFFE", "Invalid\uFFFF", "Invalid\u0001", "Invalid\uD800")) { + assertThatThrownBy(() -> new OidcProperties.AllowedUser("subject", "alice", invalid)) + .isInstanceOf(IllegalArgumentException.class); + } + for (String valid : + List.of( + "Valid\uFDD0", + "Valid" + new String(Character.toChars(0x1FFFE)), + "Line\nName\tAllowed", + "DEL\u007fC1\u0085")) { + assertThat(new OidcProperties.AllowedUser("subject", "alice", valid).displayName()) + .isEqualTo(valid); + } + } +} diff --git a/src/test/java/org/busystack/caldav/security/passkey/PasskeyCeremonyGuardTest.java b/src/test/java/org/busystack/caldav/security/passkey/PasskeyCeremonyGuardTest.java new file mode 100644 index 0000000..d09a679 --- /dev/null +++ b/src/test/java/org/busystack/caldav/security/passkey/PasskeyCeremonyGuardTest.java @@ -0,0 +1,161 @@ +package org.busystack.caldav.security.passkey; + +import static org.assertj.core.api.Assertions.*; +import static org.mockito.Mockito.*; + +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.mock.web.MockHttpSession; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.authority.FactorGrantedAuthority; +import org.springframework.security.core.context.SecurityContextHolder; + +class PasskeyCeremonyGuardTest { + @Test + void streamedJsonIsBoundedAndEncodedBodiesAreRejected() throws Exception { + var guard = + new PasskeyCeremonyGuard(mock(AccountPasskeyUsers.class), mock(CeremonyStore.class)); + var request = request("/webauthn/authenticate/options", new MockHttpSession()); + request.setContentType("application/json"); + request.setContent(new byte[65537]); + request.addHeader("Transfer-Encoding", "chunked"); + var chunked = + new jakarta.servlet.http.HttpServletRequestWrapper(request) { + @Override + public long getContentLengthLong() { + return -1; + } + + @Override + public int getContentLength() { + return -1; + } + }; + var oversized = new MockHttpServletResponse(); + var calls = new AtomicInteger(); + guard.doFilter(chunked, oversized, (q, s) -> calls.incrementAndGet()); + assertThat(oversized.getStatus()).isEqualTo(413); + assertThat(calls).hasValue(0); + request.setContent(new byte[0]); + request.addHeader("Content-Encoding", "gzip"); + var encoded = new MockHttpServletResponse(); + guard.doFilter(request, encoded, (q, s) -> calls.incrementAndGet()); + assertThat(encoded.getStatus()).isEqualTo(415); + assertThat(calls).hasValue(0); + } + + @Test + void frameworkCanReadBoundedJsonAndFormsKeepTheirParameters() throws Exception { + var guard = + new PasskeyCeremonyGuard(mock(AccountPasskeyUsers.class), mock(CeremonyStore.class)); + var request = request("/webauthn/authenticate/options", new MockHttpSession()); + request.setContentType("application/json"); + request.setContent( + "{\"value\":\"calendar\"}".getBytes(java.nio.charset.StandardCharsets.UTF_8)); + guard.doFilter( + request, + new MockHttpServletResponse(), + (q, s) -> assertThat(q.getReader().readLine()).isEqualTo("{\"value\":\"calendar\"}")); + var form = request("/webauthn/authenticate/options", new MockHttpSession()); + form.setContentType("application/x-www-form-urlencoded"); + form.setParameter("_csrf", "test-csrf"); + guard.doFilter( + form, + new MockHttpServletResponse(), + (q, s) -> { + assertThat(q.getParameter("_csrf")).isEqualTo("test-csrf"); + }); + } + + @AfterEach + void clear() { + SecurityContextHolder.clearContext(); + } + + @Test + void firstEnrollmentRequiresPasswordAndSubsequentEnrollmentRequiresBothFactors() + throws Exception { + var users = mock(AccountPasskeyUsers.class); + var guard = new PasskeyCeremonyGuard(users, mock(CeremonyStore.class)); + var calls = new AtomicInteger(); + var request = request("/webauthn/register/options", new MockHttpSession()); + var denied = new MockHttpServletResponse(); + guard.doFilter(request, denied, (q, s) -> calls.incrementAndGet()); + assertThat(denied.getStatus()).isEqualTo(403); + assertThat(calls).hasValue(0); + SecurityContextHolder.getContext() + .setAuthentication( + UsernamePasswordAuthenticationToken.authenticated( + "owner", + null, + List.of( + FactorGrantedAuthority.fromAuthority( + FactorGrantedAuthority.PASSWORD_AUTHORITY)))); + var first = new MockHttpServletResponse(); + guard.doFilter(request, first, (q, s) -> calls.incrementAndGet()); + assertThat(calls).hasValue(1); + when(users.hasCredentials("owner")).thenReturn(true); + var additional = new MockHttpServletResponse(); + guard.doFilter(request, additional, (q, s) -> calls.incrementAndGet()); + assertThat(additional.getStatus()).isEqualTo(403); + assertThat(calls).hasValue(1); + SecurityContextHolder.getContext() + .setAuthentication( + UsernamePasswordAuthenticationToken.authenticated( + "owner", + null, + List.of( + FactorGrantedAuthority.fromAuthority(FactorGrantedAuthority.PASSWORD_AUTHORITY), + FactorGrantedAuthority.fromAuthority( + FactorGrantedAuthority.WEBAUTHN_AUTHORITY)))); + guard.doFilter(request, new MockHttpServletResponse(), (q, s) -> calls.incrementAndGet()); + assertThat(calls).hasValue(2); + } + + @Test + void frameworkChallengeIsRecordedAndDurableConsumeDecisionGatesAssertion() throws Exception { + var store = mock(CeremonyStore.class); + var guard = new PasskeyCeremonyGuard(mock(AccountPasskeyUsers.class), store); + var session = new MockHttpSession(); + var challenge = org.springframework.security.web.webauthn.api.Bytes.random(); + var options = + org.springframework.security.web.webauthn.api.PublicKeyCredentialRequestOptions.builder() + .challenge(challenge) + .rpId("calendar.example") + .build(); + guard.doFilter( + request("/webauthn/authenticate/options", session), + new MockHttpServletResponse(), + (q, s) -> + ((jakarta.servlet.http.HttpServletRequest) q) + .getSession() + .setAttribute("framework.requestOptions", options)); + String hash = + org.busystack.caldav.security.audit.SecurityHashes.sha256( + java.util.Base64.getEncoder().encodeToString(challenge.getBytes())); + verify(store).issue(session.getId(), CeremonyStore.Purpose.AUTHENTICATE, hash, null); + when(store.consume(session.getId(), CeremonyStore.Purpose.AUTHENTICATE, hash)) + .thenReturn(true, false); + var assertions = new AtomicInteger(); + guard.doFilter( + request("/login/webauthn", session), + new MockHttpServletResponse(), + (q, s) -> assertions.incrementAndGet()); + var replay = new MockHttpServletResponse(); + guard.doFilter( + request("/login/webauthn", session), replay, (q, s) -> assertions.incrementAndGet()); + assertThat(replay.getStatus()).isEqualTo(401); + assertThat(assertions).hasValue(1); + } + + private MockHttpServletRequest request(String path, MockHttpSession session) { + var request = new MockHttpServletRequest("POST", path); + request.setServletPath(path); + request.setSession(session); + return request; + } +} diff --git a/src/test/java/org/busystack/caldav/security/passkey/PasskeyHttpIT.java b/src/test/java/org/busystack/caldav/security/passkey/PasskeyHttpIT.java new file mode 100644 index 0000000..38f9fd4 --- /dev/null +++ b/src/test/java/org/busystack/caldav/security/passkey/PasskeyHttpIT.java @@ -0,0 +1,536 @@ +package org.busystack.caldav.security.passkey; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.nio.charset.StandardCharsets; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.UUID; +import org.busystack.caldav.account.AccountService; +import org.busystack.caldav.security.oauth.OAuthClientService; +import org.busystack.caldav.security.session.SessionSecurity; +import org.busystack.caldav.testing.SignedPasskeyClient; +import org.busystack.caldav.testing.TestTls; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.server.LocalServerPort; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.authority.FactorGrantedAuthority; +import org.springframework.security.core.authority.SimpleGrantedAuthority; +import org.springframework.security.web.webauthn.api.Bytes; +import org.springframework.security.web.webauthn.api.CredentialRecord; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; +import org.testcontainers.postgresql.PostgreSQLContainer; + +/** + * Real signatures, CSRF/session ceremonies and persistent state; no seeded authentication grants. + */ +@SpringBootTest( + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, + properties = { + "busystack.public-base-url=https://localhost/team", + "busystack.issuer=https://localhost/team" + }) +@org.springframework.test.annotation.DirtiesContext( + classMode = org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_CLASS) +@Testcontainers +class PasskeyHttpIT { + private static final String PASSWORD = "Independent signed fixture password"; + private static final String CALLBACK = "http://127.0.0.1:34567/callback"; + + @Container + static final PostgreSQLContainer POSTGRES = + org.busystack.caldav.testing.LoopbackContainers.postgres(); + + @DynamicPropertySource + static void database(DynamicPropertyRegistry registry) { + registry.add("spring.datasource.url", POSTGRES::getJdbcUrl); + registry.add("spring.datasource.username", POSTGRES::getUsername); + registry.add("spring.datasource.password", POSTGRES::getPassword); + registry.add("server.ssl.enabled", () -> true); + registry.add("server.ssl.key-store", () -> TestTls.instance().keyStore()); + registry.add("server.ssl.key-store-password", () -> TestTls.instance().password()); + } + + @LocalServerPort int port; + @Autowired AccountService accounts; + @Autowired AccountPasskeyCredentials credentials; + @Autowired OAuthClientService clients; + @Autowired JdbcTemplate jdbc; + SignedPasskeyClient transport; + UUID owner; + UUID recipient; + UUID administrator; + + @BeforeEach + void prepare() { + org.busystack.caldav.testing.FixtureDatabase.clearAccounts(jdbc); + jdbc.execute("TRUNCATE passkey_revocations"); + // This class owns the disposable database, including its hashed account and loopback buckets. + jdbc.execute("TRUNCATE login_throttle"); + owner = accounts.provision("signed-owner", "Signed owner", PASSWORD, false); + recipient = accounts.provision("signed-recipient", "Signed recipient", PASSWORD, false); + administrator = accounts.provision("signed-admin", "Signed administrator", PASSWORD, true); + transport = new SignedPasskeyClient(port, "/team", "https://localhost"); + } + + @AfterEach + void closeBrowsers() { + if (transport != null) transport.close(); + } + + private CredentialRecord saved(SignedPasskeyClient.Credential credential) { + return credentials.findByCredentialId(Bytes.fromBase64(credential.id())); + } + + @Test + void registrationChecksClientDataFlagsAndSessionAccountBindingBeforeSaving() throws Exception { + var browser = transport.passwordLogin("signed-owner", PASSWORD); + for (String mode : + List.of( + "wrong-origin", + "wrong-challenge", + "wrong-rpidhash", + "no-up", + "no-uv", + "impossible-backup")) { + var rejected = transport.registration(browser, mode); + assertThat(rejected.response().statusCode()).as(mode).isEqualTo(400); + assertThat(saved(rejected.credential())).isNull(); + } + var prepared = transport.registration(browser, "prepare"); + String csrf = transport.csrf(browser, "/login"); + assertThat( + transport + .request( + browser, + "/login", + "POST", + csrf, + SignedPasskeyClient.form( + Map.of("username", "signed-recipient", "password", PASSWORD))) + .statusCode()) + .isEqualTo(302); + var swapped = + transport.request( + browser, + "/webauthn/register", + "POST", + transport.csrf(browser, "/login"), + prepared.body()); + assertThat(swapped.statusCode()).isEqualTo(400); + assertThat(saved(prepared.credential())).isNull(); + assertThat(jdbc.queryForObject("SELECT count(*) FROM user_credentials", Long.class)).isZero(); + var registered = transport.registration(browser, "correct"); + assertThat(registered.response().statusCode()).isEqualTo(200); + var key = registered.credential(); + var record = saved(key); + assertThat( + jdbc.queryForObject( + "SELECT account_id FROM user_entities WHERE id=?", + UUID.class, + record.getUserEntityUserId().toBase64UrlString())) + .isEqualTo(recipient); + assertThat( + transport + .assertCredential(transport.options(browser), key, "correct", 0x0d) + .statusCode()) + .isEqualTo(200); + record = saved(key); + var fresh = transport.registration(browser, "prepare"); + var duplicateBody = + SignedPasskeyClient.JSON.readTree( + SignedPasskeyClient.JSON.writeValueAsString(registered.body())); + var freshBody = + SignedPasskeyClient.JSON.readTree( + SignedPasskeyClient.JSON.writeValueAsString(fresh.body())); + ((tools.jackson.databind.node.ObjectNode) + duplicateBody.path("publicKey").path("credential").path("response")) + .set( + "clientDataJSON", + freshBody.path("publicKey").path("credential").path("response").path("clientDataJSON")); + var duplicate = + transport.request( + browser, + "/webauthn/register", + "POST", + transport.csrf(browser, "/login"), + duplicateBody); + assertUnchanged(record, saved(key)); + var afterDuplicate = transport.get(browser, "/security/passkeys"); + assertThat(duplicate.statusCode()) + .as( + "body=%s headers=%s protected-page-status=%s", + duplicate.body(), duplicate.headers().map(), afterDuplicate.statusCode()) + .isEqualTo(400); + assertThat(duplicate.headers().firstValue("Location")).isEmpty(); + assertThat(afterDuplicate.statusCode()).isEqualTo(200); + } + + @Test + void anonymousHandleAndVerifiedBackupChangesReachJdbcAndRejectedAssertionsNeverUpdateState() + throws Exception { + var browser = transport.passwordLogin("signed-owner", PASSWORD); + var key = transport.enroll(browser); + assertThat(saved(key).isBackupEligible()).isTrue(); + assertThat(saved(key).isBackupState()).isFalse(); + for (String mode : List.of("wrong", "absent")) { + var before = saved(key); + var anonymous = transport.browser(); + var denied = transport.assertCredential(transport.options(anonymous), key, mode, 0x1d); + assertThat(denied.statusCode()).as(mode).isEqualTo(401); + assertUnchanged(before, saved(key)); + assertThat(transport.get(anonymous, "/security/sessions").statusCode()).isEqualTo(302); + } + for (int flags : List.of(0x0d, 0x1d, 0x0d)) { + var accepted = + transport.assertCredential(transport.options(transport.browser()), key, "correct", flags); + assertThat(accepted.statusCode()).as(accepted.body()).isEqualTo(200); + var persisted = + new AccountPasskeyCredentials(jdbc).findByCredentialId(Bytes.fromBase64(key.id())); + assertThat(persisted.isBackupState()).isEqualTo((flags & 0x10) != 0); + assertThat(persisted.isBackupEligible()).isTrue(); + assertThat(persisted.isUvInitialized()).isTrue(); + assertThat(persisted.getSignatureCount()).isPositive(); + } + } + + @Test + void verifiedUvInitializationChangesOnlyAfterTheSameAcceptedCredentialAssertion() + throws Exception { + var key = transport.enroll(transport.passwordLogin("signed-owner", PASSWORD)); + // Exercise the conditional record transition without adding any credential-import API or + // weakening this deployment's required-UV registration policy. + jdbc.update("UPDATE user_credentials SET uv_initialized=false WHERE credential_id=?", key.id()); + var before = saved(key); + assertThat(before.isUvInitialized()).isFalse(); + var rejected = + transport.assertCredential(transport.options(transport.browser()), key, "wrong", 0x1d); + assertThat(rejected.statusCode()).isEqualTo(401); + assertUnchanged(before, saved(key)); + var accepted = + transport.assertCredential(transport.options(transport.browser()), key, "correct", 0x0d); + assertThat(accepted.statusCode()).isEqualTo(200); + assertThat(saved(key).isUvInitialized()).isTrue(); + } + + @Test + void everyAdvertisedSignatureAlgorithmRegistersAndVerifiesRealAssertions() throws Exception { + for (var variant : + Map.of("signed-owner", "ES256", "signed-recipient", "RS256", "signed-admin", "EdDSA") + .entrySet()) { + var key = + transport.enroll(transport.passwordLogin(variant.getKey(), PASSWORD), variant.getValue()); + assertThat( + transport + .assertCredential(transport.options(transport.browser()), key, "correct", 0x0d) + .statusCode()) + .as(variant.getValue()) + .isEqualTo(200); + var before = saved(key); + assertThat( + transport + .assertCredential( + transport.options(transport.browser()), key, "invalid-signature", 0x1d) + .statusCode()) + .as(variant.getValue()) + .isEqualTo(401); + assertUnchanged(before, saved(key)); + } + } + + @Test + void identifiedAccountAndAllowListBindingRejectCrossAccountAndMismatchedHandle() + throws Exception { + var ownerBrowser = transport.passwordLogin("signed-owner", PASSWORD); + var ownerKey = transport.enroll(ownerBrowser); + var recipientBrowser = transport.passwordLogin("signed-recipient", PASSWORD); + var recipientKey = transport.enroll(recipientBrowser); + var before = saved(ownerKey); + var cross = + transport.assertCredential(transport.options(recipientBrowser), ownerKey, "correct", 0x1d); + assertThat(cross.statusCode()).isEqualTo(401); + assertUnchanged(before, saved(ownerKey)); + assertThat(saved(recipientKey).getSignatureCount()).isZero(); + var mismatch = + transport.assertCredential(transport.options(ownerBrowser), ownerKey, "wrong", 0x1d); + assertThat(mismatch.statusCode()).isEqualTo(401); + assertUnchanged(before, saved(ownerKey)); + // An identified account's absent handle is allowed; its server-side allow-list binds the key. + var accepted = + transport.assertCredential(transport.options(ownerBrowser), ownerKey, "absent", 0x0d); + assertThat(accepted.statusCode()).as(accepted.body()).isEqualTo(200); + var noKey = transport.passwordLogin("signed-admin", PASSWORD); + assertThat( + transport + .request( + noKey, + "/webauthn/authenticate/options", + "POST", + transport.csrf(noKey, "/login"), + null) + .statusCode()) + .isEqualTo(403); + } + + @Test + void signedCryptographicAndAuthenticatorBoundaryFailuresDoNotPersistAnyFlagsOrCounter() + throws Exception { + var key = transport.enroll(transport.passwordLogin("signed-owner", PASSWORD)); + for (String mode : + List.of( + "wrong-origin", + "wrong-challenge", + "invalid-signature", + "wrong-type", + "wrong-rpidhash", + "cross-origin", + "untrusted-top-origin")) { + var before = saved(key); + var rejected = + transport.assertCredential(transport.options(transport.browser()), key, mode, 0x1d); + assertThat(rejected.statusCode()).as(mode).isEqualTo(401); + assertUnchanged(before, saved(key)); + } + for (int flags : List.of(0x1c, 0x19, 0x15, 0x05)) { + var before = saved(key); + var rejected = + transport.assertCredential(transport.options(transport.browser()), key, "correct", flags); + assertThat(rejected.statusCode()).as("flags %s", flags).isEqualTo(401); + assertUnchanged(before, saved(key)); + } + } + + @Test + void challengeExpirySingleUseRevocationAndDisabledAccountsFailBeforeStateMutation() + throws Exception { + var key = transport.enroll(transport.passwordLogin("signed-owner", PASSWORD)); + var ceremony = transport.options(transport.browser()); + var payload = key.assertion(ceremony.options(), "https://localhost", "correct", 0x1d); + assertThat( + transport + .request(ceremony.browser(), "/login/webauthn", "POST", ceremony.csrf(), payload) + .statusCode()) + .isEqualTo(200); + var before = saved(key); + assertThat( + transport + .request(ceremony.browser(), "/login/webauthn", "POST", ceremony.csrf(), payload) + .statusCode()) + .isEqualTo(401); + assertUnchanged(before, saved(key)); + var expired = transport.options(transport.browser()); + jdbc.update( + "UPDATE webauthn_ceremonies SET created_at=now()-interval '2 minutes',expires_at=now()-interval '1 minute'"); + assertThat(transport.assertCredential(expired, key, "correct", 0x0d).statusCode()) + .isEqualTo(401); + assertUnchanged(before, saved(key)); + var revoked = transport.options(transport.browser()); + credentials.delete(Bytes.fromBase64(key.id())); + assertThat(transport.assertCredential(revoked, key, "correct", 0x0d).statusCode()) + .isEqualTo(401); + assertThat(saved(key)).isNull(); + var other = transport.enroll(transport.passwordLogin("signed-recipient", PASSWORD)); + var disabled = transport.options(transport.browser()); + // Keep the key row to prove the relying-party's current-account check, not key absence alone. + jdbc.update("UPDATE accounts SET enabled=false WHERE id=?", recipient); + var otherBefore = saved(other); + assertThat(transport.assertCredential(disabled, other, "correct", 0x1d).statusCode()) + .isEqualTo(401); + assertUnchanged(otherBefore, saved(other)); + } + + @Test + void actualPasswordPasskeyConsentPkceTokenRefreshAndRevocationJourneyUsesOnePrincipal() + throws Exception { + var admin = accounts.find(administrator); + var adminProof = + UsernamePasswordAuthenticationToken.authenticated( + admin.username(), + null, + List.of( + new SimpleGrantedAuthority("ROLE_ADMIN"), + SessionSecurity.authority(admin.securityEpoch()), + FactorGrantedAuthority.fromAuthority(FactorGrantedAuthority.PASSWORD_AUTHORITY), + FactorGrantedAuthority.fromAuthority(FactorGrantedAuthority.WEBAUTHN_AUTHORITY))); + clients.registerNative(adminProof, "signed-native", "Signed consent client", Set.of(CALLBACK)); + var browser = transport.passwordLogin("signed-owner", PASSWORD); + var key = transport.enroll(browser); + assertThat( + transport + .assertCredential(transport.options(browser), key, "correct", 0x0d) + .statusCode()) + .isEqualTo(200); + String verifier = "independent-real-browser-code-verifier-012345678901234567890123456789"; + String challenge = + SignedPasskeyClient.encoded( + SignedPasskeyClient.hash(verifier.getBytes(StandardCharsets.US_ASCII))); + String state = UUID.randomUUID().toString(); + String query = + SignedPasskeyClient.form( + Map.of( + "response_type", + "code", + "client_id", + "signed-native", + "redirect_uri", + CALLBACK, + "scope", + "dav", + "state", + state, + "code_challenge", + challenge, + "code_challenge_method", + "S256")); + var pending = transport.get(browser, "/oauth2/authorize?" + query); + assertThat(pending.statusCode()).as(pending.body()).isEqualTo(302); + String consentLocation = pending.headers().firstValue("Location").orElseThrow(); + assertThat(consentLocation).contains("/team/oauth/consent?"); + var consent = transport.get(browser, transport.localPath(consentLocation)); + assertThat(consent.statusCode()).as(consent.body()).isEqualTo(200); + String consentState = SignedPasskeyClient.query(consentLocation).get("state"); + String body = + SignedPasskeyClient.form( + Map.of("client_id", "signed-native", "state", consentState, "scope", "dav")); + assertThat(transport.request(browser, "/oauth2/authorize", "POST", null, body).statusCode()) + .isEqualTo(403); + var granted = + transport.request( + browser, "/oauth2/authorize", "POST", SignedPasskeyClient.csrf(consent.body()), body); + assertThat(granted.statusCode()).as(granted.body()).isEqualTo(302); + String redirect = granted.headers().firstValue("Location").orElseThrow(); + assertThat(redirect).startsWith(CALLBACK + "?"); + var parameters = SignedPasskeyClient.query(redirect); + assertThat(parameters.get("state")).isEqualTo(state); + var token = + transport.request( + transport.browser(), + "/oauth2/token", + "POST", + null, + SignedPasskeyClient.form( + Map.of( + "grant_type", + "authorization_code", + "client_id", + "signed-native", + "redirect_uri", + CALLBACK, + "code", + parameters.get("code"), + "code_verifier", + verifier))); + assertThat(token.statusCode()).as(token.body()).isEqualTo(200); + assertThat(token.headers().firstValue("Cache-Control").orElseThrow()).contains("no-store"); + assertThat(token.headers().firstValue("Pragma")).hasValue("no-cache"); + var issued = SignedPasskeyClient.JSON.readTree(token.body()); + assertThat(issued.path("token_type").asString()).isEqualTo("Bearer"); + assertThat(issued.path("expires_in").asLong()).isPositive(); + String refresh = issued.path("refresh_token").asString(); + var rotated = + transport.request( + transport.browser(), + "/oauth2/token", + "POST", + null, + SignedPasskeyClient.form( + Map.of( + "grant_type", + "refresh_token", + "client_id", + "signed-native", + "refresh_token", + refresh))); + assertThat(rotated.statusCode()).as(rotated.body()).isEqualTo(200); + String rotatedRefresh = + SignedPasskeyClient.JSON.readTree(rotated.body()).path("refresh_token").asString(); + var emptyScope = + transport.request( + transport.browser(), + "/oauth2/token", + "POST", + null, + SignedPasskeyClient.form( + Map.of( + "grant_type", + "refresh_token", + "client_id", + "signed-native", + "refresh_token", + rotatedRefresh, + "scope", + "", + "unknown-audit-parameter", + "opaque"))); + assertThat(emptyScope.statusCode()).as(emptyScope.body()).isEqualTo(200); + assertThat(SignedPasskeyClient.JSON.readTree(emptyScope.body()).path("scope").asString()) + .isEqualTo("dav"); + String access = + SignedPasskeyClient.JSON.readTree(emptyScope.body()).path("access_token").asString(); + assertThat( + jdbc.queryForObject( + "SELECT count(*) FROM oauth2_authorization WHERE attributes::jsonb->>'busystack.account_id'=?", + Long.class, + owner.toString())) + .isEqualTo(1); + assertThat(transport.bearer("/dav/", "GET", access, "").statusCode()).isEqualTo(200); + var forbidden = + transport.bearer( + "/dav/calendars/" + recipient + "/missing/event.ics", + "PUT", + access, + "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Boundary//EN\r\nBEGIN:VEVENT\r\nUID:blocked\r\nDTSTAMP:20261027T000000Z\r\nDTSTART:20261028T000000Z\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n"); + assertThat(forbidden.statusCode()).isEqualTo(403); + assertThat(forbidden.headers().allValues("WWW-Authenticate").toString()) + .doesNotContain("resource_metadata"); + var revoked = + transport.request( + transport.browser(), + "/oauth2/revoke", + "POST", + null, + SignedPasskeyClient.form( + Map.of( + "client_id", + "signed-native", + "token", + access, + "token_type_hint", + "access_token"))); + assertThat(revoked.statusCode()).isEqualTo(200); + assertThat( + transport + .request( + transport.browser(), + "/oauth2/revoke", + "POST", + null, + SignedPasskeyClient.form( + Map.of("client_id", "signed-native", "token", "unknown-independent-token"))) + .statusCode()) + .isEqualTo(200); + var denied = transport.bearer("/dav/", "GET", access, ""); + assertThat(denied.statusCode()).isEqualTo(401); + assertThat(denied.headers().firstValue("WWW-Authenticate").orElseThrow()) + .startsWith("Bearer ") + .contains("invalid_token") + .doesNotContain("resource_metadata"); + assertThat(transport.get(browser, "/security/sessions").statusCode()).isEqualTo(200); + } + + private static void assertUnchanged(CredentialRecord before, CredentialRecord after) { + assertThat(after.isBackupState()).isEqualTo(before.isBackupState()); + assertThat(after.isUvInitialized()).isEqualTo(before.isUvInitialized()); + assertThat(after.getSignatureCount()).isEqualTo(before.getSignatureCount()); + assertThat(after.getLastUsed()).isEqualTo(before.getLastUsed()); + } +} diff --git a/src/test/java/org/busystack/caldav/security/passkey/PasskeyPersistenceIT.java b/src/test/java/org/busystack/caldav/security/passkey/PasskeyPersistenceIT.java new file mode 100644 index 0000000..4c204bb --- /dev/null +++ b/src/test/java/org/busystack/caldav/security/passkey/PasskeyPersistenceIT.java @@ -0,0 +1,138 @@ +package org.busystack.caldav.security.passkey; + +import static org.assertj.core.api.Assertions.*; + +import java.time.Instant; +import java.time.temporal.ChronoUnit; +import java.util.Set; +import org.busystack.caldav.account.AccountService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.security.authentication.BadCredentialsException; +import org.springframework.security.web.webauthn.api.*; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; +import org.testcontainers.postgresql.PostgreSQLContainer; + +@SpringBootTest( + properties = { + "busystack.public-base-url=https://calendar.example", + "busystack.issuer=https://calendar.example" + }) +@org.springframework.test.annotation.DirtiesContext( + classMode = org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_CLASS) +@Testcontainers +class PasskeyPersistenceIT { + @Container + static final PostgreSQLContainer POSTGRES = + org.busystack.caldav.testing.LoopbackContainers.postgres(); + + @DynamicPropertySource + static void database(DynamicPropertyRegistry registry) { + registry.add("spring.datasource.url", POSTGRES::getJdbcUrl); + registry.add("spring.datasource.username", POSTGRES::getUsername); + registry.add("spring.datasource.password", POSTGRES::getPassword); + } + + @Autowired AccountPasskeyUsers users; + @Autowired AccountPasskeyCredentials credentials; + @Autowired AccountService accounts; + @Autowired JdbcTemplate jdbc; + PublicKeyCredentialUserEntity user; + + @BeforeEach + void prepare() { + org.busystack.caldav.testing.FixtureDatabase.clearAccounts(jdbc); + jdbc.execute("TRUNCATE passkey_revocations"); + accounts.provision("owner", "Owner", "correct horse battery staple", false); + user = + ImmutablePublicKeyCredentialUserEntity.builder() + .id(Bytes.random()) + .name("owner") + .displayName("Owner") + .build(); + users.save(user); + } + + private CredentialRecord record() { + var time = Instant.now().truncatedTo(ChronoUnit.MICROS); + return ImmutableCredentialRecord.builder() + .credentialId(Bytes.random()) + .userEntityUserId(user.getId()) + .publicKey(new ImmutablePublicKeyCose(new byte[] {1, 2, 3})) + .credentialType(PublicKeyCredentialType.PUBLIC_KEY) + .signatureCount(1) + .uvInitialized(true) + .backupEligible(false) + .backupState(false) + .transports(Set.of(AuthenticatorTransport.INTERNAL)) + .label("Test key") + .created(time) + .lastUsed(time) + .build(); + } + + @Test + void jdbcPersistsKeyAndStableAccountLinkSurvivesUsernameChange() { + var record = record(); + credentials.save(record); + var reloaded = new AccountPasskeyCredentials(jdbc).findByCredentialId(record.getCredentialId()); + assertThat(reloaded.getPublicKey().getBytes()).containsExactly(1, 2, 3); + assertThat(reloaded.getSignatureCount()).isEqualTo(1); + jdbc.update("UPDATE accounts SET username='renamed' WHERE username='owner'"); + assertThat(new AccountPasskeyUsers(jdbc).findById(user.getId()).getName()).isEqualTo("renamed"); + assertThat(users.findByUsername("owner")).isNull(); + assertThat(users.findByUsername("renamed").getId()).isEqualTo(user.getId()); + } + + @Test + void counterCannotRegressAndRemovedKeyCannotBeResurrectedByInflightAssertion() { + var record = record(); + credentials.save(record); + credentials.save( + ImmutableCredentialRecord.fromCredentialRecord(record).signatureCount(5).build()); + assertThatThrownBy(() -> credentials.save(record)).isInstanceOf(BadCredentialsException.class); + credentials.delete(record.getCredentialId()); + assertThat(credentials.findByCredentialId(record.getCredentialId())).isNull(); + assertThatThrownBy(() -> credentials.save(record)).isInstanceOf(BadCredentialsException.class); + } + + @Test + void existingKeyCannotBeReboundToAnotherUserOrPublicKey() { + var record = record(); + credentials.save(record); + assertThatThrownBy( + () -> + credentials.save( + ImmutableCredentialRecord.fromCredentialRecord(record) + .publicKey(new ImmutablePublicKeyCose(new byte[] {4, 5, 6})) + .build())) + .isInstanceOf(BadCredentialsException.class); + assertThatThrownBy( + () -> + credentials.save( + ImmutableCredentialRecord.fromCredentialRecord(record) + .userEntityUserId(Bytes.random()) + .build())) + .isInstanceOf(BadCredentialsException.class); + assertThatThrownBy( + () -> + credentials.save( + ImmutableCredentialRecord.fromCredentialRecord(record) + .backupEligible(true) + .build())) + .isInstanceOf(BadCredentialsException.class); + assertThatThrownBy( + () -> + credentials.save( + ImmutableCredentialRecord.fromCredentialRecord(record) + .uvInitialized(false) + .build())) + .isInstanceOf(BadCredentialsException.class); + } +} diff --git a/src/test/java/org/busystack/caldav/security/passkey/PasskeyRestartIT.java b/src/test/java/org/busystack/caldav/security/passkey/PasskeyRestartIT.java new file mode 100644 index 0000000..443291d --- /dev/null +++ b/src/test/java/org/busystack/caldav/security/passkey/PasskeyRestartIT.java @@ -0,0 +1,151 @@ +package org.busystack.caldav.security.passkey; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.time.Duration; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; +import org.busystack.caldav.BusyStackApplication; +import org.busystack.caldav.account.AccountService; +import org.busystack.caldav.testing.SignedPasskeyClient; +import org.busystack.caldav.testing.TestTls; +import org.junit.jupiter.api.Test; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.boot.web.server.context.WebServerApplicationContext; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.datasource.DriverManagerDataSource; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; +import org.testcontainers.postgresql.PostgreSQLContainer; + +/** A new production application JVM must re-read and update verified passkey state correctly. */ +@Testcontainers +class PasskeyRestartIT { + @Container + static final PostgreSQLContainer POSTGRES = + org.busystack.caldav.testing.LoopbackContainers.postgres(); + + @Test + void verifiedBackupStatePersistsAcrossApplicationProcessRestartAndContinuesBothDirections() + throws Exception { + var database = + new JdbcTemplate( + new DriverManagerDataSource( + POSTGRES.getJdbcUrl(), POSTGRES.getUsername(), POSTGRES.getPassword())); + var arguments = + new ArrayList<>( + List.of( + "--server.port=0", + "--busystack.public-base-url=https://localhost/team", + "--busystack.issuer=https://localhost/team", + "--spring.datasource.url=" + POSTGRES.getJdbcUrl(), + "--spring.datasource.username=" + POSTGRES.getUsername(), + "--spring.datasource.password=" + POSTGRES.getPassword(), + "--server.ssl.enabled=true", + "--server.ssl.key-store=" + TestTls.instance().keyStore(), + "--server.ssl.key-store-password=" + TestTls.instance().password())); + SignedPasskeyClient.Credential key; + try (var first = + new SpringApplicationBuilder(BusyStackApplication.class) + .run(arguments.toArray(String[]::new))) { + first + .getBean(AccountService.class) + .provision("restart-passkey", "Restart passkey", "Independent restart password", false); + int port = ((WebServerApplicationContext) first).getWebServer().getPort(); + try (var transport = new SignedPasskeyClient(port, "/team", "https://localhost")) { + key = + transport.enroll( + transport.passwordLogin("restart-passkey", "Independent restart password")); + assertThat( + transport + .assertCredential(transport.options(transport.browser()), key, "correct", 0x1d) + .statusCode()) + .isEqualTo(200); + assertThat(state(database, key)).isTrue(); + } + } + // The signing key remains only in this synthetic authenticator's memory. The server starts + // from database state in a distinct JVM, without re-enrollment or a seeded authentication. + int port; + try (var reserved = + new java.net.ServerSocket(0, 1, java.net.InetAddress.getLoopbackAddress())) { + port = reserved.getLocalPort(); + } + arguments.set(0, "--server.port=" + port); + var command = + new ArrayList<>( + List.of( + Path.of(System.getProperty("java.home"), "bin", "java").toString(), + "-Xmx256m", + "-cp", + System.getProperty("java.class.path"), + BusyStackApplication.class.getName())); + command.addAll(arguments); + Path log = Path.of("target/qualification/passkey-process-restart.log"); + Files.createDirectories(log.getParent()); + Process child = + new ProcessBuilder(command).redirectErrorStream(true).redirectOutput(log.toFile()).start(); + var transport = new SignedPasskeyClient(port, "/team", "https://localhost"); + try { + awaitReady(child, transport, log); + assertThat(state(database, key)).isTrue(); + long previous = counter(database, key); + assertThat( + transport + .assertCredential(transport.options(transport.browser()), key, "correct", 0x0d) + .statusCode()) + .isEqualTo(200); + assertThat(state(database, key)).isFalse(); + assertThat(counter(database, key)).isGreaterThan(previous); + previous = counter(database, key); + assertThat( + transport + .assertCredential( + transport.options(transport.browser()), key, "invalid-signature", 0x1d) + .statusCode()) + .isEqualTo(401); + assertThat(state(database, key)).isFalse(); + assertThat(counter(database, key)).isEqualTo(previous); + } finally { + transport.close(); + child.destroy(); + if (!child.waitFor(30, TimeUnit.SECONDS)) { + child.destroyForcibly(); + assertThat(child.waitFor(10, TimeUnit.SECONDS)).isTrue(); + } + } + } + + private static boolean state(JdbcTemplate jdbc, SignedPasskeyClient.Credential key) { + return Boolean.TRUE.equals( + jdbc.queryForObject( + "SELECT backup_state FROM user_credentials WHERE credential_id=?", + Boolean.class, + key.id())); + } + + private static long counter(JdbcTemplate jdbc, SignedPasskeyClient.Credential key) { + return jdbc.queryForObject( + "SELECT signature_count FROM user_credentials WHERE credential_id=?", Long.class, key.id()); + } + + private static void awaitReady(Process child, SignedPasskeyClient transport, Path log) + throws Exception { + long deadline = System.nanoTime() + Duration.ofMinutes(2).toNanos(); + try (var browser = transport.browser()) { + while (System.nanoTime() < deadline) { + assertThat(child.isAlive()).as(Files.readString(log)).isTrue(); + try { + if (transport.get(browser, "/actuator/health/readiness").statusCode() == 200) return; + } catch (java.io.IOException starting) { + // Readiness is polled against the actual new TLS listener, without weakening trust. + } + Thread.sleep(100); + } + } + throw new AssertionError("Restarted server did not become ready: " + Files.readString(log)); + } +} diff --git a/src/test/java/org/busystack/caldav/security/password/PasswordPolicyTest.java b/src/test/java/org/busystack/caldav/security/password/PasswordPolicyTest.java new file mode 100644 index 0000000..1f22baa --- /dev/null +++ b/src/test/java/org/busystack/caldav/security/password/PasswordPolicyTest.java @@ -0,0 +1,22 @@ +package org.busystack.caldav.security.password; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +class PasswordPolicyTest { + @Test + void countsUnicodeCodePointsAndEnforcesTheBcryptByteBoundary() { + assertThatCode(() -> PasswordPolicy.validate("😀".repeat(14))).doesNotThrowAnyException(); + assertThatThrownBy(() -> PasswordPolicy.validate("😀".repeat(7))) + .isInstanceOf(IllegalArgumentException.class); + assertThatCode(() -> PasswordPolicy.validate("é".repeat(36))).doesNotThrowAnyException(); + assertThatThrownBy(() -> PasswordPolicy.validate("é".repeat(37))) + .isInstanceOf(IllegalArgumentException.class); + assertThatCode(() -> PasswordPolicy.validate("a".repeat(72))).doesNotThrowAnyException(); + assertThatThrownBy(() -> PasswordPolicy.validate("a".repeat(73))) + .isInstanceOf(IllegalArgumentException.class); + assertThatThrownBy(() -> PasswordPolicy.validate("valid length prefix" + '\uD800')) + .isInstanceOf(IllegalArgumentException.class); + } +} diff --git a/src/test/java/org/busystack/caldav/security/session/AccountSecurityWorkflowsIT.java b/src/test/java/org/busystack/caldav/security/session/AccountSecurityWorkflowsIT.java new file mode 100644 index 0000000..9eeb9c3 --- /dev/null +++ b/src/test/java/org/busystack/caldav/security/session/AccountSecurityWorkflowsIT.java @@ -0,0 +1,895 @@ +package org.busystack.caldav.security.session; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.net.CookieManager; +import java.net.CookiePolicy; +import java.net.URI; +import java.net.URLEncoder; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.nio.charset.StandardCharsets; +import java.time.Duration; +import java.time.Instant; +import java.time.temporal.ChronoUnit; +import java.util.ArrayList; +import java.util.Base64; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; +import org.busystack.caldav.account.AccountService; +import org.busystack.caldav.security.audit.SecurityAudit; +import org.busystack.caldav.security.audit.SecurityHashes; +import org.busystack.caldav.security.oauth.RefreshFamilyStore; +import org.busystack.caldav.security.passkey.AccountPasskeyCredentials; +import org.busystack.caldav.security.passkey.AccountPasskeyUsers; +import org.busystack.caldav.security.passkey.CeremonyStore; +import org.busystack.caldav.security.password.AccountPasswordService; +import org.busystack.caldav.security.recovery.RecoveryService; +import org.busystack.caldav.security.throttle.LoginThrottleService; +import org.busystack.caldav.testing.LoopbackContainers; +import org.busystack.caldav.testing.TestTls; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.server.LocalServerPort; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.security.access.AccessDeniedException; +import org.springframework.security.authentication.BadCredentialsException; +import org.springframework.security.authentication.CredentialsExpiredException; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.authority.FactorGrantedAuthority; +import org.springframework.security.core.context.SecurityContext; +import org.springframework.security.core.context.SecurityContextImpl; +import org.springframework.security.core.userdetails.User; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.security.web.context.HttpSessionSecurityContextRepository; +import org.springframework.security.web.webauthn.api.*; +import org.springframework.session.Session; +import org.springframework.session.SessionRepository; +import org.springframework.session.jdbc.JdbcIndexedSessionRepository; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.support.TransactionTemplate; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; +import org.testcontainers.postgresql.PostgreSQLContainer; + +/** + * Real JDBC/HTTPS security workflows; seeded factor contexts do not qualify WebAuthn cryptography. + */ +@SpringBootTest( + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, + properties = { + "busystack.public-base-url=https://calendar.example", + "busystack.issuer=https://calendar.example" + }) +@org.springframework.test.annotation.DirtiesContext( + classMode = org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_CLASS) +@Testcontainers +@org.springframework.context.annotation.Import( + AccountSecurityWorkflowsIT.SlowRequestConfiguration.class) +class AccountSecurityWorkflowsIT { + @Container static final PostgreSQLContainer POSTGRES = LoopbackContainers.postgres(); + + @DynamicPropertySource + static void database(DynamicPropertyRegistry registry) { + registry.add("spring.datasource.url", POSTGRES::getJdbcUrl); + registry.add("spring.datasource.username", POSTGRES::getUsername); + registry.add("spring.datasource.password", POSTGRES::getPassword); + registry.add("server.ssl.enabled", () -> true); + registry.add("server.ssl.key-store", () -> TestTls.instance().keyStore()); + registry.add("server.ssl.key-store-password", () -> TestTls.instance().password()); + } + + @Autowired AccountService accounts; + @Autowired SessionSecurity security; + @Autowired SessionService sessions; + @Autowired SessionRepository repository; + @Autowired RecoveryService recovery; + @Autowired AccountPasswordService passwords; + @Autowired PasswordEncoder encoder; + @Autowired AccountPasskeyUsers passkeyUsers; + @Autowired AccountPasskeyCredentials passkeys; + @Autowired RefreshFamilyStore families; + + @Autowired + org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository + registrations; + + @Autowired CeremonyStore ceremonies; + @Autowired LoginThrottleService throttle; + @Autowired SecurityAudit audit; + @Autowired JdbcTemplate jdbc; + @Autowired PlatformTransactionManager transactions; + @Autowired SlowRequestHarness slowRequests; + @LocalServerPort int port; + UUID owner, other; + + @BeforeEach + void prepare() { + org.busystack.caldav.testing.FixtureDatabase.reset( + jdbc, + "TRUNCATE spring_session,login_throttle,security_audit,passkey_revocations,oauth2_authorization,oauth2_authorization_consent,oauth2_registered_client RESTART IDENTITY CASCADE", + "TRUNCATE accounts CASCADE"); + owner = + accounts.provision("session-owner", "Session owner", "correct horse battery staple", true); + other = + accounts.provision( + "session-other", "Another account", "correct horse battery staple", false); + } + + @Test + void realPasswordLoginPersistsSessionFixationAndSurvivesARepositoryRestart() throws Exception { + CookieManager cookies = new CookieManager(null, CookiePolicy.ACCEPT_ALL); + HttpClient browser = browser(cookies); + String csrf = csrf(browser, "/login"); + String before = + cookies.getCookieStore().getCookies().stream() + .filter(cookie -> cookie.getName().equals("JSESSIONID")) + .findFirst() + .orElseThrow() + .getValue(); + var loggedIn = + post( + browser, + "/login", + Map.of( + "username", + "session-owner", + "password", + "correct horse battery staple", + "_csrf", + csrf)); + assertThat(loggedIn.statusCode()).isEqualTo(302); + String after = + cookies.getCookieStore().getCookies().stream() + .filter(cookie -> cookie.getName().equals("JSESSIONID")) + .findFirst() + .orElseThrow() + .getValue(); + assertThat(after).isNotEqualTo(before); + var page = get(browser, "/security/sessions"); + assertThat(page.statusCode()).isEqualTo(200); + assertThat(page.body()).contains("This browser"); + String id = + jdbc.queryForObject( + "SELECT session_id FROM spring_session WHERE principal_name='session-owner'", + String.class); + var restarted = new JdbcIndexedSessionRepository(jdbc, new TransactionTemplate(transactions)); + Session persisted = restarted.findById(id); + SecurityContext context = + persisted.getAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY); + assertThat(context.getAuthentication().getName()).isEqualTo("session-owner"); + assertThat(security.assertCurrent(context.getAuthentication()).id()).isEqualTo(owner); + assertThat(context.getAuthentication().getAuthorities()) + .anySatisfy( + authority -> + assertThat(authority.getAuthority()) + .isEqualTo(FactorGrantedAuthority.PASSWORD_AUTHORITY)); + assertThat(loggedIn.headers().allValues("Set-Cookie")) + .anySatisfy( + cookie -> + assertThat(cookie).contains("JSESSIONID=", "Secure", "HttpOnly", "SameSite=Lax")); + } + + @Test + void staleAnonymousRequestCannotRestoreThePreLoginCookie() throws Exception { + CookieManager cookies = new CookieManager(null, CookiePolicy.ACCEPT_ALL); + HttpClient browser = browser(cookies); + String csrf = csrf(browser, "/login"); + String oldCookie = + cookies.getCookieStore().getCookies().stream() + .filter(cookie -> cookie.getName().equals("JSESSIONID")) + .findFirst() + .orElseThrow() + .getValue(); + String oldId = new String(Base64.getDecoder().decode(oldCookie), StandardCharsets.UTF_8); + HttpClient oldBrowser = browserFor(oldId); + try (var workers = java.util.concurrent.Executors.newSingleThreadExecutor()) { + var delayed = + CompletableFuture.supplyAsync( + () -> { + try { + return get(oldBrowser, "/login?fixtureSlow=true"); + } catch (Exception ex) { + throw new AssertionError(ex); + } + }, + workers); + try { + assertThat(slowRequests.entered.await(10, TimeUnit.SECONDS)).isTrue(); + assertThat(oldId.equals(slowRequests.loadedId)).isTrue(); + var login = + post( + browser, + "/login", + Map.of( + "username", + "session-owner", + "password", + "correct horse battery staple", + "_csrf", + csrf)); + assertThat(login.statusCode()).isEqualTo(302); + } finally { + slowRequests.release.countDown(); + } + assertThat(delayed.get(10, TimeUnit.SECONDS).statusCode()).isEqualTo(200); + } + assertThat(repository.findById(oldId)).isNull(); + assertThat(get(browser, "/security/sessions").statusCode()).isEqualTo(200); + assertThat(get(oldBrowser, "/security/sessions").statusCode()).isEqualTo(302); + } + + @Test + void onlyRecentMfaCanRevokeOwnedSessionsAndRevocationIsDurable() { + String owned = persist(auth(owner, true)); + String foreign = persist(auth(other, true)); + UUID ownedPrimary = primaryId(owned), foreignPrimary = primaryId(foreign); + assertThatThrownBy(() -> sessions.revoke(auth(owner, false), ownedPrimary, owned, "127.0.0.1")) + .isInstanceOf(AccessDeniedException.class); + assertThat(sessions.revoke(auth(owner, true), foreignPrimary, owned, "127.0.0.1")).isFalse(); + assertThat(repository.findById(foreign)).isNotNull(); + assertThat(sessions.revoke(auth(owner, true), ownedPrimary, owned, "127.0.0.1")).isTrue(); + assertThat(repository.findById(owned)).isNull(); + Session restored = + new JdbcIndexedSessionRepository(jdbc, new TransactionTemplate(transactions)) + .findById(owned); + assertThat(restored).isNull(); + } + + @Test + void selfSessionRevocationWorksOverHttpsAndRequiresCsrf() throws Exception { + String id = persist(auth(owner, true)); + HttpClient browser = browserFor(id); + UUID primary = primaryId(id); + assertThat(post(browser, "/security/sessions/" + primary + "/revoke", Map.of()).statusCode()) + .isEqualTo(403); + String csrf = csrf(browser, "/security/sessions"); + assertThat( + post(browser, "/security/sessions/" + primary + "/revoke", Map.of("_csrf", csrf)) + .statusCode()) + .isEqualTo(302); + assertThat(repository.findById(id)).isNull(); + assertThat(get(browser, "/security/sessions").statusCode()).isEqualTo(302); + } + + @Test + void oldCredentialSnapshotCannotCreateAValidSessionAfterReset() throws Exception { + Authentication old = auth(owner, true); + security.invalidateAll(owner); + String stale = persist(old); + assertThat(get(browserFor(stale), "/security/sessions").statusCode()).isEqualTo(403); + assertThat(repository.findById(stale)).isNull(); + assertThatThrownBy(() -> security.assertCurrent(old)).isInstanceOf(AccessDeniedException.class); + assertThat(get(browserFor(persist(auth(owner, true))), "/security/sessions").statusCode()) + .isEqualTo(200); + } + + @Test + void disablingEndsSessionsAndReenablingDoesNotReviveTheirAuthority() { + Authentication old = auth(owner, true); + String session = persist(old); + accounts.disable(owner); + assertThat(repository.findById(session)).isNull(); + jdbc.update("UPDATE accounts SET enabled=true WHERE id=?", owner); + assertThatThrownBy(() -> security.assertCurrent(old)).isInstanceOf(AccessDeniedException.class); + } + + @Test + void recoveryCodesAppearOnceAndConsumeAtomicallyWithCompleteAccessReset() { + Authentication full = auth(owner, true); + var issued = recovery.issue(full, "127.0.0.1"); + assertThat(issued.codes()) + .hasSize(10) + .doesNotHaveDuplicates() + .allSatisfy(code -> assertThat(code).hasSize(80)); + assertThat(recovery.status(full).remaining()).isEqualTo(10); + assertThat(jdbc.queryForList("SELECT token_hash FROM account_recovery_codes", String.class)) + .allSatisfy(hash -> assertThat(hash).hasSize(64).isNotIn(issued.codes())); + var key = key(owner); + String session = persist(full); + var dav = + accounts.issueDavCredential(owner, "Recovery fixture", Instant.now().plusSeconds(3600)); + String grant = UUID.randomUUID().toString(); + registrations.save( + org.springframework.security.oauth2.server.authorization.client.RegisteredClient.withId( + "recovery-client") + .clientId("recovery-client") + .clientAuthenticationMethod( + org.springframework.security.oauth2.core.ClientAuthenticationMethod.NONE) + .authorizationGrantType( + org.springframework.security.oauth2.core.AuthorizationGrantType.AUTHORIZATION_CODE) + .redirectUri("http://127.0.0.1:34567/callback") + .scope("dav") + .build()); + families.register( + new RefreshFamilyStore.Family( + grant, owner, "recovery-client", Instant.now().plusSeconds(3600)), + "recovery-refresh-token-fixture", + Instant.now().plusSeconds(3600), + () -> {}); + recovery.recover(auth(owner, false), issued.codes().getFirst(), "127.0.0.1"); + assertThat(repository.findById(session)).isNull(); + assertThat(accounts.verifyDav("session-owner", dav.secret())).isFalse(); + assertThat(families.isActive(grant)).isFalse(); + assertThat(passkeys.findByCredentialId(key.getCredentialId())).isNull(); + assertThatThrownBy(() -> passkeys.save(key)).isInstanceOf(BadCredentialsException.class); + assertThat( + jdbc.queryForObject( + "SELECT count(*) FROM account_recovery_codes WHERE used_at IS NOT NULL", + Long.class)) + .isEqualTo(1); + assertThatThrownBy( + () -> recovery.recover(auth(owner, false), issued.codes().getFirst(), "127.0.0.1")) + .isInstanceOf(AccessDeniedException.class); + assertThat(recovery.status(auth(owner, false)).remaining()).isEqualTo(9); + assertThat( + jdbc.queryForList( + "SELECT row_to_json(security_audit)::text FROM security_audit", String.class)) + .allSatisfy( + value -> + assertThat(value) + .doesNotContain( + issued.codes().getFirst(), "correct horse battery staple", dav.secret())); + } + + @Test + void recoveryRequiresRecentPrimaryAndRejectsCrossAccountWrongSecretExpiryAndRestore() { + var issued = recovery.issue(auth(owner, true), "127.0.0.1"); + assertThatThrownBy(() -> recovery.issue(auth(owner, false), "127.0.0.1")) + .isInstanceOf(AccessDeniedException.class); + String original = issued.codes().getFirst(); + assertThatThrownBy(() -> recovery.recover(auth(other, false), original, "127.0.0.1")) + .isInstanceOf(AccessDeniedException.class); + String wrong = original.substring(0, 79) + (original.endsWith("A") ? "B" : "A"); + assertThatThrownBy(() -> recovery.recover(auth(owner, false), wrong, "127.0.0.1")) + .isInstanceOf(AccessDeniedException.class); + jdbc.update( + "UPDATE account_recovery_codes SET created_at=now()-interval '2 years',expires_at=now()-interval '1 year'"); + assertThatThrownBy(() -> recovery.recover(auth(owner, false), original, "127.0.0.1")) + .isInstanceOf(AccessDeniedException.class); + String restored = recovery.issue(auth(owner, true), "127.0.0.1").codes().getFirst(); + jdbc.update("UPDATE system_state SET recovery_epoch=gen_random_uuid() WHERE id=1"); + assertThatThrownBy(() -> recovery.recover(auth(owner, false), restored, "127.0.0.1")) + .isInstanceOf(AccessDeniedException.class); + assertThat( + jdbc.queryForObject( + "SELECT count(*) FROM account_recovery_codes WHERE used_at IS NOT NULL", + Long.class)) + .isZero(); + } + + @Test + void concurrentRecoveryUsesOneCodeOnlyOnce() throws Exception { + String code = recovery.issue(auth(owner, true), "127.0.0.1").codes().getFirst(); + Authentication primary = auth(owner, false); + CountDownLatch start = new CountDownLatch(1); + try (var workers = java.util.concurrent.Executors.newFixedThreadPool(2)) { + var first = + CompletableFuture.supplyAsync(() -> recoverWhenStarted(start, primary, code), workers); + var second = + CompletableFuture.supplyAsync(() -> recoverWhenStarted(start, primary, code), workers); + start.countDown(); + assertThat(List.of(first.get(15, TimeUnit.SECONDS), second.get(15, TimeUnit.SECONDS))) + .containsExactlyInAnyOrder(true, false); + assertThat( + jdbc.queryForObject( + "SELECT count(*) FROM account_recovery_codes WHERE used_at IS NOT NULL", + Long.class)) + .isEqualTo(1); + } + } + + @Test + void failedRecoveryTransactionDoesNotConsumeTheCodeOrRevokeAccess() { + String code = recovery.issue(auth(owner, true), "127.0.0.1").codes().getFirst(); + Authentication before = auth(owner, false); + jdbc.execute( + "CREATE FUNCTION fail_security_reset() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN RAISE EXCEPTION 'fixture reset failure'; END $$"); + jdbc.execute( + "CREATE TRIGGER fail_security_reset BEFORE UPDATE OF security_epoch ON accounts FOR EACH ROW EXECUTE FUNCTION fail_security_reset()"); + try { + assertThatThrownBy(() -> recovery.recover(before, code, "127.0.0.1")) + .isInstanceOf(org.springframework.dao.DataAccessException.class); + assertThat( + jdbc.queryForObject( + "SELECT count(*) FROM account_recovery_codes WHERE used_at IS NOT NULL", + Long.class)) + .isZero(); + assertThat(security.assertCurrent(before).id()).isEqualTo(owner); + } finally { + jdbc.execute("DROP TRIGGER fail_security_reset ON accounts"); + jdbc.execute("DROP FUNCTION fail_security_reset()"); + } + recovery.recover(before, code, "127.0.0.1"); + } + + @Test + void passwordChangeRetainsPasskeysAndRevokesAllOtherAccess() { + Authentication before = auth(owner, true); + var key = key(owner); + String session = persist(before); + var dav = + accounts.issueDavCredential(owner, "Password fixture", Instant.now().plusSeconds(3600)); + recovery.issue(before, "127.0.0.1"); + passwords.change(before, "a replacement password for testing", "127.0.0.1"); + assertThat( + encoder.matches( + "a replacement password for testing", accounts.find(owner).passwordHash())) + .isTrue(); + assertThat(encoder.matches("correct horse battery staple", accounts.find(owner).passwordHash())) + .isFalse(); + assertThat(passkeys.findByCredentialId(key.getCredentialId())).isNotNull(); + assertThat(repository.findById(session)).isNull(); + assertThat(accounts.verifyDav("session-owner", dav.secret())).isFalse(); + assertThat( + jdbc.queryForObject( + "SELECT count(*) FROM account_recovery_codes WHERE account_id=?", + Long.class, + owner)) + .isZero(); + assertThatThrownBy(() -> security.assertCurrent(before)) + .isInstanceOf(AccessDeniedException.class); + } + + @Test + void passwordChangeRequiresMfaAndNeverCreatesAnExternalAccountPassword() { + assertThatThrownBy( + () -> + passwords.change( + auth(owner, false), "a replacement password for testing", "127.0.0.1")) + .isInstanceOf(AccessDeniedException.class); + assertThatThrownBy(() -> passwords.change(auth(owner, true), "界".repeat(25), "127.0.0.1")) + .isInstanceOf(IllegalArgumentException.class); + jdbc.update( + "INSERT INTO external_identities(issuer,subject,account_id,source,last_verified_at) VALUES ('https://issuer.invalid','subject',?,'LINKED',now())", + owner); + assertThatThrownBy( + () -> + passwords.change( + auth(owner, true), "a replacement password for testing", "127.0.0.1")) + .isInstanceOf(AccessDeniedException.class); + } + + @Test + void hashUpgradeKeepsEpochButStaleHashUpgradeCannotOverwriteAChangedPassword() { + String oldHash = new BCryptPasswordEncoder(10).encode("correct horse battery staple"); + jdbc.update("UPDATE accounts SET password_hash=? WHERE id=?", oldHash, owner); + UserDetails snapshot = user(owner); + UUID epoch = epoch(owner); + String stronger = encoder.encode("correct horse battery staple"); + UserDetails upgraded = passwords.updatePassword(snapshot, stronger); + assertThat(upgraded.getPassword()).isEqualTo(stronger); + assertThat(epoch(owner)).isEqualTo(epoch); + assertThat(accounts.find(owner).passwordHash()).isEqualTo(stronger); + UserDetails stale = user(owner); + passwords.change(auth(owner, true), "a replacement password for testing", "127.0.0.1"); + assertThatThrownBy( + () -> passwords.updatePassword(stale, encoder.encode("correct horse battery staple"))) + .isInstanceOf(CredentialsExpiredException.class); + assertThat( + encoder.matches( + "a replacement password for testing", accounts.find(owner).passwordHash())) + .isTrue(); + } + + @Test + void actualDaoLoginUpgradesBcryptWorkFactorWithoutRotatingEpoch() throws Exception { + jdbc.update( + "UPDATE accounts SET password_hash=? WHERE id=?", + new BCryptPasswordEncoder(10).encode("correct horse battery staple"), + owner); + UUID epoch = epoch(owner); + HttpClient browser = browser(new CookieManager(null, CookiePolicy.ACCEPT_ALL)); + String csrf = csrf(browser, "/login"); + assertThat( + post( + browser, + "/login", + Map.of( + "username", + "session-owner", + "password", + "correct horse battery staple", + "_csrf", + csrf)) + .statusCode()) + .isEqualTo(302); + assertThat(accounts.find(owner).passwordHash()).startsWith("$2a$12$"); + assertThat(epoch(owner)).isEqualTo(epoch); + assertThat(get(browser, "/security/sessions").statusCode()).isEqualTo(200); + } + + @Test + void durableCeremoniesBindSessionPurposeGenerationAndSingleUse() throws Exception { + String session = UUID.randomUUID().toString(), + hash = SecurityHashes.sha256(UUID.randomUUID().toString()); + ceremonies.issue(session, CeremonyStore.Purpose.REGISTER, hash, auth(owner, false)); + assertThat( + ceremonies.consume(UUID.randomUUID().toString(), CeremonyStore.Purpose.REGISTER, hash)) + .isFalse(); + assertThat(ceremonies.consume(session, CeremonyStore.Purpose.AUTHENTICATE, hash)).isFalse(); + CountDownLatch start = new CountDownLatch(1); + try (var workers = java.util.concurrent.Executors.newFixedThreadPool(2)) { + var first = + CompletableFuture.supplyAsync(() -> consumeWhenStarted(start, session, hash), workers); + var second = + CompletableFuture.supplyAsync(() -> consumeWhenStarted(start, session, hash), workers); + start.countDown(); + assertThat(List.of(first.get(10, TimeUnit.SECONDS), second.get(10, TimeUnit.SECONDS))) + .containsExactlyInAnyOrder(true, false); + } + ceremonies.issue(session, CeremonyStore.Purpose.REGISTER, hash, auth(owner, false)); + security.invalidateAll(owner); + assertThat(ceremonies.consume(session, CeremonyStore.Purpose.REGISTER, hash)).isFalse(); + ceremonies.issue(session, CeremonyStore.Purpose.AUTHENTICATE, hash, null); + jdbc.update( + "UPDATE webauthn_ceremonies SET created_at=now()-interval '2 minutes',expires_at=now()-interval '1 minute'"); + assertThat(ceremonies.consume(session, CeremonyStore.Purpose.AUTHENTICATE, hash)).isFalse(); + } + + @Test + void frameworkOptionsAreRecordedBeforeResponseAndOneAttemptConsumesChallengeOverHttps() + throws Exception { + HttpClient browser = browser(new CookieManager(null, CookiePolicy.ACCEPT_ALL)); + String csrf = csrf(browser, "/login"); + var options = post(browser, "/webauthn/authenticate/options", Map.of("_csrf", csrf)); + assertThat(options.statusCode()).isEqualTo(200); + assertThat(options.body()).contains("challenge"); + assertThat(jdbc.queryForObject("SELECT count(*) FROM webauthn_ceremonies", Long.class)) + .isEqualTo(1); + assertThat(post(browser, "/login/webauthn", Map.of("_csrf", csrf)).statusCode()).isEqualTo(401); + assertThat(jdbc.queryForObject("SELECT count(*) FROM webauthn_ceremonies", Long.class)) + .isZero(); + assertThat(post(browser, "/login/webauthn", Map.of("_csrf", csrf)).statusCode()).isEqualTo(401); + } + + @Test + void boundedTemporaryThrottleDoesNotExtendLockoutOrDisableAccounts() { + for (int count = 0; count < 5; count++) + assertThat(throttle.reserve("session-owner", "test-peer").allowed()).isTrue(); + assertThat(throttle.reserve("session-owner", "test-peer").allowed()).isFalse(); + Instant blocked = + jdbc.queryForObject( + "SELECT max(blocked_until) FROM login_throttle", + (rs, row) -> rs.getTimestamp(1).toInstant()); + for (int count = 0; count < 5; count++) + assertThat(throttle.reserve("SESSION-OWNER", "test-peer").allowed()).isFalse(); + Instant unchanged = + jdbc.queryForObject( + "SELECT max(blocked_until) FROM login_throttle", + (rs, row) -> rs.getTimestamp(1).toInstant()); + assertThat(unchanged).isEqualTo(blocked); + assertThat(accounts.find(owner).enabled()).isTrue(); + jdbc.update( + "UPDATE login_throttle SET blocked_until=now()-interval '1 second' WHERE blocked_until IS NOT NULL"); + assertThat(throttle.reserve("session-owner", "test-peer").allowed()).isTrue(); + throttle.result("session-owner", "test-peer", SecurityAudit.Action.PASSWORD_LOGIN, true); + assertThat(throttle.reserve("session-owner", "test-peer").allowed()).isTrue(); + } + + @Test + void throttleCapacityAndAuditRetentionAreBounded() { + jdbc.update( + "INSERT INTO login_throttle(bucket_key,window_started,attempts,expires_at) SELECT lpad(to_hex(value),64,'0'),now(),0,now()+interval '1 hour' FROM generate_series(1,10000) AS value"); + assertThat(throttle.reserve("another-name", "another-peer").allowed()).isFalse(); + assertThat(jdbc.queryForObject("SELECT count(*) FROM login_throttle", Long.class)) + .isEqualTo(10000); + jdbc.update("UPDATE login_throttle SET expires_at=now()-interval '1 second'"); + assertThat(throttle.reserve("another-name", "another-peer").allowed()).isTrue(); + jdbc.update( + "INSERT INTO security_audit(action,outcome) SELECT 'PASSWORD_LOGIN','FAILURE' FROM generate_series(1,10050)"); + audit.record( + owner, SecurityAudit.Action.PASSWORD_LOGIN, SecurityAudit.Outcome.SUCCESS, "127.0.0.1"); + assertThat(jdbc.queryForObject("SELECT count(*) FROM security_audit", Long.class)) + .isLessThanOrEqualTo(10000); + jdbc.update("UPDATE security_audit SET occurred_at=now()-interval '31 days'"); + audit.prune(); + assertThat(jdbc.queryForObject("SELECT count(*) FROM security_audit", Long.class)).isZero(); + } + + @Test + void realLoginFailuresThrottleAndForwardedHeadersCannotChoosePeer() throws Exception { + HttpClient browser = browser(new CookieManager(null, CookiePolicy.ACCEPT_ALL)); + String csrf = csrf(browser, "/login"); + for (int count = 0; count < 5; count++) + assertThat( + post( + browser, + "/login", + Map.of( + "username", + "session-owner", + "password", + "invalid-password", + "_csrf", + csrf)) + .statusCode()) + .isEqualTo(302); + var denied = + post( + browser, + "/login", + Map.of( + "username", + "session-owner", + "password", + "correct horse battery staple", + "_csrf", + csrf)); + assertThat(denied.statusCode()).isEqualTo(429); + assertThat(Long.parseLong(denied.headers().firstValue("Retry-After").orElseThrow())) + .isBetween(1L, 60L); + assertThat( + jdbc.queryForObject( + "SELECT count(*) FROM security_audit WHERE outcome='FAILURE'", Long.class)) + .isEqualTo(5); + assertThat(accounts.find(owner).enabled()).isTrue(); + String fields = + form( + Map.of( + "username", + "session-owner", + "password", + "correct horse battery staple", + "_csrf", + csrf)); + var forwarded = + browser.send( + HttpRequest.newBuilder(URI.create(base() + "/login")) + .timeout(Duration.ofSeconds(15)) + .header("X-Forwarded-For", "203.0.113.99") + .header("Content-Type", "application/x-www-form-urlencoded") + .POST(HttpRequest.BodyPublishers.ofString(fields)) + .build(), + HttpResponse.BodyHandlers.ofString()); + assertThat(forwarded.statusCode()).isEqualTo(429); + assertThat( + jdbc.queryForObject( + "SELECT count(*) FROM security_audit WHERE peer_hash=?", + Long.class, + SecurityHashes.sha256("203.0.113.99"))) + .isZero(); + assertThat( + jdbc.queryForObject( + "SELECT count(*) FROM security_audit WHERE peer_hash=?", + Long.class, + SecurityHashes.sha256("127.0.0.1"))) + .isGreaterThan(0); + } + + private Authentication auth(UUID account, boolean mfa) { + UserDetails user = user(account); + var authorities = + new ArrayList(user.getAuthorities()); + authorities.add( + FactorGrantedAuthority.fromAuthority(FactorGrantedAuthority.PASSWORD_AUTHORITY)); + if (mfa) + authorities.add( + FactorGrantedAuthority.fromAuthority(FactorGrantedAuthority.WEBAUTHN_AUTHORITY)); + return UsernamePasswordAuthenticationToken.authenticated(user, null, authorities); + } + + private UserDetails user(UUID account) { + var current = accounts.find(account); + return User.withUsername(current.username()) + .password(current.passwordHash()) + .authorities("ROLE_USER", SessionSecurity.authority(epoch(account)).getAuthority()) + .build(); + } + + private UUID epoch(UUID account) { + return jdbc.queryForObject( + "SELECT security_epoch FROM accounts WHERE id=?", UUID.class, account); + } + + private String persist(Authentication authentication) { + return persist(repository, authentication); + } + + private static String persist( + SessionRepository repository, Authentication authentication) { + S session = repository.createSession(); + session.setAttribute( + HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, + new SecurityContextImpl(authentication)); + repository.save(session); + return session.getId(); + } + + private UUID primaryId(String session) { + return UUID.fromString( + jdbc.queryForObject( + "SELECT primary_id FROM spring_session WHERE session_id=?", String.class, session)); + } + + private CredentialRecord key(UUID account) { + var user = + ImmutablePublicKeyCredentialUserEntity.builder() + .id(Bytes.random()) + .name(accounts.find(account).username()) + .displayName("Recovery fixture") + .build(); + passkeyUsers.save(user); + var now = Instant.now().truncatedTo(ChronoUnit.MICROS); + var key = + ImmutableCredentialRecord.builder() + .credentialId(Bytes.random()) + .userEntityUserId(user.getId()) + .publicKey(new ImmutablePublicKeyCose(new byte[] {1, 2, 3})) + .credentialType(PublicKeyCredentialType.PUBLIC_KEY) + .signatureCount(1) + .uvInitialized(true) + .backupEligible(false) + .backupState(false) + .transports(Set.of(AuthenticatorTransport.INTERNAL)) + .label("Recovery fixture") + .created(now) + .lastUsed(now) + .build(); + passkeys.save(key); + return key; + } + + private boolean recoverWhenStarted( + CountDownLatch start, Authentication authentication, String code) { + try { + if (!start.await(10, TimeUnit.SECONDS)) throw new AssertionError("Recovery latch timed out"); + recovery.recover(authentication, code, "127.0.0.1"); + return true; + } catch (AccessDeniedException ex) { + return false; + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + throw new AssertionError(ex); + } + } + + private boolean consumeWhenStarted(CountDownLatch start, String session, String hash) { + try { + if (!start.await(10, TimeUnit.SECONDS)) throw new AssertionError("Ceremony latch timed out"); + return ceremonies.consume(session, CeremonyStore.Purpose.REGISTER, hash); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + throw new AssertionError(ex); + } + } + + private HttpClient browser(CookieManager cookies) { + return HttpClient.newBuilder() + .sslContext(TestTls.instance().context()) + .cookieHandler(cookies) + .connectTimeout(Duration.ofSeconds(10)) + .followRedirects(HttpClient.Redirect.NEVER) + .build(); + } + + private HttpClient browserFor(String id) throws java.io.IOException { + var cookies = new CookieManager(null, CookiePolicy.ACCEPT_ALL); + String encoded = Base64.getEncoder().encodeToString(id.getBytes(StandardCharsets.UTF_8)); + cookies.put( + URI.create(base()), + Map.of( + "Set-Cookie", + List.of("JSESSIONID=" + encoded + "; Path=/; Secure; HttpOnly; SameSite=Lax"))); + return browser(cookies); + } + + private HttpResponse get(HttpClient browser, String path) throws Exception { + return browser.send( + HttpRequest.newBuilder(URI.create(base() + path)) + .timeout(Duration.ofSeconds(15)) + .GET() + .build(), + HttpResponse.BodyHandlers.ofString()); + } + + private HttpResponse post(HttpClient browser, String path, Map fields) + throws Exception { + return browser.send( + HttpRequest.newBuilder(URI.create(base() + path)) + .timeout(Duration.ofSeconds(15)) + .header("Content-Type", "application/x-www-form-urlencoded") + .POST(HttpRequest.BodyPublishers.ofString(form(fields))) + .build(), + HttpResponse.BodyHandlers.ofString()); + } + + private String csrf(HttpClient browser, String path) throws Exception { + var page = get(browser, path); + assertThat(page.statusCode()).isEqualTo(200); + var token = new java.util.concurrent.atomic.AtomicReference(); + new javax.swing.text.html.parser.ParserDelegator() + .parse( + new java.io.StringReader(page.body()), + new javax.swing.text.html.HTMLEditorKit.ParserCallback() { + @Override + public void handleSimpleTag( + javax.swing.text.html.HTML.Tag tag, + javax.swing.text.MutableAttributeSet attributes, + int position) { + if (tag == javax.swing.text.html.HTML.Tag.INPUT + && "_csrf" + .equals(attributes.getAttribute(javax.swing.text.html.HTML.Attribute.NAME))) + token.set( + (String) attributes.getAttribute(javax.swing.text.html.HTML.Attribute.VALUE)); + } + }, + true); + assertThat(token.get()).isNotBlank(); + return token.get(); + } + + private static String form(Map fields) { + return fields.entrySet().stream() + .map( + entry -> + URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8) + + "=" + + URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8)) + .collect(Collectors.joining("&")); + } + + private String base() { + return "https://127.0.0.1:" + port; + } + + static final class SlowRequestHarness { + volatile String loadedId; + final CountDownLatch entered = new CountDownLatch(1); + final CountDownLatch release = new CountDownLatch(1); + } + + @org.springframework.boot.test.context.TestConfiguration + static class SlowRequestConfiguration { + @org.springframework.context.annotation.Bean + SlowRequestHarness slowRequestHarness() { + return new SlowRequestHarness(); + } + + @org.springframework.context.annotation.Bean + org.springframework.boot.web.servlet.FilterRegistrationBean slowRequest( + SlowRequestHarness harness) { + var filter = + new org.springframework.boot.web.servlet.FilterRegistrationBean(); + filter.setOrder( + org.springframework.session.web.http.SessionRepositoryFilter.DEFAULT_ORDER + 1); + filter.setFilter( + (request, response, chain) -> { + var http = (jakarta.servlet.http.HttpServletRequest) request; + if ("GET".equals(http.getMethod()) + && "/login".equals(http.getServletPath()) + && "true".equals(http.getParameter("fixtureSlow"))) { + var session = http.getSession(); + harness.loadedId = session.getId(); + harness.entered.countDown(); + try { + if (!harness.release.await(15, TimeUnit.SECONDS)) + throw new AssertionError("Delayed request latch timed out"); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + throw new jakarta.servlet.ServletException(ex); + } + session.setMaxInactiveInterval(session.getMaxInactiveInterval()); + session.setAttribute("concurrent-fixture", "completed"); + response.getWriter().write("Delayed request completed"); + } else chain.doFilter(request, response); + }); + return filter; + } + } +} diff --git a/src/test/java/org/busystack/caldav/sharing/GroupServiceIT.java b/src/test/java/org/busystack/caldav/sharing/GroupServiceIT.java new file mode 100644 index 0000000..70beea2 --- /dev/null +++ b/src/test/java/org/busystack/caldav/sharing/GroupServiceIT.java @@ -0,0 +1,524 @@ +package org.busystack.caldav.sharing; + +import static org.assertj.core.api.Assertions.*; + +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; +import java.util.UUID; +import org.busystack.caldav.account.AccountService; +import org.busystack.caldav.security.session.SessionSecurity; +import org.busystack.caldav.storage.CalendarStore; +import org.busystack.caldav.storage.DavStore; +import org.busystack.caldav.storage.StoreException; +import org.busystack.caldav.storage.WriteCondition; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.security.access.AccessDeniedException; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.authority.FactorGrantedAuthority; +import org.springframework.security.core.authority.SimpleGrantedAuthority; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; +import org.testcontainers.postgresql.PostgreSQLContainer; + +@SpringBootTest( + properties = { + "busystack.public-base-url=https://calendar.example", + "busystack.issuer=https://calendar.example" + }) +@Testcontainers +@org.springframework.test.annotation.DirtiesContext( + classMode = org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_CLASS) +class GroupServiceIT { + @Container + static final PostgreSQLContainer POSTGRES = + org.busystack.caldav.testing.LoopbackContainers.postgres(); + + @DynamicPropertySource + static void database(DynamicPropertyRegistry properties) { + properties.add("spring.datasource.url", POSTGRES::getJdbcUrl); + properties.add("spring.datasource.username", POSTGRES::getUsername); + properties.add("spring.datasource.password", POSTGRES::getPassword); + } + + @Autowired GroupService groups; + @Autowired AccountService accounts; + @Autowired CalendarStore calendars; + @Autowired DavStore dav; + @Autowired JdbcTemplate jdbc; + @Autowired org.busystack.caldav.management.ManagementService management; + @Autowired org.springframework.transaction.PlatformTransactionManager transactions; + UUID administrator, owner, bob, carol, calendar; + + @BeforeEach + void prepare() { + org.busystack.caldav.testing.FixtureDatabase.clearAccounts(jdbc); + administrator = + accounts.provision("administrator", "Administrator", "correct horse battery staple", true); + owner = accounts.provision("owner", "Calendar owner", "correct horse battery staple", false); + bob = accounts.provision("bob", "Bob", "correct horse battery staple", false); + carol = accounts.provision("carol", "Carol", "correct horse battery staple", false); + calendar = calendars.createCollection(owner, "Shared calendar", "VEVENT"); + } + + private Authentication auth(UUID id, boolean mfa) { + var account = accounts.find(id); + List authorities = new ArrayList<>(); + authorities.add( + new SimpleGrantedAuthority(account.administrator() ? "ROLE_ADMIN" : "ROLE_USER")); + authorities.add(SessionSecurity.authority(account.securityEpoch())); + authorities.add( + FactorGrantedAuthority.fromAuthority(FactorGrantedAuthority.PASSWORD_AUTHORITY)); + if (mfa) + authorities.add( + FactorGrantedAuthority.fromAuthority(FactorGrantedAuthority.WEBAUTHN_AUTHORITY)); + return UsernamePasswordAuthenticationToken.authenticated(account.username(), null, authorities); + } + + private UUID group(String name, Set members) { + return groups.create(auth(administrator, true), name, members); + } + + private byte[] event(String uid) { + return ("BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Group fixture//EN\r\nBEGIN:VEVENT\r\nUID:" + + uid + + "\r\nDTSTAMP:20261027T000000Z\r\nDTSTART:20261028T000000Z\r\nX-CLIENT:preserve\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n") + .getBytes(StandardCharsets.UTF_8); + } + + private void denied(Runnable operation) { + assertThatThrownBy(operation::run) + .isInstanceOfAny(AccessDeniedException.class, StoreException.class); + } + + @Test + void individualGrantListingIsOwnerScopedAndDisabledTargetsCanBeRevoked() { + management.share(auth(owner, true), calendar, "bob", "READ"); + assertThat(management.individualShares(auth(owner, false), 0).rows()) + .singleElement() + .satisfies( + share -> { + assertThat(share.username()).isEqualTo("bob"); + assertThat(share.permission()).isEqualTo("READ"); + }); + assertThat(management.individualShares(auth(bob, false), 0).rows()).isEmpty(); + assertThat(management.individualShares(auth(administrator, false), 0).rows()).isEmpty(); + accounts.disable(bob); + assertThat(management.individualShares(auth(owner, false), 0).rows()) + .singleElement() + .extracting(org.busystack.caldav.management.ManagementService.IndividualShare::enabled) + .isEqualTo(false); + management.share(auth(owner, true), calendar, "bob", "REVOKE"); + assertThat(management.individualShares(auth(owner, false), 0).rows()).isEmpty(); + } + + @Test + void administrationRequiresCurrentAdministratorAndMfaButSharingChoicesDoNot() { + denied(() -> groups.create(auth(administrator, false), "Denied", Set.of())); + denied(() -> groups.create(auth(owner, true), "Denied", Set.of())); + UUID group = group("Engineering", Set.of("bob")); + assertThat(groups.choices(auth(owner, false))) + .containsExactly(new GroupService.Choice(group, "Engineering")); + denied(() -> groups.list(auth(owner, true))); + denied(() -> groups.list(auth(administrator, false))); + var stale = auth(administrator, true); + jdbc.update("UPDATE accounts SET security_epoch=gen_random_uuid() WHERE id=?", administrator); + denied(() -> groups.update(stale, group, "Changed", Set.of("bob"))); + assertThat(groups.list(auth(administrator, true))) + .singleElement() + .satisfies(value -> assertThat(value.name()).isEqualTo("Engineering")); + } + + @Test + void groupReadWriteAndMembershipRevocationUseTheSameAuthoritativeStore() { + UUID group = group("Team", Set.of("bob")); + groups.grant(auth(owner, true), calendar, group, "READ"); + dav.putResource(owner, calendar, "original.ics", event("original"), WriteCondition.create()); + assertThat(calendars.collections(bob)) + .extracting(CalendarStore.Collection::id) + .contains(calendar); + assertThat(dav.snapshot(bob, bob, calendar.toString()).resources()).hasSize(1); + denied( + () -> + dav.putResource( + bob, calendar, "forbidden.ics", event("forbidden"), WriteCondition.create())); + groups.grant(auth(owner, true), calendar, group, "WRITE"); + dav.putResource(bob, calendar, "written.ics", event("written"), WriteCondition.create()); + String token = calendars.sync(bob, calendar, null, 100).token(); + groups.update(auth(administrator, true), group, "Team renamed", Set.of("carol")); + denied(() -> calendars.get(bob, calendar, "original.ics")); + denied(() -> calendars.sync(bob, calendar, token, 100)); + assertThat(dav.snapshot(carol, carol, calendar.toString()).resources()).hasSize(2); + dav.putResource(carol, calendar, "carol.ics", event("carol"), WriteCondition.create()); + assertThat(calendars.get(owner, calendar, "carol.ics")).isNotNull(); + } + + @Test + void overlappingDirectAndGroupGrantsHaveWritePrecedenceAndKeepBindingsStable() { + UUID read = group("Readers", Set.of("bob")), write = group("Writers", Set.of("bob")); + groups.grant(auth(owner, true), calendar, read, "READ"); + groups.grant(auth(owner, true), calendar, write, "WRITE"); + String path = dav.children(bob, bob, "").getFirst().binding().path(); + assertThat(dav.permission(bob, calendar)).isEqualTo("WRITE"); + groups.update(auth(administrator, true), write, "Writers", Set.of()); + assertThat(dav.permission(bob, calendar)).isEqualTo("READ"); + assertThat(dav.children(bob, bob, "").getFirst().binding().path()).isEqualTo(path); + calendars.share(owner, calendar, bob, "WRITE"); + groups.update(auth(administrator, true), read, "Readers", Set.of()); + assertThat(dav.permission(bob, calendar)).isEqualTo("WRITE"); + assertThat(dav.children(bob, bob, "").getFirst().binding().path()).isEqualTo(path); + calendars.share(owner, calendar, bob, null); + denied(() -> calendars.collection(bob, calendar)); + assertThat(dav.children(bob, bob, "")).isEmpty(); + } + + @Test + void groupAdministrationDoesNotBypassOwnershipOrGrantAnAdministratorRole() { + UUID group = group("Administrators", Set.of("bob")); + denied(() -> groups.grant(auth(administrator, true), calendar, group, "WRITE")); + denied(() -> groups.grant(auth(owner, false), calendar, group, "READ")); + groups.grant(auth(owner, true), calendar, group, "READ"); + denied(() -> calendars.collection(administrator, calendar)); + assertThat(accounts.find(bob).administrator()).isFalse(); + denied(() -> groups.list(auth(bob, true))); + assertThat(groups.ownedGrants(auth(bob, false))).isEmpty(); + groups.update( + auth(administrator, true), group, "Administrators", Set.of("bob", "administrator")); + assertThat(dav.permission(administrator, calendar)).isEqualTo("READ"); + denied(() -> groups.grant(auth(administrator, true), calendar, group, "WRITE")); + } + + @Test + void deletingAGroupRevokesOnlyDerivedPermissionsAndRetiresItsPrincipalId() { + UUID group = group("Temporary", Set.of("bob")); + groups.grant(auth(owner, true), calendar, group, "READ"); + calendars.share(owner, calendar, carol, "READ"); + dav.putResource(owner, calendar, "one.ics", event("one"), WriteCondition.create()); + byte[] preserved = calendars.get(owner, calendar, "one.ics").content(); + groups.delete(auth(administrator, true), group); + denied(() -> calendars.collection(bob, calendar)); + assertThat(calendars.get(carol, calendar, "one.ics").content()).isEqualTo(preserved); + assertThat(dav.recognizedPrincipal(group)).isFalse(); + UUID replacement = group("Temporary", Set.of()); + assertThat(replacement).isNotEqualTo(group); + assertThatThrownBy( + () -> + jdbc.update( + "INSERT INTO sharing_groups(id,name,created_by) VALUES (?,?,?)", + group, + "Cannot reuse identity", + administrator)) + .isInstanceOf(org.springframework.dao.DuplicateKeyException.class); + } + + @Test + void principalVisibilityDisclosesMembershipOnlyToMembersOwnersAndAdministrators() { + UUID group = group("Private membership", Set.of("bob")); + assertThat(groups.choices(auth(owner, false))) + .extracting(GroupService.Choice::id) + .contains(group); + denied(() -> dav.principal(owner, group)); + assertThat(dav.groupMembers(bob, group)) + .extracting(DavStore.Principal::id) + .containsExactly(bob); + assertThat(dav.principals(bob)).extracting(DavStore.Principal::id).doesNotContain(carol, owner); + groups.grant(auth(owner, true), calendar, group, "READ"); + assertThat(dav.bindingFor(bob, calendar).home()).isEqualTo(bob); + assertThat(dav.bindingFor(owner, calendar).home()).isEqualTo(owner); + assertThat(dav.bindingFor(carol, calendar)).isNull(); + assertThat(dav.principal(owner, group).group()).isTrue(); + assertThat(dav.principal(owner, group).owner()).isEqualTo(administrator); + assertThat(dav.groupMembers(owner, group)) + .extracting(DavStore.Principal::id) + .containsExactly(bob); + assertThat(dav.principalGroups(owner, bob)) + .extracting(DavStore.Principal::id) + .containsExactly(group); + assertThat(dav.principalGroups(bob, group)).isEmpty(); + UUID unrelated = group("Only another member", Set.of("carol")); + assertThat(dav.principalGroups(administrator, bob)) + .extracting(DavStore.Principal::id) + .containsExactly(group) + .doesNotContain(unrelated); + } + + @Test + void groupAndIndividualAclsReplaceAtomicallyWithoutEscalation() { + UUID group = group("Editors", Set.of("bob")); + dav.replaceAcl( + owner, + calendar, + List.of(new DavStore.Member(group, "WRITE", true), new DavStore.Member(carol, "READ"))); + assertThat(dav.members(owner, calendar)) + .containsExactlyInAnyOrder( + new DavStore.Member(group, "WRITE", true), new DavStore.Member(carol, "READ")); + assertThat(dav.permission(bob, calendar)).isEqualTo("WRITE"); + assertThat(dav.permission(carol, calendar)).isEqualTo("READ"); + assertThatThrownBy( + () -> + dav.replaceAcl( + owner, + calendar, + List.of(new DavStore.Member(UUID.randomUUID(), "WRITE", true)))) + .isInstanceOf(IllegalArgumentException.class); + assertThat(dav.permission(bob, calendar)).isEqualTo("WRITE"); + assertThatThrownBy( + () -> + jdbc.update( + "INSERT INTO sharing_groups(id,name,created_by) VALUES (?,?,?)", + owner, + "No account collision", + administrator)) + .isInstanceOf(org.springframework.dao.DuplicateKeyException.class); + assertThat(accounts.find(owner).enabled()).isTrue(); + } + + @Test + void membershipRevocationOrdersAfterAnInFlightWriteAndRejectsSubsequentWrites() throws Exception { + UUID group = group("Concurrent editors", Set.of("bob")); + var administratorAuthentication = auth(administrator, true); + groups.grant(auth(owner, true), calendar, group, "WRITE"); + var written = new java.util.concurrent.CountDownLatch(1); + var allowCommit = new java.util.concurrent.CountDownLatch(1); + var holderPid = new java.util.concurrent.atomic.AtomicInteger(); + try (var workers = java.util.concurrent.Executors.newVirtualThreadPerTaskExecutor()) { + var mutation = + java.util.concurrent.CompletableFuture.runAsync( + () -> + new org.springframework.transaction.support.TransactionTemplate(transactions) + .executeWithoutResult( + status -> { + holderPid.set( + jdbc.queryForObject("SELECT pg_backend_pid()", Integer.class)); + dav.putResource( + bob, + calendar, + "before-revocation.ics", + event("before-revocation"), + WriteCondition.create()); + written.countDown(); + try { + if (!allowCommit.await(10, java.util.concurrent.TimeUnit.SECONDS)) + throw new AssertionError("Group revocation coordination timed out"); + } catch (InterruptedException failure) { + Thread.currentThread().interrupt(); + throw new AssertionError(failure); + } + }), + workers); + try { + assertThat(written.await(10, java.util.concurrent.TimeUnit.SECONDS)).isTrue(); + var revocation = + java.util.concurrent.CompletableFuture.runAsync( + () -> + groups.update( + administratorAuthentication, group, "Concurrent editors", Set.of()), + workers); + org.awaitility.Awaitility.await() + .atMost(java.time.Duration.ofSeconds(10)) + .untilAsserted( + () -> + assertThat( + jdbc.queryForObject( + "SELECT EXISTS(SELECT 1 FROM pg_stat_activity WHERE ?=ANY(pg_blocking_pids(pid)))", + Boolean.class, + holderPid.get())) + .isTrue()); + allowCommit.countDown(); + mutation.get(15, java.util.concurrent.TimeUnit.SECONDS); + revocation.get(15, java.util.concurrent.TimeUnit.SECONDS); + } finally { + allowCommit.countDown(); + } + } + assertThat(calendars.get(owner, calendar, "before-revocation.ics")).isNotNull(); + denied( + () -> + dav.putResource( + bob, + calendar, + "after-revocation.ics", + event("after-revocation"), + WriteCondition.create())); + assertThat(calendars.get(owner, calendar, "after-revocation.ics")).isNull(); + assertThat(dav.children(bob, bob, "")).isEmpty(); + } + + @Test + void failedReconciliationRollsBackMembershipNameBindingsAndSynchronizationTogether() { + UUID group = group("Atomic", Set.of("bob")); + groups.grant(auth(owner, true), calendar, group, "READ"); + String token = calendars.sync(bob, calendar, null, 100).token(); + jdbc.execute("CREATE TABLE blocked_group_binding(account_id uuid PRIMARY KEY)"); + jdbc.update("INSERT INTO blocked_group_binding VALUES (?)", carol); + jdbc.execute( + "CREATE FUNCTION reject_group_binding() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN IF EXISTS(SELECT 1 FROM blocked_group_binding WHERE account_id=NEW.home_id) THEN RAISE EXCEPTION 'simulated group binding failure'; END IF; RETURN NEW; END $$"); + jdbc.execute( + "CREATE TRIGGER reject_group_binding BEFORE INSERT ON dav_bindings FOR EACH ROW EXECUTE FUNCTION reject_group_binding()"); + try { + assertThatThrownBy( + () -> + groups.update( + auth(administrator, true), group, "Must roll back", Set.of("bob", "carol"))) + .isInstanceOf(org.springframework.dao.DataAccessException.class); + assertThat(groups.list(auth(administrator, true)).getFirst().name()).isEqualTo("Atomic"); + assertThat(dav.groupMembers(bob, group)) + .extracting(DavStore.Principal::id) + .containsExactly(bob); + assertThat(calendars.sync(bob, calendar, token, 100).changes()).isEmpty(); + denied(() -> calendars.collection(carol, calendar)); + } finally { + jdbc.execute("DROP TRIGGER reject_group_binding ON dav_bindings"); + jdbc.execute("DROP FUNCTION reject_group_binding()"); + jdbc.execute("DROP TABLE blocked_group_binding"); + } + groups.update(auth(administrator, true), group, "Atomic", Set.of("bob", "carol")); + assertThat(dav.permission(carol, calendar)).isEqualTo("READ"); + } + + @Test + void largeOwnedGrantSetsRemainListableAndRevocable() { + new org.springframework.transaction.support.TransactionTemplate(transactions) + .executeWithoutResult( + status -> { + jdbc.query("SELECT set_config('busystack.defer_dav_bindings','on',true)", rs -> {}); + jdbc.update( + "INSERT INTO sharing_groups(id,name,created_by) SELECT gen_random_uuid(),'capacity-'||n,? FROM generate_series(1,101) n", + administrator); + jdbc.update( + "INSERT INTO collections(id,owner_id,name,component) SELECT gen_random_uuid(),?,'capacity-'||n,'VEVENT' FROM generate_series(1,100) n", + owner); + jdbc.update( + "INSERT INTO collection_group_members(collection_id,group_id,permission) SELECT c.id,g.id,'READ' FROM collections c CROSS JOIN sharing_groups g WHERE c.owner_id=? AND c.name LIKE 'capacity-%' AND g.name LIKE 'capacity-%'", + owner); + }); + assertThat(groups.ownedGrants(auth(owner, false))).hasSize(50); + var first = groups.ownedGrants(auth(owner, false)).getFirst(); + groups.grant(auth(owner, true), first.collection(), first.group(), "REVOKE"); + assertThat( + jdbc.queryForObject( + "SELECT count(*) FROM collection_group_members WHERE collection_id=? AND group_id=?", + Integer.class, + first.collection(), + first.group())) + .isZero(); + } + + @Test + void aGroupSpanningMoreThanTenThousandCollectionsRemainsMaintainable() { + UUID group = group("Large group", Set.of()); + new org.springframework.transaction.support.TransactionTemplate(transactions) + .executeWithoutResult( + status -> { + jdbc.query("SELECT set_config('busystack.defer_dav_bindings','on',true)", rs -> {}); + jdbc.update( + "INSERT INTO collections(id,owner_id,name,component) SELECT gen_random_uuid(),?,'span-'||n,'VEVENT' FROM generate_series(1,10001) n", + owner); + jdbc.update( + "INSERT INTO collection_group_members(collection_id,group_id,permission) SELECT id,?,'READ' FROM collections WHERE owner_id=? AND name LIKE 'span-%'", + group, owner); + }); + long started = System.nanoTime(); + withGenericGroupPlan( + () -> groups.update(auth(administrator, true), group, "Large renamed", Set.of("bob"))); + assertThat(groups.list(auth(administrator, true)).getFirst().name()).isEqualTo("Large renamed"); + assertThat( + jdbc.queryForObject( + "SELECT count(*) FROM dav_bindings WHERE home_id=?", Long.class, bob)) + .isEqualTo(10001); + long added = System.nanoTime(); + withGenericGroupPlan( + () -> groups.update(auth(administrator, true), group, "Large emptied", Set.of())); + assertThat( + jdbc.queryForObject( + "SELECT count(*) FROM dav_bindings WHERE home_id=?", Long.class, bob)) + .isZero(); + long removed = System.nanoTime(); + withGenericGroupPlan( + () -> groups.update(auth(administrator, true), group, "Large restored", Set.of("bob"))); + assertThat( + jdbc.queryForObject( + "SELECT count(*) FROM dav_bindings WHERE home_id=?", Long.class, bob)) + .isEqualTo(10001); + long restored = System.nanoTime(); + + withGenericGroupPlan(() -> groups.delete(auth(administrator, true), group)); + assertThat( + jdbc.queryForObject( + "SELECT count(*) FROM dav_bindings WHERE home_id=?", Long.class, bob)) + .isZero(); + assertThat( + jdbc.queryForObject( + "SELECT count(*) FROM collection_group_members WHERE group_id=?", + Integer.class, + group)) + .isZero(); + System.out.printf( + "GROUP_CAPACITY collections=10001 add_ms=%d remove_ms=%d readd_ms=%d delete_ms=%d%n", + java.util.concurrent.TimeUnit.NANOSECONDS.toMillis(added - started), + java.util.concurrent.TimeUnit.NANOSECONDS.toMillis(removed - added), + java.util.concurrent.TimeUnit.NANOSECONDS.toMillis(restored - removed), + java.util.concurrent.TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - restored)); + } + + private void withGenericGroupPlan(Runnable operation) { + // Exercise the plan PostgreSQL can cache after several small groups, with stale estimates + // after the bulk insert. Keep the production operation's existing 30-second deadline. + var transaction = new org.springframework.transaction.support.TransactionTemplate(transactions); + transaction.setTimeout(30); + transaction.executeWithoutResult( + status -> { + jdbc.execute("SET LOCAL plan_cache_mode=force_generic_plan"); + operation.run(); + }); + } + + @Test + void xmlInvalidGroupNamesAreRejectedBeforeCreationOrRename() { + UUID existing = group("Existing", Set.of("bob")); + var checks = new java.util.ArrayList(); + for (int invalid : new int[] {0xFFFE, 0xFFFF}) { + String created = "Created" + (char) invalid, renamed = "Renamed" + (char) invalid; + checks.add( + () -> + assertThatThrownBy(() -> group(created, Set.of())) + .isInstanceOf(IllegalArgumentException.class)); + checks.add( + () -> + assertThatThrownBy( + () -> + groups.update( + auth(administrator, true), existing, renamed, Set.of("bob"))) + .isInstanceOf(IllegalArgumentException.class)); + } + org.junit.jupiter.api.Assertions.assertAll(checks); + assertThat(groups.list(auth(administrator, true))) + .singleElement() + .extracting(GroupService.Group::name) + .isEqualTo("Existing"); + for (String valid : + List.of( + "Valid\uFDD0", + "Valid" + new String(Character.toChars(0x1FFFE)), + "Line\nName\tAllowed", + "DEL\u007fC1\u0085")) { + UUID id = group(valid, Set.of()); + assertThat(groups.choices(auth(owner, false))) + .anySatisfy( + choice -> { + assertThat(choice.id()).isEqualTo(id); + assertThat(choice.name()).isEqualTo(valid); + }); + groups.update(auth(administrator, true), id, valid + " renamed", Set.of()); + } + } +} diff --git a/src/test/java/org/busystack/caldav/storage/CalendarAuditAtomicityIT.java b/src/test/java/org/busystack/caldav/storage/CalendarAuditAtomicityIT.java new file mode 100644 index 0000000..72e2059 --- /dev/null +++ b/src/test/java/org/busystack/caldav/storage/CalendarAuditAtomicityIT.java @@ -0,0 +1,266 @@ +package org.busystack.caldav.storage; + +import static org.assertj.core.api.Assertions.*; + +import java.nio.charset.StandardCharsets; +import java.util.List; +import java.util.UUID; +import org.busystack.caldav.account.AccountService; +import org.busystack.caldav.calendar.*; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; +import org.testcontainers.postgresql.PostgreSQLContainer; + +@SpringBootTest( + properties = { + "busystack.public-base-url=https://calendar.example", + "busystack.issuer=https://calendar.example" + }) +@org.springframework.test.annotation.DirtiesContext( + classMode = org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_CLASS) +@Testcontainers +class CalendarAuditAtomicityIT { + @Container + static final PostgreSQLContainer POSTGRES = + org.busystack.caldav.testing.LoopbackContainers.postgres(); + + @DynamicPropertySource + static void database(DynamicPropertyRegistry registry) { + registry.add("spring.datasource.url", POSTGRES::getJdbcUrl); + registry.add("spring.datasource.username", POSTGRES::getUsername); + registry.add("spring.datasource.password", POSTGRES::getPassword); + } + + @Autowired CalendarStore store; + @Autowired AccountService accounts; + @Autowired JdbcTemplate jdbc; + UUID owner; + + @BeforeEach + void prepare() { + org.busystack.caldav.testing.FixtureDatabase.clearAccounts(jdbc); + owner = + accounts.provision( + "calendar-auditor", + "Calendar auditor", + "independent atomicity fixture password", + false); + } + + private static byte[] fixture(String name) throws Exception { + try (var input = + CalendarAuditAtomicityIT.class.getResourceAsStream("/calendar/audit/" + name)) { + if (input == null) throw new IllegalStateException("Missing original independent fixture"); + return input.readAllBytes(); + } + } + + private long count(String table, UUID calendar) { + return jdbc.queryForObject( + "SELECT count(*) FROM " + table + " WHERE collection_id=?", Long.class, calendar); + } + + private static byte[] valid(String type, String uid) { + return ("BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Atomic audit regression//EN\r\nBEGIN:" + + type + + "\r\nUID:" + + uid + + "\r\nDTSTAMP:20261027T000000Z\r\nDTSTART:20261027T120000Z\r\nEND:" + + type + + "\r\nEND:VCALENDAR\r\n") + .getBytes(StandardCharsets.UTF_8); + } + + @Test + void allNineOriginalInvalidInputsLeaveCreationReplacementHistoryAndTokensUntouched() + throws Exception { + for (String name : + List.of( + "invalid-date-hours-duration.ics", + "invalid-event-end-before-start.ics", + "invalid-event-negative-duration.ics", + "invalid-event-no-start.ics", + "invalid-rule-count-and-until.ics", + "invalid-rule-repeat-count.ics", + "invalid-rule-repeat-freq.ics", + "invalid-rule-until-type.ics", + "invalid-todo-no-start-duration.ics")) { + byte[] invalid = fixture(name); + String text = new String(invalid, StandardCharsets.UTF_8); + String uid = + text.lines() + .filter(line -> line.startsWith("UID:")) + .findFirst() + .orElseThrow() + .substring(4); + String type = name.contains("todo") ? "VTODO" : "VEVENT"; + UUID calendar = store.createCollection(owner, name, type); + String emptyToken = store.currentSyncToken(owner, calendar); + assertThatThrownBy( + () -> + store.put(owner, calendar, "invalid.ics", uid, invalid, WriteCondition.create())) + .as(name + " creation") + .isInstanceOfSatisfying( + CalendarValidationException.class, + error -> + assertThat(error.reason()) + .isEqualTo(CalendarValidationException.Reason.INVALID_DATA)); + assertThat(store.get(owner, calendar, "invalid.ics")).isNull(); + assertThat(store.currentSyncToken(owner, calendar)).isEqualTo(emptyToken); + assertThat(count("calendar_resource_versions", calendar)).isZero(); + assertThat(count("collection_changes", calendar)).isZero(); + byte[] previous = valid(type, uid); + var old = store.put(owner, calendar, "invalid.ics", uid, previous, WriteCondition.create()); + String token = store.currentSyncToken(owner, calendar); + long versions = count("calendar_resource_versions", calendar), + changes = count("collection_changes", calendar); + assertThatThrownBy( + () -> store.put(owner, calendar, "invalid.ics", uid, invalid, WriteCondition.none())) + .as(name + " replacement") + .isInstanceOf(CalendarValidationException.class); + var after = store.get(owner, calendar, "invalid.ics"); + assertThat(after.content()).isEqualTo(previous); + assertThat(after.etag()).isEqualTo(old.etag()); + assertThat(after.revision()).isEqualTo(old.revision()); + assertThat(store.currentSyncToken(owner, calendar)).isEqualTo(token); + assertThat(count("calendar_resource_versions", calendar)).isEqualTo(versions); + assertThat(count("collection_changes", calendar)).isEqualTo(changes); + assertThat(store.sync(owner, calendar, token, 100).changes()).isEmpty(); + } + } + + @Test + void residualLexicalFailuresPreserveConditionalWritesRevisionsHistoryAndSynchronization() + throws Exception { + for (String name : + List.of( + "invalid-leading-token", + "invalid-unicode-leading-token", + "invalid-unicode-bom-leading-token", + "invalid-zero-byday", + "invalid-plus-zero-byday", + "invalid-minus-zero-byday", + "invalid-weeks-days")) { + String originalName = + name.equals("invalid-minus-zero-byday") + ? "invalid-zero-byday" + : name.contains("unicode") ? "invalid-leading-token" : name; + byte[] invalid; + try (var input = + CalendarAuditAtomicityIT.class.getResourceAsStream( + "/calendar/independent-verification/" + originalName + ".ics")) { + if (input == null) throw new IllegalStateException("Missing independent residual fixture"); + invalid = input.readAllBytes(); + } + if (name.equals("invalid-minus-zero-byday")) + invalid = + new String(invalid, StandardCharsets.UTF_8) + .replace("BYDAY=0MO", "BYDAY=-0MO") + .getBytes(StandardCharsets.UTF_8); + if (name.contains("unicode")) + invalid = + new String(invalid, StandardCharsets.UTF_8) + .replace( + "XBEGIN:VCALENDAR", + (name.contains("-bom-") ? "\uFEFF" : "") + "beg\u0131n:vcalendar") + .getBytes(StandardCharsets.UTF_8); + final byte[] rejected = invalid; + String uid = + new String(rejected, StandardCharsets.UTF_8) + .lines() + .filter(line -> line.startsWith("UID:")) + .findFirst() + .orElseThrow() + .substring(4); + UUID calendar = store.createCollection(owner, name, "VEVENT"); + String emptyToken = store.currentSyncToken(owner, calendar); + long emptyRevision = revision(calendar); + assertThatThrownBy( + () -> + store.put( + owner, calendar, "residual.ics", uid, rejected, WriteCondition.create())) + .as(name + " creation") + .isInstanceOfSatisfying( + CalendarValidationException.class, + error -> + assertThat(error.reason()) + .isEqualTo(CalendarValidationException.Reason.INVALID_DATA)); + assertThat(store.get(owner, calendar, "residual.ics")).isNull(); + assertThat(count("calendar_resources", calendar)).isZero(); + assertThat(count("calendar_resource_versions", calendar)).isZero(); + assertThat(count("collection_changes", calendar)).isZero(); + assertThat(revision(calendar)).isEqualTo(emptyRevision); + assertThat(store.currentSyncToken(owner, calendar)).isEqualTo(emptyToken); + + byte[] previous = valid("VEVENT", uid); + var old = store.put(owner, calendar, "residual.ics", uid, previous, WriteCondition.create()); + String token = store.currentSyncToken(owner, calendar); + long collectionRevision = revision(calendar), + versions = count("calendar_resource_versions", calendar), + changes = count("collection_changes", calendar); + assertThatThrownBy( + () -> + store.put( + owner, + calendar, + "residual.ics", + uid, + rejected, + WriteCondition.match(old.etag()))) + .as(name + " conditional replacement") + .isInstanceOfSatisfying( + CalendarValidationException.class, + error -> + assertThat(error.reason()) + .isEqualTo(CalendarValidationException.Reason.INVALID_DATA)); + var after = store.get(owner, calendar, "residual.ics"); + assertThat(after.content()).isEqualTo(previous); + assertThat(after.etag()).isEqualTo(old.etag()); + assertThat(after.revision()).isEqualTo(old.revision()); + assertThat(revision(calendar)).isEqualTo(collectionRevision); + assertThat(count("calendar_resource_versions", calendar)).isEqualTo(versions); + assertThat(count("collection_changes", calendar)).isEqualTo(changes); + assertThat(store.currentSyncToken(owner, calendar)).isEqualTo(token); + assertThat(store.sync(owner, calendar, token, 100).changes()).isEmpty(); + } + } + + private long revision(UUID calendar) { + return jdbc.queryForObject("SELECT revision FROM collections WHERE id=?", Long.class, calendar); + } + + @Test + void previouslyAcceptedInvalidDataRemainsRetrievableAndRepairableWithoutSilentMigration() + throws Exception { + byte[] invalid = fixture("invalid-event-no-start.ics"); + String uid = "invalid-event-no-start"; + UUID calendar = store.createCollection(owner, "Legacy accepted data", "VEVENT"); + // Represent data persisted by the audited buggy version, without using today's write parser. + jdbc.update( + "INSERT INTO calendar_resources(collection_id,name,uid,content,etag,revision) VALUES (?,?,?,?,?,1)", + calendar, + "legacy.ics", + uid, + invalid, + "\"legacy-audit\""); + jdbc.update("UPDATE collections SET revision=1 WHERE id=?", calendar); + jdbc.update( + "INSERT INTO collection_changes(collection_id,revision,name,deleted) VALUES (?,1,'legacy.ics',false)", + calendar); + assertThat(store.get(owner, calendar, "legacy.ics").content()).isEqualTo(invalid); + assertThatThrownBy(() -> CalendarContent.parse(invalid, "VEVENT")) + .isInstanceOf(CalendarValidationException.class); + byte[] repaired = valid("VEVENT", uid); + store.put(owner, calendar, "legacy.ics", uid, repaired, WriteCondition.none()); + assertThat(store.get(owner, calendar, "legacy.ics").content()).isEqualTo(repaired); + store.delete(owner, calendar, "legacy.ics", WriteCondition.none()); + assertThat(store.get(owner, calendar, "legacy.ics")).isNull(); + } +} diff --git a/src/test/java/org/busystack/caldav/storage/CalendarStoreIT.java b/src/test/java/org/busystack/caldav/storage/CalendarStoreIT.java new file mode 100644 index 0000000..7349b27 --- /dev/null +++ b/src/test/java/org/busystack/caldav/storage/CalendarStoreIT.java @@ -0,0 +1,361 @@ +package org.busystack.caldav.storage; + +import static org.assertj.core.api.Assertions.*; + +import java.nio.charset.StandardCharsets; +import java.time.Duration; +import java.time.Instant; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import org.busystack.caldav.account.AccountService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.support.TransactionTemplate; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; +import org.testcontainers.postgresql.PostgreSQLContainer; + +@SpringBootTest( + properties = { + "busystack.public-base-url=https://calendar.example", + "busystack.issuer=https://calendar.example" + }) +@org.springframework.test.annotation.DirtiesContext( + classMode = org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_CLASS) +@Testcontainers +class CalendarStoreIT { + @Container + static final PostgreSQLContainer POSTGRES = + org.busystack.caldav.testing.LoopbackContainers.postgres(); + + @DynamicPropertySource + static void database(DynamicPropertyRegistry registry) { + registry.add("spring.datasource.url", POSTGRES::getJdbcUrl); + registry.add("spring.datasource.username", POSTGRES::getUsername); + registry.add("spring.datasource.password", POSTGRES::getPassword); + } + + @Autowired CalendarStore store; + @Autowired AccountService accounts; + @Autowired JdbcTemplate jdbc; + @Autowired PlatformTransactionManager transactions; + UUID owner, other, collection; + + @BeforeEach + void prepare() { + org.busystack.caldav.testing.FixtureDatabase.clearAccounts(jdbc); + owner = accounts.provision("owner", "Owner", "correct horse battery staple", true); + other = accounts.provision("other", "Other", "correct horse battery staple", false); + collection = store.createCollection(owner, "Calendar", "VEVENT"); + } + + private CalendarStore.Resource put(String name, String uid, WriteCondition condition) { + return store.put( + owner, + collection, + name, + uid, + ("BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//BusyStack tests//EN\r\nBEGIN:VEVENT\r\nUID:" + + uid + + "\r\nDTSTAMP:20261027T000000Z\r\nDTSTART:20261028T000000Z\r\nX-UNKNOWN;X-PARAM=\"a^'b\":保留\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n") + .getBytes(StandardCharsets.UTF_8), + condition); + } + + @Test + void preservesBytesEtagsAndFailedPreconditions() { + var created = put("event.ics", "one", WriteCondition.create()); + assertThat(store.get(owner, collection, "event.ics").content()).isEqualTo(created.content()); + String token = store.sync(owner, collection, null, 100).token(); + assertThatThrownBy(() -> put("event.ics", "two", WriteCondition.create())) + .isInstanceOf(StoreException.class) + .extracting("kind") + .isEqualTo(StoreException.Kind.PRECONDITION); + assertThatThrownBy(() -> put("event.ics", "two", WriteCondition.match("\"stale\""))) + .isInstanceOf(StoreException.class); + assertThat(store.sync(owner, collection, token, 100).changes()).isEmpty(); + var replacement = + store.put( + owner, + collection, + "event.ics", + "one", + new String(created.content(), StandardCharsets.UTF_8) + .replace("保留", "updated") + .getBytes(StandardCharsets.UTF_8), + WriteCondition.match(created.etag())); + assertThat(replacement.etag()).isNotEqualTo(created.etag()); + assertThatThrownBy( + () -> + store.delete(owner, collection, "event.ics", WriteCondition.match(created.etag()))) + .isInstanceOf(StoreException.class); + store.delete(owner, collection, "event.ics", WriteCondition.match(replacement.etag())); + assertThat(store.sync(owner, collection, token, 100).changes()) + .singleElement() + .satisfies(change -> assertThat(change.deleted()).isTrue()); + } + + @Test + void enforcesUidUniquenessAndRollsBackRevision() { + put("first.ics", "same", WriteCondition.create()); + String token = store.sync(owner, collection, null, 100).token(); + assertThatThrownBy(() -> put("second.ics", "same", WriteCondition.create())) + .isInstanceOf(StoreException.class) + .extracting("kind") + .isEqualTo(StoreException.Kind.UID_CONFLICT); + assertThat(store.sync(owner, collection, token, 100).token()).isEqualTo(token); + assertThat(store.get(owner, collection, "second.ics")).isNull(); + } + + @Test + void sharesRespectPrivilegesAndRevocation() { + put("event.ics", "one", WriteCondition.create()); + assertThatThrownBy(() -> store.get(other, collection, "event.ics")) + .isInstanceOf(StoreException.class); + store.share(owner, collection, other, "READ"); + assertThat(store.collections(other)).hasSize(1); + assertThat(store.get(other, collection, "event.ics")).isNotNull(); + assertThatThrownBy(() -> store.delete(other, collection, "event.ics", WriteCondition.none())) + .isInstanceOf(StoreException.class); + String token = store.sync(other, collection, null, 100).token(); + store.share(owner, collection, other, "WRITE"); + assertThatThrownBy(() -> store.sync(other, collection, token, 100)) + .isInstanceOf(StoreException.class); + store.delete(other, collection, "event.ics", WriteCondition.none()); + store.share(owner, collection, other, null); + assertThat(store.collections(other)).isEmpty(); + assertThatThrownBy(() -> store.sync(other, collection, null, 100)) + .isInstanceOf(StoreException.class); + } + + @Test + void unchangedDirectGrantsPreserveSyncStateWithoutBypassingOwnerChecks() { + String initial = store.currentSyncToken(owner, collection); + store.share(owner, collection, other, null); + assertThat(store.currentSyncToken(owner, collection)).isEqualTo(initial); + store.share(owner, collection, other, "READ"); + String read = store.currentSyncToken(owner, collection); + assertThat(read).isNotEqualTo(initial); + store.share(owner, collection, other, "READ"); + assertThat(store.currentSyncToken(owner, collection)).isEqualTo(read); + assertThatThrownBy(() -> store.share(other, collection, owner, null)) + .isInstanceOf(StoreException.class); + store.share(owner, collection, other, "WRITE"); + String write = store.currentSyncToken(owner, collection); + assertThat(write).isNotEqualTo(read); + store.share(owner, collection, other, "WRITE"); + assertThat(store.currentSyncToken(owner, collection)).isEqualTo(write); + store.share(owner, collection, other, null); + String removed = store.currentSyncToken(owner, collection); + assertThat(removed).isNotEqualTo(write); + store.share(owner, collection, other, null); + assertThat(store.currentSyncToken(owner, collection)).isEqualTo(removed); + } + + @Test + void invalidatesExpiredAndRestoredTokensAndDoesNotTruncateSilently() { + String initial = store.sync(owner, collection, null, 100).token(); + put("one.ics", "one", WriteCondition.create()); + String one = store.sync(owner, collection, null, 100).token(); + put("two.ics", "two", WriteCondition.create()); + var firstPage = store.sync(owner, collection, null, 1); + assertThat(firstPage.changes()).hasSize(1); + assertThat(firstPage.truncated()).isTrue(); + var secondPage = store.sync(owner, collection, firstPage.token(), 1); + assertThat(secondPage.changes()).hasSize(1); + assertThat(secondPage.truncated()).isFalse(); + store.pruneHistory(collection, 1); + assertThatThrownBy(() -> store.sync(owner, collection, initial, 100)) + .isInstanceOf(StoreException.class); + assertThat(store.sync(owner, collection, one, 100).changes()).hasSize(1); + jdbc.update("UPDATE system_state SET recovery_epoch=gen_random_uuid() WHERE id=1"); + assertThatThrownBy(() -> store.sync(owner, collection, one, 100)) + .isInstanceOf(StoreException.class); + UUID another = store.createCollection(owner, "Other", "VTODO"); + assertThatThrownBy(() -> store.sync(owner, another, one, 100)) + .isInstanceOf(StoreException.class); + } + + @Test + void serializesCommitOrderWhileReportsUseCommittedSnapshots() throws Exception { + var firstWritten = new CountDownLatch(1); + var allowCommit = new CountDownLatch(1); + var first = + CompletableFuture.runAsync( + () -> + new TransactionTemplate(transactions) + .executeWithoutResult( + tx -> { + put("one.ics", "one", WriteCondition.create()); + firstWritten.countDown(); + try { + if (!allowCommit.await(10, TimeUnit.SECONDS)) + throw new AssertionError("commit latch timed out"); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + throw new AssertionError(ex); + } + })); + assertThat(firstWritten.await(10, TimeUnit.SECONDS)).isTrue(); + var second = + CompletableFuture.supplyAsync(() -> put("two.ics", "two", WriteCondition.create())); + try { + org.awaitility.Awaitility.await() + .atMost(Duration.ofSeconds(5)) + .untilAsserted( + () -> + assertThat( + jdbc.queryForObject( + "SELECT count(*) FROM pg_stat_activity WHERE datname=current_database() AND wait_event_type='Lock' AND query LIKE '%collections%FOR UPDATE%'", + Long.class)) + .isGreaterThan(0)); + var during = store.sync(owner, collection, null, 100); + assertThat(during.changes()).isEmpty(); + allowCommit.countDown(); + first.get(10, TimeUnit.SECONDS); + assertThat(second.get(10, TimeUnit.SECONDS).revision()).isEqualTo(2); + assertThat(store.sync(owner, collection, during.token(), 100).changes()).hasSize(2); + } finally { + allowCommit.countDown(); + } + } + + @Test + void davCredentialsAreScopedRevocableAndAccountDisablingIsImmediate() { + var credential = + accounts.issueDavCredential(owner, "Test application", Instant.now().plusSeconds(3600)); + assertThat(accounts.verifyDav("owner", credential.secret())).isTrue(); + assertThat(accounts.verifyDav("other", credential.secret())).isFalse(); + assertThat(accounts.verifyDav("owner", "correct horse battery staple")).isFalse(); + accounts.revokeDav(owner, credential.id()); + assertThat(accounts.verifyDav("owner", credential.secret())).isFalse(); + var another = + accounts.issueDavCredential(owner, "Other device", Instant.now().plusSeconds(3600)); + accounts.disable(owner); + assertThat(accounts.verifyDav("owner", another.secret())).isFalse(); + assertThatThrownBy(() -> store.get(owner, collection, "any.ics")) + .isInstanceOf(StoreException.class); + } + + @Test + void simultaneousCreatesProduceOneSuccessAndOnePreconditionFailure() throws Exception { + var start = new CountDownLatch(1); + java.util.function.Supplier create = + () -> { + try { + if (!start.await(5, TimeUnit.SECONDS)) + throw new AssertionError("start latch timed out"); + put("same.ics", "same", WriteCondition.create()); + return "created"; + } catch (StoreException ex) { + return ex.kind().name(); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + throw new AssertionError(ex); + } + }; + var first = CompletableFuture.supplyAsync(create); + var second = CompletableFuture.supplyAsync(create); + start.countDown(); + assertThat(java.util.List.of(first.get(10, TimeUnit.SECONDS), second.get(10, TimeUnit.SECONDS))) + .containsExactlyInAnyOrder("created", "PRECONDITION"); + assertThat(store.collection(owner, collection).revision()).isEqualTo(1); + assertThat(store.sync(owner, collection, null, 100).changes()).hasSize(1); + } + + @Test + void usernameChangesDoNotChangeOwnershipOrResourceIdentity() { + var original = put("preserved.ics", "preserved", WriteCondition.create()); + jdbc.update("UPDATE accounts SET username='renamed' WHERE id=?", owner); + assertThat(accounts.find("renamed").id()).isEqualTo(owner); + assertThat(store.collections(owner)) + .singleElement() + .extracting(CalendarStore.Collection::id) + .isEqualTo(collection); + assertThat(store.get(owner, collection, "preserved.ics").etag()).isEqualTo(original.etag()); + } + + @Test + void overwritingAnExistingResourceCannotReplaceItsUid() { + var original = put("original.ics", "retained-uid", WriteCondition.create()); + String token = store.sync(owner, collection, null, 100).token(); + long revision = store.collection(owner, collection).revision(); + assertThatThrownBy( + () -> put("original.ics", "different-uid", WriteCondition.match(original.etag()))) + .isInstanceOf(StoreException.class) + .extracting("kind") + .isEqualTo(StoreException.Kind.UID_CONFLICT); + assertThat(store.get(owner, collection, "original.ics").content()) + .isEqualTo(original.content()); + assertThat(store.get(owner, collection, "original.ics").etag()).isEqualTo(original.etag()); + assertThat(store.collection(owner, collection).revision()).isEqualTo(revision); + assertThat(store.sync(owner, collection, token, 100).changes()).isEmpty(); + } + + @Test + void longRunningTransactionsAssignMutationDatesAfterThePriorCommittedMutation() throws Exception { + var initial = put("dated.ics", "dated", WriteCondition.create()); + var started = new CountDownLatch(1); + var mutate = new CountDownLatch(1); + var transactionStart = new java.util.concurrent.atomic.AtomicReference(); + try (var workers = java.util.concurrent.Executors.newVirtualThreadPerTaskExecutor()) { + var delayed = + workers.submit( + () -> + new TransactionTemplate(transactions) + .execute( + status -> { + transactionStart.set( + jdbc.queryForObject( + "SELECT transaction_timestamp()", + (rs, row) -> rs.getTimestamp(1).toInstant())); + started.countDown(); + try { + if (!mutate.await(15, TimeUnit.SECONDS)) + throw new AssertionError("Date transaction coordination timed out"); + } catch (InterruptedException interrupted) { + Thread.currentThread().interrupt(); + throw new AssertionError(interrupted); + } + return store.put( + owner, + collection, + "dated.ics", + "dated", + new String(initial.content(), StandardCharsets.UTF_8) + .replace("保留", "last mutation") + .getBytes(StandardCharsets.UTF_8), + WriteCondition.none()); + })); + try { + assertThat(started.await(10, TimeUnit.SECONDS)).isTrue(); + var preceding = + store.put( + owner, + collection, + "dated.ics", + "dated", + new String(initial.content(), StandardCharsets.UTF_8) + .replace("保留", "preceding mutation") + .getBytes(StandardCharsets.UTF_8), + WriteCondition.none()); + mutate.countDown(); + var last = delayed.get(15, TimeUnit.SECONDS); + assertThat(last.modifiedAt()).isAfter(preceding.modifiedAt()); + assertThat(last.modifiedAt()).isAfter(transactionStart.get()); + assertThat(store.get(owner, collection, "dated.ics").modifiedAt()) + .isEqualTo(last.modifiedAt()); + } finally { + mutate.countDown(); + } + } + } +} diff --git a/src/test/java/org/busystack/caldav/storage/DavNamespaceTest.java b/src/test/java/org/busystack/caldav/storage/DavNamespaceTest.java new file mode 100644 index 0000000..afe8634 --- /dev/null +++ b/src/test/java/org/busystack/caldav/storage/DavNamespaceTest.java @@ -0,0 +1,51 @@ +package org.busystack.caldav.storage; + +import static org.assertj.core.api.Assertions.*; + +import java.util.ArrayList; +import java.util.List; +import org.junit.jupiter.api.Test; + +class DavNamespaceTest { + @Test + void calendarDepthReservesBothFixedComponentsAndTheEventualLeaf() { + String usable = "f/".repeat(16) + "calendar"; + assertThatCode(() -> DavNamespace.binding(usable, true)).doesNotThrowAnyException(); + assertThatCode(() -> DavNamespace.resource(usable, "\u0800".repeat(255))) + .doesNotThrowAnyException(); + assertThatThrownBy(() -> DavNamespace.binding("f/".repeat(17) + "calendar", true)) + .isInstanceOf(IllegalArgumentException.class); + } + + @Test + void encodedPathBudgetAndMalformedSegmentsCannotProduceUnaddressableBindings() { + assertThatThrownBy(() -> DavNamespace.binding("中".repeat(100) + "/" + "中".repeat(100), true)) + .isInstanceOf(IllegalArgumentException.class); + for (String name : + List.of( + "\uD800", + "\u2000", + "..", + "x/y", + "x\\y", + "x;y", + "x\u2028y", + "x\u2029y", + "x\uFFFEy", + "x\uFFFFy")) + assertThatThrownBy(() -> DavNamespace.segment(name)) + .isInstanceOf(IllegalArgumentException.class); + assertThatCode(() -> DavNamespace.resource("safe", "valid\uFDD0.ics")) + .doesNotThrowAnyException(); + assertThatCode( + () -> + DavNamespace.resource( + "safe", "valid" + new String(Character.toChars(0x1FFFE)) + ".ics")) + .doesNotThrowAnyException(); + var path = new ArrayList<>(List.of("calendars", "00000000-0000-0000-0000-000000000000")); + for (int i = 0; i < 18; i++) path.add("f"); + assertThatCode(() -> DavNamespace.parts(path)).doesNotThrowAnyException(); + path.add("resource.ics"); + assertThatThrownBy(() -> DavNamespace.parts(path)).isInstanceOf(IllegalArgumentException.class); + } +} diff --git a/src/test/java/org/busystack/caldav/storage/DavStoreIT.java b/src/test/java/org/busystack/caldav/storage/DavStoreIT.java new file mode 100644 index 0000000..05e2f23 --- /dev/null +++ b/src/test/java/org/busystack/caldav/storage/DavStoreIT.java @@ -0,0 +1,765 @@ +package org.busystack.caldav.storage; + +import static org.assertj.core.api.Assertions.*; + +import java.nio.charset.StandardCharsets; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.UUID; +import javax.xml.namespace.QName; +import org.busystack.caldav.account.AccountService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; +import org.testcontainers.postgresql.PostgreSQLContainer; + +@SpringBootTest( + properties = { + "busystack.public-base-url=https://calendar.example", + "busystack.issuer=https://calendar.example" + }) +@Testcontainers +@org.springframework.test.annotation.DirtiesContext( + classMode = org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_CLASS) +class DavStoreIT { + @Container + static final PostgreSQLContainer POSTGRES = + org.busystack.caldav.testing.LoopbackContainers.postgres(); + + @DynamicPropertySource + static void database(DynamicPropertyRegistry registry) { + registry.add("spring.datasource.url", POSTGRES::getJdbcUrl); + registry.add("spring.datasource.username", POSTGRES::getUsername); + registry.add("spring.datasource.password", POSTGRES::getPassword); + } + + @Autowired DavStore store; + @Autowired CalendarStore calendars; + @Autowired AccountService accounts; + @Autowired JdbcTemplate jdbc; + @Autowired org.springframework.transaction.PlatformTransactionManager transactions; + UUID owner, member, administrator, calendar; + static final QName COLOR = new QName("urn:independent-client:settings", "color"); + static final String BLUE = + "blå & blue"; + + @BeforeEach + void prepare() { + org.busystack.caldav.testing.FixtureDatabase.clearAccounts(jdbc); + owner = accounts.provision("owner", "Owner", "correct horse battery staple", false); + member = accounts.provision("member", "Member", "correct horse battery staple", false); + administrator = + accounts.provision("administrator", "Administrator", "correct horse battery staple", true); + calendar = store.create(owner, owner, "work", "Work", "VEVENT", Map.of()).collection().id(); + } + + private byte[] event(String uid) { + return ("BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Independent fixture//EN\r\nBEGIN:VEVENT\r\nUID:" + + uid + + "\r\nDTSTAMP:20261027T000000Z\r\nDTSTART:20261028T000000Z\r\nX-UNKNOWN;X-PARAM=\"a^'b\":保留\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n") + .getBytes(StandardCharsets.UTF_8); + } + + private CalendarStore.Resource put(UUID id, String name, String uid) { + return store.putResource(owner, id, name, event(uid), WriteCondition.create()).resource(); + } + + private void fails(Runnable action, StoreException.Kind kind) { + assertThatThrownBy(action::run) + .isInstanceOf(StoreException.class) + .extracting("kind") + .isEqualTo(kind); + } + + @Test + void persistentBindingsFollowExistingCreationSharingAndStablePrincipalIdentity() { + UUID direct = calendars.createCollection(owner, "Existing API", "VTODO"); + assertThat(store.resolve(owner, owner, direct.toString()).collection().id()).isEqualTo(direct); + calendars.share(owner, calendar, member, "READ"); + assertThat(store.resolve(member, member, calendar.toString()).collection().id()) + .isEqualTo(calendar); + jdbc.update( + "UPDATE accounts SET username='renamed-owner',display_name='新しい名前' WHERE id=?", owner); + var restarted = new DavStore(jdbc, calendars); + assertThat(restarted.resolve(member, member, calendar.toString()).collection().owner()) + .isEqualTo(owner); + assertThat(restarted.children(owner, owner, "")).hasSize(2); + } + + @Test + void aclReplacementProtectsOwnerAndAdminAndRevokesDiscoveryAndSync() { + var original = calendars.sync(owner, calendar, null, 100).token(); + store.replaceAcl(owner, calendar, List.of(new DavStore.Member(member, "READ"))); + assertThat(store.permission(member, calendar)).isEqualTo("READ"); + fails( + () -> + store.putResource( + member, calendar, "forbidden.ics", event("blocked"), WriteCondition.create()), + StoreException.Kind.FORBIDDEN); + fails(() -> store.members(member, calendar), StoreException.Kind.FORBIDDEN); + fails(() -> store.resolve(administrator, owner, "work"), StoreException.Kind.FORBIDDEN); + assertThat(store.principals(member)) + .extracting(DavStore.Principal::id) + .containsExactlyInAnyOrder(owner, member); + fails(() -> calendars.sync(member, calendar, original, 100), StoreException.Kind.INVALID_TOKEN); + store.replaceAcl(owner, calendar, List.of(new DavStore.Member(member, "WRITE"))); + store.putResource(member, calendar, "shared.ics", event("shared"), WriteCondition.create()); + String token = calendars.sync(member, calendar, null, 100).token(); + store.replaceAcl(owner, calendar, List.of()); + fails(() -> store.resolve(member, member, calendar.toString()), StoreException.Kind.NOT_FOUND); + fails(() -> calendars.sync(member, calendar, token, 100), StoreException.Kind.FORBIDDEN); + assertThatThrownBy( + () -> store.replaceAcl(owner, calendar, List.of(new DavStore.Member(owner, "READ")))) + .isInstanceOf(IllegalArgumentException.class); + assertThat(store.permission(owner, calendar)).isEqualTo("OWNER"); + } + + @Test + void namespacesHandleUnicodePercentSignsAndMoveWithoutChangingCollectionIdentity() { + store.create(owner, owner, "📅 équipe %", "Folder", null, Map.of(COLOR, BLUE)); + assertThat( + store.transfer( + owner, owner, "work", owner, "📅 équipe %/旅行", true, false, WriteCondition.none())) + .isTrue(); + assertThat(store.resolve(owner, owner, "📅 équipe %/旅行").collection().id()).isEqualTo(calendar); + assertThat(store.children(owner, owner, "📅 équipe %")) + .extracting(item -> item.binding().path()) + .containsExactly("📅 équipe %/旅行"); + assertThat( + store.transfer( + owner, owner, "📅 équipe %", owner, "🚀 moved", true, false, WriteCondition.none())) + .isTrue(); + assertThat(store.resolve(owner, owner, "🚀 moved/旅行").collection().id()).isEqualTo(calendar); + assertThat(store.snapshot(owner, owner, "🚀 moved").properties()) + .contains(new DavStore.Property(COLOR.getNamespaceURI(), COLOR.getLocalPart(), BLUE)); + fails(() -> store.resolve(owner, owner, "work"), StoreException.Kind.NOT_FOUND); + } + + @Test + void resourceCopyMovePreserveBytesPropertiesAndCommitSafeChanges() { + UUID target = + store.create(owner, owner, "target", "Target", "VEVENT", Map.of()).collection().id(); + var source = put(calendar, "a %.ics", "source"); + store.patch(owner, owner, "work", "a %.ics", Map.of(COLOR, BLUE), Set.of()); + String sourceToken = calendars.sync(owner, calendar, null, 100).token(); + assertThat( + store.transferResource( + owner, + calendar, + "a %.ics", + target, + "copied.ics", + false, + false, + WriteCondition.match(source.etag()))) + .isTrue(); + assertThat(calendars.get(owner, target, "copied.ics").content()).isEqualTo(source.content()); + assertThat(store.deadProperties(owner, target, "copied.ics")).containsEntry(COLOR, BLUE); + fails( + () -> + store.transferResource( + owner, + calendar, + "a %.ics", + calendar, + "duplicate.ics", + false, + false, + WriteCondition.none()), + StoreException.Kind.UID_CONFLICT); + assertThat( + store.transferResource( + owner, + calendar, + "a %.ics", + calendar, + "renamed.ics", + true, + false, + WriteCondition.match(source.etag()))) + .isTrue(); + assertThat(calendars.get(owner, calendar, "a %.ics")).isNull(); + assertThat(store.deadProperties(owner, calendar, "renamed.ics")).containsEntry(COLOR, BLUE); + assertThat(calendars.sync(owner, calendar, sourceToken, 100).changes()) + .hasSize(2) + .anySatisfy( + change -> { + assertThat(change.name()).isEqualTo("a %.ics"); + assertThat(change.deleted()).isTrue(); + }); + assertThat(calendars.get(owner, calendar, "renamed.ics").etag()).isEqualTo(source.etag()); + } + + @Test + void staleSourceDestinationCollisionAndComponentMismatchNeverPartiallyMutate() { + UUID target = + store.create(owner, owner, "target", "Target", "VEVENT", Map.of()).collection().id(); + UUID tasks = store.create(owner, owner, "tasks", "Tasks", "VTODO", Map.of()).collection().id(); + var source = put(calendar, "source.ics", "source"); + var destination = put(target, "occupied.ics", "destination"); + fails( + () -> + store.transferResource( + owner, + calendar, + "source.ics", + target, + "occupied.ics", + true, + false, + WriteCondition.none()), + StoreException.Kind.PRECONDITION); + fails( + () -> + store.transferResource( + owner, + calendar, + "source.ics", + target, + "occupied.ics", + true, + true, + WriteCondition.match("\"stale\"")), + StoreException.Kind.PRECONDITION); + assertThatThrownBy( + () -> + store.transferResource( + owner, + calendar, + "source.ics", + tasks, + "task.ics", + true, + false, + WriteCondition.none())) + .isInstanceOf(IllegalArgumentException.class); + assertThat(calendars.get(owner, calendar, "source.ics").etag()).isEqualTo(source.etag()); + assertThat(calendars.get(owner, target, "occupied.ics").etag()).isEqualTo(destination.etag()); + } + + @Test + void depthZeroAndDeepCopiesUseNewCollectionsAndNeverCopySharing() { + put(calendar, "one.ics", "one"); + store.patch(owner, owner, "work", null, Map.of(COLOR, BLUE), Set.of()); + calendars.share(owner, calendar, member, "WRITE"); + store.transfer( + owner, owner, "work", owner, "empty", false, false, WriteCondition.none(), false); + var empty = store.snapshot(owner, owner, "empty"); + assertThat(empty.entry().collection().id()).isNotEqualTo(calendar); + assertThat(empty.resources()).isEmpty(); + assertThat(empty.properties()).hasSize(1); + store.transfer(owner, owner, "work", owner, "deep", false, false, WriteCondition.none()); + var deep = store.snapshot(owner, owner, "deep"); + assertThat(deep.resources()).hasSize(1); + assertThat(store.members(owner, deep.entry().collection().id())).isEmpty(); + fails(() -> store.resolve(member, owner, "deep"), StoreException.Kind.FORBIDDEN); + } + + @Test + void recursiveFolderDeletionRemovesAuthoritativeCollectionsSharesAndOldTokens() { + store.create(owner, owner, "folder", "Folder", null, Map.of()); + store.transfer(owner, owner, "work", owner, "folder/work", true, false, WriteCondition.none()); + calendars.share(owner, calendar, member, "READ"); + String token = calendars.sync(member, calendar, null, 100).token(); + store.deleteBinding(owner, owner, "folder", new WriteCondition(false, true, Set.of())); + fails(() -> store.resolve(member, member, calendar.toString()), StoreException.Kind.NOT_FOUND); + fails(() -> calendars.sync(member, calendar, token, 100), StoreException.Kind.NOT_FOUND); + UUID recreated = + store + .create(owner, owner, "folder", "New collection", "VEVENT", Map.of()) + .collection() + .id(); + assertThat(recreated).isNotEqualTo(calendar); + fails(() -> calendars.sync(owner, recreated, token, 100), StoreException.Kind.INVALID_TOKEN); + } + + @Test + void propertyUpdatesAreAtomicAndRetainAcceptedXmlWithoutRewriting() { + QName display = new QName("DAV:", "displayname"); + store.patch( + owner, + owner, + "work", + null, + Map.of(COLOR, BLUE, display, "新しい名前"), + Set.of()); + assertThat(store.resolve(owner, owner, "work").binding().displayName()).isEqualTo("新しい名前"); + assertThat(store.deadProperties(owner, calendar, null)) + .containsEntry(COLOR, BLUE) + .containsKey(display) + .hasSize(2); + fails( + () -> + store.patch( + owner, + owner, + "work", + null, + Map.of( + COLOR, + "changed", + new QName("DAV:", "getetag"), + "evil"), + Set.of()), + StoreException.Kind.INVALID_PROPERTY); + assertThat(store.deadProperties(owner, calendar, null)).containsEntry(COLOR, BLUE); + fails( + () -> + store.patch( + owner, + owner, + "work", + null, + Map.of( + COLOR, + "]>&e;"), + Set.of()), + StoreException.Kind.INVALID_PROPERTY); + assertThat(store.deadProperties(owner, calendar, null)).containsEntry(COLOR, BLUE); + } + + @Test + void databaseFailureRollsBackPropertyMetadataAndDisplayNameTogether() { + QName display = new QName("DAV:", "displayname"); + QName blocked = new QName("urn:test", "blocked"); + jdbc.execute( + "CREATE FUNCTION dav_reject_property() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN RAISE EXCEPTION 'simulated property persistence failure'; END $$"); + jdbc.execute( + "CREATE TRIGGER dav_reject_property BEFORE INSERT ON dav_collection_properties FOR EACH ROW EXECUTE FUNCTION dav_reject_property()"); + try { + assertThatThrownBy( + () -> + store.patch( + owner, + owner, + "work", + null, + Map.of( + display, + "must roll back", + blocked, + "value"), + Set.of())) + .isInstanceOf(org.springframework.dao.DataAccessException.class); + assertThat(store.resolve(owner, owner, "work").binding().displayName()).isEqualTo("Work"); + assertThat(store.deadProperties(owner, calendar, null)).isEmpty(); + } finally { + jdbc.execute("DROP TRIGGER dav_reject_property ON dav_collection_properties"); + jdbc.execute("DROP FUNCTION dav_reject_property()"); + } + } + + @Test + void metadataSnapshotDoesNotFetchOrBudgetUnrequestedCalendarBodies() { + jdbc.update( + "INSERT INTO calendar_resources(collection_id,name,uid,content,etag,revision) SELECT ?,value::text||'.ics',value::text,decode(repeat('41',1048576),'hex'),'\"budget-fixture\"',value FROM generate_series(1,33) value", + calendar); + assertThat(store.snapshot(owner, owner, "work").resources()) + .hasSize(33) + .allSatisfy(metadata -> assertThat(metadata.contentLength()).isEqualTo(1048576)); + assertThat( + jdbc.queryForObject( + "SELECT count(*) FROM calendar_resources WHERE collection_id=?", + Long.class, + calendar)) + .isEqualTo(33); + } + + @Test + void crossResourceConditionLockPreventsAWriteBetweenValidationAndMutation() throws Exception { + var original = put(calendar, "atomic.ics", "atomic"); + byte[] replacement = + new String(original.content(), StandardCharsets.UTF_8) + .replace("保留", "winner") + .getBytes(StandardCharsets.UTF_8); + var validated = new java.util.concurrent.CountDownLatch(1); + var finish = new java.util.concurrent.CountDownLatch(1); + var holderPid = new java.util.concurrent.atomic.AtomicInteger(); + try (var workers = java.util.concurrent.Executors.newVirtualThreadPerTaskExecutor()) { + var conditional = + java.util.concurrent.CompletableFuture.supplyAsync( + () -> + new org.springframework.transaction.support.TransactionTemplate(transactions) + .execute( + status -> { + DavMutationLock.exclusive(jdbc); + holderPid.set( + jdbc.queryForObject("SELECT pg_backend_pid()", Integer.class)); + assertThat(calendars.get(owner, calendar, "atomic.ics").etag()) + .isEqualTo(original.etag()); + validated.countDown(); + try { + if (!finish.await(10, java.util.concurrent.TimeUnit.SECONDS)) + throw new AssertionError("Condition coordination timed out"); + } catch (InterruptedException failure) { + Thread.currentThread().interrupt(); + throw new AssertionError(failure); + } + return store.putResource( + owner, + calendar, + "atomic.ics", + replacement, + WriteCondition.match(original.etag())); + }), + workers); + try { + assertThat(validated.await(10, java.util.concurrent.TimeUnit.SECONDS)).isTrue(); + var ordinary = + java.util.concurrent.CompletableFuture.supplyAsync( + () -> + store.putResource( + owner, + calendar, + "atomic.ics", + event("atomic"), + WriteCondition.match(original.etag())), + workers); + org.awaitility.Awaitility.await() + .atMost(java.time.Duration.ofSeconds(10)) + .untilAsserted( + () -> + assertThat( + jdbc.queryForObject( + "SELECT EXISTS(SELECT 1 FROM pg_stat_activity WHERE ?=ANY(pg_blocking_pids(pid)))", + Boolean.class, + holderPid.get())) + .isTrue()); + finish.countDown(); + assertThat(conditional.get(15, java.util.concurrent.TimeUnit.SECONDS).resource().content()) + .isEqualTo(replacement); + assertThatThrownBy(() -> ordinary.get(15, java.util.concurrent.TimeUnit.SECONDS)) + .isInstanceOf(java.util.concurrent.ExecutionException.class) + .cause() + .isInstanceOf(StoreException.class) + .extracting("kind") + .isEqualTo(StoreException.Kind.PRECONDITION); + } finally { + finish.countDown(); + } + } + assertThat(calendars.get(owner, calendar, "atomic.ics").content()).isEqualTo(replacement); + } + + @Test + void writeSharesModifyOneAuthoritativeResourceButNeverOwnershipOrAcl() { + put(calendar, "shared.ics", "shared"); + store.replaceAcl(owner, calendar, List.of(new DavStore.Member(member, "WRITE"))); + store.patch(member, member, calendar.toString(), "shared.ics", Map.of(COLOR, BLUE), Set.of()); + assertThat(store.deadProperties(owner, calendar, "shared.ics")).containsEntry(COLOR, BLUE); + fails(() -> store.replaceAcl(member, calendar, List.of()), StoreException.Kind.FORBIDDEN); + fails( + () -> store.deleteBinding(member, member, calendar.toString(), WriteCondition.none()), + StoreException.Kind.FORBIDDEN); + assertThat(store.resolve(owner, owner, "work").collection().owner()).isEqualTo(owner); + assertThat(calendars.get(owner, calendar, "shared.ics")).isNotNull(); + } + + @Test + void resourceTransfersAssignDatesForChangedDestinationBytesAndKeepUnchangedDates() { + UUID target = + store.create(owner, owner, "dated-target", "Target", "VEVENT", Map.of()).collection().id(); + UUID movedTarget = + store.create(owner, owner, "dated-move", "Move", "VEVENT", Map.of()).collection().id(); + var source = put(calendar, "source.ics", "dated-source"); + java.time.Instant old = java.time.Instant.parse("2000-01-01T00:00:00.123456Z"); + jdbc.update( + "UPDATE calendar_resources SET updated_at=? WHERE collection_id=?", + java.sql.Timestamp.from(old), + calendar); + java.time.Instant before = + jdbc.queryForObject( + "SELECT clock_timestamp()", (rs, row) -> rs.getTimestamp(1).toInstant()); + assertThat( + store.transferResource( + owner, + calendar, + "source.ics", + target, + "copy.ics", + false, + false, + WriteCondition.none())) + .isTrue(); + var copy = calendars.get(owner, target, "copy.ics"); + assertThat(copy.content()).isEqualTo(source.content()); + assertThat(copy.modifiedAt()).isAfter(before); + java.time.Instant preserved = old.plusSeconds(3600); + jdbc.update( + "UPDATE calendar_resources SET updated_at=? WHERE collection_id=?", + java.sql.Timestamp.from(preserved), + target); + assertThat( + store.transferResource( + owner, + calendar, + "source.ics", + target, + "copy.ics", + false, + true, + WriteCondition.none())) + .isFalse(); + assertThat(calendars.get(owner, target, "copy.ics").modifiedAt()).isEqualTo(preserved); + store.patch(owner, owner, "dated-target", "copy.ics", Map.of(COLOR, BLUE), Set.of()); + assertThat(calendars.get(owner, target, "copy.ics").modifiedAt()).isEqualTo(preserved); + store.putResource( + owner, + movedTarget, + "moved.ics", + new String(source.content(), StandardCharsets.UTF_8) + .replace("保留", "different bytes") + .getBytes(StandardCharsets.UTF_8), + WriteCondition.create()); + jdbc.update( + "UPDATE calendar_resources SET updated_at=? WHERE collection_id=?", + java.sql.Timestamp.from(preserved), + movedTarget); + java.time.Instant beforeMove = + jdbc.queryForObject( + "SELECT clock_timestamp()", (rs, row) -> rs.getTimestamp(1).toInstant()); + assertThat( + store.transferResource( + owner, + calendar, + "source.ics", + movedTarget, + "moved.ics", + true, + true, + WriteCondition.none())) + .isFalse(); + assertThat(calendars.get(owner, movedTarget, "moved.ics").modifiedAt()).isAfter(beforeMove); + assertThat(calendars.get(owner, movedTarget, "moved.ics").content()) + .isEqualTo(source.content()); + } + + @Test + void namespaceMoveDoesNotInheritAnOlderSourceDateAtAnOverwrittenDestination() { + UUID target = + store + .create(owner, owner, "dated-overwrite", "Target", "VEVENT", Map.of()) + .collection() + .id(); + put(calendar, "event.ics", "namespace-source"); + put(target, "event.ics", "namespace-target"); + jdbc.update( + "UPDATE calendar_resources SET updated_at=? WHERE collection_id=?", + java.sql.Timestamp.from(java.time.Instant.parse("1990-01-01T00:00:00Z")), + calendar); + jdbc.update( + "UPDATE calendar_resources SET updated_at=? WHERE collection_id=?", + java.sql.Timestamp.from(java.time.Instant.parse("2000-01-01T00:00:00Z")), + target); + java.time.Instant before = + jdbc.queryForObject( + "SELECT clock_timestamp()", (rs, row) -> rs.getTimestamp(1).toInstant()); + assertThat( + store.transfer( + owner, owner, "work", owner, "dated-overwrite", true, true, WriteCondition.none())) + .isFalse(); + assertThat(store.resolve(owner, owner, "dated-overwrite").collection().id()) + .isEqualTo(calendar); + assertThat(calendars.get(owner, calendar, "event.ics").modifiedAt()).isAfter(before); + assertThat(calendars.get(owner, calendar, "event.ics").uid()).isEqualTo("namespace-source"); + } + + @Test + void namespaceAdmissionReservesFixedComponentsAndTheCalendarResourceLeaf() { + String parent = ""; + for (int i = 1; i <= 17; i++) { + parent = parent.isEmpty() ? "é" : parent + "/é"; + store.create(owner, owner, parent, "Folder", null, Map.of()); + } + String tooDeep = parent + "/extra"; + assertThatThrownBy(() -> store.create(owner, owner, tooDeep, "Unusable", "VEVENT", Map.of())) + .isInstanceOfAny(IllegalArgumentException.class, StoreException.class); + } + + @Test + void transferPreflightRejectsInaccessibleDescendantsBeforeReplacingDestination() { + store.create(owner, owner, "tree", "Source", null, Map.of(COLOR, BLUE)); + UUID child = + store.create(owner, owner, "tree/calendar", "Child", "VEVENT", Map.of()).collection().id(); + put(child, "📅 %.ics", "nested-leaf"); + String parent = ""; + for (int i = 1; i <= 16; i++) { + parent = parent.isEmpty() ? "p" : parent + "/p"; + store.create(owner, owner, parent, "Folder", null, Map.of()); + } + String target = parent + "/target"; + store.create(owner, owner, target, "Existing", null, Map.of(COLOR, BLUE)); + String before = calendars.currentSyncToken(owner, child); + assertThatThrownBy( + () -> + store.transfer( + owner, owner, "tree", owner, target, true, true, WriteCondition.none())) + .isInstanceOfAny(IllegalArgumentException.class, StoreException.class); + assertThat(store.resolve(owner, owner, "tree/calendar").collection().id()).isEqualTo(child); + assertThat(store.resolve(owner, owner, target).binding().displayName()).isEqualTo("Existing"); + assertThat(calendars.currentSyncToken(owner, child)).isEqualTo(before); + assertThat(calendars.get(owner, child, "📅 %.ics")).isNotNull(); + } + + @Test + void databaseNamespaceGuardsMatchJavaDepthEncodingAndUtf16LeafBounds() { + record Case(String path, boolean calendar, String leaf) {} + var cases = + List.of( + new Case("f/".repeat(16) + "calendar", true, null), + new Case("a".repeat(255), true, null), + new Case("a".repeat(256), true, null), + new Case("AZaz09._~-", false, "AZaz09._~-"), + new Case("a".repeat(255), false, "b".repeat(255)), + new Case("safe", false, "b".repeat(256)), + new Case(".", true, null), + new Case("..", false, "leaf"), + new Case("safe", false, "."), + new Case("safe", false, ".."), + new Case("safe%2F", true, null), + new Case("safe;param", true, null), + new Case("safe", false, "leaf;param"), + new Case("safe\u2028part", true, null), + new Case("safe\u2029part", true, null), + new Case("safe", false, "leaf\u2028part"), + new Case("safe", false, "leaf\u2029part"), + new Case("safe\n", true, null), + new Case("safe", false, "file\n"), + new Case("safe\r", true, null), + new Case("safe\t", true, null), + new Case("Ạ".repeat(255), true, null), + new Case("f/".repeat(17) + "calendar", true, null), + new Case("中".repeat(100) + "/" + "中".repeat(100), true, null), + new Case("中".repeat(100) + "/" + "中".repeat(100), false, null), + new Case("\u2000", false, null), + new Case("safe", false, "🗓".repeat(127)), + new Case("safe", false, "🗓".repeat(128)), + new Case("safe", false, "\u0800".repeat(255)), + new Case("safe", false, "bad/name"), + new Case("\uFFFE", true, null), + new Case("\uFFFF", false, null), + new Case("safe", false, "bad\uFFFE.ics"), + new Case("safe", false, "bad\uFFFF.ics"), + new Case("\uFDD0", true, null), + new Case("safe", false, "valid\uFDD0.ics"), + new Case(new String(Character.toChars(0x1FFFE)), true, null), + new Case("safe", false, "valid" + new String(Character.toChars(0x1FFFE)) + ".ics"), + new Case("safe", false, "\u2000")); + for (var sample : cases) { + boolean accepted = true; + try { + if (sample.leaf() == null) DavNamespace.binding(sample.path(), sample.calendar()); + else DavNamespace.resource(sample.path(), sample.leaf()); + } catch (IllegalArgumentException invalid) { + accepted = false; + } + assertThat( + jdbc.queryForObject( + "SELECT dav_namespace_valid(?::text,?::boolean,?::text)", + Boolean.class, + sample.path(), + sample.calendar(), + sample.leaf())) + .as(sample.toString()) + .isEqualTo(accepted); + } + assertThatThrownBy( + () -> + jdbc.update( + "INSERT INTO calendar_resources(collection_id,name,uid,content,etag,revision) VALUES (?,?,?,?,?,1)", + calendar, + "🗓".repeat(128), + "invalid-utf16", + event("invalid-utf16"), + "\"invalid\"")) + .isInstanceOf(org.springframework.dao.DataIntegrityViolationException.class); + assertThat(calendars.get(owner, calendar, "🗓".repeat(128))).isNull(); + for (String invalid : List.of("bad\uFFFE.ics", "bad\uFFFF.ics")) { + String uid = "invalid-xml-" + invalid.codePointAt(3); + assertThatThrownBy( + () -> + jdbc.update( + "INSERT INTO calendar_resources(collection_id,name,uid,content,etag,revision) VALUES (?,?,?,?,?,1)", + calendar, + invalid, + uid, + event(uid), + "\"invalid-xml\"")) + .isInstanceOf(org.springframework.dao.DataIntegrityViolationException.class); + assertThat(calendars.get(owner, calendar, invalid)).isNull(); + } + } + + @Test + void xmlInvalidAccountDisplayNamesAreRejectedBeforePersistence() { + var checks = new java.util.ArrayList(); + for (int invalid : new int[] {0xFFFE, 0xFFFF}) { + String label = "Invalid" + (char) invalid; + checks.add( + () -> + assertThatThrownBy( + () -> + accounts.provision( + "invalid-label-" + invalid, + label, + "correct horse battery staple", + false)) + .isInstanceOf(IllegalArgumentException.class)); + } + org.junit.jupiter.api.Assertions.assertAll(checks); + assertThat( + jdbc.queryForObject( + "SELECT count(*) FROM accounts WHERE username LIKE 'invalid-label-%'", + Integer.class)) + .isZero(); + for (String valid : List.of("Valid\uFDD0", "Valid" + new String(Character.toChars(0x1FFFE)))) { + UUID id = + accounts.provision( + "valid-label-" + valid.codePointAt(5), valid, "correct horse battery staple", false); + assertThat(accounts.find(id).displayName()).isEqualTo(valid); + } + } + + @Test + void xmlInvalidCollectionDisplayNamesAreRejectedAcrossCreationAndRename() { + var checks = new java.util.ArrayList(); + for (int invalid : new int[] {0xFFFE, 0xFFFF}) { + String label = "Invalid" + (char) invalid; + checks.add( + () -> + assertThatThrownBy( + () -> + store.create( + owner, owner, "invalid-label-" + invalid, label, "VEVENT", Map.of())) + .isInstanceOf(IllegalArgumentException.class)); + checks.add( + () -> + assertThatThrownBy(() -> calendars.createCollection(owner, label, "VEVENT")) + .isInstanceOf(IllegalArgumentException.class)); + checks.add( + () -> + assertThatThrownBy(() -> store.renameDisplayName(owner, owner, "work", label)) + .isInstanceOf(IllegalArgumentException.class)); + } + org.junit.jupiter.api.Assertions.assertAll(checks); + assertThat(store.resolve(owner, owner, "work").binding().displayName()).isEqualTo("Work"); + for (String valid : + List.of( + "Valid\uFDD0", + "Valid" + new String(Character.toChars(0x1FFFE)), + "Line\nName\tAllowed", + "DEL\u007fC1\u0085")) { + UUID id = calendars.createCollection(owner, valid, "VEVENT"); + assertThat(calendars.collection(owner, id).name()).isEqualTo(valid); + store.renameDisplayName(owner, owner, "work", valid); + assertThat(store.resolve(owner, owner, "work").binding().displayName()).isEqualTo(valid); + } + } +} diff --git a/src/test/java/org/busystack/caldav/storage/SyncPagingIT.java b/src/test/java/org/busystack/caldav/storage/SyncPagingIT.java new file mode 100644 index 0000000..62a46fb --- /dev/null +++ b/src/test/java/org/busystack/caldav/storage/SyncPagingIT.java @@ -0,0 +1,690 @@ +package org.busystack.caldav.storage; + +import static org.assertj.core.api.Assertions.*; + +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.UUID; +import javax.xml.namespace.QName; +import org.busystack.caldav.account.AccountService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.support.TransactionTemplate; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; +import org.testcontainers.postgresql.PostgreSQLContainer; + +@SpringBootTest( + properties = { + "busystack.public-base-url=https://calendar.example", + "busystack.issuer=https://calendar.example" + }) +@org.springframework.test.annotation.DirtiesContext( + classMode = org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_CLASS) +@Testcontainers +class SyncPagingIT { + @Container + static final PostgreSQLContainer POSTGRES = + org.busystack.caldav.testing.LoopbackContainers.postgres(); + + @DynamicPropertySource + static void database(DynamicPropertyRegistry registry) { + registry.add("spring.datasource.url", POSTGRES::getJdbcUrl); + registry.add("spring.datasource.username", POSTGRES::getUsername); + registry.add("spring.datasource.password", POSTGRES::getPassword); + } + + @Autowired CalendarStore store; + @Autowired AccountService accounts; + @Autowired JdbcTemplate jdbc; + @Autowired PlatformTransactionManager transactions; + @Autowired DavStore dav; + @Autowired SyncContinuationCleanup cleanup; + @Autowired SyncContinuationStore continuations; + UUID owner, other, collection; + + @BeforeEach + void prepare() { + org.busystack.caldav.testing.FixtureDatabase.clearAccounts(jdbc); + owner = accounts.provision("owner", "Owner", "correct horse battery staple", true); + other = accounts.provision("other", "Other", "correct horse battery staple", false); + collection = store.createCollection(owner, "Calendar", "VEVENT"); + } + + private byte[] content(String uid, String summary) { + return ("BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Independent sync fixture//EN\r\nBEGIN:VEVENT\r\nUID:" + + uid + + "\r\nDTSTAMP:20261027T000000Z\r\nDTSTART:20261028T000000Z\r\nSUMMARY:" + + summary + + "\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n") + .getBytes(StandardCharsets.UTF_8); + } + + private void put(String name, String uid, String summary) { + store.put(owner, collection, name, uid, content(uid, summary), WriteCondition.none()); + } + + @Test + void initialSmallPageReturnsContinuationRatherThanRejectingAcceptedCollection() { + put("a.ics", "a", "one"); + put("b.ics", "b", "two"); + var first = store.sync(owner, collection, null, 1); + assertThat(first.changes()).hasSize(1); + var second = store.sync(owner, collection, first.token(), 1); + assertThat(second.changes()).hasSize(1); + assertThat(Set.of(first.changes().getFirst().name(), second.changes().getFirst().name())) + .containsExactlyInAnyOrder("a.ics", "b.ics"); + } + + @Test + void initialSyncExceedsFormerRowAndBodyThresholdsWithoutMissingSubsequentChanges() { + // SQL constructs independently valid resources in the production PostgreSQL schema without + // spending this capacity regression's runtime on 10,037 identical parser invocations. + int count = 10037; + String text = new String(content("fixture-UID", "x".repeat(3400)), StandardCharsets.UTF_8); + jdbc.update( + "INSERT INTO calendar_resources(collection_id,name,uid,content,etag,revision) SELECT ?, 'item-'||lpad(i::text,6,'0')||'.ics','fixture-'||i,convert_to(replace(?,'fixture-UID','fixture-'||i),'UTF8'),'\"etag-'||i||'\"',i FROM generate_series(1,?) i", + collection, + text, + count); + jdbc.update( + "INSERT INTO collection_changes(collection_id,revision,name,deleted) SELECT collection_id,revision,name,false FROM calendar_resources WHERE collection_id=?", + collection); + jdbc.update("UPDATE collections SET revision=? WHERE id=?", count, collection); + assertThat( + jdbc.queryForObject( + "SELECT sum(octet_length(content)) FROM calendar_resources WHERE collection_id=?", + Long.class, + collection)) + .isGreaterThan(32L * 1024 * 1024); + Set metadataNames = new HashSet<>(); + String metadataToken = null; + do { + var metadataPage = + store.sync(owner, collection, metadataToken, 137, false, change -> 256, 137L * 256); + for (var item : metadataPage.changes()) { + assertThat(item.resource()).isNull(); + assertThat(metadataNames.add(item.name())).isTrue(); + } + metadataToken = metadataPage.token(); + if (!metadataPage.truncated()) break; + } while (true); + assertThat(metadataNames).hasSize(count); + Set names = new HashSet<>(); + String token = null; + int pages = 0; + do { + var page = store.sync(owner, collection, token, 137); + for (var item : page.changes()) { + assertThat(names.add(item.name())).isTrue(); + assertThat(new String(item.resource().content(), StandardCharsets.UTF_8)) + .contains("UID:" + item.metadata().uid() + "\r\n"); + } + token = page.token(); + pages++; + if (!page.truncated()) break; + assertThat(pages).isLessThan(100); + } while (true); + assertThat(names).hasSize(count); + put("after.ics", "after", "later"); + assertThat(store.sync(owner, collection, token, 137).changes()) + .singleElement() + .extracting(CalendarStore.Change::name) + .isEqualTo("after.ics"); + + String beforeBulkChanges = store.currentSyncToken(owner, collection); + long bulkHead = + new TransactionTemplate(transactions) + .execute( + status -> { + long base = + jdbc.queryForObject( + "SELECT revision FROM collections WHERE id=? FOR UPDATE", + Long.class, + collection); + // Equivalent to individually accepted no-op PUTs, with the same collection lock + // and + // one atomic commit. Triggers retain every new immutable resource representation. + int updated = + jdbc.update( + "WITH numbered AS (SELECT name,?+row_number() OVER(ORDER BY name COLLATE \"C\") AS next_revision FROM calendar_resources WHERE collection_id=? AND name LIKE 'item-%') UPDATE calendar_resources r SET revision=n.next_revision FROM numbered n WHERE r.collection_id=? AND r.name=n.name", + base, collection, collection); + assertThat(updated).isEqualTo(count); + jdbc.update( + "INSERT INTO collection_changes(collection_id,revision,name,deleted) SELECT collection_id,revision,name,false FROM calendar_resources WHERE collection_id=? AND revision>?", + collection, + base); + jdbc.update( + "UPDATE collections SET revision=? WHERE id=?", base + count, collection); + return base + count; + }); + Set incrementalMetadata = new HashSet<>(); + metadataToken = beforeBulkChanges; + do { + var page = + store.sync(owner, collection, metadataToken, 137, false, change -> 256, 137L * 256); + for (var item : page.changes()) { + assertThat(item.resource()).isNull(); + assertThat(incrementalMetadata.add(item.name())).isTrue(); + } + metadataToken = page.token(); + if (!page.truncated()) break; + } while (true); + assertThat(incrementalMetadata).containsExactlyInAnyOrderElementsOf(names); + Set incrementalBodies = new HashSet<>(); + token = beforeBulkChanges; + do { + var page = store.sync(owner, collection, token, 137); + for (var item : page.changes()) { + assertThat(incrementalBodies.add(item.name())).isTrue(); + assertThat(new String(item.resource().content(), StandardCharsets.UTF_8)) + .contains("UID:" + item.metadata().uid() + "\r\n"); + } + token = page.token(); + if (!page.truncated()) break; + } while (true); + assertThat(incrementalBodies).containsExactlyInAnyOrderElementsOf(names); + assertThat(token).isEqualTo(metadataToken).endsWith(":" + bulkHead); + assertThat(store.sync(owner, collection, token, 137).changes()).isEmpty(); + } + + private Map drain(CalendarStore.SyncPage first, int limit) { + Map result = new HashMap<>(); + var page = first; + int pages = 0; + while (true) { + for (var change : page.changes()) assertThat(result.put(change.name(), change)).isNull(); + if (!page.truncated()) break; + assertThat(page.changes()).isNotEmpty(); + assertThat(++pages).isLessThan(100); + page = store.sync(owner, collection, page.token(), limit); + } + return result; + } + + @Test + void initialWatermarkSurvivesUpdatesDeletesRecreationUnicodeAndRetriedRequests() { + put("a.ics", "a", "original-a"); + put("b.ics", "b", "original-b"); + put("c.ics", "c", "original-c"); + put("é % 保留.ics", "unicode", "original-unicode"); + var first = store.sync(owner, collection, null, 1); + assertThat(first.truncated()).isTrue(); + put("b.ics", "b", "changed-b-one"); + put("b.ics", "b", "changed-b-two"); + store.delete(owner, collection, "c.ics", WriteCondition.none()); + put("c.ics", "recreated-c", "new-c"); + store.delete(owner, collection, "é % 保留.ics", WriteCondition.none()); + put("0-before-cursor.ics", "before", "new-before"); + put("z-after-cursor.ics", "after", "new-after"); + var second = store.sync(owner, collection, first.token(), 1); + var retry = store.sync(owner, collection, first.token(), 1); + assertThat(retry.changes().getFirst().name()).isEqualTo(second.changes().getFirst().name()); + assertThat(retry.changes().getFirst().resource().content()) + .isEqualTo(second.changes().getFirst().resource().content()); + assertThat(new String(second.changes().getFirst().resource().content(), StandardCharsets.UTF_8)) + .contains("original-b"); + Map snapshot = new HashMap<>(); + snapshot.put(first.changes().getFirst().name(), first.changes().getFirst()); + var page = second; + String finalToken; + while (true) { + for (var item : page.changes()) assertThat(snapshot.put(item.name(), item)).isNull(); + if (!page.truncated()) { + finalToken = page.token(); + break; + } + page = store.sync(owner, collection, page.token(), 1); + } + assertThat(snapshot).containsOnlyKeys("a.ics", "b.ics", "c.ics", "é % 保留.ics"); + assertThat(new String(snapshot.get("c.ics").resource().content(), StandardCharsets.UTF_8)) + .contains("original-c"); + Map delta = + drain(store.sync(owner, collection, finalToken, 1), 1); + assertThat(delta) + .containsOnlyKeys( + "b.ics", "c.ics", "é % 保留.ics", "0-before-cursor.ics", "z-after-cursor.ics"); + assertThat(delta.get("é % 保留.ics").deleted()).isTrue(); + assertThat(new String(delta.get("b.ics").resource().content(), StandardCharsets.UTF_8)) + .contains("changed-b-two"); + assertThat(delta.get("c.ics").metadata().uid()).isEqualTo("recreated-c"); + } + + @Test + void incrementalWatermarkReturnsOneFinalStatePerNameThenLaterMutations() { + String empty = store.currentSyncToken(owner, collection); + put("a.ics", "a", "one"); + put("b.ics", "b", "one"); + put("c.ics", "c", "one"); + put("gone.ics", "gone", "one"); + store.delete(owner, collection, "gone.ics", WriteCondition.none()); + var first = store.sync(owner, collection, empty, 1); + put("b.ics", "b", "after-watermark"); + store.delete(owner, collection, "c.ics", WriteCondition.none()); + var page = first; + Map initial = new HashMap<>(); + String finalToken; + while (true) { + for (var item : page.changes()) assertThat(initial.put(item.name(), item)).isNull(); + if (!page.truncated()) { + finalToken = page.token(); + break; + } + page = store.sync(owner, collection, page.token(), 1); + } + assertThat(initial).containsOnlyKeys("a.ics", "b.ics", "c.ics", "gone.ics"); + assertThat(initial.get("gone.ics").deleted()).isTrue(); + assertThat(initial.get("c.ics").deleted()).isFalse(); + assertThat(new String(initial.get("b.ics").resource().content(), StandardCharsets.UTF_8)) + .contains("SUMMARY:one"); + var delta = drain(store.sync(owner, collection, finalToken, 1), 1); + assertThat(delta).containsOnlyKeys("b.ics", "c.ics"); + assertThat(delta.get("c.ics").deleted()).isTrue(); + } + + @Test + void metadataAndActualSelectedByteBudgetsAreIndependentOfStoredBodySize() { + put("amp&unicode保留.ics", "a", "&".repeat(10000)); + put("b.ics", "b", "x".repeat(10000)); + var first = + store.sync( + owner, + collection, + null, + 100, + false, + change -> { + assertThat(change.resource()).isNull(); + return change.name().getBytes(StandardCharsets.UTF_8).length + 80; + }, + 110); + assertThat(first.changes()).hasSize(1); + assertThat(first.truncated()).isTrue(); + assertThat(first.changes().getFirst().metadata().contentLength()).isGreaterThan(10000); + var last = store.sync(owner, collection, first.token(), 100, false, change -> 100, 110); + assertThat(last.changes()).hasSize(1); + assertThat(last.truncated()).isFalse(); + long cursors = jdbc.queryForObject("SELECT count(*) FROM sync_continuations", Long.class); + assertThatThrownBy(() -> store.sync(owner, collection, null, 100, true, change -> 111, 110)) + .isInstanceOf(StoreException.class) + .extracting("kind") + .isEqualTo(StoreException.Kind.LIMIT); + assertThat(jdbc.queryForObject("SELECT count(*) FROM sync_continuations", Long.class)) + .isEqualTo(cursors); + assertThat(store.sync(owner, collection, null, 100, true, change -> 111, 222).truncated()) + .isFalse(); + } + + @Test + void continuationIsDurableScopedAndRejectsExpiryRetentionRecoveryAndRevokedPermissions() { + put("a.ics", "a", "a"); + put("b.ics", "b", "b"); + put("c.ics", "c", "c"); + String continuation = store.sync(owner, collection, null, 1).token(); + // A new service instance has no process-local continuation data. Execute it in a fresh real + // database transaction, as a restarted application's service proxy would do. + var restarted = + new TransactionTemplate(transactions) + .execute( + status -> + new CalendarStore(jdbc, continuations) + .sync(owner, collection, continuation, 1)); + assertThat(restarted.changes()) + .singleElement() + .extracting(CalendarStore.Change::name) + .isEqualTo("b.ics"); + UUID another = store.createCollection(owner, "Other", "VEVENT"); + assertThatThrownBy(() -> store.sync(owner, another, continuation, 1)) + .isInstanceOf(StoreException.class) + .extracting("kind") + .isEqualTo(StoreException.Kind.INVALID_TOKEN); + store.share(owner, collection, other, "READ"); + assertThatThrownBy(() -> store.sync(owner, collection, continuation, 1)) + .isInstanceOf(StoreException.class) + .extracting("kind") + .isEqualTo(StoreException.Kind.INVALID_TOKEN); + String reader = store.sync(other, collection, null, 1).token(); + assertThatThrownBy(() -> store.sync(owner, collection, reader, 1)) + .isInstanceOf(StoreException.class) + .extracting("kind") + .isEqualTo(StoreException.Kind.INVALID_TOKEN); + store.share(owner, collection, other, null); + assertThatThrownBy(() -> store.sync(other, collection, reader, 1)) + .isInstanceOf(StoreException.class) + .extracting("kind") + .isEqualTo(StoreException.Kind.FORBIDDEN); + String current = store.sync(owner, collection, null, 1).token(); + final String beforeExpiry = current; + jdbc.update("UPDATE sync_continuations SET expires_at=now()-interval '1 second'"); + assertThatThrownBy(() -> store.sync(owner, collection, beforeExpiry, 1)) + .isInstanceOf(StoreException.class) + .extracting("kind") + .isEqualTo(StoreException.Kind.INVALID_TOKEN); + current = store.sync(owner, collection, null, 1).token(); + final String beforePrune = current; + store.pruneHistory(collection, 2); + assertThatThrownBy(() -> store.sync(owner, collection, beforePrune, 1)) + .isInstanceOf(StoreException.class) + .extracting("kind") + .isEqualTo(StoreException.Kind.INVALID_TOKEN); + assertThat(drain(store.sync(owner, collection, null, 1), 1)).hasSize(3); + final String beforeRestore = store.sync(owner, collection, null, 1).token(); + jdbc.update("UPDATE system_state SET recovery_epoch=gen_random_uuid() WHERE id=1"); + assertThatThrownBy(() -> store.sync(owner, collection, beforeRestore, 1)) + .isInstanceOf(StoreException.class) + .extracting("kind") + .isEqualTo(StoreException.Kind.INVALID_TOKEN); + final String beforeDisable = store.sync(owner, collection, null, 1).token(); + accounts.disable(owner); + assertThatThrownBy(() -> store.sync(owner, collection, beforeDisable, 1)) + .isInstanceOf(StoreException.class) + .extracting("kind") + .isEqualTo(StoreException.Kind.FORBIDDEN); + } + + @Test + void versionedPropertiesRemainFrozenAndCopyCapturesDeferredProperties() { + var binding = dav.create(owner, owner, "calendar", "Calendar", "VEVENT", Map.of()); + collection = binding.collection().id(); + put("a.ics", "a", "a"); + put("b.ics", "b", "b"); + QName title = new QName("urn:sync-fixture", "title"); + dav.patch( + owner, + owner, + "calendar", + "b.ics", + Map.of(title, "before"), + Set.of(), + WriteCondition.none()); + var first = store.sync(owner, collection, null, 1); + long version = store.get(owner, collection, "b.ics").revision(); + dav.patch( + owner, + owner, + "calendar", + "b.ics", + Map.of(title, "after"), + Set.of(), + WriteCondition.none()); + var second = store.sync(owner, collection, first.token(), 1); + assertThat(second.changes().getFirst().metadata().revision()).isEqualTo(version); + assertThat( + store + .historicalProperties(owner, collection, "b.ics", version, Set.of(title)) + .get(title)) + .contains("before") + .doesNotContain("after"); + assertThat(store.historicalPropertyNames(owner, collection, "b.ics", version)).contains(title); + var target = dav.create(owner, owner, "copied", "Copied", "VEVENT", Map.of()); + dav.transferResource( + owner, + collection, + "b.ics", + target.collection().id(), + "copy.ics", + false, + false, + WriteCondition.none()); + long copied = store.get(owner, target.collection().id(), "copy.ics").revision(); + assertThat( + store + .historicalProperties( + owner, target.collection().id(), "copy.ics", copied, Set.of(title)) + .get(title)) + .contains("after"); + } + + @Test + void forwardMigrationFromReviewedSchemaPreservesOldTokensAndAuthoritativeData() { + String schema = "sync_upgrade_" + UUID.randomUUID().toString().replace("-", ""); + var properties = new java.util.Properties(); + properties.setProperty("currentSchema", schema); + var source = + new org.springframework.jdbc.datasource.DriverManagerDataSource( + POSTGRES.getJdbcUrl(), POSTGRES.getUsername(), POSTGRES.getPassword()); + source.setConnectionProperties(properties); + var migrated = new JdbcTemplate(source); + try { + org.flywaydb.core.Flyway.configure() + .dataSource(POSTGRES.getJdbcUrl(), POSTGRES.getUsername(), POSTGRES.getPassword()) + .schemas(schema) + .defaultSchema(schema) + .target("9") + .load() + .migrate(); + UUID actor = UUID.randomUUID(), id = UUID.randomUUID(); + migrated.update( + "INSERT INTO accounts(id,username,display_name) VALUES (?,'upgrade','Upgrade')", actor); + migrated.update( + "INSERT INTO collections(id,owner_id,name,component,revision) VALUES (?,?,'Retained','VEVENT',2)", + id, + actor); + byte[] authoritative = content("retained", "Unicode 保留"); + migrated.update( + "INSERT INTO calendar_resources(collection_id,name,uid,content,etag,revision) VALUES (?,'retained.ics','retained',?,'\"retained\"',1)", + id, + authoritative); + migrated.update( + "INSERT INTO collection_changes(collection_id,revision,name,deleted) VALUES (?,1,'retained.ics',false),(?,2,'removed.ics',true)", + id, + id); + migrated.update( + "INSERT INTO dav_resource_properties(collection_id,resource_name,namespace,name,xml) VALUES (?,'retained.ics','urn:upgrade','language',' a b ')", + id); + UUID generation = + migrated.queryForObject("SELECT generation FROM collections WHERE id=?", UUID.class, id); + UUID epoch = + migrated.queryForObject("SELECT recovery_epoch FROM system_state WHERE id=1", UUID.class); + String oldToken = "urn:busystack:sync:" + epoch + ":" + id + ":" + generation + ":0"; + org.flywaydb.core.Flyway.configure() + .dataSource(POSTGRES.getJdbcUrl(), POSTGRES.getUsername(), POSTGRES.getPassword()) + .schemas(schema) + .defaultSchema(schema) + .load() + .migrate(); + org.flywaydb.core.Flyway.configure() + .dataSource(POSTGRES.getJdbcUrl(), POSTGRES.getUsername(), POSTGRES.getPassword()) + .schemas(schema) + .defaultSchema(schema) + .load() + .validate(); + var upgradedTransactions = + new org.springframework.jdbc.datasource.DataSourceTransactionManager(source); + var transaction = new TransactionTemplate(upgradedTransactions); + var upgraded = + new CalendarStore(migrated, new SyncContinuationStore(migrated, upgradedTransactions)); + var delta = transaction.execute(status -> upgraded.sync(actor, id, oldToken, 100)); + assertThat(delta.changes()).hasSize(2); + assertThat(delta.changes()) + .filteredOn(CalendarStore.Change::deleted) + .singleElement() + .extracting(CalendarStore.Change::name) + .isEqualTo("removed.ics"); + assertThat(upgraded.get(actor, id, "retained.ics").content()).isEqualTo(authoritative); + assertThat( + upgraded + .historicalProperties( + actor, id, "retained.ics", 1, Set.of(new QName("urn:upgrade", "language"))) + .values()) + .singleElement() + .asString() + .contains("xml:lang=\"fr\"", " a ", " b "); + } finally { + jdbc.execute("DROP SCHEMA IF EXISTS " + schema + " CASCADE"); + } + } + + @Test + void projectedContentPagesAlsoBoundRetainedUnprojectedBodies() { + String summary = "x".repeat(900_000); + for (int i = 0; i < 7; i++) put("large-" + i + ".ics", "large-" + i, summary); + var metadata = store.sync(owner, collection, null, 100, false, change -> 100, 10000); + assertThat(metadata.truncated()).isFalse(); + assertThat(metadata.changes()).hasSize(7); + var projected = store.sync(owner, collection, null, 100, true, change -> 100, 10000); + assertThat(projected.truncated()).isTrue(); + assertThat(projected.changes()).hasSize(4); + assertThat(projected.changes().stream().mapToLong(c -> c.metadata().contentLength()).sum()) + .isLessThanOrEqualTo(4L * 1024 * 1024); + var remaining = + store.sync(owner, collection, projected.token(), 100, true, change -> 100, 10000); + assertThat(remaining.truncated()).isFalse(); + assertThat(remaining.changes()).hasSize(3); + } + + @Test + void committedWriteDuringPageConstructionStaysAboveFrozenWatermark() throws Exception { + put("a.ics", "a", "before-a"); + put("b.ics", "b", "before-b"); + var entered = new java.util.concurrent.CountDownLatch(1); + var finish = new java.util.concurrent.CountDownLatch(1); + var first = + java.util.concurrent.CompletableFuture.supplyAsync( + () -> + store.sync( + owner, + collection, + null, + 1, + true, + change -> { + entered.countDown(); + try { + if (!finish.await(15, java.util.concurrent.TimeUnit.SECONDS)) + throw new AssertionError("synchronization callback release timed out"); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + throw new AssertionError(ex); + } + return 100; + }, + 1000)); + try { + assertThat(entered.await(15, java.util.concurrent.TimeUnit.SECONDS)).isTrue(); + put("b.ics", "b", "committed-during-page"); + finish.countDown(); + var page = first.get(15, java.util.concurrent.TimeUnit.SECONDS); + assertThat(page.truncated()).isTrue(); + var second = store.sync(owner, collection, page.token(), 1); + assertThat(second.truncated()).isFalse(); + assertThat( + new String(second.changes().getFirst().resource().content(), StandardCharsets.UTF_8)) + .contains("SUMMARY:before-b") + .doesNotContain("committed-during-page"); + var later = store.sync(owner, collection, second.token(), 1); + assertThat(later.changes()) + .singleElement() + .satisfies( + change -> + assertThat(new String(change.resource().content(), StandardCharsets.UTF_8)) + .contains("committed-during-page")); + } finally { + finish.countDown(); + } + } + + @Test + void boundedReadCommittedCleanupSkipsAnotherWorkersLockedExpiredState() throws Exception { + put("a.ics", "a", "a"); + put("b.ics", "b", "b"); + store.sync(owner, collection, null, 1); + jdbc.update( + "INSERT INTO sync_continuations(id,collection_id,account_id,generation,recovery_epoch,lower_revision,upper_revision,retention_floor,initial_sync,last_name,expires_at) SELECT gen_random_uuid(),collection_id,account_id,generation,recovery_epoch,lower_revision,upper_revision,retention_floor,initial_sync,last_name,now()-interval '1 second' FROM sync_continuations CROSS JOIN generate_series(1,2) WHERE collection_id=?", + collection); + var locked = new java.util.concurrent.CountDownLatch(1); + var release = new java.util.concurrent.CountDownLatch(1); + var worker = + java.util.concurrent.CompletableFuture.runAsync( + () -> + new TransactionTemplate(transactions) + .executeWithoutResult( + status -> { + jdbc.queryForObject( + "SELECT id FROM sync_continuations WHERE expires_atnow()", Long.class)) + .isEqualTo(1); + } finally { + release.countDown(); + worker.get(15, java.util.concurrent.TimeUnit.SECONDS); + } + cleanup.pruneExpired(); + assertThat(jdbc.queryForObject("SELECT count(*) FROM sync_continuations", Long.class)) + .isEqualTo(1); + } + + @Test + void cursorPersistenceRechecksPermissionRevokedDuringPageConstruction() throws Exception { + put("a.ics", "a", "a"); + put("b.ics", "b", "b"); + store.share(owner, collection, other, "READ"); + var entered = new java.util.concurrent.CountDownLatch(1); + var finish = new java.util.concurrent.CountDownLatch(1); + var pending = + java.util.concurrent.CompletableFuture.supplyAsync( + () -> + store.sync( + other, + collection, + null, + 1, + false, + change -> { + entered.countDown(); + try { + if (!finish.await(15, java.util.concurrent.TimeUnit.SECONDS)) + throw new AssertionError("revocation release timed out"); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + throw new AssertionError(ex); + } + return 100; + }, + 1000)); + try { + assertThat(entered.await(15, java.util.concurrent.TimeUnit.SECONDS)).isTrue(); + store.share(owner, collection, other, null); + finish.countDown(); + assertThatThrownBy(() -> pending.get(15, java.util.concurrent.TimeUnit.SECONDS)) + .hasCauseInstanceOf(StoreException.class) + .satisfies( + error -> + assertThat(((StoreException) error.getCause()).kind()) + .isEqualTo(StoreException.Kind.FORBIDDEN)); + assertThat( + jdbc.queryForObject( + "SELECT count(*) FROM sync_continuations WHERE account_id=?", Long.class, other)) + .isZero(); + } finally { + finish.countDown(); + } + } +} diff --git a/src/test/java/org/busystack/caldav/storage/WriteConditionTest.java b/src/test/java/org/busystack/caldav/storage/WriteConditionTest.java new file mode 100644 index 0000000..72b12ad --- /dev/null +++ b/src/test/java/org/busystack/caldav/storage/WriteConditionTest.java @@ -0,0 +1,50 @@ +package org.busystack.caldav.storage; + +import static org.assertj.core.api.Assertions.*; + +import java.util.Set; +import org.junit.jupiter.api.Test; + +class WriteConditionTest { + @Test + void ifNoneMatchUsesWeakComparisonWithoutChangingIfMatch() { + var excluded = new WriteCondition(false, false, Set.of(), Set.of("W/\"same\"")); + assertThatThrownBy(() -> excluded.check("\"same\"")).isInstanceOf(StoreException.class); + assertThatCode(() -> excluded.check("\"other\"")).doesNotThrowAnyException(); + assertThatThrownBy(() -> WriteCondition.match("W/\"same\"").check("\"same\"")) + .isInstanceOf(StoreException.class); + } + + @Test + void existenceConditionsWorkForCollectionsWithoutAnAdvertisedEntityTag() { + var exists = new WriteCondition(false, true, Set.of()); + assertThatCode(() -> exists.check(null, true)).doesNotThrowAnyException(); + assertThatThrownBy(() -> exists.check(null, false)).isInstanceOf(StoreException.class); + assertThatThrownBy(() -> WriteCondition.create().check(null, true)) + .isInstanceOf(StoreException.class); + } + + @Test + void dateConditionsUseWholeSecondsAndIgnoreUnavailableDates() { + var cutoff = java.time.Instant.parse("2020-01-01T12:00:00Z"); + var condition = new WriteCondition(false, false, Set.of(), Set.of(), cutoff); + assertThatCode(() -> condition.check("\"same\"", true, cutoff.plusNanos(999999999))) + .doesNotThrowAnyException(); + assertThatThrownBy(() -> condition.check("\"same\"", true, cutoff.plusSeconds(1))) + .isInstanceOf(StoreException.class) + .extracting("kind") + .isEqualTo(StoreException.Kind.PRECONDITION); + assertThatCode(() -> condition.check(null, true, null)).doesNotThrowAnyException(); + assertThatCode(() -> condition.check(null, false, null)).doesNotThrowAnyException(); + } + + @Test + void effectiveDatesAreConsistentAndNeverLaterThanResponseOrigination() { + var origin = java.time.Instant.parse("2026-10-27T12:00:00.900Z"); + assertThat(ModificationDates.effective(origin.plusSeconds(86400), origin)) + .isEqualTo(java.time.Instant.parse("2026-10-27T12:00:00Z")); + assertThat(ModificationDates.effective(origin.minusSeconds(3600), origin)) + .isEqualTo(java.time.Instant.parse("2026-10-27T11:00:00Z")); + assertThat(ModificationDates.effective(null, origin)).isNull(); + } +} diff --git a/src/test/java/org/busystack/caldav/testing/ContainerIsolationIT.java b/src/test/java/org/busystack/caldav/testing/ContainerIsolationIT.java new file mode 100644 index 0000000..473a173 --- /dev/null +++ b/src/test/java/org/busystack/caldav/testing/ContainerIsolationIT.java @@ -0,0 +1,27 @@ +package org.busystack.caldav.testing; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; +import org.testcontainers.DockerClientFactory; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; +import org.testcontainers.postgresql.PostgreSQLContainer; + +/** Verifies actual daemon bindings, including the framework-created Docker-control sidecar. */ +@Testcontainers +class ContainerIsolationIT { + @Container static final PostgreSQLContainer POSTGRES = LoopbackContainers.postgres(); + + @Test + void databaseAndResourceReaperArePublishedOnlyOnLoopback() { + assertThat(Boolean.parseBoolean(System.getenv("TESTCONTAINERS_RYUK_DISABLED"))) + .as("The fixture isolation policy retains framework-managed cleanup") + .isFalse(); + var docker = DockerClientFactory.instance().client(); + LoopbackContainers.requireLoopbackBindings( + docker.inspectContainerCmd(POSTGRES.getContainerId()).exec()); + LoopbackContainers.requireLoopbackBindings( + docker.inspectContainerCmd("testcontainers-ryuk-" + DockerClientFactory.SESSION_ID).exec()); + } +} diff --git a/src/test/java/org/busystack/caldav/testing/FixtureDatabase.java b/src/test/java/org/busystack/caldav/testing/FixtureDatabase.java new file mode 100644 index 0000000..76c1bfe --- /dev/null +++ b/src/test/java/org/busystack/caldav/testing/FixtureDatabase.java @@ -0,0 +1,30 @@ +package org.busystack.caldav.testing; + +import java.util.Objects; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.datasource.DataSourceTransactionManager; +import org.springframework.transaction.support.TransactionTemplate; + +/** Disposable fixtures must not deadlock TRUNCATE against the real maintenance jobs. */ +public final class FixtureDatabase { + private FixtureDatabase() {} + + public static void clearAccounts(JdbcTemplate jdbc) { + reset(jdbc, "TRUNCATE accounts CASCADE"); + } + + public static void reset(JdbcTemplate jdbc, String... statements) { + var transaction = + new TransactionTemplate( + new DataSourceTransactionManager(Objects.requireNonNull(jdbc.getDataSource()))); + transaction.executeWithoutResult( + status -> { + // Match the production audit-prune order, then Spring Session's parent/child delete + // order. + // Jobs remain enabled; no sleep, retry or suppression hides a failure. + jdbc.execute( + "LOCK TABLE security_audit,login_throttle,webauthn_ceremonies,spring_session,spring_session_attributes IN ACCESS EXCLUSIVE MODE"); + for (String statement : statements) jdbc.execute(statement); + }); + } +} diff --git a/src/test/java/org/busystack/caldav/testing/LoopbackContainerModifier.java b/src/test/java/org/busystack/caldav/testing/LoopbackContainerModifier.java new file mode 100644 index 0000000..d4e207f --- /dev/null +++ b/src/test/java/org/busystack/caldav/testing/LoopbackContainerModifier.java @@ -0,0 +1,12 @@ +package org.busystack.caldav.testing; + +import com.github.dockerjava.api.command.CreateContainerCmd; +import org.testcontainers.core.CreateContainerCmdModifier; + +/** Test-only ServiceLoader provider; includes the framework-created Ryuk control port. */ +public final class LoopbackContainerModifier implements CreateContainerCmdModifier { + @Override + public CreateContainerCmd modify(CreateContainerCmd command) { + return LoopbackContainers.loopback(command); + } +} diff --git a/src/test/java/org/busystack/caldav/testing/LoopbackContainers.java b/src/test/java/org/busystack/caldav/testing/LoopbackContainers.java new file mode 100644 index 0000000..164dc80 --- /dev/null +++ b/src/test/java/org/busystack/caldav/testing/LoopbackContainers.java @@ -0,0 +1,90 @@ +package org.busystack.caldav.testing; + +import com.github.dockerjava.api.command.CreateContainerCmd; +import com.github.dockerjava.api.command.InspectContainerResponse; +import com.github.dockerjava.api.model.ExposedPort; +import com.github.dockerjava.api.model.HostConfig; +import com.github.dockerjava.api.model.Ports; +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.Map; +import org.testcontainers.postgresql.PostgreSQLContainer; +import org.testcontainers.utility.DockerImageName; + +/** Disposable fixtures never publish development credentials on external host interfaces. */ +public final class LoopbackContainers { + private static final String LOOPBACK = "127.0.0.1"; + private static final String POSTGRES = + "postgres:18.6@sha256:edb0c08dc33befd0262ab3b5cf1c4f3f8cfd8b09e88c05b45adee29d6761ddb6"; + + private LoopbackContainers() {} + + public static PostgreSQLContainer postgres() { + return new LoopbackPostgres(); + } + + public static CreateContainerCmd bind(CreateContainerCmd command, int port) { + var exposed = new LinkedHashSet(); + if (command.getExposedPorts() != null) exposed.addAll(Arrays.asList(command.getExposedPorts())); + exposed.add(ExposedPort.tcp(port)); + command.withExposedPorts(exposed.toArray(ExposedPort[]::new)); + return loopback(command); + } + + /** Public Testcontainers SPI also applies this policy to its otherwise private Ryuk container. */ + public static CreateContainerCmd loopback(CreateContainerCmd command) { + HostConfig host = command.getHostConfig(); + if (host == null) host = new HostConfig(); + Map previous = + host.getPortBindings() == null ? Map.of() : host.getPortBindings().getBindings(); + var exposed = new LinkedHashSet<>(previous.keySet()); + if (command.getExposedPorts() != null) exposed.addAll(Arrays.asList(command.getExposedPorts())); + var bindings = new Ports(); + for (ExposedPort port : exposed) { + Ports.Binding[] existing = previous.get(port); + if (existing == null || existing.length == 0) { + bindings.bind(port, Ports.Binding.bindIp(LOOPBACK)); + } else { + for (Ports.Binding binding : existing) { + bindings.bind( + port, + new Ports.Binding(LOOPBACK, binding == null ? null : binding.getHostPortSpec())); + } + } + } + command.withHostConfig(host.withPortBindings(bindings).withPublishAllPorts(false)); + var labels = new LinkedHashMap(); + if (command.getLabels() != null) labels.putAll(command.getLabels()); + labels.put("org.busystack.caldav.test", "true"); + command.withLabels(labels); + return command; + } + + public static void requireLoopbackBindings(InspectContainerResponse container) { + boolean published = false; + for (Ports.Binding[] bindings : + container.getNetworkSettings().getPorts().getBindings().values()) { + if (bindings == null) continue; + for (Ports.Binding binding : bindings) { + published = true; + if (binding == null || !LOOPBACK.equals(binding.getHostIp())) + throw new IllegalStateException("Test fixture published a non-loopback port"); + } + } + if (!published) throw new IllegalStateException("Expected a published loopback fixture port"); + } + + private static final class LoopbackPostgres extends PostgreSQLContainer { + private LoopbackPostgres() { + super(DockerImageName.parse(POSTGRES).asCompatibleSubstituteFor("postgres")); + withCreateContainerCmdModifier(command -> bind(command, 5432)); + } + + @Override + protected void containerIsStarted(InspectContainerResponse container) { + super.containerIsStarted(container); + requireLoopbackBindings(container); + } + } +} diff --git a/src/test/java/org/busystack/caldav/testing/SignedPasskeyClient.java b/src/test/java/org/busystack/caldav/testing/SignedPasskeyClient.java new file mode 100644 index 0000000..bcc699c --- /dev/null +++ b/src/test/java/org/busystack/caldav/testing/SignedPasskeyClient.java @@ -0,0 +1,447 @@ +package org.busystack.caldav.testing; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.ByteArrayOutputStream; +import java.math.BigInteger; +import java.net.CookieManager; +import java.net.CookiePolicy; +import java.net.URI; +import java.net.URLEncoder; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; +import java.security.KeyPair; +import java.security.KeyPairGenerator; +import java.security.MessageDigest; +import java.security.SecureRandom; +import java.security.Signature; +import java.security.interfaces.ECPublicKey; +import java.security.interfaces.EdECPublicKey; +import java.security.interfaces.RSAPublicKey; +import java.security.spec.ECGenParameterSpec; +import java.time.Duration; +import java.util.Base64; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.regex.Pattern; +import tools.jackson.databind.JsonNode; +import tools.jackson.databind.json.JsonMapper; + +/** Real signed ES256/none-attestation ceremonies derived from the independent audit fixture. */ +public final class SignedPasskeyClient implements AutoCloseable { + public static final JsonMapper JSON = JsonMapper.builder().build(); + private final String base; + private final String context; + private final String origin; + private final List browsers = new java.util.ArrayList<>(); + + public SignedPasskeyClient(int port, String context, String origin) { + this.base = "https://127.0.0.1:" + port; + this.context = context; + this.origin = origin; + } + + public HttpClient browser() { + var browser = + HttpClient.newBuilder() + .sslContext(TestTls.instance().context()) + .cookieHandler(new CookieManager(null, CookiePolicy.ACCEPT_ALL)) + .followRedirects(HttpClient.Redirect.NEVER) + .connectTimeout(Duration.ofSeconds(10)) + .build(); + browsers.add(browser); + return browser; + } + + @Override + public void close() { + browsers.forEach(HttpClient::close); + browsers.clear(); + } + + public HttpResponse request( + HttpClient browser, String path, String method, String csrf, Object body) throws Exception { + var request = + HttpRequest.newBuilder(URI.create(base + context + path)).timeout(Duration.ofSeconds(30)); + if (csrf != null) request.header("X-CSRF-TOKEN", csrf); + String text = + body == null ? "" : body instanceof String value ? value : JSON.writeValueAsString(body); + request.header( + "Content-Type", + body instanceof String ? "application/x-www-form-urlencoded" : "application/json"); + return browser.send( + request.method(method, HttpRequest.BodyPublishers.ofString(text)).build(), + HttpResponse.BodyHandlers.ofString()); + } + + public HttpResponse get(HttpClient browser, String path) throws Exception { + return request(browser, path, "GET", null, null); + } + + public HttpResponse bearer(String path, String method, String token, String body) + throws Exception { + return browser() + .send( + HttpRequest.newBuilder(URI.create(base + context + path)) + .timeout(Duration.ofSeconds(30)) + .header("Authorization", "Bearer " + token) + .header("Content-Type", "text/calendar") + .method(method, HttpRequest.BodyPublishers.ofString(body)) + .build(), + HttpResponse.BodyHandlers.ofString()); + } + + public String csrf(HttpClient browser, String path) throws Exception { + var response = get(browser, path); + assertThat(response.statusCode()).as(path).isEqualTo(200); + return csrf(response.body()); + } + + public static String csrf(String html) { + for (String pattern : + List.of("name=\"csrf-token\" content=\"([^\"]+)\"", "name=\"_csrf\" value=\"([^\"]+)\"")) { + var match = Pattern.compile(pattern).matcher(html); + if (match.find()) return match.group(1); + } + throw new AssertionError("Response has no CSRF token"); + } + + public HttpClient passwordLogin(String username, String password) throws Exception { + var browser = browser(); + String csrf = csrf(browser, "/login"); + var response = + request( + browser, + "/login", + "POST", + csrf, + form(Map.of("username", username, "password", password))); + assertThat(response.statusCode()).isEqualTo(302); + return browser; + } + + public Credential enroll(HttpClient browser) throws Exception { + return enroll(browser, "ES256"); + } + + public Credential enroll(HttpClient browser, String algorithm) throws Exception { + var registration = registration(browser, "correct", algorithm); + assertThat(registration.response().statusCode()) + .as(registration.response().body()) + .isEqualTo(200); + return registration.credential(); + } + + public Registration registration(HttpClient browser, String mode) throws Exception { + return registration(browser, mode, "ES256"); + } + + private Registration registration(HttpClient browser, String mode, String algorithm) + throws Exception { + String csrf = csrf(browser, "/security/passkeys"); + var response = request(browser, "/webauthn/register/options", "POST", csrf, null); + assertThat(response.statusCode()).isEqualTo(200); + JsonNode options = JSON.readTree(response.body()); + var generator = + KeyPairGenerator.getInstance( + algorithm.equals("RS256") ? "RSA" : algorithm.equals("EdDSA") ? "Ed25519" : "EC"); + if (algorithm.equals("RS256")) generator.initialize(2048); + else if (algorithm.equals("ES256")) generator.initialize(new ECGenParameterSpec("secp256r1")); + KeyPair key = generator.generateKeyPair(); + long coseAlgorithm = algorithm.equals("RS256") ? -257 : algorithm.equals("EdDSA") ? -8 : -7; + assertThat( + options + .path("pubKeyCredParams") + .valueStream() + .anyMatch(value -> value.path("alg").asLong() == coseAlgorithm)) + .as(algorithm) + .isTrue(); + byte[] cose; + if (key.getPublic() instanceof RSAPublicKey rsa) { + cose = + concat( + new byte[] {(byte) 0xa4, 1, 3, 3, 0x39, 1, 0, 0x20}, + bstr(unsigned(rsa.getModulus())), + new byte[] {0x21}, + bstr(unsigned(rsa.getPublicExponent()))); + } else if (key.getPublic() instanceof EdECPublicKey ed) { + byte[] compressed = fixed(ed.getPoint().getY()); + for (int low = 0, high = compressed.length - 1; low < high; low++, high--) { + byte saved = compressed[low]; + compressed[low] = compressed[high]; + compressed[high] = saved; + } + if (ed.getPoint().isXOdd()) compressed[31] |= (byte) 0x80; + cose = concat(new byte[] {(byte) 0xa4, 1, 1, 3, 0x27, 0x20, 6, 0x21}, bstr(compressed)); + } else { + ECPublicKey ec = (ECPublicKey) key.getPublic(); + cose = + concat( + new byte[] {(byte) 0xa5, 1, 2, 3, 0x26, 0x20, 1, 0x21}, + bstr(fixed(ec.getW().getAffineX())), + new byte[] {0x22}, + bstr(fixed(ec.getW().getAffineY()))); + } + byte[] id = new byte[32]; + new SecureRandom().nextBytes(id); + byte[] rpHash = hash(options.path("rp").path("id").asString().getBytes(StandardCharsets.UTF_8)); + byte[] authenticatorData = + concat( + mode.equals("wrong-rpidhash") ? new byte[32] : rpHash, + new byte[] { + (byte) + (mode.equals("no-up") + ? 0x4c + : mode.equals("no-uv") + ? 0x49 + : mode.equals("impossible-backup") ? 0x55 : 0x4d) + }, + new byte[4], + new byte[16], + new byte[] {0, 32}, + id, + cose); + byte[] attestation = + concat( + new byte[] { + (byte) 0xa3, + 0x63, + 'f', + 'm', + 't', + 0x64, + 'n', + 'o', + 'n', + 'e', + 0x67, + 'a', + 't', + 't', + 'S', + 't', + 'm', + 't', + (byte) 0xa0, + 0x68, + 'a', + 'u', + 't', + 'h', + 'D', + 'a', + 't', + 'a' + }, + bstr(authenticatorData)); + byte[] clientData = + JSON.writeValueAsBytes( + Map.of( + "type", + "webauthn.create", + "challenge", + mode.equals("wrong-challenge") + ? encoded(new byte[32]) + : options.path("challenge").asString(), + "origin", + mode.equals("wrong-origin") ? "https://attacker.invalid" : origin, + "crossOrigin", + false)); + var submitted = + Map.of( + "id", encoded(id), + "rawId", encoded(id), + "type", "public-key", + "response", + Map.of( + "attestationObject", encoded(attestation), + "clientDataJSON", encoded(clientData), + "transports", List.of("internal")), + "clientExtensionResults", Map.of()); + Map body = + Map.of("publicKey", Map.of("credential", submitted, "label", "signed-test-passkey")); + var registered = + mode.equals("prepare") ? null : request(browser, "/webauthn/register", "POST", csrf, body); + return new Registration( + new Credential( + key, + id, + decoded(options.path("user").path("id").asString()), + rpHash, + algorithm.equals("RS256") + ? "SHA256withRSA" + : algorithm.equals("EdDSA") ? "Ed25519" : "SHA256withECDSA"), + body, + registered); + } + + public Ceremony options(HttpClient browser) throws Exception { + String csrf = csrf(browser, "/login"); + var response = request(browser, "/webauthn/authenticate/options", "POST", csrf, null); + assertThat(response.statusCode()).as(response.body()).isEqualTo(200); + return new Ceremony(browser, csrf, JSON.readTree(response.body())); + } + + public HttpResponse assertCredential( + Ceremony ceremony, Credential credential, String mode, int flags) throws Exception { + return request( + ceremony.browser(), + "/login/webauthn", + "POST", + ceremony.csrf(), + credential.assertion(ceremony.options(), origin, mode, flags)); + } + + public String localPath(String canonicalLocation) { + URI location = URI.create(canonicalLocation); + String path = location.getRawPath(); + assertThat(path).startsWith(context + "/"); + return path.substring(context.length()) + + (location.getRawQuery() == null ? "" : "?" + location.getRawQuery()); + } + + public static String form(Map values) { + return values.entrySet().stream() + .map(entry -> encode(entry.getKey()) + "=" + encode(entry.getValue())) + .collect(java.util.stream.Collectors.joining("&")); + } + + public static Map query(String uri) { + var result = new LinkedHashMap(); + for (String part : URI.create(uri).getRawQuery().split("&")) { + var pair = part.split("=", 2); + result.put( + java.net.URLDecoder.decode(pair[0], StandardCharsets.UTF_8), + pair.length == 1 ? "" : java.net.URLDecoder.decode(pair[1], StandardCharsets.UTF_8)); + } + return result; + } + + public record Ceremony(HttpClient browser, String csrf, JsonNode options) {} + + public record Registration( + Credential credential, Map body, HttpResponse response) {} + + public static final class Credential { + private final KeyPair key; + private final byte[] id; + private final byte[] handle; + private final byte[] rpHash; + private final String signatureAlgorithm; + private int counter; + + private Credential( + KeyPair key, byte[] id, byte[] handle, byte[] rpHash, String signatureAlgorithm) { + this.key = key; + this.id = id; + this.handle = handle; + this.rpHash = rpHash; + this.signatureAlgorithm = signatureAlgorithm; + } + + public String id() { + return encoded(id); + } + + public Map assertion(JsonNode options, String origin, String mode, int flags) + throws Exception { + var client = new HashMap(); + client.put("type", mode.equals("wrong-type") ? "webauthn.create" : "webauthn.get"); + client.put( + "challenge", + mode.equals("wrong-challenge") + ? encoded(new byte[32]) + : options.path("challenge").asString()); + client.put("origin", mode.equals("wrong-origin") ? "https://attacker.invalid" : origin); + client.put("crossOrigin", mode.equals("cross-origin") || mode.equals("untrusted-top-origin")); + if (mode.equals("untrusted-top-origin")) client.put("topOrigin", "https://attacker.invalid"); + byte[] clientData = JSON.writeValueAsBytes(client); + byte[] authenticatorData = + concat( + mode.equals("wrong-rpidhash") ? new byte[32] : rpHash, + new byte[] {(byte) flags}, + ByteBuffer.allocate(4).putInt(++counter).array()); + var signer = Signature.getInstance(signatureAlgorithm); + signer.initSign(key.getPrivate()); + signer.update(concat(authenticatorData, hash(clientData))); + byte[] signature = signer.sign(); + if (mode.equals("invalid-signature")) signature[signature.length - 1] ^= 1; + var response = new HashMap(); + response.put("authenticatorData", encoded(authenticatorData)); + response.put("clientDataJSON", encoded(clientData)); + response.put("signature", encoded(signature)); + response.put( + "userHandle", + mode.equals("absent") + ? null + : mode.equals("wrong") ? encoded(new byte[] {9, 8, 7}) : encoded(handle)); + return Map.of( + "id", + id(), + "rawId", + id(), + "type", + "public-key", + "response", + response, + "clientExtensionResults", + Map.of()); + } + } + + public static String encoded(byte[] value) { + return Base64.getUrlEncoder().withoutPadding().encodeToString(value); + } + + private static byte[] decoded(String value) { + return Base64.getUrlDecoder().decode(value); + } + + public static byte[] hash(byte[] value) throws Exception { + return MessageDigest.getInstance("SHA-256").digest(value); + } + + private static String encode(String value) { + return URLEncoder.encode(value, StandardCharsets.UTF_8); + } + + private static byte[] concat(byte[]... arrays) { + var out = new ByteArrayOutputStream(); + for (byte[] array : arrays) out.writeBytes(array); + return out.toByteArray(); + } + + private static byte[] fixed(BigInteger value) { + byte[] encoded = value.toByteArray(); + byte[] result = new byte[32]; + System.arraycopy( + encoded, + Math.max(0, encoded.length - 32), + result, + Math.max(0, 32 - encoded.length), + Math.min(32, encoded.length)); + return result; + } + + private static byte[] unsigned(BigInteger value) { + byte[] result = value.toByteArray(); + return result.length > 1 && result[0] == 0 + ? java.util.Arrays.copyOfRange(result, 1, result.length) + : result; + } + + private static byte[] bstr(byte[] value) { + byte[] header = + value.length < 24 + ? new byte[] {(byte) (0x40 + value.length)} + : value.length < 256 + ? new byte[] {0x58, (byte) value.length} + : new byte[] {0x59, (byte) (value.length >> 8), (byte) value.length}; + return concat(header, value); + } +} diff --git a/src/test/java/org/busystack/caldav/testing/TestTls.java b/src/test/java/org/busystack/caldav/testing/TestTls.java new file mode 100644 index 0000000..ab9bea7 --- /dev/null +++ b/src/test/java/org/busystack/caldav/testing/TestTls.java @@ -0,0 +1,179 @@ +package org.busystack.caldav.testing; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.security.KeyStore; +import java.util.ArrayList; +import java.util.List; +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManagerFactory; + +/** Disposable CA and localhost certificate; production trust settings are never changed. */ +public final class TestTls { + private static final TestTls INSTANCE = create(); + private final Path serverStore; + private final SSLContext context; + + private TestTls(Path serverStore, SSLContext context) { + this.serverStore = serverStore; + this.context = context; + } + + public static TestTls instance() { + return INSTANCE; + } + + public String keyStore() { + return serverStore.toUri().toString(); + } + + public SSLContext context() { + return context; + } + + public String password() { + return "test-only-keystore-password"; + } + + private static TestTls create() { + try { + Path dir = Files.createTempDirectory("busystack-test-tls-"); + Path ca = dir.resolve("ca.p12"), + server = dir.resolve("server.p12"), + caCert = dir.resolve("ca.pem"), + csr = dir.resolve("server.csr"), + signed = dir.resolve("server.pem"); + String password = "test-only-keystore-password"; + keytool( + "-genkeypair", + "-alias", + "ca", + "-dname", + "CN=BusyStack disposable test CA", + "-keyalg", + "EC", + "-groupname", + "secp256r1", + "-ext", + "bc:c=ca:true", + "-ext", + "ku:c=keyCertSign,cRLSign", + "-validity", + "7", + "-keystore", + ca.toString(), + "-storepass", + password, + "-noprompt"); + keytool( + "-exportcert", + "-alias", + "ca", + "-keystore", + ca.toString(), + "-storepass", + password, + "-rfc", + "-file", + caCert.toString()); + keytool( + "-genkeypair", + "-alias", + "server", + "-dname", + "CN=localhost", + "-keyalg", + "EC", + "-groupname", + "secp256r1", + "-validity", + "7", + "-keystore", + server.toString(), + "-storepass", + password, + "-noprompt"); + keytool( + "-certreq", + "-alias", + "server", + "-keystore", + server.toString(), + "-storepass", + password, + "-file", + csr.toString()); + keytool( + "-gencert", + "-alias", + "ca", + "-keystore", + ca.toString(), + "-storepass", + password, + "-infile", + csr.toString(), + "-outfile", + signed.toString(), + "-rfc", + "-validity", + "7", + "-ext", + "san=dns:localhost,ip:127.0.0.1", + "-ext", + "eku=serverAuth", + "-ext", + "ku=digitalSignature"); + keytool( + "-importcert", + "-alias", + "ca", + "-keystore", + server.toString(), + "-storepass", + password, + "-file", + caCert.toString(), + "-noprompt"); + keytool( + "-importcert", + "-alias", + "server", + "-keystore", + server.toString(), + "-storepass", + password, + "-file", + signed.toString(), + "-noprompt"); + var trusted = KeyStore.getInstance("PKCS12"); + trusted.load(null, null); + try (var input = Files.newInputStream(caCert)) { + trusted.setCertificateEntry( + "test-ca", + java.security.cert.CertificateFactory.getInstance("X.509").generateCertificate(input)); + } + var trust = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); + trust.init(trusted); + var context = SSLContext.getInstance("TLS"); + context.init(null, trust.getTrustManagers(), null); + for (Path path : List.of(ca, server, caCert, csr, signed)) path.toFile().deleteOnExit(); + dir.toFile().deleteOnExit(); + return new TestTls(server, context); + } catch (Exception ex) { + throw new IllegalStateException("Could not create isolated TLS fixture", ex); + } + } + + private static void keytool(String... arguments) throws Exception { + List command = new ArrayList<>(); + command.add(Path.of(System.getProperty("java.home"), "bin", "keytool").toString()); + command.addAll(List.of(arguments)); + var process = new ProcessBuilder(command).redirectErrorStream(true).start(); + String output = + new String( + process.getInputStream().readAllBytes(), java.nio.charset.StandardCharsets.UTF_8); + if (process.waitFor() != 0) + throw new IllegalStateException("Test certificate generation failed: " + output); + } +} diff --git a/src/test/java/org/busystack/caldav/testing/TestTlsTest.java b/src/test/java/org/busystack/caldav/testing/TestTlsTest.java new file mode 100644 index 0000000..c7b4b9a --- /dev/null +++ b/src/test/java/org/busystack/caldav/testing/TestTlsTest.java @@ -0,0 +1,30 @@ +package org.busystack.caldav.testing; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.net.URI; +import java.nio.file.Files; +import java.nio.file.Path; +import java.security.KeyStore; +import java.security.cert.X509Certificate; +import org.junit.jupiter.api.Test; + +class TestTlsTest { + @Test + void disposableAuthorityHasCriticalCertificateSigningUsage() throws Exception { + var tls = TestTls.instance(); + var store = KeyStore.getInstance("PKCS12"); + try (var input = Files.newInputStream(Path.of(URI.create(tls.keyStore())))) { + store.load(input, tls.password().toCharArray()); + } + var authority = (X509Certificate) store.getCertificate("ca"); + assertThat(authority.getBasicConstraints()).isGreaterThanOrEqualTo(0); + assertThat(authority.getCriticalExtensionOIDs()).contains("2.5.29.15"); + assertThat(authority.getKeyUsage()[5]).isTrue(); + assertThat(authority.getKeyUsage()[6]).isTrue(); + var server = (X509Certificate) store.getCertificate("server"); + server.verify(authority.getPublicKey()); + assertThat(server.getBasicConstraints()).isEqualTo(-1); + assertThat(server.getExtendedKeyUsage()).contains("1.3.6.1.5.5.7.3.1"); + } +} diff --git a/src/test/java/org/busystack/caldav/xml/XmlCharactersTest.java b/src/test/java/org/busystack/caldav/xml/XmlCharactersTest.java new file mode 100644 index 0000000..ea9d6ad --- /dev/null +++ b/src/test/java/org/busystack/caldav/xml/XmlCharactersTest.java @@ -0,0 +1,31 @@ +package org.busystack.caldav.xml; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; + +class XmlCharactersTest { + @Test + void acceptsExactlyXml10CharacterRangesIncludingValidNoncharacters() { + for (int codePoint : + new int[] { + 0x9, 0xA, 0xD, 0x20, 0x7F, 0x85, 0xD7FF, 0xE000, 0xFDD0, 0xFFFD, 0x10000, 0x1FFFE, + 0x10FFFF + }) { + assertThat(XmlCharacters.valid("Before" + new String(Character.toChars(codePoint)) + "After")) + .as("XML character U+%X", codePoint) + .isTrue(); + } + assertThat(XmlCharacters.valid("")).isTrue(); + } + + @Test + void rejectsInvalidControlsUnpairedSurrogatesAndExcludedBmpValues() { + for (int codePoint : new int[] {0, 1, 8, 0xB, 0xC, 0x1F, 0xD800, 0xDFFF, 0xFFFE, 0xFFFF}) { + assertThat(XmlCharacters.valid("Before" + (char) codePoint + "After")) + .as("Invalid XML character U+%X", codePoint) + .isFalse(); + } + assertThat(XmlCharacters.valid(null)).isFalse(); + } +} diff --git a/src/test/resources/META-INF/services/org.testcontainers.core.CreateContainerCmdModifier b/src/test/resources/META-INF/services/org.testcontainers.core.CreateContainerCmdModifier new file mode 100644 index 0000000..0055225 --- /dev/null +++ b/src/test/resources/META-INF/services/org.testcontainers.core.CreateContainerCmdModifier @@ -0,0 +1 @@ +org.busystack.caldav.testing.LoopbackContainerModifier diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties new file mode 100644 index 0000000..cfc0b3e --- /dev/null +++ b/src/test/resources/application.properties @@ -0,0 +1,2 @@ +# Real embedded HTTP/TLS fixtures are local, including tests that use demonstration accounts. +server.address=127.0.0.1 diff --git a/src/test/resources/calendar/America-New_York-ical4j4.4.0.ics b/src/test/resources/calendar/America-New_York-ical4j4.4.0.ics new file mode 100644 index 0000000..46aa0e7 --- /dev/null +++ b/src/test/resources/calendar/America-New_York-ical4j4.4.0.ics @@ -0,0 +1,115 @@ +BEGIN:VTIMEZONE +TZID:America/New_York +LAST-MODIFIED:20250121T082933Z +TZURL:https://www.tzurl.org/zoneinfo/America/New_York +X-LIC-LOCATION:America/New_York +X-PROLEPTIC-TZNAME:LMT +BEGIN:STANDARD +TZNAME:EST +TZOFFSETFROM:-045602 +TZOFFSETTO:-0500 +DTSTART:18831118T120358 +END:STANDARD +BEGIN:DAYLIGHT +TZNAME:EDT +TZOFFSETFROM:-0500 +TZOFFSETTO:-0400 +DTSTART:19180331T020000 +RRULE:FREQ=YEARLY;UNTIL=19200328T070000Z;BYMONTH=3;BYDAY=-1SU +END:DAYLIGHT +BEGIN:STANDARD +TZNAME:EST +TZOFFSETFROM:-0400 +TZOFFSETTO:-0500 +DTSTART:19181027T020000 +RRULE:FREQ=YEARLY;UNTIL=19201031T060000Z;BYMONTH=10;BYDAY=-1SU +END:STANDARD +BEGIN:DAYLIGHT +TZNAME:EDT +TZOFFSETFROM:-0500 +TZOFFSETTO:-0400 +DTSTART:19210424T020000 +RRULE:FREQ=YEARLY;UNTIL=19410427T070000Z;BYMONTH=4;BYDAY=-1SU +END:DAYLIGHT +BEGIN:STANDARD +TZNAME:EST +TZOFFSETFROM:-0400 +TZOFFSETTO:-0500 +DTSTART:19210925T020000 +RRULE:FREQ=YEARLY;UNTIL=19410928T060000Z;BYMONTH=9;BYDAY=-1SU +END:STANDARD +BEGIN:DAYLIGHT +TZNAME:EWT +TZOFFSETFROM:-0500 +TZOFFSETTO:-0400 +DTSTART:19420209T020000 +END:DAYLIGHT +BEGIN:DAYLIGHT +TZNAME:EPT +TZOFFSETFROM:-0400 +TZOFFSETTO:-0400 +DTSTART:19450814T190000 +END:DAYLIGHT +BEGIN:STANDARD +TZNAME:EST +TZOFFSETFROM:-0400 +TZOFFSETTO:-0500 +DTSTART:19450930T020000 +END:STANDARD +BEGIN:DAYLIGHT +TZNAME:EDT +TZOFFSETFROM:-0500 +TZOFFSETTO:-0400 +DTSTART:19460428T020000 +RRULE:FREQ=YEARLY;UNTIL=19730429T070000Z;BYMONTH=4;BYDAY=-1SU +END:DAYLIGHT +BEGIN:STANDARD +TZNAME:EST +TZOFFSETFROM:-0400 +TZOFFSETTO:-0500 +DTSTART:19460929T020000 +RRULE:FREQ=YEARLY;UNTIL=19540926T060000Z;BYMONTH=9;BYDAY=-1SU +END:STANDARD +BEGIN:STANDARD +TZNAME:EST +TZOFFSETFROM:-0400 +TZOFFSETTO:-0500 +DTSTART:19551030T020000 +RRULE:FREQ=YEARLY;UNTIL=20061029T060000Z;BYMONTH=10;BYDAY=-1SU +END:STANDARD +BEGIN:DAYLIGHT +TZNAME:EDT +TZOFFSETFROM:-0500 +TZOFFSETTO:-0400 +DTSTART:19740106T020000 +RDATE:19750223T020000 +END:DAYLIGHT +BEGIN:DAYLIGHT +TZNAME:EDT +TZOFFSETFROM:-0500 +TZOFFSETTO:-0400 +DTSTART:19760425T020000 +RRULE:FREQ=YEARLY;UNTIL=19860427T070000Z;BYMONTH=4;BYDAY=-1SU +END:DAYLIGHT +BEGIN:DAYLIGHT +TZNAME:EDT +TZOFFSETFROM:-0500 +TZOFFSETTO:-0400 +DTSTART:19870405T020000 +RRULE:FREQ=YEARLY;UNTIL=20060402T070000Z;BYMONTH=4;BYDAY=1SU +END:DAYLIGHT +BEGIN:DAYLIGHT +TZNAME:EDT +TZOFFSETFROM:-0500 +TZOFFSETTO:-0400 +DTSTART:20070311T020000 +RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=2SU +END:DAYLIGHT +BEGIN:STANDARD +TZNAME:EST +TZOFFSETFROM:-0400 +TZOFFSETTO:-0500 +DTSTART:20071104T020000 +RRULE:FREQ=YEARLY;BYMONTH=11;BYDAY=1SU +END:STANDARD +END:VTIMEZONE diff --git a/src/test/resources/calendar/Europe-Berlin-ical4j4.4.0.ics b/src/test/resources/calendar/Europe-Berlin-ical4j4.4.0.ics new file mode 100644 index 0000000..f4bdfd3 --- /dev/null +++ b/src/test/resources/calendar/Europe-Berlin-ical4j4.4.0.ics @@ -0,0 +1,107 @@ +BEGIN:VTIMEZONE +TZID:Europe/Berlin +LAST-MODIFIED:20250121T082933Z +TZURL:https://www.tzurl.org/zoneinfo/Europe/Berlin +X-LIC-LOCATION:Europe/Berlin +X-PROLEPTIC-TZNAME:LMT +BEGIN:STANDARD +TZNAME:CET +TZOFFSETFROM:+005328 +TZOFFSETTO:+0100 +DTSTART:18930401T000000 +END:STANDARD +BEGIN:DAYLIGHT +TZNAME:CEST +TZOFFSETFROM:+0100 +TZOFFSETTO:+0200 +DTSTART:19160430T230000 +RDATE:19400401T020000 +RDATE:19430329T020000 +RDATE:19460414T020000 +RDATE:19470406T030000 +RDATE:19480418T020000 +RDATE:19490410T020000 +RDATE:19800406T020000 +END:DAYLIGHT +BEGIN:STANDARD +TZNAME:CET +TZOFFSETFROM:+0200 +TZOFFSETTO:+0100 +DTSTART:19161001T010000 +RDATE:19421102T030000 +RDATE:19431004T030000 +RDATE:19441002T030000 +RDATE:19451118T030000 +RDATE:19461007T030000 +END:STANDARD +BEGIN:DAYLIGHT +TZNAME:CEST +TZOFFSETFROM:+0100 +TZOFFSETTO:+0200 +DTSTART:19170416T020000 +RRULE:FREQ=YEARLY;UNTIL=19180415T010000Z;BYMONTH=4;BYDAY=3MO +END:DAYLIGHT +BEGIN:STANDARD +TZNAME:CET +TZOFFSETFROM:+0200 +TZOFFSETTO:+0100 +DTSTART:19170917T030000 +RRULE:FREQ=YEARLY;UNTIL=19180916T010000Z;BYMONTH=9;BYDAY=3MO +END:STANDARD +BEGIN:DAYLIGHT +TZNAME:CEST +TZOFFSETFROM:+0100 +TZOFFSETTO:+0200 +DTSTART:19440403T020000 +RRULE:FREQ=YEARLY;UNTIL=19450402T010000Z;BYMONTH=4;BYDAY=1MO +END:DAYLIGHT +BEGIN:DAYLIGHT +TZNAME:CEMT +TZOFFSETFROM:+0200 +TZOFFSETTO:+0300 +DTSTART:19450524T020000 +RDATE:19470511T030000 +END:DAYLIGHT +BEGIN:DAYLIGHT +TZNAME:CEST +TZOFFSETFROM:+0300 +TZOFFSETTO:+0200 +DTSTART:19450924T030000 +RDATE:19470629T030000 +END:DAYLIGHT +BEGIN:STANDARD +TZNAME:CET +TZOFFSETFROM:+0100 +TZOFFSETTO:+0100 +DTSTART:19460101T000000 +RDATE:19800101T000000 +END:STANDARD +BEGIN:STANDARD +TZNAME:CET +TZOFFSETFROM:+0200 +TZOFFSETTO:+0100 +DTSTART:19471005T030000 +RRULE:FREQ=YEARLY;UNTIL=19491002T010000Z;BYMONTH=10;BYDAY=1SU +END:STANDARD +BEGIN:STANDARD +TZNAME:CET +TZOFFSETFROM:+0200 +TZOFFSETTO:+0100 +DTSTART:19800928T030000 +RRULE:FREQ=YEARLY;UNTIL=19950924T010000Z;BYMONTH=9;BYDAY=-1SU +END:STANDARD +BEGIN:DAYLIGHT +TZNAME:CEST +TZOFFSETFROM:+0100 +TZOFFSETTO:+0200 +DTSTART:19810329T020000 +RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU +END:DAYLIGHT +BEGIN:STANDARD +TZNAME:CET +TZOFFSETFROM:+0200 +TZOFFSETTO:+0100 +DTSTART:19961027T030000 +RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU +END:STANDARD +END:VTIMEZONE diff --git a/src/test/resources/calendar/README.md b/src/test/resources/calendar/README.md new file mode 100644 index 0000000..9327510 --- /dev/null +++ b/src/test/resources/calendar/README.md @@ -0,0 +1,8 @@ +# Calendar semantic fixtures + +The NY/Berlin VTIMEZONE fragments are exact `TimeZoneRegistryImpl.getTimeZone(id).getVTimeZone().toString()` output from the pinned BSD-3-Clause `org.mnode.ical4j:ical4j:4.4.0` artifact, exported on 2026-10-27 with external definition refresh disabled. These are test definitions added by the test helper to original semantic fixtures; production resources are never repaired or supplemented this way. Their embedded TZURL properties are preserved data and are not fetched. Dependency attribution remains in the distributable's iCal4j BSD notice. + +- `America-New_York-ical4j4.4.0.ics`: SHA-256 `9a234fcadd5f04fd9e546bd3cee287151fb7b52ac36e28beb094ab927813d274`. +- `Europe-Berlin-ical4j4.4.0.ics`: SHA-256 `c74bc8ef7d9dca519c5e133f2df7835ff0a93653e99eec0241ac44dce8221eeb`. + +`clients/` contains separately qualified native Thunderbird output and its provenance. Parameter-encoding cases are original fixtures in `CalendarParameterSemanticsTest`. Native-client fixtures are not replaced with library-generated data. diff --git a/src/test/resources/calendar/audit/README.txt b/src/test/resources/calendar/audit/README.txt new file mode 100644 index 0000000..d1dcef6 --- /dev/null +++ b/src/test/resources/calendar/audit/README.txt @@ -0,0 +1,8 @@ +Original independent standards-conformance audit counterexamples and neighboring controls. +Audited revision:923ecacc6b35a77be15b6cdcde5e1b5f2b9f76f0; audit date2026-10-27. +Copied byte-for-byte from audit-evidence.zip/calendar-probes/fixtures and calendar-probes query XML. +The independent input archive SHA256 is bf1549d4b968cd3cd85ad9c255d5b95a8f59214c9e362d551fef39eea524bb09. +SHA256SUMS records the retained original bytes. These files are synthetic, not personal calendar data. +invalid-*.ics are negative controls; valid-leap-second.ics records a separate SHOULD fallback limitation, +not one of the17 mandatory finding groups. future-zone.ics must be queried in its own clean calendar. +The correction record and executed evidence live in the project qualification documentation. diff --git a/src/test/resources/calendar/audit/SHA256SUMS b/src/test/resources/calendar/audit/SHA256SUMS new file mode 100644 index 0000000..9634f25 --- /dev/null +++ b/src/test/resources/calendar/audit/SHA256SUMS @@ -0,0 +1,22 @@ +5d191de9e5674f19d29198230bfc63abe33160f000532bad150ab0ddd385aaa4 categories-query.xml +9423ad7e4f4d4cb2e4d1ef9fa92520f22b5e74c1e2059578abfb8c1ab21412f0 control-event.ics +38800e907cf6d629d7be155b62e12b5a2e315c922ade97f66e4c356471968fbf control-last-ordinary-second.ics +099fe6e44b581169436a493be8a8009dff3dabdfb458b148ad6dea51192fa250 future-query-correct.xml +ca224469fbe3130584b62855eb1b1a332d22af8dccb9644d71d74ac678fe3cde future-query-incorrect.xml +43513a833f5742292a2dcf1f34d0a79b8296168ac1d003de27f58eaadf1cf1ea future-zone.ics +f77d5675354778b01f1fec47428811e9b524bc0cb6cf2a8171d8c5600788c96b invalid-date-hours-duration.ics +07a0790f793295e4ea3727b37ae2a5dcb2e83a57797a924ea8c251b8e2a283f5 invalid-event-end-before-start.ics +327e8a3da4ef58a29c822814dc64aff853e3cdd7cac345520bcbf39087bf8377 invalid-event-negative-duration.ics +0cf8a44b8d40f1856ac93ce024c8e82c6d8251cf732b85097d1dfc31979ba07d invalid-event-no-start.ics +d773bf1f407350928dc6b358e939915031109a9694696de429317a437bf83d0b invalid-rule-count-and-until.ics +9813c71cb96f9270a213c583f11871091a335d15a29e0afa90ee964e0b86102d invalid-rule-repeat-count.ics +b7a280de63eb8b8dbd672758480911650963dbbd6fff8738e223e23004335e93 invalid-rule-repeat-freq.ics +ca6f2950e67a3983c8eff43ba904fa97c7c41687562d7ddb0169479d667339c8 invalid-rule-until-type.ics +a24b223c59e4a1166ae35eba3ef6ff9d26daf9f8919aba12d58e8a747a7ad805 invalid-todo-no-start-duration.ics +9fe2036746eabb208d7f4d3728a79f9a0c7f55da967872cdb4b4de766259f6fe valid-leap-second.ics +8a459bdc4e5d3d3780e7ae07e01fb074d8fe47fe349003024699ee1a23ded7e2 valid-lowercase-delimiters.ics +36148eae78d6392eb0d52d211c14f865291c0588b77ed88e2a1171b3513c3b00 valid-rfc9073-location.ics +b5c7e053b72355214435a644ad70bb90637ad996f4a43ab31ec4e1e49f701b0c valid-rfc9074-alarm.ics +41e6c86d524634bd692f1e8f4417590ca5ec4f54f53695a5af1ac3d5a6117555 valid-rfc9253-relationship.ics +b1b14464a55365b18e3535258792686b58478dc86d74f1b622dc32eb0f3940fd valid-text-list.ics +dd8cfd131ebaa485c6f9fc20566ac49e6d856eae755eb56916a70bb70c1b77a2 valid-unknown-x-properties.ics diff --git a/src/test/resources/calendar/audit/categories-query.xml b/src/test/resources/calendar/audit/categories-query.xml new file mode 100644 index 0000000..0f8c823 --- /dev/null +++ b/src/test/resources/calendar/audit/categories-query.xml @@ -0,0 +1 @@ +alpha;beta \ No newline at end of file diff --git a/src/test/resources/calendar/audit/control-event.ics b/src/test/resources/calendar/audit/control-event.ics new file mode 100644 index 0000000..6ce36ee --- /dev/null +++ b/src/test/resources/calendar/audit/control-event.ics @@ -0,0 +1,9 @@ +BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//IndependentAudit//EN +BEGIN:VEVENT +UID:control-event +DTSTAMP:20261027T000000Z +DTSTART:20261027T120000Z +END:VEVENT +END:VCALENDAR diff --git a/src/test/resources/calendar/audit/control-last-ordinary-second.ics b/src/test/resources/calendar/audit/control-last-ordinary-second.ics new file mode 100644 index 0000000..0bad755 --- /dev/null +++ b/src/test/resources/calendar/audit/control-last-ordinary-second.ics @@ -0,0 +1,9 @@ +BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//IndependentAudit//EN +BEGIN:VEVENT +UID:control-last-ordinary-second +DTSTAMP:20261027T000000Z +DTSTART:19970630T235959Z +END:VEVENT +END:VCALENDAR diff --git a/src/test/resources/calendar/audit/future-query-correct.xml b/src/test/resources/calendar/audit/future-query-correct.xml new file mode 100644 index 0000000..57d8d48 --- /dev/null +++ b/src/test/resources/calendar/audit/future-query-correct.xml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/test/resources/calendar/audit/future-query-incorrect.xml b/src/test/resources/calendar/audit/future-query-incorrect.xml new file mode 100644 index 0000000..032448b --- /dev/null +++ b/src/test/resources/calendar/audit/future-query-incorrect.xml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/test/resources/calendar/audit/future-zone.ics b/src/test/resources/calendar/audit/future-zone.ics new file mode 100644 index 0000000..1ca706e --- /dev/null +++ b/src/test/resources/calendar/audit/future-zone.ics @@ -0,0 +1,31 @@ +BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//IndependentAudit//EN +BEGIN:VTIMEZONE +TZID:AuditRules +BEGIN:STANDARD +DTSTART:20200101T000000 +RRULE:FREQ=YEARLY;BYMONTH=1;BYMONTHDAY=1 +TZOFFSETFROM:+0200 +TZOFFSETTO:+0100 +END:STANDARD +BEGIN:DAYLIGHT +DTSTART:20200601T000000 +RRULE:FREQ=YEARLY;BYMONTH=6;BYMONTHDAY=1;UNTIL=20290531T230000Z +TZOFFSETFROM:+0100 +TZOFFSETTO:+0200 +END:DAYLIGHT +BEGIN:DAYLIGHT +DTSTART:20300301T000000 +RRULE:FREQ=YEARLY;BYMONTH=3;BYMONTHDAY=1 +TZOFFSETFROM:+0100 +TZOFFSETTO:+0200 +END:DAYLIGHT +END:VTIMEZONE +BEGIN:VEVENT +UID:future-zone +DTSTAMP:20261027T000000Z +DTSTART;TZID=AuditRules:20400401T120000 +DURATION:PT1H +END:VEVENT +END:VCALENDAR diff --git a/src/test/resources/calendar/audit/invalid-date-hours-duration.ics b/src/test/resources/calendar/audit/invalid-date-hours-duration.ics new file mode 100644 index 0000000..4288c33 --- /dev/null +++ b/src/test/resources/calendar/audit/invalid-date-hours-duration.ics @@ -0,0 +1,10 @@ +BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//IndependentAudit//EN +BEGIN:VEVENT +UID:invalid-date-hours-duration +DTSTAMP:20261027T000000Z +DTSTART;VALUE=DATE:20261027 +DURATION:PT1H +END:VEVENT +END:VCALENDAR diff --git a/src/test/resources/calendar/audit/invalid-event-end-before-start.ics b/src/test/resources/calendar/audit/invalid-event-end-before-start.ics new file mode 100644 index 0000000..070fa1d --- /dev/null +++ b/src/test/resources/calendar/audit/invalid-event-end-before-start.ics @@ -0,0 +1,10 @@ +BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//IndependentAudit//EN +BEGIN:VEVENT +UID:invalid-event-end-before-start +DTSTAMP:20261027T000000Z +DTSTART:20261027T120000Z +DTEND:20261027T100000Z +END:VEVENT +END:VCALENDAR diff --git a/src/test/resources/calendar/audit/invalid-event-negative-duration.ics b/src/test/resources/calendar/audit/invalid-event-negative-duration.ics new file mode 100644 index 0000000..04de99e --- /dev/null +++ b/src/test/resources/calendar/audit/invalid-event-negative-duration.ics @@ -0,0 +1,10 @@ +BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//IndependentAudit//EN +BEGIN:VEVENT +UID:invalid-event-negative-duration +DTSTAMP:20261027T000000Z +DTSTART:20261027T120000Z +DURATION:-PT1H +END:VEVENT +END:VCALENDAR diff --git a/src/test/resources/calendar/audit/invalid-event-no-start.ics b/src/test/resources/calendar/audit/invalid-event-no-start.ics new file mode 100644 index 0000000..16b380c --- /dev/null +++ b/src/test/resources/calendar/audit/invalid-event-no-start.ics @@ -0,0 +1,8 @@ +BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//IndependentAudit//EN +BEGIN:VEVENT +UID:invalid-event-no-start +DTSTAMP:20261027T000000Z +END:VEVENT +END:VCALENDAR diff --git a/src/test/resources/calendar/audit/invalid-rule-count-and-until.ics b/src/test/resources/calendar/audit/invalid-rule-count-and-until.ics new file mode 100644 index 0000000..acaa3ad --- /dev/null +++ b/src/test/resources/calendar/audit/invalid-rule-count-and-until.ics @@ -0,0 +1,10 @@ +BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//IndependentAudit//EN +BEGIN:VEVENT +UID:invalid-rule-count-and-until +DTSTAMP:20261027T000000Z +DTSTART:20261027T120000Z +RRULE:FREQ=DAILY;COUNT=3;UNTIL=20261029T120000Z +END:VEVENT +END:VCALENDAR diff --git a/src/test/resources/calendar/audit/invalid-rule-repeat-count.ics b/src/test/resources/calendar/audit/invalid-rule-repeat-count.ics new file mode 100644 index 0000000..ecd5812 --- /dev/null +++ b/src/test/resources/calendar/audit/invalid-rule-repeat-count.ics @@ -0,0 +1,10 @@ +BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//IndependentAudit//EN +BEGIN:VEVENT +UID:invalid-rule-repeat-count +DTSTAMP:20261027T000000Z +DTSTART:20261027T120000Z +RRULE:FREQ=DAILY;COUNT=3;COUNT=5 +END:VEVENT +END:VCALENDAR diff --git a/src/test/resources/calendar/audit/invalid-rule-repeat-freq.ics b/src/test/resources/calendar/audit/invalid-rule-repeat-freq.ics new file mode 100644 index 0000000..17b88c6 --- /dev/null +++ b/src/test/resources/calendar/audit/invalid-rule-repeat-freq.ics @@ -0,0 +1,10 @@ +BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//IndependentAudit//EN +BEGIN:VEVENT +UID:invalid-rule-repeat-freq +DTSTAMP:20261027T000000Z +DTSTART:20261027T120000Z +RRULE:FREQ=DAILY;FREQ=YEARLY +END:VEVENT +END:VCALENDAR diff --git a/src/test/resources/calendar/audit/invalid-rule-until-type.ics b/src/test/resources/calendar/audit/invalid-rule-until-type.ics new file mode 100644 index 0000000..91cafeb --- /dev/null +++ b/src/test/resources/calendar/audit/invalid-rule-until-type.ics @@ -0,0 +1,10 @@ +BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//IndependentAudit//EN +BEGIN:VEVENT +UID:invalid-rule-until-type +DTSTAMP:20261027T000000Z +DTSTART;VALUE=DATE:20261027 +RRULE:FREQ=DAILY;UNTIL=20261029T120000Z +END:VEVENT +END:VCALENDAR diff --git a/src/test/resources/calendar/audit/invalid-todo-no-start-duration.ics b/src/test/resources/calendar/audit/invalid-todo-no-start-duration.ics new file mode 100644 index 0000000..370d1dd --- /dev/null +++ b/src/test/resources/calendar/audit/invalid-todo-no-start-duration.ics @@ -0,0 +1,9 @@ +BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//IndependentAudit//EN +BEGIN:VTODO +UID:invalid-todo-no-start-duration +DTSTAMP:20261027T000000Z +DURATION:PT1H +END:VTODO +END:VCALENDAR diff --git a/src/test/resources/calendar/audit/valid-leap-second.ics b/src/test/resources/calendar/audit/valid-leap-second.ics new file mode 100644 index 0000000..dac98c9 --- /dev/null +++ b/src/test/resources/calendar/audit/valid-leap-second.ics @@ -0,0 +1,9 @@ +BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//IndependentAudit//EN +BEGIN:VEVENT +UID:valid-leap-second +DTSTAMP:20261027T000000Z +DTSTART:19970630T235960Z +END:VEVENT +END:VCALENDAR diff --git a/src/test/resources/calendar/audit/valid-lowercase-delimiters.ics b/src/test/resources/calendar/audit/valid-lowercase-delimiters.ics new file mode 100644 index 0000000..b7f5d94 --- /dev/null +++ b/src/test/resources/calendar/audit/valid-lowercase-delimiters.ics @@ -0,0 +1,9 @@ +begin:VCALENDAR +VERSION:2.0 +PRODID:-//IndependentAudit//EN +begin:VEVENT +UID:valid-lowercase-delimiters +DTSTAMP:20261027T000000Z +DTSTART:20261027T120000Z +end:VEVENT +end:VCALENDAR diff --git a/src/test/resources/calendar/audit/valid-rfc9073-location.ics b/src/test/resources/calendar/audit/valid-rfc9073-location.ics new file mode 100644 index 0000000..6dd74a5 --- /dev/null +++ b/src/test/resources/calendar/audit/valid-rfc9073-location.ics @@ -0,0 +1,14 @@ +BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//IndependentAudit//EN +BEGIN:VEVENT +UID:valid-rfc9073-location +DTSTAMP:20261027T000000Z +DTSTART:20261027T120000Z +BEGIN:VLOCATION +UID:location +NAME:Room A +GEO:49.28;-123.12 +END:VLOCATION +END:VEVENT +END:VCALENDAR diff --git a/src/test/resources/calendar/audit/valid-rfc9074-alarm.ics b/src/test/resources/calendar/audit/valid-rfc9074-alarm.ics new file mode 100644 index 0000000..b08414c --- /dev/null +++ b/src/test/resources/calendar/audit/valid-rfc9074-alarm.ics @@ -0,0 +1,16 @@ +BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//IndependentAudit//EN +BEGIN:VEVENT +UID:valid-rfc9074-alarm +DTSTAMP:20261027T000000Z +DTSTART:20261027T120000Z +BEGIN:VALARM +UID:alarm +ACTION:DISPLAY +DESCRIPTION:Reminder +TRIGGER:-PT5M +ACKNOWLEDGED:20261027T115500Z +END:VALARM +END:VEVENT +END:VCALENDAR diff --git a/src/test/resources/calendar/audit/valid-rfc9253-relationship.ics b/src/test/resources/calendar/audit/valid-rfc9253-relationship.ics new file mode 100644 index 0000000..3b5b087 --- /dev/null +++ b/src/test/resources/calendar/audit/valid-rfc9253-relationship.ics @@ -0,0 +1,9 @@ +BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//IndependentAudit//EN +BEGIN:VTODO +UID:valid-rfc9253-relationship +DTSTAMP:20261027T000000Z +RELATED-TO;RELTYPE=DEPENDS-ON:other-task +END:VTODO +END:VCALENDAR diff --git a/src/test/resources/calendar/audit/valid-text-list.ics b/src/test/resources/calendar/audit/valid-text-list.ics new file mode 100644 index 0000000..d4cdcc9 --- /dev/null +++ b/src/test/resources/calendar/audit/valid-text-list.ics @@ -0,0 +1,10 @@ +BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//IndependentAudit//EN +BEGIN:VEVENT +UID:valid-text-list +DTSTAMP:20261027T000000Z +DTSTART:20261027T120000Z +CATEGORIES:alpha\;beta,other +END:VEVENT +END:VCALENDAR diff --git a/src/test/resources/calendar/audit/valid-unknown-x-properties.ics b/src/test/resources/calendar/audit/valid-unknown-x-properties.ics new file mode 100644 index 0000000..15c8ece --- /dev/null +++ b/src/test/resources/calendar/audit/valid-unknown-x-properties.ics @@ -0,0 +1,10 @@ +BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//IndependentAudit//EN +BEGIN:VEVENT +UID:valid-unknown-x-properties +DTSTAMP:20261027T000000Z +DTSTART:20261027T120000Z +X-UNKNOWN;X-QUOTED="a^'b";X-LIST="one,two",three:value\nsecond\,last +END:VEVENT +END:VCALENDAR diff --git a/src/test/resources/calendar/clients/README.md b/src/test/resources/calendar/clients/README.md new file mode 100644 index 0000000..a6c8cba --- /dev/null +++ b/src/test/resources/calendar/clients/README.md @@ -0,0 +1,3 @@ +These two resources were emitted by Mozilla Thunderbird 157.0.1's native CalDAV provider during BusyStack qualification on 2026-10-27. The original synthetic event/task data was created for this repository. The fixtures preserve the provider's exact CRLF output, including Mozilla extension properties. They contain no user data and no Thunderbird source code. + +The reproducible harness is `qualification/clients/thunderbird.py`; its pinned official archive and checksum are selected by `qualification/clients/install-thunderbird.sh`. The event was created, changed through the provider and fetched back from this server. The undated task was created and completed through Thunderbird's VTODO-capable provider. Qualification evidence is generated in `target/qualification/thunderbird.json`. This is native-provider automation, not a manual GUI onboarding claim. diff --git a/src/test/resources/calendar/clients/thunderbird-event.ics b/src/test/resources/calendar/clients/thunderbird-event.ics new file mode 100644 index 0000000..7c7acf1 --- /dev/null +++ b/src/test/resources/calendar/clients/thunderbird-event.ics @@ -0,0 +1,20 @@ +BEGIN:VCALENDAR +PRODID:-//Mozilla.org/NONSGML Mozilla Calendar V1.1//EN +VERSION:2.0 +BEGIN:VEVENT +LAST-MODIFIED:20261027T131810Z +DTSTAMP:20261027T131810Z +UID:thunderbird-event +SUMMARY:Thunderbird changed +RRULE:FREQ=DAILY;COUNT=3 +DTSTART:20261027T100000Z +DTEND:20261027T110000Z +X-NATIVE-CLIENT:preserved +X-MOZ-GENERATION:1 +BEGIN:VALARM +ACTION:DISPLAY +TRIGGER:-PT15M +DESCRIPTION:Stored alarm +END:VALARM +END:VEVENT +END:VCALENDAR diff --git a/src/test/resources/calendar/clients/thunderbird-task.ics b/src/test/resources/calendar/clients/thunderbird-task.ics new file mode 100644 index 0000000..ff22c48 --- /dev/null +++ b/src/test/resources/calendar/clients/thunderbird-task.ics @@ -0,0 +1,15 @@ +BEGIN:VCALENDAR +PRODID:-//Mozilla.org/NONSGML Mozilla Calendar V1.1//EN +VERSION:2.0 +BEGIN:VTODO +LAST-MODIFIED:20261027T131811Z +DTSTAMP:20261027T131811Z +UID:thunderbird-task +SUMMARY:Thunderbird undated task +PRIORITY:2 +STATUS:COMPLETED +COMPLETED:20261027T131811Z +PERCENT-COMPLETE:100 +X-MOZ-GENERATION:1 +END:VTODO +END:VCALENDAR diff --git a/src/test/resources/calendar/independent-verification/README.md b/src/test/resources/calendar/independent-verification/README.md new file mode 100644 index 0000000..a8226a6 --- /dev/null +++ b/src/test/resources/calendar/independent-verification/README.md @@ -0,0 +1,3 @@ +These eight original request bodies are copied byte-for-byte from the independent verification `http-adjacent-results.json` records for submitted revision1328727. The original evidence remains unchanged outside this repository. Invalid and valid controls are preserved separately; successful prior persistence is baseline evidence, not the corrected expectation. Additional ordinal, duration and BOM controls are standards-derived in CalendarAdmissionSyntaxTest. + +Original JSON SHA-256: a417942187365150f31b048a428aa9fdfb0f317ec99b411ea565ec104227147a diff --git a/src/test/resources/calendar/independent-verification/SHA256SUMS b/src/test/resources/calendar/independent-verification/SHA256SUMS new file mode 100644 index 0000000..a46d2e1 --- /dev/null +++ b/src/test/resources/calendar/independent-verification/SHA256SUMS @@ -0,0 +1,8 @@ +583c9f1c74015c247a2fc9f1984d3a39cbdc3204787b2835f872123ab1843f85 invalid-leading-token.ics +c3a7ab821e1c7c0719bc1bb0d7e32c0e15febd998c45401ada941a696539c588 invalid-zero-byday.ics +0f4a606c46c60df0dba43a4e92949eef68619475e3564056991d2f3dfde965b7 invalid-plus-zero-byday.ics +9e34c041065211f332c37e5d8d8483f6575a0051dd0c528eb5869c6ac2e51d1f invalid-weeks-days.ics +eb7906a03820b5d2142ff12532e90c39bebd2afb0d5e6eacf7bc5e9bc128baf7 valid-first-weekday.ics +9cdeab5b081e249dd2a8c4cb5512a3578043902224d298ab472ee3816911b573 valid-last-weekday.ics +c00d39f8e4300f803de0753388b18acfd7d97c105a28911cfc71befbc038d496 valid-one-week.ics +a9a696e3fb8854e17a5a6a44dcbe61d2aed1b5b984430f8ae73e9552712ce209 valid-nine-days.ics diff --git a/src/test/resources/calendar/independent-verification/invalid-leading-token.ics b/src/test/resources/calendar/independent-verification/invalid-leading-token.ics new file mode 100644 index 0000000..088d702 --- /dev/null +++ b/src/test/resources/calendar/independent-verification/invalid-leading-token.ics @@ -0,0 +1,9 @@ +XBEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//Independent Verification//EN +BEGIN:VEVENT +UID:invalid-leading-token +DTSTAMP:20261028T000000Z +DTSTART:20261101T120000Z +END:VEVENT +END:VCALENDAR diff --git a/src/test/resources/calendar/independent-verification/invalid-plus-zero-byday.ics b/src/test/resources/calendar/independent-verification/invalid-plus-zero-byday.ics new file mode 100644 index 0000000..ab0b8c4 --- /dev/null +++ b/src/test/resources/calendar/independent-verification/invalid-plus-zero-byday.ics @@ -0,0 +1,10 @@ +BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//Independent Verification//EN +BEGIN:VEVENT +UID:invalid-plus-zero-byday +DTSTAMP:20261028T000000Z +DTSTART:20261101T120000Z +RRULE:FREQ=MONTHLY;BYDAY=+0MO +END:VEVENT +END:VCALENDAR diff --git a/src/test/resources/calendar/independent-verification/invalid-weeks-days.ics b/src/test/resources/calendar/independent-verification/invalid-weeks-days.ics new file mode 100644 index 0000000..377079e --- /dev/null +++ b/src/test/resources/calendar/independent-verification/invalid-weeks-days.ics @@ -0,0 +1,10 @@ +BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//Independent Verification//EN +BEGIN:VEVENT +UID:invalid-weeks-days +DTSTAMP:20261028T000000Z +DTSTART:20261101T120000Z +DURATION:P1W2D +END:VEVENT +END:VCALENDAR diff --git a/src/test/resources/calendar/independent-verification/invalid-zero-byday.ics b/src/test/resources/calendar/independent-verification/invalid-zero-byday.ics new file mode 100644 index 0000000..9621f3f --- /dev/null +++ b/src/test/resources/calendar/independent-verification/invalid-zero-byday.ics @@ -0,0 +1,10 @@ +BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//Independent Verification//EN +BEGIN:VEVENT +UID:invalid-zero-byday +DTSTAMP:20261028T000000Z +DTSTART:20261101T120000Z +RRULE:FREQ=MONTHLY;BYDAY=0MO +END:VEVENT +END:VCALENDAR diff --git a/src/test/resources/calendar/independent-verification/valid-first-weekday.ics b/src/test/resources/calendar/independent-verification/valid-first-weekday.ics new file mode 100644 index 0000000..14271c1 --- /dev/null +++ b/src/test/resources/calendar/independent-verification/valid-first-weekday.ics @@ -0,0 +1,10 @@ +BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//Independent Verification//EN +BEGIN:VEVENT +UID:valid-first-weekday +DTSTAMP:20261028T000000Z +DTSTART:20261101T120000Z +RRULE:FREQ=MONTHLY;BYDAY=1MO;COUNT=2 +END:VEVENT +END:VCALENDAR diff --git a/src/test/resources/calendar/independent-verification/valid-last-weekday.ics b/src/test/resources/calendar/independent-verification/valid-last-weekday.ics new file mode 100644 index 0000000..9a572e9 --- /dev/null +++ b/src/test/resources/calendar/independent-verification/valid-last-weekday.ics @@ -0,0 +1,10 @@ +BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//Independent Verification//EN +BEGIN:VEVENT +UID:valid-last-weekday +DTSTAMP:20261028T000000Z +DTSTART:20261101T120000Z +RRULE:FREQ=MONTHLY;BYDAY=-1MO;COUNT=2 +END:VEVENT +END:VCALENDAR diff --git a/src/test/resources/calendar/independent-verification/valid-nine-days.ics b/src/test/resources/calendar/independent-verification/valid-nine-days.ics new file mode 100644 index 0000000..90cdb1f --- /dev/null +++ b/src/test/resources/calendar/independent-verification/valid-nine-days.ics @@ -0,0 +1,10 @@ +BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//Independent Verification//EN +BEGIN:VEVENT +UID:valid-nine-days +DTSTAMP:20261028T000000Z +DTSTART:20261101T120000Z +DURATION:P9D +END:VEVENT +END:VCALENDAR diff --git a/src/test/resources/calendar/independent-verification/valid-one-week.ics b/src/test/resources/calendar/independent-verification/valid-one-week.ics new file mode 100644 index 0000000..7ffea7f --- /dev/null +++ b/src/test/resources/calendar/independent-verification/valid-one-week.ics @@ -0,0 +1,10 @@ +BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//Independent Verification//EN +BEGIN:VEVENT +UID:valid-one-week +DTSTAMP:20261028T000000Z +DTSTART:20261101T120000Z +DURATION:P1W +END:VEVENT +END:VCALENDAR diff --git a/src/test/resources/calendar/observance-until/README.md b/src/test/resources/calendar/observance-until/README.md new file mode 100644 index 0000000..29452ed --- /dev/null +++ b/src/test/resources/calendar/observance-until/README.md @@ -0,0 +1,14 @@ +# Captured Thunderbird input (immutable test fixtures) + +These two files are byte-for-byte copies of Thunderbird 159.0 editor uploads retained in +`~/busystack/caldav-server-timezone-verification-20261030/thunderbird159/cases/`: + +* `system-vancouver/evidence/creation-put-1.ics` → `thunderbird159-vancouver.ics` +* `explicit-berlin/evidence/creation-put-1.ics` → `thunderbird159-berlin.ics` + +They intentionally contain invalid local observance UNTIL values. Strict parsing must still +reject them. Admission with the narrow default repair must produce valid corrected bytes; +tests verify all six Vancouver and seven Berlin boundaries against local-onset recurrences +using each observance's TZOFFSETFROM. The original external evidence is unchanged. + +The fixtures are client-generated synthetic events, not host IANA timezone replacements. diff --git a/src/test/resources/calendar/observance-until/SHA256SUMS b/src/test/resources/calendar/observance-until/SHA256SUMS new file mode 100644 index 0000000..0cc74cf --- /dev/null +++ b/src/test/resources/calendar/observance-until/SHA256SUMS @@ -0,0 +1,2 @@ +6b884299d3e11a8c7856895b10743f92862b0ec0f273fe9e46cea2287ced237d thunderbird159-berlin.ics +3f9aa2efae75716aa40272fe96534fb9cb87af3068c8b8cac3fc6d9741d3387d thunderbird159-vancouver.ics diff --git a/src/test/resources/calendar/observance-until/thunderbird159-berlin.ics b/src/test/resources/calendar/observance-until/thunderbird159-berlin.ics new file mode 100644 index 0000000..b5d7fe5 --- /dev/null +++ b/src/test/resources/calendar/observance-until/thunderbird159-berlin.ics @@ -0,0 +1,208 @@ +BEGIN:VCALENDAR +PRODID:-//Mozilla.org/NONSGML Mozilla Calendar V1.1//EN +VERSION:2.0 +BEGIN:VTIMEZONE +TZID:Europe/Berlin +X-TZINFO:Europe/Berlin[2026e] +BEGIN:STANDARD +TZOFFSETTO:+010000 +TZOFFSETFROM:+005328 +TZNAME:Europe/Berlin(STD) +DTSTART:18930401T000000 +RDATE:18930401T000000 +END:STANDARD +BEGIN:DAYLIGHT +TZOFFSETTO:+020000 +TZOFFSETFROM:+010000 +TZNAME:Europe/Berlin(DST) +DTSTART:19160430T230000 +RDATE:19160430T230000 +END:DAYLIGHT +BEGIN:STANDARD +TZOFFSETTO:+010000 +TZOFFSETFROM:+020000 +TZNAME:Europe/Berlin(STD) +DTSTART:19161001T010000 +RDATE:19161001T010000 +END:STANDARD +BEGIN:DAYLIGHT +TZOFFSETTO:+020000 +TZOFFSETFROM:+010000 +TZNAME:Europe/Berlin(DST) +DTSTART:19170416T020000 +RRULE:FREQ=YEARLY;BYMONTH=4;BYDAY=3MO;UNTIL=19180415T020000 +END:DAYLIGHT +BEGIN:STANDARD +TZOFFSETTO:+010000 +TZOFFSETFROM:+020000 +TZNAME:Europe/Berlin(STD) +DTSTART:19170917T030000 +RRULE:FREQ=YEARLY;BYMONTH=9;BYDAY=3MO;UNTIL=19180916T030000 +END:STANDARD +BEGIN:DAYLIGHT +TZOFFSETTO:+020000 +TZOFFSETFROM:+010000 +TZNAME:Europe/Berlin(DST) +DTSTART:19400401T020000 +RDATE:19400401T020000 +END:DAYLIGHT +BEGIN:STANDARD +TZOFFSETTO:+010000 +TZOFFSETFROM:+020000 +TZNAME:Europe/Berlin(STD) +DTSTART:19421102T030000 +RDATE:19421102T030000 +END:STANDARD +BEGIN:DAYLIGHT +TZOFFSETTO:+020000 +TZOFFSETFROM:+010000 +TZNAME:Europe/Berlin(DST) +DTSTART:19430329T020000 +RDATE:19430329T020000 +END:DAYLIGHT +BEGIN:DAYLIGHT +TZOFFSETTO:+020000 +TZOFFSETFROM:+010000 +TZNAME:Europe/Berlin(DST) +DTSTART:19440403T020000 +RRULE:FREQ=YEARLY;BYMONTH=4;BYDAY=1MO;UNTIL=19450402T020000 +END:DAYLIGHT +BEGIN:DAYLIGHT +TZOFFSETTO:+030000 +TZOFFSETFROM:+020000 +TZNAME:Europe/Berlin(DST) +DTSTART:19450524T020000 +RDATE:19450524T020000 +END:DAYLIGHT +BEGIN:STANDARD +TZOFFSETTO:+010000 +TZOFFSETFROM:+020000 +TZNAME:Europe/Berlin(STD) +DTSTART:19431004T030000 +RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=1MO;UNTIL=19441002T030000 +END:STANDARD +BEGIN:DAYLIGHT +TZOFFSETTO:+020000 +TZOFFSETFROM:+030000 +TZNAME:Europe/Berlin(DST) +DTSTART:19450924T030000 +RDATE:19450924T030000 +END:DAYLIGHT +BEGIN:STANDARD +TZOFFSETTO:+010000 +TZOFFSETFROM:+020000 +TZNAME:Europe/Berlin(STD) +DTSTART:19451118T030000 +RDATE:19451118T030000 +END:STANDARD +BEGIN:DAYLIGHT +TZOFFSETTO:+020000 +TZOFFSETFROM:+010000 +TZNAME:Europe/Berlin(DST) +DTSTART:19460414T020000 +RDATE:19460414T020000 +END:DAYLIGHT +BEGIN:DAYLIGHT +TZOFFSETTO:+020000 +TZOFFSETFROM:+010000 +TZNAME:Europe/Berlin(DST) +DTSTART:19470406T030000 +RDATE:19470406T030000 +END:DAYLIGHT +BEGIN:DAYLIGHT +TZOFFSETTO:+030000 +TZOFFSETFROM:+020000 +TZNAME:Europe/Berlin(DST) +DTSTART:19470511T030000 +RDATE:19470511T030000 +END:DAYLIGHT +BEGIN:STANDARD +TZOFFSETTO:+010000 +TZOFFSETFROM:+020000 +TZNAME:Europe/Berlin(STD) +DTSTART:19461007T030000 +RDATE:19461007T030000 +END:STANDARD +BEGIN:DAYLIGHT +TZOFFSETTO:+020000 +TZOFFSETFROM:+030000 +TZNAME:Europe/Berlin(DST) +DTSTART:19470629T030000 +RDATE:19470629T030000 +END:DAYLIGHT +BEGIN:DAYLIGHT +TZOFFSETTO:+020000 +TZOFFSETFROM:+010000 +TZNAME:Europe/Berlin(DST) +DTSTART:19480418T020000 +RDATE:19480418T020000 +END:DAYLIGHT +BEGIN:DAYLIGHT +TZOFFSETTO:+020000 +TZOFFSETFROM:+010000 +TZNAME:Europe/Berlin(DST) +DTSTART:19490410T020000 +RDATE:19490410T020000 +END:DAYLIGHT +BEGIN:STANDARD +TZOFFSETTO:+010000 +TZOFFSETFROM:+020000 +TZNAME:Europe/Berlin(STD) +DTSTART:19471005T030000 +RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=1SU;UNTIL=19491002T030000 +END:STANDARD +BEGIN:DAYLIGHT +TZOFFSETTO:+020000 +TZOFFSETFROM:+010000 +TZNAME:Europe/Berlin(DST) +DTSTART:19800406T020000 +RDATE:19800406T020000 +END:DAYLIGHT +BEGIN:STANDARD +TZOFFSETTO:+010000 +TZOFFSETFROM:+020000 +TZNAME:Europe/Berlin(STD) +DTSTART:19800928T030000 +RRULE:FREQ=YEARLY;BYMONTH=9;BYDAY=-1SU;UNTIL=19950924T030000 +END:STANDARD +BEGIN:DAYLIGHT +TZOFFSETTO:+020000 +TZOFFSETFROM:+010000 +TZNAME:Europe/Berlin(DST) +DTSTART:19810329T020000 +RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU;UNTIL=19960331T020000 +END:DAYLIGHT +BEGIN:STANDARD +TZOFFSETTO:+010000 +TZOFFSETFROM:+020000 +TZNAME:Europe/Berlin(STD) +DTSTART:19961027T030000 +RDATE:19961027T030000 +END:STANDARD +BEGIN:DAYLIGHT +TZOFFSETTO:+020000 +TZOFFSETFROM:+010000 +TZNAME:(DST) +DTSTART:19970330T020000 +RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU +END:DAYLIGHT +BEGIN:STANDARD +TZOFFSETTO:+010000 +TZOFFSETFROM:+020000 +TZNAME:(STD) +DTSTART:19971026T030000 +RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU +END:STANDARD +END:VTIMEZONE +BEGIN:VEVENT +CREATED:20261030T114530Z +LAST-MODIFIED:20261030T114532Z +DTSTAMP:20261030T114532Z +UID:fb3aa85d-5151-4c59-afda-22d46d4f03da +SUMMARY:TB159 explicit-berlin +DTSTART;TZID=Europe/Berlin:20261109T100000 +DTEND;TZID=Europe/Berlin:20261109T110000 +TRANSP:OPAQUE +LOCATION:Isolated interoperability probe +END:VEVENT +END:VCALENDAR diff --git a/src/test/resources/calendar/observance-until/thunderbird159-vancouver.ics b/src/test/resources/calendar/observance-until/thunderbird159-vancouver.ics new file mode 100644 index 0000000..98d24da --- /dev/null +++ b/src/test/resources/calendar/observance-until/thunderbird159-vancouver.ics @@ -0,0 +1,89 @@ +BEGIN:VCALENDAR +PRODID:-//Mozilla.org/NONSGML Mozilla Calendar V1.1//EN +VERSION:2.0 +BEGIN:VTIMEZONE +TZID:America/Vancouver +X-TZINFO:America/Vancouver[2026e] +BEGIN:STANDARD +TZOFFSETTO:-080000 +TZOFFSETFROM:-081228 +TZNAME:America/Vancouver(STD) +DTSTART:18840101T000000 +RDATE:18840101T000000 +END:STANDARD +BEGIN:DAYLIGHT +TZOFFSETTO:-070000 +TZOFFSETFROM:-080000 +TZNAME:America/Vancouver(DST) +DTSTART:19180414T020000 +RDATE:19180414T020000 +END:DAYLIGHT +BEGIN:STANDARD +TZOFFSETTO:-080000 +TZOFFSETFROM:-070000 +TZNAME:America/Vancouver(STD) +DTSTART:19181027T020000 +RDATE:19181027T020000 +END:STANDARD +BEGIN:DAYLIGHT +TZOFFSETTO:-070000 +TZOFFSETFROM:-080000 +TZNAME:America/Vancouver(DST) +DTSTART:19420209T020000 +RDATE:19420209T020000 +END:DAYLIGHT +BEGIN:STANDARD +TZOFFSETTO:-080000 +TZOFFSETFROM:-070000 +TZNAME:America/Vancouver(STD) +DTSTART:19450930T020000 +RRULE:FREQ=YEARLY;BYMONTH=9;BYDAY=-1SU;UNTIL=19610924T020000 +END:STANDARD +BEGIN:DAYLIGHT +TZOFFSETTO:-070000 +TZOFFSETFROM:-080000 +TZNAME:America/Vancouver(DST) +DTSTART:19460428T020000 +RRULE:FREQ=YEARLY;BYMONTH=4;BYDAY=-1SU;UNTIL=19860427T020000 +END:DAYLIGHT +BEGIN:DAYLIGHT +TZOFFSETTO:-070000 +TZOFFSETFROM:-080000 +TZNAME:America/Vancouver(DST) +DTSTART:19870405T020000 +RRULE:FREQ=YEARLY;BYMONTH=4;BYDAY=1SU;UNTIL=20060402T020000 +END:DAYLIGHT +BEGIN:STANDARD +TZOFFSETTO:-080000 +TZOFFSETFROM:-070000 +TZNAME:America/Vancouver(STD) +DTSTART:19621028T020000 +RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU;UNTIL=20061029T020000 +END:STANDARD +BEGIN:DAYLIGHT +TZOFFSETTO:-070000 +TZOFFSETFROM:-080000 +TZNAME:America/Vancouver(DST) +DTSTART:20070311T020000 +RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=2SU;UNTIL=20260308T020000 +END:DAYLIGHT +BEGIN:STANDARD +TZOFFSETTO:-080000 +TZOFFSETFROM:-070000 +TZNAME:America/Vancouver(STD) +DTSTART:20071104T020000 +RRULE:FREQ=YEARLY;BYMONTH=11;BYDAY=1SU;UNTIL=20251102T020000 +END:STANDARD +END:VTIMEZONE +BEGIN:VEVENT +CREATED:20261030T114237Z +LAST-MODIFIED:20261030T114239Z +DTSTAMP:20261030T114239Z +UID:d31b425d-4da9-48f8-bacd-15ea6ab2c7f8 +SUMMARY:TB159 system-vancouver +DTSTART;TZID=America/Vancouver:20261109T100000 +DTEND;TZID=America/Vancouver:20261109T110000 +TRANSP:OPAQUE +LOCATION:Isolated interoperability probe +END:VEVENT +END:VCALENDAR diff --git a/src/test/resources/testcontainers.properties b/src/test/resources/testcontainers.properties new file mode 100644 index 0000000..6a35be0 --- /dev/null +++ b/src/test/resources/testcontainers.properties @@ -0,0 +1 @@ +ryuk.container.image=testcontainers/ryuk:0.14.0@sha256:7c1a8a9a47c780ed0f983770a662f80deb115d95cce3e2daa3d12115b8cd28f0