create-windy 0.2.33 → 0.3.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/dist/cli.js +173 -17
- package/package.json +1 -1
- package/template/.agents/skills/windy-business-module/SKILL.md +10 -1
- package/template/.windy-template.json +2 -2
- package/template/AGENTS.md +9 -0
- package/template/README.md +3 -1
- package/template/apps/agent-server/Dockerfile +1 -0
- package/template/apps/agent-server/README.md +28 -5
- package/template/apps/agent-server/adk_web_agents/__init__.py +1 -0
- package/template/apps/agent-server/adk_web_agents/safe_debug/__init__.py +3 -0
- package/template/apps/agent-server/adk_web_agents/safe_debug/agent.py +40 -0
- package/template/apps/agent-server/pyproject.toml +1 -1
- package/template/apps/agent-server/src/southwind_agent_server/adk_adapter.py +191 -16
- package/template/apps/agent-server/src/southwind_agent_server/adk_failure.py +13 -0
- package/template/apps/agent-server/src/southwind_agent_server/adk_model.py +225 -0
- package/template/apps/agent-server/src/southwind_agent_server/adk_tools.py +76 -0
- package/template/apps/agent-server/src/southwind_agent_server/app.py +63 -3
- package/template/apps/agent-server/src/southwind_agent_server/contracts.py +35 -0
- package/template/apps/agent-server/src/southwind_agent_server/engine.py +2 -0
- package/template/apps/agent-server/src/southwind_agent_server/openai_engine.py +31 -3
- package/template/apps/agent-server/src/southwind_agent_server/openai_provider.py +22 -9
- package/template/apps/agent-server/src/southwind_agent_server/provider_audit.py +144 -0
- package/template/apps/agent-server/src/southwind_agent_server/provider_governance.py +227 -0
- package/template/apps/agent-server/src/southwind_agent_server/service.py +1 -0
- package/template/apps/agent-server/tests/test_adk_adapter.py +305 -0
- package/template/apps/agent-server/tests/test_adk_web_agent.py +12 -0
- package/template/apps/agent-server/tests/test_api.py +16 -1
- package/template/apps/agent-server/tests/test_openai_provider.py +79 -0
- package/template/apps/agent-server/tests/test_provider_governance.py +215 -0
- package/template/apps/server/package.json +4 -1
- package/template/apps/server/src/agent/provider-input-governance.test.ts +82 -0
- package/template/apps/server/src/agent/provider-input-governance.ts +140 -0
- package/template/apps/server/src/agent/remote-runtime.test.ts +22 -0
- package/template/apps/server/src/agent/remote-runtime.ts +1 -0
- package/template/apps/server/src/agent/run-contracts.ts +2 -0
- package/template/apps/server/src/agent/run-facade.test.ts +52 -2
- package/template/apps/server/src/agent/run-facade.ts +38 -9
- package/template/apps/server/src/agent/tool-registry.ts +2 -6
- package/template/apps/server/src/application-services.ts +50 -0
- package/template/apps/server/src/audit/drizzle-log-store.ts +25 -0
- package/template/apps/server/src/audit/search-summary.ts +2 -6
- package/template/apps/server/src/auth/credential-restriction.ts +1 -0
- package/template/apps/server/src/auth/password.ts +30 -21
- package/template/apps/server/src/auth/routes.test.ts +44 -3
- package/template/apps/server/src/auth/routes.ts +6 -1
- package/template/apps/server/src/auth/runtime.ts +4 -1
- package/template/apps/server/src/auth/service.ts +25 -7
- package/template/apps/server/src/configuration/bootstrap.ts +20 -1
- package/template/apps/server/src/configuration/definition.test.ts +71 -0
- package/template/apps/server/src/configuration/definition.ts +242 -12
- package/template/apps/server/src/configuration/drizzle-repository.ts +36 -2
- package/template/apps/server/src/configuration/repository.ts +27 -2
- package/template/apps/server/src/configuration/routes.ts +4 -1
- package/template/apps/server/src/configuration/service.test.ts +100 -0
- package/template/apps/server/src/configuration/service.ts +50 -10
- package/template/apps/server/src/data-access/scoped-repository.integration.test.ts +3 -2
- package/template/apps/server/src/data-governance/export/service.ts +2 -6
- package/template/apps/server/src/example-modules.ts +4 -1
- package/template/apps/server/src/guards.ts +18 -5
- package/template/apps/server/src/http/request-source.test.ts +85 -0
- package/template/apps/server/src/http/request-source.ts +90 -0
- package/template/apps/server/src/index.ts +45 -45
- package/template/apps/server/src/license/routes.ts +2 -0
- package/template/apps/server/src/license/runtime-service.ts +10 -11
- package/template/apps/server/src/module-bootstrap.ts +2 -2
- package/template/apps/server/src/module-host/initialize-contracts.ts +6 -0
- package/template/apps/server/src/module-host/initialize.ts +30 -44
- package/template/apps/server/src/module-host/request-context.ts +29 -23
- package/template/apps/server/src/module-host/resource-file-policies.test.ts +197 -0
- package/template/apps/server/src/module-host/resource-file-policies.ts +108 -0
- package/template/apps/server/src/module-host/route-declaration.ts +45 -0
- package/template/apps/server/src/module-host/route-installer.ts +27 -3
- package/template/apps/server/src/module-host/storage-port.test.ts +170 -1
- package/template/apps/server/src/module-host/storage-port.ts +134 -1
- package/template/apps/server/src/module-host/storage-scope.ts +89 -0
- package/template/apps/server/src/module-host/tool-handlers.test.ts +305 -0
- package/template/apps/server/src/module-host/tool-handlers.ts +38 -4
- package/template/apps/server/src/module-host/user-directory-composition.test.ts +126 -0
- package/template/apps/server/src/module-host/user-directory-port.test.ts +238 -0
- package/template/apps/server/src/module-host/user-directory-port.ts +139 -0
- package/template/apps/server/src/module-host.ts +4 -0
- package/template/apps/server/src/notification/channel-adapter.ts +46 -0
- package/template/apps/server/src/notification/delivery-policy.test.ts +59 -0
- package/template/apps/server/src/notification/delivery-policy.ts +37 -0
- package/template/apps/server/src/notification/delivery-repository.ts +198 -0
- package/template/apps/server/src/notification/delivery-worker.test.ts +87 -0
- package/template/apps/server/src/notification/delivery-worker.ts +88 -0
- package/template/apps/server/src/notification/drizzle-management.ts +180 -0
- package/template/apps/server/src/notification/drizzle-query.ts +160 -0
- package/template/apps/server/src/notification/drizzle-repository.integration.test.ts +301 -0
- package/template/apps/server/src/notification/drizzle-repository.ts +156 -90
- package/template/apps/server/src/notification/extension-routes.test.ts +119 -0
- package/template/apps/server/src/notification/extension-routes.ts +111 -0
- package/template/apps/server/src/notification/memory-delivery-repository.ts +137 -0
- package/template/apps/server/src/notification/memory-support.ts +72 -0
- package/template/apps/server/src/notification/preference-repository.ts +95 -0
- package/template/apps/server/src/notification/recipient-directory.ts +104 -0
- package/template/apps/server/src/notification/repository.test.ts +171 -0
- package/template/apps/server/src/notification/repository.ts +179 -32
- package/template/apps/server/src/notification/revision-routes.test.ts +186 -0
- package/template/apps/server/src/notification/route-support.ts +225 -0
- package/template/apps/server/src/notification/routes.test.ts +182 -0
- package/template/apps/server/src/notification/routes.ts +225 -132
- package/template/apps/server/src/notification/service.test.ts +104 -0
- package/template/apps/server/src/notification/service.ts +140 -0
- package/template/apps/server/src/notification/smtp-adapter.test.ts +65 -0
- package/template/apps/server/src/notification/smtp-adapter.ts +68 -0
- package/template/apps/server/src/persistence.integration.test.ts +41 -2
- package/template/apps/server/src/route-guards.ts +1 -0
- package/template/apps/server/src/runtime-audit-list.test.ts +39 -0
- package/template/apps/server/src/runtime-development.ts +2 -0
- package/template/apps/server/src/runtime.test.ts +23 -9
- package/template/apps/server/src/runtime.ts +39 -2
- package/template/apps/server/src/scheduler/audit.ts +2 -11
- package/template/apps/server/src/scheduler/definitions.test.ts +6 -0
- package/template/apps/server/src/scheduler/definitions.ts +7 -0
- package/template/apps/server/src/scheduler/recovery-service.ts +2 -11
- package/template/apps/server/src/scheduler/routes.ts +2 -7
- package/template/apps/server/src/search/opensearch-client.test.ts +45 -0
- package/template/apps/server/src/search/opensearch-client.ts +145 -0
- package/template/apps/server/src/search/service.ts +2 -6
- package/template/apps/server/src/settings/routes.ts +15 -3
- package/template/apps/server/src/settings/runtime.ts +10 -1
- package/template/apps/server/src/storage/runtime.ts +13 -0
- package/template/apps/server/src/system/audit-query.ts +33 -4
- package/template/apps/server/src/system/audit-routes.test.ts +58 -4
- package/template/apps/server/src/system/audit-routes.ts +6 -5
- package/template/apps/server/src/system/built-in-roles.ts +1 -0
- package/template/apps/server/src/system/department-organization.ts +320 -0
- package/template/apps/server/src/system/department-routes.test.ts +207 -48
- package/template/apps/server/src/system/department-routes.ts +114 -33
- package/template/apps/server/src/system/drizzle-repository.ts +15 -7
- package/template/apps/server/src/system/drizzle-scoped-repository.ts +5 -3
- package/template/apps/server/src/system/drizzle-system.ts +37 -9
- package/template/apps/server/src/system/drizzle-transaction-context.ts +14 -0
- package/template/apps/server/src/system/identity-route-config.ts +7 -2
- package/template/apps/server/src/system/mutation-runner.ts +23 -0
- package/template/apps/server/src/system/organization-postgres.integration.test.ts +176 -0
- package/template/apps/server/src/system/resource-access-response.ts +12 -6
- package/template/apps/server/src/system/role-department-scope-policy.ts +55 -0
- package/template/apps/server/src/system/route-helpers.ts +2 -5
- package/template/apps/server/src/system/route-types.ts +12 -3
- package/template/apps/server/src/system/routes-validation.test.ts +26 -0
- package/template/apps/server/src/system/routes.test.ts +10 -2
- package/template/apps/server/src/system/routes.ts +70 -36
- package/template/apps/server/src/system/seed.ts +7 -0
- package/template/apps/server/src/work-order/data-scope.integration.test.ts +9 -2
- package/template/apps/server/src/work-order/opensearch-index.test.ts +97 -0
- package/template/apps/server/src/work-order/opensearch-index.ts +313 -0
- package/template/apps/server/src/work-order/search-provider.ts +60 -1
- package/template/apps/server/src/work-order/service.ts +42 -10
- package/template/apps/web/src/app/error-pages.ts +9 -0
- package/template/apps/web/src/components/auth/PasswordChangeForm.vue +16 -11
- package/template/apps/web/src/components/auth/PasswordChangeForm.webtest.ts +14 -0
- package/template/apps/web/src/components/common/FormDialog.vue +16 -2
- package/template/apps/web/src/components/common/ModalLayout.webtest.ts +20 -1
- package/template/apps/web/src/composables/useGlobalSearch.ts +68 -0
- package/template/apps/web/src/composables/usePlatformSettings.ts +8 -7
- package/template/apps/web/src/layout/GlobalSearchResults.vue +58 -0
- package/template/apps/web/src/layout/NavigationSearchDialog.vue +42 -6
- package/template/apps/web/src/layout/NavigationSearchDialog.webtest.ts +71 -0
- package/template/apps/web/src/layout/NotificationMenu.vue +73 -31
- package/template/apps/web/src/layout/NotificationMenu.webtest.ts +44 -1
- package/template/apps/web/src/layout/NotificationMenuItem.vue +61 -0
- package/template/apps/web/src/layout/NotificationPreferencesDialog.vue +181 -0
- package/template/apps/web/src/layout/NotificationPreferencesDialog.webtest.ts +80 -0
- package/template/apps/web/src/lib/date-time.ts +22 -10
- package/template/apps/web/src/lib/date-time.webtest.ts +9 -1
- package/template/apps/web/src/main.ts +14 -1
- package/template/apps/web/src/pages/auth/ChangePasswordPage.vue +16 -1
- package/template/apps/web/src/pages/configuration/components/ModuleConfigVersionList.vue +1 -0
- package/template/apps/web/src/pages/configuration/components/ModuleConfigurationEditor.vue +10 -0
- package/template/apps/web/src/pages/configuration/components/ModuleConfigurationWorkbench.vue +37 -6
- package/template/apps/web/src/pages/errors/PlatformErrorPage.vue +115 -0
- package/template/apps/web/src/pages/errors/PlatformErrorPage.webtest.ts +44 -0
- package/template/apps/web/src/pages/errors/error-page-extension.ts +26 -0
- package/template/apps/web/src/pages/system/SystemNotificationsPage.vue +136 -68
- package/template/apps/web/src/pages/system/SystemNotificationsPage.webtest.ts +224 -12
- package/template/apps/web/src/pages/system/audit-log-presenter.ts +86 -0
- package/template/apps/web/src/pages/system/audit-time-range.ts +28 -0
- package/template/apps/web/src/pages/system/components/AuditActionCell.vue +10 -69
- package/template/apps/web/src/pages/system/components/AuditActionCell.webtest.ts +20 -27
- package/template/apps/web/src/pages/system/components/AuditLogDetailDialog.vue +134 -0
- package/template/apps/web/src/pages/system/components/AuditLogDetailDialog.webtest.ts +76 -0
- package/template/apps/web/src/pages/system/components/AuditLogFilters.vue +116 -52
- package/template/apps/web/src/pages/system/components/AuditLogFilters.webtest.ts +52 -15
- package/template/apps/web/src/pages/system/components/NotificationHistoryDialog.vue +129 -0
- package/template/apps/web/src/pages/system/components/NotificationManagementCard.vue +91 -0
- package/template/apps/web/src/pages/system/components/NotificationPublishDialog.vue +121 -0
- package/template/apps/web/src/pages/system/components/NotificationPublishForm.vue +162 -1
- package/template/apps/web/src/pages/system/components/SystemResourceActions.vue +68 -0
- package/template/apps/web/src/pages/system/components/SystemResourceEditor.vue +74 -0
- package/template/apps/web/src/pages/system/components/SystemResourceFilters.vue +3 -4
- package/template/apps/web/src/pages/system/components/SystemResourcePage.vue +91 -98
- package/template/apps/web/src/pages/system/components/SystemResourcePage.webtest.ts +68 -0
- package/template/apps/web/src/pages/system/components/SystemResourceTable.vue +20 -1
- package/template/apps/web/src/pages/system/components/SystemResourceTable.webtest.ts +28 -7
- package/template/apps/web/src/pages/system/components/department/DepartmentOrganizationForm.vue +210 -0
- package/template/apps/web/src/pages/system/components/department/DepartmentTransitionDialog.vue +112 -0
- package/template/apps/web/src/pages/system/components/user-account/UserAccountForm.vue +17 -4
- package/template/apps/web/src/pages/system/composables/useDepartmentTransitions.ts +79 -0
- package/template/apps/web/src/pages/system/composables/useNotificationPermissions.ts +14 -0
- package/template/apps/web/src/pages/system/composables/useSystemResource.ts +2 -0
- package/template/apps/web/src/pages/system/composables/useSystemResource.webtest.ts +15 -2
- package/template/apps/web/src/pages/system/resource-config.ts +12 -4
- package/template/apps/web/src/router/error-navigation.ts +50 -0
- package/template/apps/web/src/router/error-navigation.webtest.ts +106 -0
- package/template/apps/web/src/router/index.ts +71 -0
- package/template/apps/web/src/router/index.webtest.ts +29 -0
- package/template/apps/web/src/router/manifest-routes.webtest.ts +4 -0
- package/template/apps/web/src/router/shared-scaffold-neutrality.webtest.ts +12 -0
- package/template/apps/web/src/services/auth-api.ts +5 -0
- package/template/apps/web/src/services/http.ts +43 -6
- package/template/apps/web/src/services/http.webtest.ts +70 -0
- package/template/apps/web/src/services/notification-api.ts +85 -2
- package/template/apps/web/src/services/notification-api.webtest.ts +110 -0
- package/template/apps/web/src/services/search-api.ts +39 -0
- package/template/apps/web/src/services/system-api.ts +18 -0
- package/template/apps/web/vitest.config.ts +1 -0
- package/template/docker-compose.yml +68 -0
- package/template/docs/architecture/ai-egress.md +49 -7
- package/template/docs/architecture/ai-runtime.md +34 -6
- package/template/docs/architecture/data-scope-query-enforcement.md +4 -0
- package/template/docs/architecture/object-storage.md +22 -2
- package/template/docs/architecture/search-provider-protocol.md +38 -2
- package/template/docs/development/custom-error-pages.md +78 -0
- package/template/docs/development/custom-login-page.md +3 -3
- package/template/docs/platform/agent-runtime.md +76 -7
- package/template/docs/platform/audit-reliability.md +46 -2
- package/template/docs/platform/business-user-directory.md +82 -0
- package/template/docs/platform/module-configuration.md +13 -4
- package/template/docs/platform/notifications.md +67 -11
- package/template/docs/platform/organization-membership.md +17 -3
- package/template/docs/platform/system-crud-api.md +16 -0
- package/template/docs/platform/web-system-crud.md +6 -2
- package/template/package.json +4 -0
- package/template/packages/config/index.test.ts +45 -0
- package/template/packages/config/package.json +1 -1
- package/template/packages/config/src/platform.ts +169 -48
- package/template/packages/database/drizzle/0035_material_nightshade.sql +10 -0
- package/template/packages/database/drizzle/0036_hesitant_speed.sql +35 -0
- package/template/packages/database/drizzle/0037_absent_nick_fury.sql +2 -0
- package/template/packages/database/drizzle/0038_uneven_wasp.sql +38 -0
- package/template/packages/database/drizzle/meta/0035_snapshot.json +7331 -0
- package/template/packages/database/drizzle/meta/0036_snapshot.json +7436 -0
- package/template/packages/database/drizzle/meta/0037_snapshot.json +7457 -0
- package/template/packages/database/drizzle/meta/0038_snapshot.json +7749 -0
- package/template/packages/database/drizzle/meta/_journal.json +28 -0
- package/template/packages/database/package.json +1 -1
- package/template/packages/database/src/schema/identity.ts +7 -0
- package/template/packages/database/src/schema/notifications.ts +135 -0
- package/template/packages/database/src/schema-notification-delivery.test.ts +54 -0
- package/template/packages/database/src/schema-workflow-notification.test.ts +40 -1
- package/template/packages/modules/package.json +1 -1
- package/template/packages/modules/src/ai-operation-composition.test.ts +68 -0
- package/template/packages/modules/src/ai-operation-composition.ts +115 -0
- package/template/packages/modules/src/manifest.ts +26 -0
- package/template/packages/modules/src/provider-data-policy-composition.test.ts +74 -0
- package/template/packages/modules/src/system-api-permissions.ts +54 -0
- package/template/packages/modules/src/system-audit-actions.ts +15 -0
- package/template/packages/modules/src/system-features.ts +6 -1
- package/template/packages/modules/src/system-module-catalog.test.ts +16 -0
- package/template/packages/modules/src/system-module-catalog.ts +28 -4
- package/template/packages/modules/src/system-modules.test.ts +13 -1
- package/template/packages/modules/src/system-modules.ts +12 -0
- package/template/packages/modules/src/system-permissions.ts +11 -5
- package/template/packages/server-sdk/index.ts +11 -0
- package/template/packages/server-sdk/package.json +3 -2
- package/template/packages/server-sdk/src/ai-operation-registration.ts +2 -1
- package/template/packages/server-sdk/src/module-host.ts +8 -0
- package/template/packages/server-sdk/src/request-context.ts +2 -0
- package/template/packages/server-sdk/src/storage-port.ts +66 -0
- package/template/packages/server-sdk/src/tool-registration.ts +6 -0
- package/template/packages/server-sdk/src/user-directory-port.ts +42 -0
- package/template/packages/shared/package.json +1 -1
- package/template/packages/shared/src/audit.ts +25 -0
- package/template/packages/shared/src/module-configuration.ts +5 -0
- package/template/packages/shared/src/notification.ts +92 -1
- package/template/packages/shared/src/password.ts +7 -2
- package/template/packages/shared/src/platform-settings.ts +2 -0
- package/template/packages/storage/package.json +1 -1
- package/template/packages/storage/src/upload-service.test.ts +16 -0
- package/template/packages/storage/src/upload-service.ts +5 -1
- package/template/scripts/notification-postgres-regression.ts +27 -0
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { describe, expect, test } from "bun:test";
|
|
2
|
-
import type {
|
|
2
|
+
import type {
|
|
3
|
+
ModuleInitializer,
|
|
4
|
+
ModuleResourceFileAccessPolicy,
|
|
5
|
+
} from "@southwind-ai/server-sdk";
|
|
3
6
|
import type { FileMetadata, UploadPolicy } from "@southwind-ai/storage";
|
|
4
7
|
import { createStorageRuntime } from "../storage/runtime.js";
|
|
5
8
|
import { initializeModuleHosts } from "./initialize.js";
|
|
@@ -83,6 +86,120 @@ describe("模块受限 Upload/BlobRead Port", () => {
|
|
|
83
86
|
expect(opened.id).toBe(file.id);
|
|
84
87
|
});
|
|
85
88
|
|
|
89
|
+
test("资源绑定策略允许案件范围内跨上传者读取且只返回最小元数据", async () => {
|
|
90
|
+
const { storage } = setup();
|
|
91
|
+
const file = await seedFile(storage, "user_uploader", "shop.attachment");
|
|
92
|
+
const decisions: Array<{
|
|
93
|
+
outcome: "success" | "denied";
|
|
94
|
+
fileId: string;
|
|
95
|
+
}> = [];
|
|
96
|
+
const port = resourcePort(storage, {
|
|
97
|
+
authorize: ({ fileId, resourceId }, context) =>
|
|
98
|
+
fileId === file.id &&
|
|
99
|
+
resourceId === "order_1" &&
|
|
100
|
+
context.actor.id === "user_reader",
|
|
101
|
+
record: (_policy, input, outcome) => {
|
|
102
|
+
decisions.push({ outcome, fileId: input.fileId });
|
|
103
|
+
},
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
const opened = await port.openResourceFile({
|
|
107
|
+
fileId: file.id,
|
|
108
|
+
purpose: "shop.attachment",
|
|
109
|
+
resourceType: "shop.order-file",
|
|
110
|
+
resourceId: "order_1",
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
expect(opened).toMatchObject({
|
|
114
|
+
id: file.id,
|
|
115
|
+
purpose: "shop.attachment",
|
|
116
|
+
filename: "a.txt",
|
|
117
|
+
mimeType: "text/plain",
|
|
118
|
+
byteSize: 3,
|
|
119
|
+
sha256: file.contentHash.value,
|
|
120
|
+
});
|
|
121
|
+
expect("ownerId" in opened).toBe(false);
|
|
122
|
+
expect("artifactRef" in opened).toBe(false);
|
|
123
|
+
expect(decisions).toEqual([{ outcome: "success", fileId: file.id }]);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test("资源绑定、平台 Guard、用途与资源类型任一不满足均 fail-closed", async () => {
|
|
127
|
+
const { storage } = setup();
|
|
128
|
+
const file = await seedFile(storage, "user_uploader", "shop.attachment");
|
|
129
|
+
let resolverCalls = 0;
|
|
130
|
+
const deniedByGuard = resourcePort(storage, {
|
|
131
|
+
platformAllowed: false,
|
|
132
|
+
authorize: () => {
|
|
133
|
+
resolverCalls += 1;
|
|
134
|
+
return true;
|
|
135
|
+
},
|
|
136
|
+
});
|
|
137
|
+
await expect(
|
|
138
|
+
deniedByGuard.openResourceFile(resourceRequest(file.id)),
|
|
139
|
+
).rejects.toThrow("资源文件访问未授权");
|
|
140
|
+
expect(resolverCalls).toBe(0);
|
|
141
|
+
|
|
142
|
+
const deniedByBinding = resourcePort(storage, {
|
|
143
|
+
authorize: () => false,
|
|
144
|
+
});
|
|
145
|
+
await expect(
|
|
146
|
+
deniedByBinding.openResourceFile(resourceRequest(file.id)),
|
|
147
|
+
).rejects.toThrow("资源文件访问未授权");
|
|
148
|
+
await expect(
|
|
149
|
+
deniedByBinding.openResourceFile(resourceRequest("file_not_exists")),
|
|
150
|
+
).rejects.toThrow("资源文件访问未授权");
|
|
151
|
+
|
|
152
|
+
await expect(
|
|
153
|
+
deniedByBinding.openResourceFile({
|
|
154
|
+
...resourceRequest(file.id),
|
|
155
|
+
resourceType: "shop.unknown",
|
|
156
|
+
}),
|
|
157
|
+
).rejects.toThrow("资源文件访问未授权");
|
|
158
|
+
await expect(
|
|
159
|
+
deniedByBinding.openResourceFile({
|
|
160
|
+
...resourceRequest(file.id),
|
|
161
|
+
purpose: "other.attachment",
|
|
162
|
+
}),
|
|
163
|
+
).rejects.toThrow("资源文件访问未授权");
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
test("资源拒绝与成功审计只接收稳定 ID 和 purpose", async () => {
|
|
167
|
+
const { storage } = setup();
|
|
168
|
+
const file = await seedFile(storage, "user_uploader", "shop.attachment");
|
|
169
|
+
const outcomes: string[] = [];
|
|
170
|
+
const port = resourcePort(storage, {
|
|
171
|
+
authorize: () => false,
|
|
172
|
+
record: (_policy, input, outcome) => {
|
|
173
|
+
outcomes.push(`${outcome}:${input.resourceId}:${input.purpose}`);
|
|
174
|
+
},
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
await expect(
|
|
178
|
+
port.openResourceFile(resourceRequest(file.id)),
|
|
179
|
+
).rejects.toThrow("资源文件访问未授权");
|
|
180
|
+
expect(outcomes).toEqual(["denied:order_1:shop.attachment"]);
|
|
181
|
+
|
|
182
|
+
const failedResolver = resourcePort(storage, {
|
|
183
|
+
authorize: () => {
|
|
184
|
+
throw new Error("database details");
|
|
185
|
+
},
|
|
186
|
+
});
|
|
187
|
+
await expect(
|
|
188
|
+
failedResolver.openResourceFile(resourceRequest(file.id)),
|
|
189
|
+
).rejects.toThrow("资源文件访问未授权");
|
|
190
|
+
|
|
191
|
+
const storageDenied: string[] = [];
|
|
192
|
+
const missingFile = resourcePort(storage, {
|
|
193
|
+
record: (_policy, _input, outcome) => {
|
|
194
|
+
storageDenied.push(outcome);
|
|
195
|
+
},
|
|
196
|
+
});
|
|
197
|
+
await expect(
|
|
198
|
+
missingFile.openResourceFile(resourceRequest("file_not_exists")),
|
|
199
|
+
).rejects.toThrow("资源文件访问未授权");
|
|
200
|
+
expect(storageDenied).toEqual(["denied"]);
|
|
201
|
+
});
|
|
202
|
+
|
|
86
203
|
test("命名空间重叠模块注册同一用途在启动时失败", async () => {
|
|
87
204
|
const initializer = (
|
|
88
205
|
moduleName: string,
|
|
@@ -121,6 +238,58 @@ function setup() {
|
|
|
121
238
|
return { storage, port };
|
|
122
239
|
}
|
|
123
240
|
|
|
241
|
+
interface ResourcePortOptions {
|
|
242
|
+
platformAllowed?: boolean;
|
|
243
|
+
authorize?: ModuleResourceFileAccessPolicy["authorize"];
|
|
244
|
+
record?: NonNullable<
|
|
245
|
+
Parameters<typeof createModuleStoragePort>[1]["recordResourceFileDecision"]
|
|
246
|
+
>;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function resourcePort(
|
|
250
|
+
storage: ReturnType<typeof createStorageRuntime>,
|
|
251
|
+
options: ResourcePortOptions = {},
|
|
252
|
+
) {
|
|
253
|
+
const policy: ModuleResourceFileAccessPolicy = {
|
|
254
|
+
purpose: "shop.attachment",
|
|
255
|
+
resourceType: "shop.order-file",
|
|
256
|
+
featureKey: "shop.order",
|
|
257
|
+
permissionKey: "shop.order.read",
|
|
258
|
+
auditAction: "shop.order.file-read",
|
|
259
|
+
authorize: options.authorize ?? (() => true),
|
|
260
|
+
};
|
|
261
|
+
return createModuleStoragePort(
|
|
262
|
+
{ uploads: storage.uploads, files: storage.files },
|
|
263
|
+
{
|
|
264
|
+
moduleName: "shop",
|
|
265
|
+
ownerId: "user_reader",
|
|
266
|
+
purposes: new Set(["shop.attachment"]),
|
|
267
|
+
actor: {
|
|
268
|
+
id: "user_reader",
|
|
269
|
+
type: "user",
|
|
270
|
+
name: "读取者",
|
|
271
|
+
roleCodes: ["reader"],
|
|
272
|
+
permissionKeys: ["shop.order.read"],
|
|
273
|
+
},
|
|
274
|
+
requestId: "request_resource",
|
|
275
|
+
signal: new AbortController().signal,
|
|
276
|
+
resourceFileAccessPolicies: [policy],
|
|
277
|
+
authorizeResourceFileAccess: () =>
|
|
278
|
+
Promise.resolve(options.platformAllowed ?? true),
|
|
279
|
+
recordResourceFileDecision: options.record,
|
|
280
|
+
},
|
|
281
|
+
);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function resourceRequest(fileId: string) {
|
|
285
|
+
return {
|
|
286
|
+
fileId,
|
|
287
|
+
purpose: "shop.attachment",
|
|
288
|
+
resourceType: "shop.order-file",
|
|
289
|
+
resourceId: "order_1",
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
|
|
124
293
|
function policy(purpose: string): UploadPolicy {
|
|
125
294
|
return {
|
|
126
295
|
purpose,
|
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
ModuleActor,
|
|
3
|
+
ModuleResourceFileAccessPolicy,
|
|
4
|
+
ModuleResourceFileRequest,
|
|
5
|
+
ModuleStoragePort,
|
|
6
|
+
} from "@southwind-ai/server-sdk";
|
|
2
7
|
import {
|
|
3
8
|
UploadError,
|
|
4
9
|
type FileService,
|
|
@@ -15,6 +20,19 @@ export interface ModuleStorageScope {
|
|
|
15
20
|
ownerId: string;
|
|
16
21
|
/** 本模块经 Host 注册的上传用途集合;跨模块与平台用途一律拒绝。 */
|
|
17
22
|
purposes: ReadonlySet<string>;
|
|
23
|
+
/** Host 颁发的 Actor 只读视图;资源文件访问策略不能从输入伪造 Actor。 */
|
|
24
|
+
actor?: ModuleActor;
|
|
25
|
+
requestId?: string;
|
|
26
|
+
signal?: AbortSignal;
|
|
27
|
+
resourceFileAccessPolicies?: readonly ModuleResourceFileAccessPolicy[];
|
|
28
|
+
authorizeResourceFileAccess?: (
|
|
29
|
+
policy: ModuleResourceFileAccessPolicy,
|
|
30
|
+
) => Promise<boolean>;
|
|
31
|
+
recordResourceFileDecision?: (
|
|
32
|
+
policy: ModuleResourceFileAccessPolicy,
|
|
33
|
+
input: ModuleResourceFileRequest,
|
|
34
|
+
outcome: "success" | "denied",
|
|
35
|
+
) => Promise<void> | void;
|
|
18
36
|
}
|
|
19
37
|
|
|
20
38
|
/**
|
|
@@ -23,6 +41,7 @@ export interface ModuleStorageScope {
|
|
|
23
41
|
* - 上传会话强制 purpose ∈ 本模块已注册 Policy,Owner 由宿主以当前 Actor 补齐;
|
|
24
42
|
* - 文件读取走 FileService 的 Owner 约束,且文件用途必须属于本模块;
|
|
25
43
|
* - 公开读取走 FileService 的 Purpose 约束,用途同样必须属于本模块;
|
|
44
|
+
* - 资源文件读取须匹配当前模块注册的绑定策略,可读取显式依赖模块的用途;
|
|
26
45
|
* - 模块不直接接触 BlobStore 或 Upload Repository。
|
|
27
46
|
*/
|
|
28
47
|
export function createModuleStoragePort(
|
|
@@ -43,9 +62,105 @@ export function createModuleStoragePort(
|
|
|
43
62
|
requireModulePurpose(scope, purpose);
|
|
44
63
|
return services.files.openPublic(fileId, purpose);
|
|
45
64
|
},
|
|
65
|
+
async openResourceFile(input) {
|
|
66
|
+
const policy = findResourceFileAccessPolicy(scope, input);
|
|
67
|
+
const actor = scope.actor;
|
|
68
|
+
if (
|
|
69
|
+
!actor ||
|
|
70
|
+
!scope.requestId ||
|
|
71
|
+
!scope.signal ||
|
|
72
|
+
!scope.authorizeResourceFileAccess
|
|
73
|
+
) {
|
|
74
|
+
throw forbiddenResourceFile();
|
|
75
|
+
}
|
|
76
|
+
if (!(await authorizeHost(scope, policy))) {
|
|
77
|
+
return denyResourceFile(scope, policy, input);
|
|
78
|
+
}
|
|
79
|
+
if (!(await authorizeBusinessResource(scope, policy, input, actor))) {
|
|
80
|
+
return denyResourceFile(scope, policy, input);
|
|
81
|
+
}
|
|
82
|
+
let opened: Awaited<ReturnType<FileService["open"]>>;
|
|
83
|
+
try {
|
|
84
|
+
opened = await services.files.open(input.fileId, actor.id, true);
|
|
85
|
+
} catch {
|
|
86
|
+
return denyResourceFile(scope, policy, input);
|
|
87
|
+
}
|
|
88
|
+
if (opened.purpose !== input.purpose) {
|
|
89
|
+
return denyResourceFile(scope, policy, input);
|
|
90
|
+
}
|
|
91
|
+
await scope.recordResourceFileDecision?.(policy, input, "success");
|
|
92
|
+
return {
|
|
93
|
+
id: opened.id,
|
|
94
|
+
purpose: opened.purpose,
|
|
95
|
+
filename: opened.filename,
|
|
96
|
+
mimeType: opened.mimeType,
|
|
97
|
+
byteSize: opened.byteSize,
|
|
98
|
+
sha256: opened.contentHash.value,
|
|
99
|
+
body: opened.body,
|
|
100
|
+
};
|
|
101
|
+
},
|
|
46
102
|
};
|
|
47
103
|
}
|
|
48
104
|
|
|
105
|
+
async function denyResourceFile(
|
|
106
|
+
scope: ModuleStorageScope,
|
|
107
|
+
policy: ModuleResourceFileAccessPolicy,
|
|
108
|
+
input: ModuleResourceFileRequest,
|
|
109
|
+
): Promise<never> {
|
|
110
|
+
try {
|
|
111
|
+
await scope.recordResourceFileDecision?.(policy, input, "denied");
|
|
112
|
+
} catch {
|
|
113
|
+
// 审计失败不能把底层错误暴露给调用方,访问仍保持 fail-closed。
|
|
114
|
+
}
|
|
115
|
+
throw forbiddenResourceFile();
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
async function authorizeHost(
|
|
119
|
+
scope: ModuleStorageScope,
|
|
120
|
+
policy: ModuleResourceFileAccessPolicy,
|
|
121
|
+
): Promise<boolean> {
|
|
122
|
+
try {
|
|
123
|
+
return (await scope.authorizeResourceFileAccess?.(policy)) === true;
|
|
124
|
+
} catch {
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
async function authorizeBusinessResource(
|
|
130
|
+
scope: ModuleStorageScope,
|
|
131
|
+
policy: ModuleResourceFileAccessPolicy,
|
|
132
|
+
input: ModuleResourceFileRequest,
|
|
133
|
+
actor: ModuleActor,
|
|
134
|
+
): Promise<boolean> {
|
|
135
|
+
try {
|
|
136
|
+
return (
|
|
137
|
+
(await policy.authorize(
|
|
138
|
+
{ fileId: input.fileId, resourceId: input.resourceId },
|
|
139
|
+
{
|
|
140
|
+
actor,
|
|
141
|
+
requestId: scope.requestId!,
|
|
142
|
+
signal: scope.signal!,
|
|
143
|
+
},
|
|
144
|
+
)) === true
|
|
145
|
+
);
|
|
146
|
+
} catch {
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function findResourceFileAccessPolicy(
|
|
152
|
+
scope: ModuleStorageScope,
|
|
153
|
+
input: ModuleResourceFileRequest,
|
|
154
|
+
): ModuleResourceFileAccessPolicy {
|
|
155
|
+
const policy = scope.resourceFileAccessPolicies?.find(
|
|
156
|
+
(candidate) =>
|
|
157
|
+
candidate.purpose === input.purpose &&
|
|
158
|
+
candidate.resourceType === input.resourceType,
|
|
159
|
+
);
|
|
160
|
+
if (!policy) throw forbiddenResourceFile();
|
|
161
|
+
return policy;
|
|
162
|
+
}
|
|
163
|
+
|
|
49
164
|
function requireModulePurpose(
|
|
50
165
|
scope: ModuleStorageScope,
|
|
51
166
|
purpose: string,
|
|
@@ -57,3 +172,21 @@ function requireModulePurpose(
|
|
|
57
172
|
);
|
|
58
173
|
}
|
|
59
174
|
}
|
|
175
|
+
|
|
176
|
+
function forbiddenResourceFile(): UploadError {
|
|
177
|
+
return new UploadError("UPLOAD_FORBIDDEN", "资源文件访问未授权");
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export function unavailableModuleStoragePort(
|
|
181
|
+
moduleName: string,
|
|
182
|
+
): ModuleStoragePort {
|
|
183
|
+
const fail = (): never => {
|
|
184
|
+
throw new Error(`模块 ${moduleName} 的存储 Port 未挂载`);
|
|
185
|
+
};
|
|
186
|
+
return {
|
|
187
|
+
createUploadSession: () => fail(),
|
|
188
|
+
openFile: () => fail(),
|
|
189
|
+
openPublicFile: () => fail(),
|
|
190
|
+
openResourceFile: () => fail(),
|
|
191
|
+
};
|
|
192
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { randomUUIDv7 } from "bun";
|
|
2
|
+
import type { FeatureFlagDefinition } from "@southwind-ai/shared";
|
|
3
|
+
import type {
|
|
4
|
+
ModuleResourceFileAccessPolicy,
|
|
5
|
+
ModuleResourceFileRequest,
|
|
6
|
+
} from "@southwind-ai/server-sdk";
|
|
7
|
+
import { evaluateAccessGuards } from "../access-guards.js";
|
|
8
|
+
import { isServerScopeContext } from "../data-access/context.js";
|
|
9
|
+
import { auditActor, type RequestGuardContext } from "../guards.js";
|
|
10
|
+
import type { createServerRuntime } from "../runtime.js";
|
|
11
|
+
import { findFeature } from "../route-guards.js";
|
|
12
|
+
import type { ModuleStorageScope } from "./storage-port.js";
|
|
13
|
+
import { toModuleActor } from "./request-context.js";
|
|
14
|
+
|
|
15
|
+
type ServerRuntime = ReturnType<typeof createServerRuntime>;
|
|
16
|
+
|
|
17
|
+
export interface ModuleStorageScopeInput {
|
|
18
|
+
readonly moduleName: string;
|
|
19
|
+
readonly purposes: ReadonlySet<string>;
|
|
20
|
+
readonly resourceFileAccessPolicies: readonly ModuleResourceFileAccessPolicy[];
|
|
21
|
+
readonly guardContext: RequestGuardContext;
|
|
22
|
+
readonly runtime: ServerRuntime;
|
|
23
|
+
readonly features: readonly FeatureFlagDefinition[];
|
|
24
|
+
readonly signal: AbortSignal;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function createModuleStorageScope(
|
|
28
|
+
input: ModuleStorageScopeInput,
|
|
29
|
+
): ModuleStorageScope {
|
|
30
|
+
const actor = toModuleActor(input.guardContext.actor);
|
|
31
|
+
return {
|
|
32
|
+
moduleName: input.moduleName,
|
|
33
|
+
ownerId: actor.id,
|
|
34
|
+
purposes: input.purposes,
|
|
35
|
+
actor,
|
|
36
|
+
requestId: input.guardContext.requestId,
|
|
37
|
+
signal: input.signal,
|
|
38
|
+
resourceFileAccessPolicies: input.resourceFileAccessPolicies,
|
|
39
|
+
async authorizeResourceFileAccess(policy) {
|
|
40
|
+
if (
|
|
41
|
+
!isServerScopeContext(input.guardContext.actor.scopeContext) ||
|
|
42
|
+
input.guardContext.actor.scopeContext.actorId !== actor.id
|
|
43
|
+
) {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
const feature = findFeature([...input.features], policy.featureKey);
|
|
47
|
+
if (!feature) return false;
|
|
48
|
+
const result = await evaluateAccessGuards(
|
|
49
|
+
input.runtime,
|
|
50
|
+
input.guardContext,
|
|
51
|
+
{
|
|
52
|
+
permission: { permissionKey: policy.permissionKey },
|
|
53
|
+
feature,
|
|
54
|
+
},
|
|
55
|
+
);
|
|
56
|
+
return result.decision.allowed;
|
|
57
|
+
},
|
|
58
|
+
recordResourceFileDecision(policy, request, outcome) {
|
|
59
|
+
input.runtime.auditSink.recordEvent(
|
|
60
|
+
resourceFileEvent(input, policy, request, outcome),
|
|
61
|
+
);
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function resourceFileEvent(
|
|
67
|
+
input: ModuleStorageScopeInput,
|
|
68
|
+
policy: ModuleResourceFileAccessPolicy,
|
|
69
|
+
request: ModuleResourceFileRequest,
|
|
70
|
+
outcome: "success" | "denied",
|
|
71
|
+
) {
|
|
72
|
+
return {
|
|
73
|
+
id: randomUUIDv7(),
|
|
74
|
+
action: policy.auditAction,
|
|
75
|
+
outcome,
|
|
76
|
+
actor: auditActor(input.guardContext),
|
|
77
|
+
target: {
|
|
78
|
+
type: request.resourceType,
|
|
79
|
+
id: request.resourceId,
|
|
80
|
+
},
|
|
81
|
+
module: input.moduleName,
|
|
82
|
+
requestId: input.guardContext.requestId,
|
|
83
|
+
metadata: {
|
|
84
|
+
fileId: request.fileId,
|
|
85
|
+
purpose: request.purpose,
|
|
86
|
+
},
|
|
87
|
+
occurredAt: new Date().toISOString(),
|
|
88
|
+
};
|
|
89
|
+
}
|