11use crate :: errors:: { Result , ResultExt } ;
22use fernet:: MultiFernet ;
33use openssl:: hash;
4+ use url:: Url ;
45use uuid:: Uuid ;
56
67/// Create an v1 or v2 WebPush endpoint from the identifiers
@@ -15,7 +16,10 @@ pub fn make_endpoint(
1516 endpoint_url : & str ,
1617 fernet : & MultiFernet ,
1718) -> Result < String > {
18- let root = format ! ( "{}/wpush/" , endpoint_url) ;
19+ let root = Url :: parse ( endpoint_url)
20+ . chain_err ( || "endpoint_url is not a valid URL" ) ?
21+ . join ( "wpush/" )
22+ . chain_err ( || "Error creating URL" ) ?;
1923 let mut base = uaid. as_bytes ( ) . to_vec ( ) ;
2024 base. extend ( chid. as_bytes ( ) ) ;
2125
@@ -26,9 +30,15 @@ pub fn make_endpoint(
2630 . chain_err ( || "Error creating message digest for key" ) ?;
2731 base. extend ( key_digest. iter ( ) ) ;
2832 let encrypted = fernet. encrypt ( & base) . trim_matches ( '=' ) . to_string ( ) ;
29- Ok ( format ! ( "{}v2/{}" , root, encrypted) )
33+ let final_url = root
34+ . join ( & format ! ( "v2/{}" , encrypted) )
35+ . chain_err ( || "Encrypted data is not URL-safe" ) ?;
36+ Ok ( final_url. to_string ( ) )
3037 } else {
3138 let encrypted = fernet. encrypt ( & base) . trim_matches ( '=' ) . to_string ( ) ;
32- Ok ( format ! ( "{}v1/{}" , root, encrypted) )
39+ let final_url = root
40+ . join ( & format ! ( "v1/{}" , encrypted) )
41+ . chain_err ( || "Encrypted data is not URL-safe" ) ?;
42+ Ok ( final_url. to_string ( ) )
3343 }
3444}
0 commit comments