@@ -25,8 +25,7 @@ const TEST_FILES = fs
2525const CLOUD_URL_DEFAULT = "https://api.tinybird.co" ;
2626const WORKSPACE_ID_PATTERN =
2727 / ^ [ 0 - 9 a - f ] { 8 } - [ 0 - 9 a - f ] { 4 } - [ 1 - 8 ] [ 0 - 9 a - f ] { 3 } - [ 8 9 a b ] [ 0 - 9 a - f ] { 3 } - [ 0 - 9 a - f ] { 12 } $ / i;
28- const WORKSPACE_ID_SEARCH_PATTERN =
29- / [ 0 - 9 a - f ] { 8 } - [ 0 - 9 a - f ] { 4 } - [ 1 - 8 ] [ 0 - 9 a - f ] { 3 } - [ 8 9 a b ] [ 0 - 9 a - f ] { 3 } - [ 0 - 9 a - f ] { 12 } / gi;
28+ const LOCAL_TOKEN_SIGNING_KEY = "tinybird-local" ;
3029const LOCAL_IDENTIFIERS = {
3130 workspaceId : "00000000-0000-4000-8000-000000000001" ,
3231 workspaceTokenId : "00000000-0000-4000-8000-000000000002" ,
@@ -373,7 +372,7 @@ const cloudEnvironment = (env = process.env) => {
373372const encodeLocalToken = ( userId , tokenId ) => {
374373 const payload = `{"u": "${ userId } ", "id": "${ tokenId } ", "host": null}` ;
375374 const encodedPayload = Buffer . from ( payload ) . toString ( "base64url" ) ;
376- const signature = createHmac ( "sha256" , "abcd" )
375+ const signature = createHmac ( "sha256" , LOCAL_TOKEN_SIGNING_KEY )
377376 . update ( encodedPayload )
378377 . digest ( "base64url" ) ;
379378 return `p.${ encodedPayload } .${ signature } ` ;
@@ -433,18 +432,34 @@ const runProcessCapture = (command, args, options = {}) => {
433432 if ( result . error || result . status !== 0 ) {
434433 throw new Error ( "Unable to verify Tinybird workspace identity." ) ;
435434 }
436- return ` ${ result . stdout ?? "" } \n ${ result . stderr ?? "" } ` ;
435+ return result . stdout ?? "" ;
437436} ;
438437
439438const verifyCloudWorkspace = ( env = process . env , run = runProcessCapture ) => {
440439 const environment = cloudEnvironment ( env ) ;
441- const step = cloudCliStep ( "--cloud" , "workspace" , "current" ) ;
440+ const step = cloudCliStep (
441+ "--cloud" ,
442+ "--output" ,
443+ "json" ,
444+ "workspace" ,
445+ "current" ,
446+ ) ;
442447 assertSafeStep ( step ) ;
443448 const output = run ( step . command , step . args , { env : environment } ) ;
444- const workspaceIds = ( output . match ( WORKSPACE_ID_SEARCH_PATTERN ) ?? [ ] ) . map (
445- ( workspaceId ) => workspaceId . toLowerCase ( ) ,
446- ) ;
447- if ( ! workspaceIds . includes ( environment . TINYBIRD_WORKSPACE_ID . toLowerCase ( ) ) ) {
449+ let workspace ;
450+ try {
451+ workspace = JSON . parse ( output ) ;
452+ } catch {
453+ throw new Error ( "Unable to parse Tinybird workspace identity." ) ;
454+ }
455+ if (
456+ ! workspace ||
457+ typeof workspace !== "object" ||
458+ Array . isArray ( workspace ) ||
459+ typeof workspace . id !== "string" ||
460+ workspace . id . toLowerCase ( ) !==
461+ environment . TINYBIRD_WORKSPACE_ID . toLowerCase ( )
462+ ) {
448463 throw new Error (
449464 "Tinybird deploy token does not target TINYBIRD_WORKSPACE_ID." ,
450465 ) ;
0 commit comments