@@ -5,13 +5,33 @@ import path from "node:path";
55import { importFreshModule } from "openclaw/plugin-sdk/test-fixtures" ;
66import { afterEach , describe , expect , it , vi } from "vitest" ;
77import type { ModelProviderConfig } from "../config/types.models.js" ;
8- import { resolveBundledProviderPolicySurface } from "./provider-public-artifacts.js" ;
8+ import {
9+ resolveBundledProviderPolicySurface ,
10+ resolveProviderPolicySurface ,
11+ } from "./provider-public-artifacts.js" ;
12+
13+ function writeExternalPolicyFixture ( ) : string {
14+ const pluginRoot = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "openclaw-provider-policy-external-" ) ) ;
15+ fs . writeFileSync (
16+ path . join ( pluginRoot , "provider-policy-api.js" ) ,
17+ [
18+ "export function resolveThinkingProfile({ modelId }) {" ,
19+ ' return modelId === "full"' ,
20+ ' ? { levels: [{ id: "off" }, { id: "high" }, { id: "max" }], defaultLevel: "off" }' ,
21+ ' : { levels: [{ id: "off" }, { id: "low", label: "on" }], defaultLevel: "off" };' ,
22+ "}" ,
23+ "" ,
24+ ] . join ( "\n" ) ,
25+ "utf8" ,
26+ ) ;
27+ return pluginRoot ;
28+ }
929
1030describe ( "provider public artifacts" , ( ) => {
1131 const originalBundledPluginsDir = process . env . OPENCLAW_BUNDLED_PLUGINS_DIR ;
1232 const originalTrustBundledPluginsDir = process . env . OPENCLAW_TEST_TRUST_BUNDLED_PLUGINS_DIR ;
1333
14- afterEach ( ( ) => {
34+ function restoreBundledPluginEnv ( ) {
1535 if ( originalBundledPluginsDir === undefined ) {
1636 delete process . env . OPENCLAW_BUNDLED_PLUGINS_DIR ;
1737 } else {
@@ -22,6 +42,10 @@ describe("provider public artifacts", () => {
2242 } else {
2343 process . env . OPENCLAW_TEST_TRUST_BUNDLED_PLUGINS_DIR = originalTrustBundledPluginsDir ;
2444 }
45+ }
46+
47+ afterEach ( ( ) => {
48+ restoreBundledPluginEnv ( ) ;
2549 vi . doUnmock ( "./bundled-dir.js" ) ;
2650 vi . doUnmock ( "./manifest-registry.js" ) ;
2751 vi . doUnmock ( "./public-surface-loader.js" ) ;
@@ -98,6 +122,67 @@ describe("provider public artifacts", () => {
98122 } ) ;
99123 } ) ;
100124
125+ it ( "loads trusted official external provider policy before runtime registration" , ( ) => {
126+ const bundledPluginsDir = fs . mkdtempSync (
127+ path . join ( os . tmpdir ( ) , "openclaw-empty-bundled-plugins-" ) ,
128+ ) ;
129+ const pluginRoot = writeExternalPolicyFixture ( ) ;
130+
131+ try {
132+ process . env . OPENCLAW_BUNDLED_PLUGINS_DIR = bundledPluginsDir ;
133+ process . env . OPENCLAW_TEST_TRUST_BUNDLED_PLUGINS_DIR = "1" ;
134+ const fixturePlugin = {
135+ id : "fixture-provider" ,
136+ origin : "external" ,
137+ trustedOfficialInstall : true ,
138+ rootDir : pluginRoot ,
139+ providers : [ "fixture-provider" ] ,
140+ cliBackends : [ ] ,
141+ } as const ;
142+ const surface = resolveProviderPolicySurface ( "fixture-provider" , {
143+ manifestRegistry : { plugins : [ fixturePlugin as never ] } ,
144+ } ) ;
145+
146+ expect (
147+ surface
148+ ?. resolveThinkingProfile ?.( { provider : "fixture-provider" , modelId : "full" } )
149+ ?. levels . map ( ( level ) => level . id ) ,
150+ ) . toEqual ( [ "off" , "high" , "max" ] ) ;
151+ expect (
152+ surface
153+ ?. resolveThinkingProfile ?.( { provider : "fixture-provider" , modelId : "legacy" } )
154+ ?. levels . map ( ( level ) => level . label ) ,
155+ ) . toEqual ( [ undefined , "on" ] ) ;
156+ } finally {
157+ restoreBundledPluginEnv ( ) ;
158+ fs . rmSync ( pluginRoot , { recursive : true , force : true } ) ;
159+ fs . rmSync ( bundledPluginsDir , { recursive : true , force : true } ) ;
160+ }
161+ } ) ;
162+
163+ it ( "does not load public policy code from untrusted external plugins" , ( ) => {
164+ const pluginRoot = writeExternalPolicyFixture ( ) ;
165+ try {
166+ expect (
167+ resolveProviderPolicySurface ( "fixture-provider" , {
168+ manifestRegistry : {
169+ plugins : [
170+ {
171+ id : "fixture-provider" ,
172+ origin : "external" ,
173+ rootDir : pluginRoot ,
174+ providers : [ "fixture-provider" ] ,
175+ cliBackends : [ ] ,
176+ } as never ,
177+ ] ,
178+ } ,
179+ } ) ,
180+ ) . toBeNull ( ) ;
181+ } finally {
182+ fs . rmSync ( pluginRoot , { recursive : true , force : true } ) ;
183+ }
184+ } ) ;
185+
101186 it ( "resolves multi-provider policy artifacts by manifest-owned provider id" , async ( ) => {
102187 const bundledPluginsDir = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "openclaw-provider-policy-" ) ) ;
103188 const pluginDir = path . join ( bundledPluginsDir , "openai" ) ;
0 commit comments