@@ -23,6 +23,10 @@ function validateIdentifier(name: string, label: string): void {
2323 }
2424}
2525
26+ const MAX_PUBLIC_KEY_LENGTH = 256 ;
27+ const MAX_MESSAGE_LENGTH = 4096 ;
28+ const MAX_SIGNATURE_LENGTH = 1024 ;
29+
2630export const PublicKeySignature = ( pubkey_challenge : PublicKeyChallengeConfig ) : GraphileConfig . Plugin => {
2731 const {
2832 schema,
@@ -90,23 +94,29 @@ export const PublicKeySignature = (pubkey_challenge: PublicKeyChallengeConfig):
9094 const $combined = object ( { input : $input , withPgClient : $withPgClient } ) ;
9195
9296 return lambda ( $combined , async ( { input, withPgClient } : any ) => {
97+ if ( ! input . publicKey || typeof input . publicKey !== 'string' || input . publicKey . length > MAX_PUBLIC_KEY_LENGTH ) {
98+ throw new Error ( 'INVALID_PUBLIC_KEY' ) ;
99+ }
100+
93101 return withPgClient ( null , async ( pgClient : any ) => {
94102 await pgClient . query ( 'BEGIN' ) ;
95103 try {
96104 await pgQueryWithContext ( {
97105 client : pgClient ,
98106 context : { role : 'anonymous' } ,
99- query : `SELECT * FROM "${ schema } ".${ sign_up_with_key } ($1)` ,
100- variables : [ input . publicKey ]
107+ query : `SELECT * FROM "${ schema } "."${ sign_up_with_key } "($1)` ,
108+ variables : [ input . publicKey ] ,
109+ skipTransaction : true
101110 } ) ;
102111
103112 const {
104113 rows : [ { [ sign_in_request_challenge ] : message } ]
105114 } = await pgQueryWithContext ( {
106115 client : pgClient ,
107116 context : { role : 'anonymous' } ,
108- query : `SELECT * FROM "${ schema } ".${ sign_in_request_challenge } ($1)` ,
109- variables : [ input . publicKey ]
117+ query : `SELECT * FROM "${ schema } "."${ sign_in_request_challenge } "($1)` ,
118+ variables : [ input . publicKey ] ,
119+ skipTransaction : true
110120 } ) ;
111121
112122 await pgClient . query ( 'COMMIT' ) ;
@@ -125,14 +135,19 @@ export const PublicKeySignature = (pubkey_challenge: PublicKeyChallengeConfig):
125135 const $combined = object ( { input : $input , withPgClient : $withPgClient } ) ;
126136
127137 return lambda ( $combined , async ( { input, withPgClient } : any ) => {
138+ if ( ! input . publicKey || typeof input . publicKey !== 'string' || input . publicKey . length > MAX_PUBLIC_KEY_LENGTH ) {
139+ throw new Error ( 'INVALID_PUBLIC_KEY' ) ;
140+ }
141+
128142 return withPgClient ( null , async ( pgClient : any ) => {
129143 const {
130144 rows : [ { [ sign_in_request_challenge ] : message } ]
131145 } = await pgQueryWithContext ( {
132146 client : pgClient ,
133147 context : { role : 'anonymous' } ,
134- query : `SELECT * FROM "${ schema } ".${ sign_in_request_challenge } ($1)` ,
135- variables : [ input . publicKey ]
148+ query : `SELECT * FROM "${ schema } "."${ sign_in_request_challenge } "($1)` ,
149+ variables : [ input . publicKey ] ,
150+ skipTransaction : true
136151 } ) ;
137152
138153 if ( ! message ) throw new Error ( 'NO_ACCOUNT_EXISTS' ) ;
@@ -155,6 +170,16 @@ export const PublicKeySignature = (pubkey_challenge: PublicKeyChallengeConfig):
155170 return lambda ( $combined , async ( { input, withPgClient } : any ) => {
156171 const { publicKey, message, signature : _signature } = input ;
157172
173+ if ( ! publicKey || typeof publicKey !== 'string' || publicKey . length > MAX_PUBLIC_KEY_LENGTH ) {
174+ throw new Error ( 'INVALID_PUBLIC_KEY' ) ;
175+ }
176+ if ( ! message || typeof message !== 'string' || message . length > MAX_MESSAGE_LENGTH ) {
177+ throw new Error ( 'INVALID_MESSAGE' ) ;
178+ }
179+ if ( ! _signature || typeof _signature !== 'string' || _signature . length > MAX_SIGNATURE_LENGTH ) {
180+ throw new Error ( 'INVALID_SIGNATURE' ) ;
181+ }
182+
158183 // TODO: Re-implement crypto verification (e.g. using interchainJS).
159184 // const network = Networks[crypto_network];
160185 // const result = verifyMessage(message, publicKey, signature, network);
@@ -166,8 +191,9 @@ export const PublicKeySignature = (pubkey_challenge: PublicKeyChallengeConfig):
166191 await pgQueryWithContext ( {
167192 client : pgClient ,
168193 context : { role : 'anonymous' } ,
169- query : `SELECT * FROM "${ schema } ".${ sign_in_record_failure } ($1)` ,
170- variables : [ publicKey ]
194+ query : `SELECT * FROM "${ schema } "."${ sign_in_record_failure } "($1)` ,
195+ variables : [ publicKey ] ,
196+ skipTransaction : true
171197 } ) ;
172198 throw new Error ( 'BAD_SIGNIN' ) ;
173199 }
@@ -180,8 +206,9 @@ export const PublicKeySignature = (pubkey_challenge: PublicKeyChallengeConfig):
180206 } = await pgQueryWithContext ( {
181207 client : pgClient ,
182208 context : { role : 'anonymous' } ,
183- query : `SELECT * FROM "${ schema } ".${ sign_in_with_challenge } ($1, $2)` ,
184- variables : [ publicKey , message ]
209+ query : `SELECT * FROM "${ schema } "."${ sign_in_with_challenge } "($1, $2)` ,
210+ variables : [ publicKey , message ] ,
211+ skipTransaction : true
185212 } ) ;
186213
187214 if ( ! token ?. access_token ) throw new Error ( 'BAD_SIGNIN' ) ;
0 commit comments