@@ -15,7 +15,9 @@ import { resolveOpenClawStateSqlitePath } from "../state/openclaw-state-db.paths
1515import type {
1616 HostedOfficialExternalPluginCatalogMetadata ,
1717 HostedOfficialExternalPluginCatalogSnapshot ,
18+ HostedOfficialExternalPluginCatalogSnapshotMonotonicState ,
1819 HostedOfficialExternalPluginCatalogSnapshotStore ,
20+ HostedOfficialExternalPluginCatalogTrustState ,
1921} from "./official-external-plugin-catalog.js" ;
2022
2123export type HostedOfficialExternalPluginCatalogSnapshotStoreOptions = {
@@ -32,6 +34,11 @@ type HostedCatalogSnapshotRow = {
3234 last_modified : string | null ;
3335 checksum : string ;
3436 saved_at : string ;
37+ trust_mode : string | null ;
38+ trust_key_id : string | null ;
39+ trust_signature_count : number | bigint | null ;
40+ trust_threshold : number | bigint | null ;
41+ trust_verified_at : string | null ;
3542} ;
3643
3744type HostedCatalogSnapshotDatabase = Pick <
@@ -70,6 +77,90 @@ function resolveStateDatabasePath(
7077 return resolveOpenClawStateSqlitePath ( resolveStoreEnv ( options ) ?? process . env ) ;
7178}
7279
80+ function rowToTrustState (
81+ row : HostedCatalogSnapshotRow ,
82+ ) : HostedOfficialExternalPluginCatalogTrustState | undefined {
83+ if (
84+ row . trust_mode !== "signed" ||
85+ ! row . trust_key_id ||
86+ row . trust_signature_count === null ||
87+ row . trust_threshold === null ||
88+ ! row . trust_verified_at
89+ ) {
90+ return undefined ;
91+ }
92+ return {
93+ mode : "signed" ,
94+ signedBy : row . trust_key_id ,
95+ signatureCount : Number ( row . trust_signature_count ) ,
96+ threshold : Number ( row . trust_threshold ) ,
97+ verifiedAt : row . trust_verified_at ,
98+ } ;
99+ }
100+
101+ function decodeBase64Payload ( payload : string ) : string {
102+ const normalized = payload . replace ( / - / g, "+" ) . replace ( / _ / g, "/" ) ;
103+ return Buffer . from ( normalized , "base64" ) . toString ( "utf8" ) ;
104+ }
105+
106+ function readMonotonicStateFromBody (
107+ body : string ,
108+ ) : HostedOfficialExternalPluginCatalogSnapshotMonotonicState | undefined {
109+ try {
110+ const document = JSON . parse ( body ) as {
111+ payload ?: unknown ;
112+ sequence ?: unknown ;
113+ generatedAt ?: unknown ;
114+ } ;
115+ const feed =
116+ typeof document . payload === "string"
117+ ? ( JSON . parse ( decodeBase64Payload ( document . payload ) ) as {
118+ sequence ?: unknown ;
119+ generatedAt ?: unknown ;
120+ } )
121+ : document ;
122+ if ( typeof feed . sequence !== "number" || typeof feed . generatedAt !== "string" ) {
123+ return undefined ;
124+ }
125+ return {
126+ mode : "signed-feed" ,
127+ sequence : feed . sequence ,
128+ generatedAt : feed . generatedAt ,
129+ } ;
130+ } catch {
131+ return undefined ;
132+ }
133+ }
134+
135+ function isMonotonicRollback ( params : {
136+ candidate : HostedOfficialExternalPluginCatalogSnapshotMonotonicState ;
137+ current : HostedOfficialExternalPluginCatalogSnapshotMonotonicState ;
138+ } ) : boolean {
139+ if ( params . candidate . sequence < params . current . sequence ) {
140+ return true ;
141+ }
142+ if ( params . candidate . sequence > params . current . sequence ) {
143+ return false ;
144+ }
145+ return Date . parse ( params . candidate . generatedAt ) < Date . parse ( params . current . generatedAt ) ;
146+ }
147+
148+ function assertSignedSnapshotWriteIsMonotonic ( params : {
149+ candidate : HostedOfficialExternalPluginCatalogSnapshotMonotonicState | undefined ;
150+ current : HostedCatalogSnapshotRow | undefined ;
151+ } ) : void {
152+ if ( params . candidate ?. mode !== "signed-feed" || params . current ?. trust_mode !== "signed" ) {
153+ return ;
154+ }
155+ const current = readMonotonicStateFromBody ( params . current . body ) ;
156+ if ( ! current ) {
157+ return ;
158+ }
159+ if ( isMonotonicRollback ( { candidate : params . candidate , current } ) ) {
160+ throw new Error ( "hosted catalog signed feed sequence is older than current snapshot" ) ;
161+ }
162+ }
163+
73164function rowToSnapshot (
74165 row : HostedCatalogSnapshotRow | undefined ,
75166) : HostedOfficialExternalPluginCatalogSnapshot | null {
@@ -83,10 +174,12 @@ function rowToSnapshot(
83174 ...( row . etag ? { etag : row . etag } : { } ) ,
84175 ...( row . last_modified ? { lastModified : row . last_modified } : { } ) ,
85176 } ;
177+ const trust = rowToTrustState ( row ) ;
86178 return {
87179 body : row . body ,
88180 metadata,
89181 savedAt : row . saved_at ,
182+ ...( trust ? { trust } : { } ) ,
90183 } ;
91184}
92185
@@ -106,7 +199,20 @@ export function createSqliteHostedOfficialExternalPluginCatalogSnapshotStore(
106199 database . db ,
107200 stateDb
108201 . selectFrom ( "official_external_plugin_catalog_snapshots" )
109- . select ( [ "feed_url" , "body" , "status" , "etag" , "last_modified" , "checksum" , "saved_at" ] )
202+ . select ( [
203+ "feed_url" ,
204+ "body" ,
205+ "status" ,
206+ "etag" ,
207+ "last_modified" ,
208+ "checksum" ,
209+ "saved_at" ,
210+ "trust_mode" ,
211+ "trust_key_id" ,
212+ "trust_signature_count" ,
213+ "trust_threshold" ,
214+ "trust_verified_at" ,
215+ ] )
110216 . where ( "feed_url" , "=" , url ) ,
111217 ) as HostedCatalogSnapshotRow | undefined ;
112218 return rowToSnapshot ( row ) ;
@@ -115,6 +221,30 @@ export function createSqliteHostedOfficialExternalPluginCatalogSnapshotStore(
115221 const now = Date . now ( ) ;
116222 runOpenClawStateWriteTransaction ( ( database ) => {
117223 const stateDb = getNodeSqliteKysely < HostedCatalogSnapshotDatabase > ( database . db ) ;
224+ const current = executeSqliteQueryTakeFirstSync (
225+ database . db ,
226+ stateDb
227+ . selectFrom ( "official_external_plugin_catalog_snapshots" )
228+ . select ( [
229+ "feed_url" ,
230+ "body" ,
231+ "status" ,
232+ "etag" ,
233+ "last_modified" ,
234+ "checksum" ,
235+ "saved_at" ,
236+ "trust_mode" ,
237+ "trust_key_id" ,
238+ "trust_signature_count" ,
239+ "trust_threshold" ,
240+ "trust_verified_at" ,
241+ ] )
242+ . where ( "feed_url" , "=" , snapshot . metadata . url ) ,
243+ ) as HostedCatalogSnapshotRow | undefined ;
244+ assertSignedSnapshotWriteIsMonotonic ( {
245+ candidate : snapshot . monotonic ,
246+ current,
247+ } ) ;
118248 executeSqliteQuerySync (
119249 database . db ,
120250 stateDb
@@ -128,6 +258,11 @@ export function createSqliteHostedOfficialExternalPluginCatalogSnapshotStore(
128258 checksum : snapshot . metadata . checksum ,
129259 saved_at : snapshot . savedAt ,
130260 updated_at_ms : now ,
261+ trust_mode : snapshot . trust ?. mode ?? null ,
262+ trust_key_id : snapshot . trust ?. signedBy ?? null ,
263+ trust_signature_count : snapshot . trust ?. signatureCount ?? null ,
264+ trust_threshold : snapshot . trust ?. threshold ?? null ,
265+ trust_verified_at : snapshot . trust ?. verifiedAt ?? null ,
131266 } )
132267 . onConflict ( ( conflict ) =>
133268 conflict . column ( "feed_url" ) . doUpdateSet ( {
@@ -138,6 +273,11 @@ export function createSqliteHostedOfficialExternalPluginCatalogSnapshotStore(
138273 checksum : snapshot . metadata . checksum ,
139274 saved_at : snapshot . savedAt ,
140275 updated_at_ms : now ,
276+ trust_mode : snapshot . trust ?. mode ?? null ,
277+ trust_key_id : snapshot . trust ?. signedBy ?? null ,
278+ trust_signature_count : snapshot . trust ?. signatureCount ?? null ,
279+ trust_threshold : snapshot . trust ?. threshold ?? null ,
280+ trust_verified_at : snapshot . trust ?. verifiedAt ?? null ,
141281 } ) ,
142282 ) ,
143283 ) ;
0 commit comments