@@ -97,6 +97,7 @@ const deleteApiKey = async (id: string, workspaceId?: string) => {
9797const importKeys = async ( body : any ) => {
9898 try {
9999 const jsonFile = body . jsonFile
100+ const workspaceId = body . workspaceId
100101 const splitDataURI = jsonFile . split ( ',' )
101102 if ( splitDataURI [ 0 ] !== 'data:application/json;base64' ) {
102103 throw new InternalFlowiseError ( StatusCodes . INTERNAL_SERVER_ERROR , `Invalid dataURI` )
@@ -105,11 +106,46 @@ const importKeys = async (body: any) => {
105106 const plain = bf . toString ( 'utf8' )
106107 const keys = JSON . parse ( plain )
107108
109+ // Validate schema of imported keys
110+ if ( ! Array . isArray ( keys ) ) {
111+ throw new InternalFlowiseError ( StatusCodes . BAD_REQUEST , `Invalid format: Expected an array of API keys` )
112+ }
113+
114+ const requiredFields = [ 'keyName' , 'apiKey' , 'apiSecret' , 'createdAt' , 'id' ]
115+ for ( let i = 0 ; i < keys . length ; i ++ ) {
116+ const key = keys [ i ]
117+ if ( typeof key !== 'object' || key === null ) {
118+ throw new InternalFlowiseError ( StatusCodes . BAD_REQUEST , `Invalid format: Key at index ${ i } is not an object` )
119+ }
120+
121+ for ( const field of requiredFields ) {
122+ if ( ! ( field in key ) ) {
123+ throw new InternalFlowiseError (
124+ StatusCodes . BAD_REQUEST ,
125+ `Invalid format: Key at index ${ i } is missing required field '${ field } '`
126+ )
127+ }
128+ if ( typeof key [ field ] !== 'string' ) {
129+ throw new InternalFlowiseError (
130+ StatusCodes . BAD_REQUEST ,
131+ `Invalid format: Key at index ${ i } field '${ field } ' must be a string`
132+ )
133+ }
134+ if ( key [ field ] . trim ( ) === '' ) {
135+ throw new InternalFlowiseError (
136+ StatusCodes . BAD_REQUEST ,
137+ `Invalid format: Key at index ${ i } field '${ field } ' cannot be empty`
138+ )
139+ }
140+ }
141+ }
142+
108143 const appServer = getRunningExpressApp ( )
109- const allApiKeys = await appServer . AppDataSource . getRepository ( ApiKey ) . find ( )
144+ const allApiKeys = await appServer . AppDataSource . getRepository ( ApiKey ) . findBy ( getWorkspaceSearchOptions ( workspaceId ) )
110145 if ( body . importMode === 'replaceAll' ) {
111146 await appServer . AppDataSource . getRepository ( ApiKey ) . delete ( {
112- id : Not ( IsNull ( ) )
147+ id : Not ( IsNull ( ) ) ,
148+ workspaceId : workspaceId
113149 } )
114150 }
115151 if ( body . importMode === 'errorIfExist' ) {
@@ -127,12 +163,13 @@ const importKeys = async (body: any) => {
127163 if ( keyNameExists ) {
128164 const keyIndex = allApiKeys . findIndex ( ( k ) => k . keyName === key . keyName )
129165 switch ( body . importMode ) {
130- case 'overwriteIfExist' : {
166+ case 'overwriteIfExist' :
167+ case 'replaceAll' : {
131168 const currentKey = allApiKeys [ keyIndex ]
132169 currentKey . id = uuidv4 ( )
133170 currentKey . apiKey = key . apiKey
134171 currentKey . apiSecret = key . apiSecret
135- currentKey . workspaceId = body . workspaceId
172+ currentKey . workspaceId = workspaceId
136173 await appServer . AppDataSource . getRepository ( ApiKey ) . save ( currentKey )
137174 break
138175 }
@@ -154,12 +191,12 @@ const importKeys = async (body: any) => {
154191 newKey . apiKey = key . apiKey
155192 newKey . apiSecret = key . apiSecret
156193 newKey . keyName = key . keyName
157- newKey . workspaceId = body . workspaceId
194+ newKey . workspaceId = workspaceId
158195 const newKeyEntity = appServer . AppDataSource . getRepository ( ApiKey ) . create ( newKey )
159196 await appServer . AppDataSource . getRepository ( ApiKey ) . save ( newKeyEntity )
160197 }
161198 }
162- return await getAllApiKeysFromDB ( body . workspaceId )
199+ return await getAllApiKeysFromDB ( workspaceId )
163200 } catch ( error ) {
164201 throw new InternalFlowiseError ( StatusCodes . INTERNAL_SERVER_ERROR , `Error: apikeyService.importKeys - ${ getErrorMessage ( error ) } ` )
165202 }
0 commit comments