@@ -34,6 +34,14 @@ impl BatcherServer {
3434 Self { db, config }
3535 }
3636
37+ fn is_valid_eth_address ( addr : & str ) -> bool {
38+ let addr = addr. strip_prefix ( "0x" ) . unwrap_or ( addr) ;
39+ if addr. len ( ) != 40 {
40+ return false ;
41+ }
42+ addr. chars ( ) . all ( |c| c. is_ascii_hexdigit ( ) )
43+ }
44+
3745 pub async fn start ( & self ) {
3846 // Note: BatcherServer is thread safe so we can just clone it (no need to add mutexes)
3947 let port = self . config . port ;
@@ -57,11 +65,18 @@ impl BatcherServer {
5765
5866 // Returns the nonce (number of submitted tasks) for a given address
5967 async fn get_nonce ( req : HttpRequest ) -> impl Responder {
60- let Some ( address ) = req. match_info ( ) . get ( "address" ) else {
68+ let Some ( address_raw ) = req. match_info ( ) . get ( "address" ) else {
6169 return HttpResponse :: BadRequest ( )
6270 . json ( AppResponse :: new_unsucessfull ( "Missing address" , 400 ) ) ;
6371 } ;
6472
73+ if !Self :: is_valid_eth_address ( address_raw) {
74+ return HttpResponse :: BadRequest ( )
75+ . json ( AppResponse :: new_unsucessfull ( "Invalid address" , 400 ) ) ;
76+ }
77+
78+ let address = address_raw. to_lowercase ( ) ;
79+
6580 // TODO: validate valid ethereum address
6681
6782 let Some ( state) = req. app_data :: < Data < BatcherServer > > ( ) else {
@@ -70,7 +85,7 @@ impl BatcherServer {
7085 } ;
7186
7287 let state = state. get_ref ( ) ;
73- match state. db . count_tasks_by_address ( address) . await {
88+ match state. db . count_tasks_by_address ( & address) . await {
7489 Ok ( count) => HttpResponse :: Ok ( ) . json ( AppResponse :: new_sucessfull ( serde_json:: json!(
7590 {
7691 "nonce" : count
@@ -186,11 +201,18 @@ impl BatcherServer {
186201
187202 let state = state. get_ref ( ) ;
188203
189- let Some ( address ) = params. address . clone ( ) else {
204+ let Some ( address_raw ) = params. address . clone ( ) else {
190205 return HttpResponse :: BadRequest ( )
191206 . json ( AppResponse :: new_unsucessfull ( "Missing address" , 400 ) ) ;
192207 } ;
193208
209+ if !Self :: is_valid_eth_address ( & address_raw) {
210+ return HttpResponse :: BadRequest ( )
211+ . json ( AppResponse :: new_unsucessfull ( "Invalid address" , 400 ) ) ;
212+ }
213+
214+ let address = address_raw. to_lowercase ( ) ;
215+
194216 let receipts = if let Some ( nonce) = params. nonce {
195217 match state
196218 . db
0 commit comments