@cosmicdrift/kumiko-framework 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +159 -0
- package/package.json +91 -0
- package/src/__tests__/anonymous-access.integration.ts +325 -0
- package/src/__tests__/error-contract.integration.ts +435 -0
- package/src/__tests__/field-access.integration.ts +269 -0
- package/src/__tests__/full-stack.integration.ts +914 -0
- package/src/__tests__/ownership.integration.ts +449 -0
- package/src/__tests__/reference-data.integration.ts +198 -0
- package/src/__tests__/transition-guard.integration.ts +340 -0
- package/src/api/__tests__/api.test.ts +337 -0
- package/src/api/__tests__/auth-middleware-transport.test.ts +80 -0
- package/src/api/__tests__/auth-routes-cookie.test.ts +179 -0
- package/src/api/__tests__/batch.integration.ts +404 -0
- package/src/api/__tests__/body-limit.test.ts +88 -0
- package/src/api/__tests__/csrf-middleware.test.ts +97 -0
- package/src/api/__tests__/dispatcher-live.integration.ts +216 -0
- package/src/api/__tests__/metrics-endpoint.test.ts +126 -0
- package/src/api/__tests__/nested-write.integration.ts +213 -0
- package/src/api/__tests__/readiness.test.ts +76 -0
- package/src/api/__tests__/request-id-middleware.test.ts +72 -0
- package/src/api/__tests__/sse-broker.test.ts +58 -0
- package/src/api/__tests__/sse-route.test.ts +112 -0
- package/src/api/anonymous-cookie.ts +60 -0
- package/src/api/api-constants.ts +64 -0
- package/src/api/auth-middleware.ts +418 -0
- package/src/api/auth-routes.ts +982 -0
- package/src/api/csrf-middleware.ts +77 -0
- package/src/api/index.ts +31 -0
- package/src/api/jwt.ts +66 -0
- package/src/api/observability-middleware.ts +89 -0
- package/src/api/readiness.ts +132 -0
- package/src/api/request-context.ts +49 -0
- package/src/api/request-id-middleware.ts +50 -0
- package/src/api/route-registrars.ts +195 -0
- package/src/api/routes.ts +135 -0
- package/src/api/server.ts +640 -0
- package/src/api/sse-broker.ts +71 -0
- package/src/api/sse-route.ts +62 -0
- package/src/api/tokens.ts +16 -0
- package/src/db/__tests__/apply-entity-event-tenant.integration.ts +159 -0
- package/src/db/__tests__/compound-types.test.ts +114 -0
- package/src/db/__tests__/connection-options.test.ts +68 -0
- package/src/db/__tests__/cursor.test.ts +41 -0
- package/src/db/__tests__/db-helpers.test.ts +369 -0
- package/src/db/__tests__/dialect-instant.test.ts +50 -0
- package/src/db/__tests__/drizzle-helpers.integration.ts +186 -0
- package/src/db/__tests__/drizzle-table-types.test.ts +162 -0
- package/src/db/__tests__/encryption.test.ts +39 -0
- package/src/db/__tests__/event-store-executor-list.integration.ts +313 -0
- package/src/db/__tests__/event-store-executor.integration.ts +235 -0
- package/src/db/__tests__/implicit-projection-equivalence.integration.ts +304 -0
- package/src/db/__tests__/located-timestamp.test.ts +184 -0
- package/src/db/__tests__/money.test.ts +199 -0
- package/src/db/__tests__/multi-row-insert.integration.ts +76 -0
- package/src/db/__tests__/parse-auto-verb.test.ts +70 -0
- package/src/db/__tests__/required-not-null-migration-safety.integration.ts +105 -0
- package/src/db/__tests__/row-helpers.test.ts +59 -0
- package/src/db/__tests__/schema-migration.integration.ts +273 -0
- package/src/db/__tests__/table-builder-indexes.test.ts +153 -0
- package/src/db/__tests__/table-builder-required.test.ts +216 -0
- package/src/db/__tests__/tenant-db.integration.ts +606 -0
- package/src/db/__tests__/unique-violation-mapping.integration.ts +166 -0
- package/src/db/apply-entity-event.ts +188 -0
- package/src/db/assert-exists-in.ts +59 -0
- package/src/db/compound-types.ts +47 -0
- package/src/db/connection.ts +104 -0
- package/src/db/cursor.ts +83 -0
- package/src/db/dialect.ts +109 -0
- package/src/db/eagerload.ts +174 -0
- package/src/db/encryption.ts +39 -0
- package/src/db/event-store-executor.ts +906 -0
- package/src/db/index.ts +55 -0
- package/src/db/located-timestamp.ts +114 -0
- package/src/db/money.ts +120 -0
- package/src/db/pg-error.ts +46 -0
- package/src/db/reference-data.ts +77 -0
- package/src/db/row-helpers.ts +53 -0
- package/src/db/schema-inspection.ts +25 -0
- package/src/db/table-builder.ts +475 -0
- package/src/db/tenant-db.ts +434 -0
- package/src/engine/__tests__/auth-claims-registrar.test.ts +74 -0
- package/src/engine/__tests__/boot-validator-located-timestamps.test.ts +108 -0
- package/src/engine/__tests__/boot-validator.test.ts +1865 -0
- package/src/engine/__tests__/build-app-schema.test.ts +154 -0
- package/src/engine/__tests__/claim-keys.test.ts +274 -0
- package/src/engine/__tests__/config-helpers.test.ts +236 -0
- package/src/engine/__tests__/effective-features.test.ts +86 -0
- package/src/engine/__tests__/engine.test.ts +1461 -0
- package/src/engine/__tests__/entity-handlers.test.ts +274 -0
- package/src/engine/__tests__/event-helpers.test.ts +68 -0
- package/src/engine/__tests__/extends-registrar.test.ts +159 -0
- package/src/engine/__tests__/factories-long-text.test.ts +84 -0
- package/src/engine/__tests__/factories-time.test.ts +158 -0
- package/src/engine/__tests__/field-predicates.test.ts +48 -0
- package/src/engine/__tests__/hook-phases.test.ts +132 -0
- package/src/engine/__tests__/identifiers.test.ts +35 -0
- package/src/engine/__tests__/lifecycle-hooks.test.ts +237 -0
- package/src/engine/__tests__/nav.test.ts +267 -0
- package/src/engine/__tests__/ownership.test.ts +421 -0
- package/src/engine/__tests__/parse-ref-target.test.ts +43 -0
- package/src/engine/__tests__/projection-helpers.test.ts +62 -0
- package/src/engine/__tests__/projection.test.ts +191 -0
- package/src/engine/__tests__/qualified-name.test.ts +264 -0
- package/src/engine/__tests__/resolve-config-or-param.test.ts +315 -0
- package/src/engine/__tests__/run-in.test.ts +38 -0
- package/src/engine/__tests__/schema-builder.test.ts +380 -0
- package/src/engine/__tests__/screen.test.ts +408 -0
- package/src/engine/__tests__/state-machine.test.ts +148 -0
- package/src/engine/__tests__/system-user.test.ts +57 -0
- package/src/engine/__tests__/validation-hooks.test.ts +71 -0
- package/src/engine/access.ts +23 -0
- package/src/engine/boot-validator.ts +1528 -0
- package/src/engine/build-app-schema.ts +125 -0
- package/src/engine/config-helpers.ts +115 -0
- package/src/engine/constants.ts +85 -0
- package/src/engine/create-app.ts +98 -0
- package/src/engine/define-feature.ts +702 -0
- package/src/engine/define-handler.ts +78 -0
- package/src/engine/define-roles.ts +19 -0
- package/src/engine/effective-features.ts +87 -0
- package/src/engine/entity-handlers.ts +364 -0
- package/src/engine/event-helpers.ts +73 -0
- package/src/engine/factories.ts +328 -0
- package/src/engine/feature-ast/__tests__/canonical-form.test.ts +416 -0
- package/src/engine/feature-ast/__tests__/parse-happy-path.test.ts +197 -0
- package/src/engine/feature-ast/__tests__/parse-real-features.test.ts +128 -0
- package/src/engine/feature-ast/__tests__/parse.test.ts +888 -0
- package/src/engine/feature-ast/__tests__/patch.test.ts +360 -0
- package/src/engine/feature-ast/__tests__/patcher.test.ts +469 -0
- package/src/engine/feature-ast/__tests__/render-roundtrip.test.ts +287 -0
- package/src/engine/feature-ast/extractors.ts +2562 -0
- package/src/engine/feature-ast/index.ts +105 -0
- package/src/engine/feature-ast/parse.ts +369 -0
- package/src/engine/feature-ast/patch.ts +525 -0
- package/src/engine/feature-ast/patcher.ts +518 -0
- package/src/engine/feature-ast/patterns.ts +434 -0
- package/src/engine/feature-ast/render.ts +602 -0
- package/src/engine/feature-ast/source-location.ts +45 -0
- package/src/engine/field-access.ts +120 -0
- package/src/engine/index.ts +254 -0
- package/src/engine/ownership.ts +337 -0
- package/src/engine/parse-ref-target.ts +22 -0
- package/src/engine/pattern-library/__tests__/library.test.ts +351 -0
- package/src/engine/pattern-library/index.ts +24 -0
- package/src/engine/pattern-library/library.ts +1117 -0
- package/src/engine/pattern-library/types.ts +255 -0
- package/src/engine/projection-helpers.ts +85 -0
- package/src/engine/qualified-name.ts +122 -0
- package/src/engine/read-claim.ts +31 -0
- package/src/engine/registry.ts +1325 -0
- package/src/engine/resolve-config-or-param.ts +153 -0
- package/src/engine/run-in.ts +29 -0
- package/src/engine/schema-builder.ts +175 -0
- package/src/engine/screen-filter-ops.ts +51 -0
- package/src/engine/state-machine.ts +70 -0
- package/src/engine/system-user.ts +32 -0
- package/src/engine/types/config.ts +306 -0
- package/src/engine/types/event-type-map.ts +37 -0
- package/src/engine/types/feature.ts +574 -0
- package/src/engine/types/fields.ts +422 -0
- package/src/engine/types/handlers.ts +742 -0
- package/src/engine/types/hooks.ts +142 -0
- package/src/engine/types/http-route.ts +54 -0
- package/src/engine/types/identifiers.ts +47 -0
- package/src/engine/types/index.ts +208 -0
- package/src/engine/types/nav.ts +46 -0
- package/src/engine/types/projection.ts +132 -0
- package/src/engine/types/relations.ts +51 -0
- package/src/engine/types/screen.ts +452 -0
- package/src/engine/types/workspace.ts +42 -0
- package/src/engine/validation.ts +33 -0
- package/src/entrypoint/__tests__/entrypoint-job-wiring.integration.ts +173 -0
- package/src/entrypoint/__tests__/split-deploy.integration.ts +297 -0
- package/src/entrypoint/index.ts +442 -0
- package/src/errors/__tests__/classes.test.ts +371 -0
- package/src/errors/__tests__/write-failures.test.ts +109 -0
- package/src/errors/classes.ts +249 -0
- package/src/errors/i18n/de.yaml +83 -0
- package/src/errors/i18n/en.yaml +80 -0
- package/src/errors/index.ts +41 -0
- package/src/errors/kumiko-error.ts +67 -0
- package/src/errors/reasons.ts +36 -0
- package/src/errors/serialize.ts +136 -0
- package/src/errors/transition-details.ts +30 -0
- package/src/errors/write-error-info.ts +123 -0
- package/src/errors/zod-bridge.ts +49 -0
- package/src/event-store/__tests__/admin-api.integration.ts +361 -0
- package/src/event-store/__tests__/event-store.integration.ts +584 -0
- package/src/event-store/__tests__/get-stream-version-perf.integration.ts +83 -0
- package/src/event-store/__tests__/perf.integration.ts +255 -0
- package/src/event-store/__tests__/snapshot.integration.ts +267 -0
- package/src/event-store/__tests__/upcaster-dead-letter.integration.ts +204 -0
- package/src/event-store/__tests__/upcaster.integration.ts +460 -0
- package/src/event-store/admin-api.ts +257 -0
- package/src/event-store/archive.ts +106 -0
- package/src/event-store/errors.ts +35 -0
- package/src/event-store/event-store.ts +405 -0
- package/src/event-store/events-schema.ts +90 -0
- package/src/event-store/index.ts +50 -0
- package/src/event-store/snapshot.ts +210 -0
- package/src/event-store/upcaster-dead-letter.ts +119 -0
- package/src/event-store/upcaster.ts +147 -0
- package/src/files/__tests__/content-disposition.test.ts +123 -0
- package/src/files/__tests__/file-field-column.integration.ts +103 -0
- package/src/files/__tests__/file-field-pipeline.integration.ts +211 -0
- package/src/files/__tests__/file-handle.test.ts +122 -0
- package/src/files/__tests__/files.integration.ts +830 -0
- package/src/files/__tests__/storage-tracking.integration.ts +153 -0
- package/src/files/content-disposition.ts +55 -0
- package/src/files/file-handle.ts +63 -0
- package/src/files/file-ref-table.ts +22 -0
- package/src/files/file-routes.ts +353 -0
- package/src/files/in-memory-provider.ts +62 -0
- package/src/files/index.ts +29 -0
- package/src/files/local-provider.ts +35 -0
- package/src/files/storage-tracking.ts +60 -0
- package/src/files/types.ts +118 -0
- package/src/i18n/__tests__/i18n.test.ts +72 -0
- package/src/i18n/index.ts +29 -0
- package/src/jobs/__tests__/job-event-trigger.integration.ts +172 -0
- package/src/jobs/__tests__/job-multi-trigger.integration.ts +144 -0
- package/src/jobs/__tests__/jobs.integration.ts +566 -0
- package/src/jobs/index.ts +2 -0
- package/src/jobs/job-runner.ts +574 -0
- package/src/lifecycle/__tests__/create-test-lifecycle.ts +19 -0
- package/src/lifecycle/__tests__/lifecycle-server.integration.ts +108 -0
- package/src/lifecycle/__tests__/lifecycle.test.ts +212 -0
- package/src/lifecycle/__tests__/signal-handlers.test.ts +106 -0
- package/src/lifecycle/index.ts +13 -0
- package/src/lifecycle/lifecycle.ts +160 -0
- package/src/lifecycle/signal-handlers.ts +62 -0
- package/src/logging/__tests__/pino-trace-bridge.test.ts +50 -0
- package/src/logging/index.ts +3 -0
- package/src/logging/pino-logger.ts +64 -0
- package/src/logging/types.ts +7 -0
- package/src/migrations/__tests__/compare-snapshots.test.ts +150 -0
- package/src/migrations/__tests__/detect-drift.integration.ts +320 -0
- package/src/migrations/__tests__/detect-projections-to-rebuild.integration.ts +134 -0
- package/src/migrations/__tests__/rebuild-marker.test.ts +79 -0
- package/src/migrations/index.ts +28 -0
- package/src/migrations/projection-detection.ts +149 -0
- package/src/migrations/rebuild-marker.ts +64 -0
- package/src/migrations/schema-drift.ts +395 -0
- package/src/observability/__tests__/console-provider.test.ts +67 -0
- package/src/observability/__tests__/metric-validator.test.ts +87 -0
- package/src/observability/__tests__/noop-provider.test.ts +82 -0
- package/src/observability/__tests__/observability.integration.ts +559 -0
- package/src/observability/__tests__/prometheus-meter.test.ts +144 -0
- package/src/observability/__tests__/recording-meter.test.ts +101 -0
- package/src/observability/__tests__/recording-tracer.test.ts +110 -0
- package/src/observability/__tests__/sensitive-filter.test.ts +98 -0
- package/src/observability/console-provider.ts +130 -0
- package/src/observability/context.ts +26 -0
- package/src/observability/fallback.ts +34 -0
- package/src/observability/ids.ts +25 -0
- package/src/observability/index.ts +79 -0
- package/src/observability/metric-validator.ts +86 -0
- package/src/observability/metrics-handle.ts +56 -0
- package/src/observability/noop-provider.ts +146 -0
- package/src/observability/prometheus-meter.ts +284 -0
- package/src/observability/recording-meter.ts +156 -0
- package/src/observability/recording-tracer.ts +198 -0
- package/src/observability/redis-wrapper.ts +132 -0
- package/src/observability/sensitive-filter.ts +108 -0
- package/src/observability/standard-metrics.ts +213 -0
- package/src/observability/types/index.ts +29 -0
- package/src/observability/types/metric.ts +56 -0
- package/src/observability/types/provider.ts +32 -0
- package/src/observability/types/span.ts +64 -0
- package/src/pipeline/__tests__/archive-stream.integration.ts +220 -0
- package/src/pipeline/__tests__/auth-claims-resolver.test.ts +279 -0
- package/src/pipeline/__tests__/cascade-handler.integration.ts +419 -0
- package/src/pipeline/__tests__/cascade-handler.test.ts +52 -0
- package/src/pipeline/__tests__/causation-chain.integration.ts +206 -0
- package/src/pipeline/__tests__/ctx-bridge.integration.ts +234 -0
- package/src/pipeline/__tests__/dispatcher.test.ts +379 -0
- package/src/pipeline/__tests__/distributed-lock.integration.ts +67 -0
- package/src/pipeline/__tests__/domain-events-projections.integration.ts +323 -0
- package/src/pipeline/__tests__/event-dedup.integration.ts +153 -0
- package/src/pipeline/__tests__/event-define-event-strict.integration.ts +202 -0
- package/src/pipeline/__tests__/event-dispatcher-lifecycle.integration.ts +220 -0
- package/src/pipeline/__tests__/event-dispatcher-multi-instance.integration.ts +423 -0
- package/src/pipeline/__tests__/event-dispatcher-pg-listen.integration.ts +123 -0
- package/src/pipeline/__tests__/event-dispatcher-recovery.integration.ts +202 -0
- package/src/pipeline/__tests__/event-dispatcher-second-audit.integration.ts +290 -0
- package/src/pipeline/__tests__/event-dispatcher-strict.test.ts +65 -0
- package/src/pipeline/__tests__/event-dispatcher.integration.ts +287 -0
- package/src/pipeline/__tests__/event-retention.integration.ts +239 -0
- package/src/pipeline/__tests__/fetch-for-writing.integration.ts +281 -0
- package/src/pipeline/__tests__/lifecycle-pipeline.test.ts +430 -0
- package/src/pipeline/__tests__/load-aggregate-query.integration.ts +266 -0
- package/src/pipeline/__tests__/msp-error-mode.integration.ts +149 -0
- package/src/pipeline/__tests__/msp-multi-hop.integration.ts +228 -0
- package/src/pipeline/__tests__/msp-rebuild.integration.ts +368 -0
- package/src/pipeline/__tests__/multi-stream-projection.integration.ts +341 -0
- package/src/pipeline/__tests__/perf-rebuild.integration.ts +147 -0
- package/src/pipeline/__tests__/projection-rebuild.integration.ts +551 -0
- package/src/pipeline/__tests__/query-projection.integration.ts +201 -0
- package/src/pipeline/__tests__/redis-pipeline.integration.ts +306 -0
- package/src/pipeline/append-event-core.ts +117 -0
- package/src/pipeline/auth-claims-resolver.ts +103 -0
- package/src/pipeline/cascade-handler.ts +113 -0
- package/src/pipeline/dispatcher.ts +1585 -0
- package/src/pipeline/distributed-lock.ts +37 -0
- package/src/pipeline/entity-cache.ts +113 -0
- package/src/pipeline/event-consumer-state.ts +108 -0
- package/src/pipeline/event-dedup.ts +23 -0
- package/src/pipeline/event-dispatcher.ts +1016 -0
- package/src/pipeline/event-retention.ts +154 -0
- package/src/pipeline/idempotency.ts +76 -0
- package/src/pipeline/index.ts +66 -0
- package/src/pipeline/lifecycle-pipeline.ts +409 -0
- package/src/pipeline/msp-rebuild.ts +242 -0
- package/src/pipeline/multi-stream-apply-context.ts +115 -0
- package/src/pipeline/projection-rebuild.ts +334 -0
- package/src/pipeline/projection-state.ts +72 -0
- package/src/pipeline/projections-runner.ts +56 -0
- package/src/pipeline/redis-keys.ts +11 -0
- package/src/pipeline/system-hooks.ts +190 -0
- package/src/random/__tests__/generate.test.ts +149 -0
- package/src/random/generate.ts +141 -0
- package/src/random/index.ts +8 -0
- package/src/random/words.ts +392 -0
- package/src/rate-limit/__tests__/dispatcher-l3.integration.ts +111 -0
- package/src/rate-limit/__tests__/middleware.integration.ts +189 -0
- package/src/rate-limit/__tests__/resolver.integration.ts +189 -0
- package/src/rate-limit/bucket.ts +36 -0
- package/src/rate-limit/index.ts +14 -0
- package/src/rate-limit/middleware.ts +152 -0
- package/src/rate-limit/resolver.ts +267 -0
- package/src/redis/__tests__/redis-options.test.ts +54 -0
- package/src/redis/index.ts +74 -0
- package/src/search/__tests__/meilisearch-adapter.integration.ts +236 -0
- package/src/search/__tests__/search-adapter.test.ts +256 -0
- package/src/search/in-memory-adapter.ts +123 -0
- package/src/search/index.ts +12 -0
- package/src/search/meilisearch-adapter.ts +106 -0
- package/src/search/types.ts +39 -0
- package/src/secrets/__tests__/dek-cache.test.ts +213 -0
- package/src/secrets/__tests__/env-master-key-provider.test.ts +119 -0
- package/src/secrets/__tests__/envelope.test.ts +74 -0
- package/src/secrets/__tests__/leak-guard.test.ts +92 -0
- package/src/secrets/__tests__/rotation.test.ts +149 -0
- package/src/secrets/dek-cache.ts +116 -0
- package/src/secrets/env-master-key-provider.ts +162 -0
- package/src/secrets/envelope.ts +55 -0
- package/src/secrets/index.ts +19 -0
- package/src/secrets/leak-guard.ts +87 -0
- package/src/secrets/rotation.ts +34 -0
- package/src/secrets/types.ts +107 -0
- package/src/stack/db.ts +104 -0
- package/src/stack/event-collector.ts +23 -0
- package/src/stack/index.ts +32 -0
- package/src/stack/redis.ts +44 -0
- package/src/stack/request-helper.ts +168 -0
- package/src/stack/table-helpers.ts +104 -0
- package/src/stack/test-stack.ts +357 -0
- package/src/stack/test-users.ts +37 -0
- package/src/testing/__tests__/e2e-generator.test.ts +230 -0
- package/src/testing/__tests__/ensure-entity-table.integration.ts +54 -0
- package/src/testing/access-assertions.ts +15 -0
- package/src/testing/assertions.ts +35 -0
- package/src/testing/e2e-generator.ts +465 -0
- package/src/testing/expect-error.ts +25 -0
- package/src/testing/handler-context.ts +125 -0
- package/src/testing/http-cookies.ts +52 -0
- package/src/testing/index.ts +41 -0
- package/src/testing/late-bound.ts +39 -0
- package/src/testing/mutable-master-key-provider.ts +31 -0
- package/src/testing/observability-recorder.ts +54 -0
- package/src/testing/shared-entities.ts +49 -0
- package/src/testing/utils.ts +1 -0
- package/src/testing/wait-for.ts +31 -0
- package/src/time/__tests__/polyfill.test.ts +73 -0
- package/src/time/__tests__/tz-context.test.ts +121 -0
- package/src/time/index.ts +21 -0
- package/src/time/polyfill.ts +70 -0
- package/src/time/tz-context.ts +107 -0
- package/src/ui-types/app-schema.ts +57 -0
- package/src/ui-types/index.ts +65 -0
- package/src/utils/__tests__/assert.test.ts +17 -0
- package/src/utils/__tests__/env-parse.test.ts +54 -0
- package/src/utils/assert.ts +18 -0
- package/src/utils/env-parse.ts +16 -0
- package/src/utils/ids.ts +16 -0
- package/src/utils/index.ts +5 -0
- package/src/utils/safe-json.ts +30 -0
- package/src/utils/serialization.ts +7 -0
|
@@ -0,0 +1,830 @@
|
|
|
1
|
+
import { mkdtemp, rm } from "node:fs/promises";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import type { Hono } from "hono";
|
|
5
|
+
import { afterAll, beforeAll, describe, expect, test } from "vitest";
|
|
6
|
+
import type { JwtHelper } from "../../api/jwt";
|
|
7
|
+
import { buildServer } from "../../api/server";
|
|
8
|
+
import {
|
|
9
|
+
createEntity,
|
|
10
|
+
createImageField,
|
|
11
|
+
createRegistry,
|
|
12
|
+
createTextField,
|
|
13
|
+
defineFeature,
|
|
14
|
+
type SessionUser,
|
|
15
|
+
} from "../../engine";
|
|
16
|
+
import { createEventsTable, loadAggregate } from "../../event-store";
|
|
17
|
+
import {
|
|
18
|
+
createEntityTable,
|
|
19
|
+
createTestDb,
|
|
20
|
+
createTestUser,
|
|
21
|
+
pushTables,
|
|
22
|
+
type TestDb,
|
|
23
|
+
TestUsers,
|
|
24
|
+
} from "../../stack";
|
|
25
|
+
import { expectErrorIncludes } from "../../testing";
|
|
26
|
+
import { fileRefsTable } from "../file-ref-table";
|
|
27
|
+
import { FILE_UPLOADED_EVENT_TYPE, type FileRoutesOptions } from "../file-routes";
|
|
28
|
+
import { createInMemoryFileProvider } from "../in-memory-provider";
|
|
29
|
+
import { createLocalProvider } from "../local-provider";
|
|
30
|
+
import type { FileStorageProvider } from "../types";
|
|
31
|
+
import { parseMaxSize, validateFile } from "../types";
|
|
32
|
+
|
|
33
|
+
// UUID for "this row doesn't exist" assertions. Valid v4 format so PG accepts
|
|
34
|
+
// the query — the row just isn't there. Pre-v1 files-feature tests used
|
|
35
|
+
// `99999` which Postgres now rejects with an invalid-uuid error.
|
|
36
|
+
const NONEXISTENT_UUID = "00000000-0000-4000-8000-999999999999";
|
|
37
|
+
|
|
38
|
+
// --- Setup ---
|
|
39
|
+
|
|
40
|
+
let testDb: TestDb;
|
|
41
|
+
let app: Hono;
|
|
42
|
+
let jwt: JwtHelper;
|
|
43
|
+
let storagePath: string;
|
|
44
|
+
|
|
45
|
+
const adminUser = TestUsers.admin;
|
|
46
|
+
const otherTenantUser = createTestUser({ id: 2, tenantId: "00000000-0000-4000-8000-000000000002" });
|
|
47
|
+
const JWT_SECRET = "files-test-secret-at-least-32-characters!!";
|
|
48
|
+
|
|
49
|
+
// A tenant feature with a logo field
|
|
50
|
+
const testTenantEntity = createEntity({
|
|
51
|
+
table: "test_tenants",
|
|
52
|
+
fields: {
|
|
53
|
+
name: createTextField({ required: true }),
|
|
54
|
+
logo: createImageField({ maxSize: "2mb", accept: ["png", "jpg"] }),
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
const tenantFeature = defineFeature("tenant", (r) => {
|
|
59
|
+
r.entity("tenant", testTenantEntity);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
beforeAll(async () => {
|
|
63
|
+
testDb = await createTestDb();
|
|
64
|
+
storagePath = await mkdtemp(join(tmpdir(), "kumiko-files-test-"));
|
|
65
|
+
|
|
66
|
+
// Create tables
|
|
67
|
+
await pushTables(testDb.db, { fileRefsTable });
|
|
68
|
+
await createEntityTable(testDb.db, testTenantEntity);
|
|
69
|
+
// Event-store table: the upload route appends files:event:uploaded in the
|
|
70
|
+
// same tx as the FileRef insert. Without events, upload would 500.
|
|
71
|
+
await createEventsTable(testDb.db);
|
|
72
|
+
|
|
73
|
+
const registry = createRegistry([tenantFeature]);
|
|
74
|
+
const storageProvider = createLocalProvider(storagePath);
|
|
75
|
+
|
|
76
|
+
const server = buildServer({
|
|
77
|
+
registry,
|
|
78
|
+
context: { db: testDb.db },
|
|
79
|
+
jwtSecret: JWT_SECRET,
|
|
80
|
+
files: { db: testDb.db, storageProvider },
|
|
81
|
+
});
|
|
82
|
+
app = server.app;
|
|
83
|
+
jwt = server.jwt;
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
afterAll(async () => {
|
|
87
|
+
await testDb.cleanup();
|
|
88
|
+
await rm(storagePath, { recursive: true, force: true });
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// --- Helpers ---
|
|
92
|
+
|
|
93
|
+
async function uploadFile(
|
|
94
|
+
user: SessionUser,
|
|
95
|
+
fileName: string,
|
|
96
|
+
content: Uint8Array,
|
|
97
|
+
mimeType: string,
|
|
98
|
+
extra?: Record<string, string>,
|
|
99
|
+
): Promise<Response> {
|
|
100
|
+
const token = await jwt.sign(user);
|
|
101
|
+
const formData = new FormData();
|
|
102
|
+
formData.append("file", new File([Buffer.from(content)], fileName, { type: mimeType }));
|
|
103
|
+
if (extra) {
|
|
104
|
+
for (const [k, v] of Object.entries(extra)) {
|
|
105
|
+
formData.append(k, v);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return app.request("/api/files", {
|
|
109
|
+
method: "POST",
|
|
110
|
+
headers: { Authorization: `Bearer ${token}` },
|
|
111
|
+
body: formData,
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async function getFile(user: SessionUser, fileId: string): Promise<Response> {
|
|
116
|
+
const token = await jwt.sign(user);
|
|
117
|
+
return app.request(`/api/files/${fileId}`, {
|
|
118
|
+
method: "GET",
|
|
119
|
+
headers: { Authorization: `Bearer ${token}` },
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
async function getFileMeta(user: SessionUser, fileId: string): Promise<Response> {
|
|
124
|
+
const token = await jwt.sign(user);
|
|
125
|
+
return app.request(`/api/files/${fileId}/meta`, {
|
|
126
|
+
method: "GET",
|
|
127
|
+
headers: { Authorization: `Bearer ${token}` },
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
async function deleteFile(user: SessionUser, fileId: string): Promise<Response> {
|
|
132
|
+
const token = await jwt.sign(user);
|
|
133
|
+
return app.request(`/api/files/${fileId}`, {
|
|
134
|
+
method: "DELETE",
|
|
135
|
+
headers: { Authorization: `Bearer ${token}` },
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// --- Unit tests for validation helpers ---
|
|
140
|
+
|
|
141
|
+
describe("file validation", () => {
|
|
142
|
+
test("parseMaxSize converts units correctly", () => {
|
|
143
|
+
expect(parseMaxSize("1kb")).toBe(1024);
|
|
144
|
+
expect(parseMaxSize("2mb")).toBe(2 * 1024 * 1024);
|
|
145
|
+
expect(parseMaxSize("1gb")).toBe(1024 * 1024 * 1024);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
test("parseMaxSize rejects invalid format", () => {
|
|
149
|
+
expect(() => parseMaxSize("abc")).toThrow();
|
|
150
|
+
expect(() => parseMaxSize("10")).toThrow();
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
test("validateFile rejects oversized files", () => {
|
|
154
|
+
const error = validateFile(
|
|
155
|
+
{ fileName: "big.pdf", mimeType: "application/pdf", size: 3 * 1024 * 1024 },
|
|
156
|
+
{ maxSize: "2mb" },
|
|
157
|
+
);
|
|
158
|
+
expectErrorIncludes(error, "file_too_large");
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
test("validateFile rejects wrong extension", () => {
|
|
162
|
+
const error = validateFile(
|
|
163
|
+
{ fileName: "doc.exe", mimeType: "application/exe", size: 100 },
|
|
164
|
+
{ accept: ["pdf", "jpg"] },
|
|
165
|
+
);
|
|
166
|
+
expectErrorIncludes(error, "invalid_file_type");
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
test("validateFile accepts valid file", () => {
|
|
170
|
+
const error = validateFile(
|
|
171
|
+
{ fileName: "photo.jpg", mimeType: "image/jpeg", size: 500_000 },
|
|
172
|
+
{ maxSize: "2mb", accept: ["jpg", "png"] },
|
|
173
|
+
);
|
|
174
|
+
expect(error).toBeNull();
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
test("validateFile rejects MIME mismatch (extension says jpg, client claims PDF)", () => {
|
|
178
|
+
const error = validateFile(
|
|
179
|
+
{ fileName: "sneaky.jpg", mimeType: "application/pdf", size: 500 },
|
|
180
|
+
{ accept: ["jpg", "png"] },
|
|
181
|
+
);
|
|
182
|
+
expectErrorIncludes(error, "mime_mismatch");
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
test("validateFile accepts jpeg mimeType variants for jpg extension", () => {
|
|
186
|
+
expect(
|
|
187
|
+
validateFile({ fileName: "a.jpg", mimeType: "image/jpeg", size: 100 }, { accept: ["jpg"] }),
|
|
188
|
+
).toBeNull();
|
|
189
|
+
expect(
|
|
190
|
+
validateFile({ fileName: "a.jpg", mimeType: "image/jpg", size: 100 }, { accept: ["jpg"] }),
|
|
191
|
+
).toBeNull();
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
// --- Integration: Upload → Download → Delete via real HTTP API ---
|
|
196
|
+
|
|
197
|
+
describe("file upload flow via API", () => {
|
|
198
|
+
let uploadedFileId: string;
|
|
199
|
+
|
|
200
|
+
// Create a small PNG-like test file
|
|
201
|
+
const testPngContent = new Uint8Array([
|
|
202
|
+
0x89,
|
|
203
|
+
0x50,
|
|
204
|
+
0x4e,
|
|
205
|
+
0x47,
|
|
206
|
+
0x0d,
|
|
207
|
+
0x0a,
|
|
208
|
+
0x1a,
|
|
209
|
+
0x0a, // PNG header
|
|
210
|
+
...Array(100).fill(0),
|
|
211
|
+
]);
|
|
212
|
+
|
|
213
|
+
test("upload a logo image", async () => {
|
|
214
|
+
const res = await uploadFile(adminUser, "logo.png", testPngContent, "image/png", {
|
|
215
|
+
entityType: "tenant",
|
|
216
|
+
entityId: "1",
|
|
217
|
+
fieldName: "logo",
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
expect(res.status).toBe(201);
|
|
221
|
+
const body = await res.json();
|
|
222
|
+
expect(body.id).toBeDefined();
|
|
223
|
+
expect(body.fileName).toBe("logo.png");
|
|
224
|
+
expect(body.mimeType).toBe("image/png");
|
|
225
|
+
expect(body.size).toBe(testPngContent.length);
|
|
226
|
+
expect(body.storageKey).toContain("1/tenant/1/logo/");
|
|
227
|
+
|
|
228
|
+
uploadedFileId = body.id;
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
test("download the uploaded file", async () => {
|
|
232
|
+
const res = await getFile(adminUser, uploadedFileId);
|
|
233
|
+
expect(res.status).toBe(200);
|
|
234
|
+
expect(res.headers.get("Content-Type")).toBe("image/png");
|
|
235
|
+
expect(res.headers.get("Content-Disposition")).toContain("logo.png");
|
|
236
|
+
|
|
237
|
+
const downloaded = new Uint8Array(await res.arrayBuffer());
|
|
238
|
+
expect(downloaded.length).toBe(testPngContent.length);
|
|
239
|
+
expect(downloaded[0]).toBe(0x89); // PNG magic byte
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
test("upload appends files:event:uploaded to the fileRef stream", async () => {
|
|
243
|
+
// Load the full event stream for the just-uploaded FileRef. Phase 1
|
|
244
|
+
// guarantees exactly one event per upload — "uploaded" at version 1.
|
|
245
|
+
const events = await loadAggregate(testDb.db, uploadedFileId, adminUser.tenantId);
|
|
246
|
+
|
|
247
|
+
expect(events).toHaveLength(1);
|
|
248
|
+
const event = events[0];
|
|
249
|
+
expect(event?.type).toBe(FILE_UPLOADED_EVENT_TYPE);
|
|
250
|
+
expect(event?.version).toBe(1);
|
|
251
|
+
|
|
252
|
+
type UploadedPayload = {
|
|
253
|
+
fileRefId: string;
|
|
254
|
+
fileName: string;
|
|
255
|
+
mimeType: string;
|
|
256
|
+
size: number;
|
|
257
|
+
entityType: string;
|
|
258
|
+
fieldName: string;
|
|
259
|
+
storageKey: string;
|
|
260
|
+
data?: unknown;
|
|
261
|
+
binary?: unknown;
|
|
262
|
+
};
|
|
263
|
+
const payload = event!.payload as UploadedPayload;
|
|
264
|
+
expect(payload.fileRefId).toBe(uploadedFileId);
|
|
265
|
+
expect(payload.fileName).toBe("logo.png");
|
|
266
|
+
expect(payload.mimeType).toBe("image/png");
|
|
267
|
+
expect(payload.size).toBe(testPngContent.length);
|
|
268
|
+
expect(payload.entityType).toBe("tenant");
|
|
269
|
+
expect(payload.fieldName).toBe("logo");
|
|
270
|
+
// The binary never hits the event — payload carries a pointer only.
|
|
271
|
+
expect(payload.data).toBeUndefined();
|
|
272
|
+
expect(payload.binary).toBeUndefined();
|
|
273
|
+
expect(typeof payload.storageKey).toBe("string");
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
test("get file metadata", async () => {
|
|
277
|
+
const res = await getFileMeta(adminUser, uploadedFileId);
|
|
278
|
+
expect(res.status).toBe(200);
|
|
279
|
+
const body = await res.json();
|
|
280
|
+
expect(body.fileName).toBe("logo.png");
|
|
281
|
+
expect(body.entityType).toBe("tenant");
|
|
282
|
+
// entity_id is text post-migration — the upload route passes whatever
|
|
283
|
+
// string the client sent (here "1") straight through.
|
|
284
|
+
expect(body.entityId).toBe("1");
|
|
285
|
+
expect(body.fieldName).toBe("logo");
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
test("other tenant cannot access the file", async () => {
|
|
289
|
+
const res = await getFile(otherTenantUser, uploadedFileId);
|
|
290
|
+
expect(res.status).toBe(404);
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
test("delete the file", async () => {
|
|
294
|
+
const res = await deleteFile(adminUser, uploadedFileId);
|
|
295
|
+
expect(res.status).toBe(200);
|
|
296
|
+
const body = await res.json();
|
|
297
|
+
expect(body.ok).toBe(true);
|
|
298
|
+
|
|
299
|
+
// File is gone
|
|
300
|
+
const getRes = await getFile(adminUser, uploadedFileId);
|
|
301
|
+
expect(getRes.status).toBe(404);
|
|
302
|
+
});
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
// --- Cross-user access within a tenant (attached file owner-scope) ---
|
|
306
|
+
|
|
307
|
+
describe("attached file owner-scope", () => {
|
|
308
|
+
const testPng = new Uint8Array([
|
|
309
|
+
0x89,
|
|
310
|
+
0x50,
|
|
311
|
+
0x4e,
|
|
312
|
+
0x47,
|
|
313
|
+
0x0d,
|
|
314
|
+
0x0a,
|
|
315
|
+
0x1a,
|
|
316
|
+
0x0a,
|
|
317
|
+
...Array(50).fill(0),
|
|
318
|
+
]);
|
|
319
|
+
// Same tenant as adminUser (tenantId 1), different id and no privileged role.
|
|
320
|
+
const memberUser: SessionUser = {
|
|
321
|
+
id: "11111111-0000-4000-8000-000000000042",
|
|
322
|
+
tenantId: "00000000-0000-4000-8000-000000000001",
|
|
323
|
+
roles: ["User"],
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
test("non-uploader, non-admin in same tenant cannot download an entity-attached file", async () => {
|
|
327
|
+
const uploadRes = await uploadFile(memberUser, "mine.png", testPng, "image/png", {
|
|
328
|
+
entityType: "tenant",
|
|
329
|
+
entityId: "1",
|
|
330
|
+
fieldName: "logo",
|
|
331
|
+
});
|
|
332
|
+
expect(uploadRes.status).toBe(201);
|
|
333
|
+
const { id } = await uploadRes.json();
|
|
334
|
+
|
|
335
|
+
// A different non-privileged user in the SAME tenant — the old code leaked
|
|
336
|
+
// here (tenant check alone passed). New code rejects with 404.
|
|
337
|
+
const otherMember: SessionUser = {
|
|
338
|
+
id: "11111111-0000-4000-8000-000000000043",
|
|
339
|
+
tenantId: "00000000-0000-4000-8000-000000000001",
|
|
340
|
+
roles: ["User"],
|
|
341
|
+
};
|
|
342
|
+
const res = await getFile(otherMember, id);
|
|
343
|
+
expect(res.status).toBe(404);
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
test("uploader can download their own entity-attached file", async () => {
|
|
347
|
+
const uploadRes = await uploadFile(memberUser, "mine2.png", testPng, "image/png", {
|
|
348
|
+
entityType: "tenant",
|
|
349
|
+
entityId: "1",
|
|
350
|
+
fieldName: "logo",
|
|
351
|
+
});
|
|
352
|
+
const { id } = await uploadRes.json();
|
|
353
|
+
|
|
354
|
+
const res = await getFile(memberUser, id);
|
|
355
|
+
expect(res.status).toBe(200);
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
test("Admin in same tenant can download any attached file", async () => {
|
|
359
|
+
const uploadRes = await uploadFile(memberUser, "mine3.png", testPng, "image/png", {
|
|
360
|
+
entityType: "tenant",
|
|
361
|
+
entityId: "1",
|
|
362
|
+
fieldName: "logo",
|
|
363
|
+
});
|
|
364
|
+
const { id } = await uploadRes.json();
|
|
365
|
+
|
|
366
|
+
const res = await getFile(adminUser, id); // Admin role
|
|
367
|
+
expect(res.status).toBe(200);
|
|
368
|
+
});
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
// --- Custom access guard + privilegedRoles ---
|
|
372
|
+
|
|
373
|
+
describe("custom file access guard", () => {
|
|
374
|
+
const testPng = new Uint8Array([
|
|
375
|
+
0x89,
|
|
376
|
+
0x50,
|
|
377
|
+
0x4e,
|
|
378
|
+
0x47,
|
|
379
|
+
0x0d,
|
|
380
|
+
0x0a,
|
|
381
|
+
0x1a,
|
|
382
|
+
0x0a,
|
|
383
|
+
...Array(30).fill(0),
|
|
384
|
+
]);
|
|
385
|
+
|
|
386
|
+
// Spin up an isolated DB + storage dir for a single-test server. Runs the
|
|
387
|
+
// body inside try/finally so the DB and tmpdir are cleaned up even if
|
|
388
|
+
// assertions fail.
|
|
389
|
+
async function withIsolatedFileServer(
|
|
390
|
+
options: Omit<FileRoutesOptions, "db" | "storageProvider"> & {
|
|
391
|
+
// Overrides the default local-filesystem provider. Needed for tests
|
|
392
|
+
// that exercise optional provider methods (e.g. getSignedUrl) which
|
|
393
|
+
// the local provider deliberately doesn't implement.
|
|
394
|
+
readonly storageProvider?: FileStorageProvider;
|
|
395
|
+
},
|
|
396
|
+
body: (args: {
|
|
397
|
+
app: Hono;
|
|
398
|
+
jwt: JwtHelper;
|
|
399
|
+
upload: (user: SessionUser, name: string) => Promise<Response>;
|
|
400
|
+
request: (user: SessionUser, fileId: string, init?: RequestInit) => Promise<Response>;
|
|
401
|
+
}) => Promise<void>,
|
|
402
|
+
): Promise<void> {
|
|
403
|
+
const { storageProvider: providerOverride, ...routeOptions } = options;
|
|
404
|
+
const isolatedDb = await createTestDb();
|
|
405
|
+
await pushTables(isolatedDb.db, { fileRefsTable });
|
|
406
|
+
await createEntityTable(isolatedDb.db, testTenantEntity);
|
|
407
|
+
const storagePath = await mkdtemp(join(tmpdir(), "kumiko-files-custom-"));
|
|
408
|
+
const provider = providerOverride ?? createLocalProvider(storagePath);
|
|
409
|
+
const isolatedRegistry = createRegistry([tenantFeature]);
|
|
410
|
+
const isolatedServer = buildServer({
|
|
411
|
+
registry: isolatedRegistry,
|
|
412
|
+
context: { db: isolatedDb.db },
|
|
413
|
+
jwtSecret: JWT_SECRET,
|
|
414
|
+
files: { db: isolatedDb.db, storageProvider: provider, ...routeOptions },
|
|
415
|
+
});
|
|
416
|
+
|
|
417
|
+
try {
|
|
418
|
+
const upload = async (user: SessionUser, name: string) => {
|
|
419
|
+
const token = await isolatedServer.jwt.sign(user);
|
|
420
|
+
const fd = new FormData();
|
|
421
|
+
fd.append("file", new File([Buffer.from(testPng)], name, { type: "image/png" }));
|
|
422
|
+
fd.append("entityType", "tenant");
|
|
423
|
+
fd.append("entityId", "1");
|
|
424
|
+
fd.append("fieldName", "logo");
|
|
425
|
+
return isolatedServer.app.request("/api/files", {
|
|
426
|
+
method: "POST",
|
|
427
|
+
headers: { Authorization: `Bearer ${token}` },
|
|
428
|
+
body: fd,
|
|
429
|
+
});
|
|
430
|
+
};
|
|
431
|
+
const request = async (user: SessionUser, fileId: string, init: RequestInit = {}) => {
|
|
432
|
+
const token = await isolatedServer.jwt.sign(user);
|
|
433
|
+
return isolatedServer.app.request(`/api/files/${fileId}`, {
|
|
434
|
+
...init,
|
|
435
|
+
headers: {
|
|
436
|
+
...((init.headers as Record<string, string> | undefined) ?? {}),
|
|
437
|
+
Authorization: `Bearer ${token}`,
|
|
438
|
+
},
|
|
439
|
+
});
|
|
440
|
+
};
|
|
441
|
+
await body({
|
|
442
|
+
app: isolatedServer.app,
|
|
443
|
+
jwt: isolatedServer.jwt,
|
|
444
|
+
upload,
|
|
445
|
+
request,
|
|
446
|
+
});
|
|
447
|
+
} finally {
|
|
448
|
+
await isolatedDb.cleanup();
|
|
449
|
+
await rm(storagePath, { recursive: true, force: true });
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
test("privilegedRoles override: app-defined role (e.g. Supervisor) replaces the default Admin+SystemAdmin", async () => {
|
|
454
|
+
await withIsolatedFileServer(
|
|
455
|
+
{ privilegedRoles: ["Supervisor"] },
|
|
456
|
+
async ({ upload, request }) => {
|
|
457
|
+
const uploader: SessionUser = {
|
|
458
|
+
id: "11111111-0000-4000-8000-000000000010",
|
|
459
|
+
tenantId: "00000000-0000-4000-8000-000000000001",
|
|
460
|
+
roles: ["User"],
|
|
461
|
+
};
|
|
462
|
+
const supervisor: SessionUser = {
|
|
463
|
+
id: "11111111-0000-4000-8000-000000000020",
|
|
464
|
+
tenantId: "00000000-0000-4000-8000-000000000001",
|
|
465
|
+
roles: ["Supervisor"],
|
|
466
|
+
};
|
|
467
|
+
const adminCaller: SessionUser = {
|
|
468
|
+
id: "11111111-0000-4000-8000-000000000030",
|
|
469
|
+
tenantId: "00000000-0000-4000-8000-000000000001",
|
|
470
|
+
roles: ["Admin"],
|
|
471
|
+
};
|
|
472
|
+
|
|
473
|
+
const uploaded = await upload(uploader, "custom-role.png");
|
|
474
|
+
const { id } = await uploaded.json();
|
|
475
|
+
|
|
476
|
+
// Supervisor (the new privileged role) can read.
|
|
477
|
+
expect((await request(supervisor, id)).status).toBe(200);
|
|
478
|
+
// Admin is NO longer privileged under this config.
|
|
479
|
+
expect((await request(adminCaller, id)).status).toBe(404);
|
|
480
|
+
},
|
|
481
|
+
);
|
|
482
|
+
});
|
|
483
|
+
|
|
484
|
+
test("custom accessGuard receives read/delete operation and can distinguish", async () => {
|
|
485
|
+
const guardCalls: Array<{ operation: string; userId: string }> = [];
|
|
486
|
+
await withIsolatedFileServer(
|
|
487
|
+
{
|
|
488
|
+
// Everyone in the tenant can read; only the uploader can delete.
|
|
489
|
+
accessGuard: ({ fileRef, user, operation }) => {
|
|
490
|
+
guardCalls.push({ operation, userId: user.id });
|
|
491
|
+
if (operation === "read") return "allow";
|
|
492
|
+
return fileRef.insertedById === user.id ? "allow" : "deny";
|
|
493
|
+
},
|
|
494
|
+
},
|
|
495
|
+
async ({ upload, request }) => {
|
|
496
|
+
const uploader: SessionUser = {
|
|
497
|
+
id: "11111111-0000-4000-8000-000000000040",
|
|
498
|
+
tenantId: "00000000-0000-4000-8000-000000000001",
|
|
499
|
+
roles: ["User"],
|
|
500
|
+
};
|
|
501
|
+
const other: SessionUser = {
|
|
502
|
+
id: "11111111-0000-4000-8000-000000000041",
|
|
503
|
+
tenantId: "00000000-0000-4000-8000-000000000001",
|
|
504
|
+
roles: ["User"],
|
|
505
|
+
};
|
|
506
|
+
|
|
507
|
+
const { id } = await (await upload(uploader, "guard.png")).json();
|
|
508
|
+
|
|
509
|
+
// Other user can read (guard allowed).
|
|
510
|
+
expect((await request(other, id)).status).toBe(200);
|
|
511
|
+
// Other user cannot delete — guard denied.
|
|
512
|
+
expect((await request(other, id, { method: "DELETE" })).status).toBe(404);
|
|
513
|
+
// Uploader can delete.
|
|
514
|
+
expect((await request(uploader, id, { method: "DELETE" })).status).toBe(200);
|
|
515
|
+
|
|
516
|
+
expect(guardCalls.map((c) => c.operation)).toEqual(["read", "delete", "delete"]);
|
|
517
|
+
},
|
|
518
|
+
);
|
|
519
|
+
});
|
|
520
|
+
});
|
|
521
|
+
|
|
522
|
+
// --- Tenant isolation ---
|
|
523
|
+
|
|
524
|
+
describe("tenant isolation", () => {
|
|
525
|
+
test("tenant 2 uploads a file, tenant 1 cannot see it", async () => {
|
|
526
|
+
const content = new TextEncoder().encode("tenant2-secret-file");
|
|
527
|
+
const uploadRes = await uploadFile(otherTenantUser, "secret.pdf", content, "application/pdf");
|
|
528
|
+
expect(uploadRes.status).toBe(201);
|
|
529
|
+
const { id } = await uploadRes.json();
|
|
530
|
+
|
|
531
|
+
// Tenant 1 cannot access
|
|
532
|
+
const getRes = await getFile(adminUser, id);
|
|
533
|
+
expect(getRes.status).toBe(404);
|
|
534
|
+
|
|
535
|
+
// Tenant 2 can access
|
|
536
|
+
const getRes2 = await getFile(otherTenantUser, id);
|
|
537
|
+
expect(getRes2.status).toBe(200);
|
|
538
|
+
});
|
|
539
|
+
});
|
|
540
|
+
|
|
541
|
+
// --- Error handling ---
|
|
542
|
+
|
|
543
|
+
describe("error handling", () => {
|
|
544
|
+
test("upload without file returns 400", async () => {
|
|
545
|
+
const token = await jwt.sign(adminUser);
|
|
546
|
+
const formData = new FormData();
|
|
547
|
+
formData.append("notafile", "just text");
|
|
548
|
+
|
|
549
|
+
const res = await app.request("/api/files", {
|
|
550
|
+
method: "POST",
|
|
551
|
+
headers: { Authorization: `Bearer ${token}` },
|
|
552
|
+
body: formData,
|
|
553
|
+
});
|
|
554
|
+
|
|
555
|
+
expect(res.status).toBe(400);
|
|
556
|
+
const body = await res.json();
|
|
557
|
+
// /files route uses its own lightweight error shape (plain string).
|
|
558
|
+
expect(body.error).toContain("missing_file");
|
|
559
|
+
});
|
|
560
|
+
|
|
561
|
+
test("download non-existent file returns 404", async () => {
|
|
562
|
+
const res = await getFile(adminUser, NONEXISTENT_UUID);
|
|
563
|
+
expect(res.status).toBe(404);
|
|
564
|
+
});
|
|
565
|
+
|
|
566
|
+
test("delete non-existent file returns 404", async () => {
|
|
567
|
+
const res = await deleteFile(adminUser, NONEXISTENT_UUID);
|
|
568
|
+
expect(res.status).toBe(404);
|
|
569
|
+
});
|
|
570
|
+
|
|
571
|
+
test("upload wrong file type for entity field is rejected", async () => {
|
|
572
|
+
const pdfContent = new TextEncoder().encode("fake-pdf-content");
|
|
573
|
+
const res = await uploadFile(adminUser, "document.pdf", pdfContent, "application/pdf", {
|
|
574
|
+
entityType: "tenant",
|
|
575
|
+
entityId: "1",
|
|
576
|
+
fieldName: "logo", // logo only accepts png, jpg
|
|
577
|
+
});
|
|
578
|
+
expect(res.status).toBe(400);
|
|
579
|
+
const body = await res.json();
|
|
580
|
+
expect(body.error).toContain("invalid_file_type");
|
|
581
|
+
});
|
|
582
|
+
|
|
583
|
+
test("upload without auth returns 401", async () => {
|
|
584
|
+
const formData = new FormData();
|
|
585
|
+
formData.append("file", new File([new Uint8Array(10)], "test.png", { type: "image/png" }));
|
|
586
|
+
|
|
587
|
+
const res = await app.request("/api/files", {
|
|
588
|
+
method: "POST",
|
|
589
|
+
body: formData,
|
|
590
|
+
});
|
|
591
|
+
expect(res.status).toBe(401);
|
|
592
|
+
});
|
|
593
|
+
});
|
|
594
|
+
|
|
595
|
+
// --- Content-Disposition hardening (Phase 2.3 follow-up) ---
|
|
596
|
+
|
|
597
|
+
describe("Content-Disposition header hardening", () => {
|
|
598
|
+
const smallPng = new Uint8Array([
|
|
599
|
+
0x89,
|
|
600
|
+
0x50,
|
|
601
|
+
0x4e,
|
|
602
|
+
0x47,
|
|
603
|
+
0x0d,
|
|
604
|
+
0x0a,
|
|
605
|
+
0x1a,
|
|
606
|
+
0x0a,
|
|
607
|
+
...Array(20).fill(0),
|
|
608
|
+
]);
|
|
609
|
+
|
|
610
|
+
// Helper: upload a file WITHOUT entity attachment so validateFile skips
|
|
611
|
+
// the extension/mime whitelist. That's what lets us test with arbitrary
|
|
612
|
+
// filenames that wouldn't pass the attached-upload validator.
|
|
613
|
+
async function uploadUnattached(fileName: string): Promise<string> {
|
|
614
|
+
const token = await jwt.sign(adminUser);
|
|
615
|
+
const fd = new FormData();
|
|
616
|
+
fd.append("file", new File([Buffer.from(smallPng)], fileName, { type: "image/png" }));
|
|
617
|
+
const res = await app.request("/api/files", {
|
|
618
|
+
method: "POST",
|
|
619
|
+
headers: { Authorization: `Bearer ${token}` },
|
|
620
|
+
body: fd,
|
|
621
|
+
});
|
|
622
|
+
expect(res.status).toBe(201);
|
|
623
|
+
const body = await res.json();
|
|
624
|
+
return body.id;
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
test("malicious filename cannot inject a second header parameter", async () => {
|
|
628
|
+
// Name with a quote would break `filename="..."` quoting and inject
|
|
629
|
+
// `filename*=utf-8''evil.exe` if we interpolated the raw name.
|
|
630
|
+
const evil = `normal.png"; filename*=utf-8''evil.exe`;
|
|
631
|
+
const id = await uploadUnattached(evil);
|
|
632
|
+
|
|
633
|
+
const res = await getFile(adminUser, id);
|
|
634
|
+
expect(res.status).toBe(200);
|
|
635
|
+
const header = res.headers.get("Content-Disposition") ?? "";
|
|
636
|
+
|
|
637
|
+
// Header has the RFC-6266 shape: attachment; filename="..."; filename*=UTF-8''...
|
|
638
|
+
// Critically: exactly two parameters after `attachment`, i.e. no third
|
|
639
|
+
// parameter injected. split by ";" yields ["attachment", filename=, filename*=]
|
|
640
|
+
expect(header.split(";")).toHaveLength(3);
|
|
641
|
+
expect(header.startsWith('attachment; filename="')).toBe(true);
|
|
642
|
+
|
|
643
|
+
// The ASCII fallback inside `filename="..."` MUST NOT contain a quote
|
|
644
|
+
// character — that would close the string early and let the tail
|
|
645
|
+
// become a new parameter. This is the core fix.
|
|
646
|
+
const fallbackMatch = header.match(/filename="([^"]*)"/);
|
|
647
|
+
expect(fallbackMatch).not.toBeNull();
|
|
648
|
+
expect(fallbackMatch?.[1]).not.toContain('"');
|
|
649
|
+
expect(fallbackMatch?.[1]).not.toContain(";");
|
|
650
|
+
|
|
651
|
+
// filename* uses UTF-8 percent-encoding. The attacker's quote char
|
|
652
|
+
// (0x22) must appear as %22 — proving the raw bytes are preserved
|
|
653
|
+
// losslessly without escape-sequence injection.
|
|
654
|
+
expect(header).toContain("filename*=UTF-8''");
|
|
655
|
+
expect(header).toContain("%22"); // the quote char, percent-encoded
|
|
656
|
+
});
|
|
657
|
+
|
|
658
|
+
test("unicode filename is percent-encoded in filename*", async () => {
|
|
659
|
+
const unicode = "測試.png"; // Chinese characters
|
|
660
|
+
const id = await uploadUnattached(unicode);
|
|
661
|
+
|
|
662
|
+
const res = await getFile(adminUser, id);
|
|
663
|
+
const header = res.headers.get("Content-Disposition") ?? "";
|
|
664
|
+
|
|
665
|
+
// ASCII fallback collapses non-ASCII to underscore.
|
|
666
|
+
expect(header).toMatch(/^attachment; filename="[A-Za-z0-9._\-()]+";/);
|
|
667
|
+
// Modern filename* carries the UTF-8 bytes percent-encoded.
|
|
668
|
+
expect(header).toContain("filename*=UTF-8''");
|
|
669
|
+
// 測 = 0xE6 0xB8 0xAC in UTF-8 — at least one of those bytes must
|
|
670
|
+
// appear percent-encoded. Check E6 (lead byte of 測).
|
|
671
|
+
expect(header.toUpperCase()).toContain("%E6");
|
|
672
|
+
});
|
|
673
|
+
|
|
674
|
+
test("empty fallback (all chars stripped) falls back to 'download'", async () => {
|
|
675
|
+
// Name made entirely of characters outside the safe set — fallback
|
|
676
|
+
// would be empty; the builder substitutes a sane default instead.
|
|
677
|
+
const allStripped = "@@@###$$$.png"; // dots survive but the rest is stripped
|
|
678
|
+
const id = await uploadUnattached(allStripped);
|
|
679
|
+
|
|
680
|
+
const res = await getFile(adminUser, id);
|
|
681
|
+
const header = res.headers.get("Content-Disposition") ?? "";
|
|
682
|
+
|
|
683
|
+
// The dots + .png survive, so fallback is "____.png" rather than
|
|
684
|
+
// the "download" default — prove the fallback is non-empty and safe.
|
|
685
|
+
const fallbackMatch = header.match(/filename="([^"]+)"/);
|
|
686
|
+
expect(fallbackMatch).not.toBeNull();
|
|
687
|
+
expect(fallbackMatch?.[1]).not.toBe("");
|
|
688
|
+
expect(fallbackMatch?.[1]).toMatch(/^[A-Za-z0-9._\-()]+$/);
|
|
689
|
+
});
|
|
690
|
+
});
|
|
691
|
+
|
|
692
|
+
// --- Download-URL endpoint (Phase 2.3) ---
|
|
693
|
+
|
|
694
|
+
describe("download-url endpoint", () => {
|
|
695
|
+
const testPng = new Uint8Array([
|
|
696
|
+
0x89,
|
|
697
|
+
0x50,
|
|
698
|
+
0x4e,
|
|
699
|
+
0x47,
|
|
700
|
+
0x0d,
|
|
701
|
+
0x0a,
|
|
702
|
+
0x1a,
|
|
703
|
+
0x0a,
|
|
704
|
+
...Array(40).fill(0),
|
|
705
|
+
]);
|
|
706
|
+
|
|
707
|
+
// Mirrors the helper from the custom-guard block — same DB/storage
|
|
708
|
+
// lifecycle, but accepts a provider override so we can inject an in-memory
|
|
709
|
+
// provider that implements getSignedUrl.
|
|
710
|
+
async function withIsolatedServer(
|
|
711
|
+
storageProvider: FileStorageProvider,
|
|
712
|
+
body: (args: {
|
|
713
|
+
jwt: JwtHelper;
|
|
714
|
+
upload: (user: SessionUser) => Promise<Response>;
|
|
715
|
+
getDownloadUrl: (user: SessionUser, fileId: string) => Promise<Response>;
|
|
716
|
+
}) => Promise<void>,
|
|
717
|
+
): Promise<void> {
|
|
718
|
+
const isolatedDb = await createTestDb();
|
|
719
|
+
await pushTables(isolatedDb.db, { fileRefsTable });
|
|
720
|
+
await createEntityTable(isolatedDb.db, testTenantEntity);
|
|
721
|
+
const isolatedRegistry = createRegistry([tenantFeature]);
|
|
722
|
+
const isolatedServer = buildServer({
|
|
723
|
+
registry: isolatedRegistry,
|
|
724
|
+
context: { db: isolatedDb.db },
|
|
725
|
+
jwtSecret: JWT_SECRET,
|
|
726
|
+
files: { db: isolatedDb.db, storageProvider },
|
|
727
|
+
});
|
|
728
|
+
|
|
729
|
+
try {
|
|
730
|
+
const upload = async (user: SessionUser) => {
|
|
731
|
+
const token = await isolatedServer.jwt.sign(user);
|
|
732
|
+
const fd = new FormData();
|
|
733
|
+
fd.append("file", new File([Buffer.from(testPng)], "photo.png", { type: "image/png" }));
|
|
734
|
+
fd.append("entityType", "tenant");
|
|
735
|
+
fd.append("entityId", "1");
|
|
736
|
+
fd.append("fieldName", "logo");
|
|
737
|
+
return isolatedServer.app.request("/api/files", {
|
|
738
|
+
method: "POST",
|
|
739
|
+
headers: { Authorization: `Bearer ${token}` },
|
|
740
|
+
body: fd,
|
|
741
|
+
});
|
|
742
|
+
};
|
|
743
|
+
const getDownloadUrl = async (user: SessionUser, fileId: string) => {
|
|
744
|
+
const token = await isolatedServer.jwt.sign(user);
|
|
745
|
+
return isolatedServer.app.request(`/api/files/${fileId}/download-url`, {
|
|
746
|
+
method: "GET",
|
|
747
|
+
headers: { Authorization: `Bearer ${token}` },
|
|
748
|
+
});
|
|
749
|
+
};
|
|
750
|
+
await body({ jwt: isolatedServer.jwt, upload, getDownloadUrl });
|
|
751
|
+
} finally {
|
|
752
|
+
await isolatedDb.cleanup();
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
test("returns signed URL + expiresAt for authorized caller", async () => {
|
|
757
|
+
await withIsolatedServer(createInMemoryFileProvider(), async ({ upload, getDownloadUrl }) => {
|
|
758
|
+
const before = Date.now();
|
|
759
|
+
const { id } = await (await upload(adminUser)).json();
|
|
760
|
+
|
|
761
|
+
const res = await getDownloadUrl(adminUser, id);
|
|
762
|
+
expect(res.status).toBe(200);
|
|
763
|
+
const body = await res.json();
|
|
764
|
+
// The in-memory provider returns a memory:// URL with key + expiry —
|
|
765
|
+
// that's enough to prove the route wired the provider through.
|
|
766
|
+
expect(body.url).toMatch(/^memory:\/\//);
|
|
767
|
+
expect(body.url).toContain(`${adminUser.tenantId}/tenant/1/logo/`);
|
|
768
|
+
expect(body.url).toContain("expires=900");
|
|
769
|
+
// expiresAt is ~15 min in the future (ISO-8601).
|
|
770
|
+
const expiresAtMs = Date.parse(body.expiresAt);
|
|
771
|
+
expect(expiresAtMs).toBeGreaterThan(before + 14 * 60 * 1000);
|
|
772
|
+
expect(expiresAtMs).toBeLessThan(before + 16 * 60 * 1000);
|
|
773
|
+
});
|
|
774
|
+
});
|
|
775
|
+
|
|
776
|
+
test("returns 404 for nonexistent file", async () => {
|
|
777
|
+
await withIsolatedServer(createInMemoryFileProvider(), async ({ getDownloadUrl }) => {
|
|
778
|
+
const res = await getDownloadUrl(adminUser, NONEXISTENT_UUID);
|
|
779
|
+
expect(res.status).toBe(404);
|
|
780
|
+
});
|
|
781
|
+
});
|
|
782
|
+
|
|
783
|
+
test("returns 404 for other tenant (tenant isolation)", async () => {
|
|
784
|
+
await withIsolatedServer(createInMemoryFileProvider(), async ({ upload, getDownloadUrl }) => {
|
|
785
|
+
const { id } = await (await upload(adminUser)).json();
|
|
786
|
+
const res = await getDownloadUrl(otherTenantUser, id);
|
|
787
|
+
expect(res.status).toBe(404);
|
|
788
|
+
});
|
|
789
|
+
});
|
|
790
|
+
|
|
791
|
+
test("returns 404 when access guard denies (non-uploader, non-privileged)", async () => {
|
|
792
|
+
const memberUploader: SessionUser = {
|
|
793
|
+
id: "11111111-0000-4000-8000-000000000050",
|
|
794
|
+
tenantId: "00000000-0000-4000-8000-000000000001",
|
|
795
|
+
roles: ["User"],
|
|
796
|
+
};
|
|
797
|
+
const memberOther: SessionUser = {
|
|
798
|
+
id: "11111111-0000-4000-8000-000000000051",
|
|
799
|
+
tenantId: "00000000-0000-4000-8000-000000000001",
|
|
800
|
+
roles: ["User"],
|
|
801
|
+
};
|
|
802
|
+
await withIsolatedServer(createInMemoryFileProvider(), async ({ upload, getDownloadUrl }) => {
|
|
803
|
+
const { id } = await (await upload(memberUploader)).json();
|
|
804
|
+
// Different non-privileged user in the SAME tenant — guard denies.
|
|
805
|
+
const res = await getDownloadUrl(memberOther, id);
|
|
806
|
+
expect(res.status).toBe(404);
|
|
807
|
+
});
|
|
808
|
+
});
|
|
809
|
+
|
|
810
|
+
test("returns 501 when provider has no getSignedUrl (local filesystem)", async () => {
|
|
811
|
+
// The main test server uses createLocalProvider which deliberately does
|
|
812
|
+
// not implement getSignedUrl. Upload a fresh file, then request its
|
|
813
|
+
// download URL — the route must detect the missing method and 501.
|
|
814
|
+
const uploadRes = await uploadFile(adminUser, "no-signed.png", testPng, "image/png", {
|
|
815
|
+
entityType: "tenant",
|
|
816
|
+
entityId: "1",
|
|
817
|
+
fieldName: "logo",
|
|
818
|
+
});
|
|
819
|
+
const { id } = await uploadRes.json();
|
|
820
|
+
|
|
821
|
+
const token = await jwt.sign(adminUser);
|
|
822
|
+
const res = await app.request(`/api/files/${id}/download-url`, {
|
|
823
|
+
method: "GET",
|
|
824
|
+
headers: { Authorization: `Bearer ${token}` },
|
|
825
|
+
});
|
|
826
|
+
expect(res.status).toBe(501);
|
|
827
|
+
const body = await res.json();
|
|
828
|
+
expect(body.error).toContain("signed_urls_not_supported");
|
|
829
|
+
});
|
|
830
|
+
});
|