@@ -5,14 +5,17 @@ use crate::{
55 authority:: {
66 AuthorityError , AuthorityStore , Challenge , Delegation , DeliveryDisposition , NewInstallation ,
77 } ,
8- config:: GatewayUrls ,
8+ config:: {
9+ DELEGATE_AUDIENCE , ENROLL_AUDIENCE , REVOKE_DELEGATION_AUDIENCE ,
10+ REVOKE_INSTALLATION_AUDIENCE , ROTATE_ENDPOINT_AUDIENCE ,
11+ } ,
912 grant:: GrantKeyring ,
1013 model:: * ,
1114 token:: TokenKeyring ,
1215} ;
1316use axum:: {
1417 body:: Bytes ,
15- extract:: State ,
18+ extract:: { OriginalUri , State } ,
1619 http:: { HeaderMap , StatusCode } ,
1720 response:: { IntoResponse , Response } ,
1821 routing:: { get, post} ,
@@ -21,7 +24,7 @@ use axum::{
2124use base64:: { engine:: general_purpose:: STANDARD , Engine as _} ;
2225use nostr:: {
2326 nips:: nip98:: { verify_auth_header, HttpMethod } ,
24- Event , JsonUtil , Timestamp ,
27+ Event , JsonUtil , TagKind , TagStandard , Timestamp ,
2528} ;
2629use std:: {
2730 sync:: {
@@ -47,8 +50,6 @@ pub struct AppState {
4750 /// Server-owned dogfood application identity and APNs transport. The wire
4851 /// profile selector is fixed and App Attest verifies the configured app ID.
4952 pub profile : Arc < ProfileRuntime > ,
50- /// Security-sensitive endpoints and audiences derived from one gateway origin.
51- pub gateway_urls : Arc < GatewayUrls > ,
5253 pub max_grant_lifetime_seconds : i64 ,
5354 pub max_installation_lifetime_seconds : i64 ,
5455 pub endpoint_quota_window_seconds : i64 ,
@@ -71,14 +72,46 @@ fn valid_relay_pubkey(v: &str) -> bool {
7172 && v. bytes ( )
7273 . all ( |b| b. is_ascii_digit ( ) || ( b'a' ..=b'f' ) . contains ( & b) )
7374}
74- fn auth_event_id ( header : & str ) -> Option < String > {
75+ pub ( crate ) const DELIVERY_PATH : & str = "/v1/deliveries/apns" ;
76+
77+ // Keep preliminary decoding bounded to the pinned nostr verifier's limits.
78+ fn delivery_auth ( header : & str , body : & [ u8 ] ) -> Option < ( String , String ) > {
7579 let ( prefix, encoded) = header. split_once ( ' ' ) ?;
76- if prefix != "Nostr" {
80+ const MAX_EVENT_BYTES : usize = 64 * 1024 ;
81+ const MAX_ENCODED_BYTES : usize = MAX_EVENT_BYTES . div_ceil ( 3 ) * 4 ;
82+ if prefix != "Nostr" || encoded. len ( ) > MAX_ENCODED_BYTES {
7783 return None ;
7884 }
79- Event :: from_json ( STANDARD . decode ( encoded) . ok ( ) ?)
80- . ok ( )
81- . map ( |e| e. id . to_hex ( ) )
85+ let decoded = STANDARD . decode ( encoded) . ok ( ) ?;
86+ if decoded. len ( ) > MAX_EVENT_BYTES {
87+ return None ;
88+ }
89+ let event = Event :: from_json ( decoded) . ok ( ) ?;
90+ let signed_url = match event. tags . find_standardized ( TagKind :: u ( ) ) ? {
91+ TagStandard :: AbsoluteURL ( url) => url,
92+ _ => return None ,
93+ } ;
94+ if !matches ! ( signed_url. scheme( ) , "http" | "https" )
95+ || signed_url. path ( ) != DELIVERY_PATH
96+ || signed_url. query ( ) . is_some ( )
97+ || signed_url. fragment ( ) . is_some ( )
98+ || !signed_url. username ( ) . is_empty ( )
99+ || signed_url. password ( ) . is_some ( )
100+ {
101+ return None ;
102+ }
103+ // This delivery profile binds method and path, not request origin. Passing
104+ // the validated signed URL retains the library's kind, method, timestamp,
105+ // body hash, event ID and signature checks without reconstructing a URL.
106+ let relay = verify_auth_header (
107+ header,
108+ signed_url,
109+ HttpMethod :: POST ,
110+ Timestamp :: now ( ) ,
111+ Some ( body) ,
112+ )
113+ . ok ( ) ?;
114+ Some ( ( event. id . to_hex ( ) , relay. to_hex ( ) ) )
82115}
83116
84117fn decode_challenge ( value : & str ) -> Option < [ u8 ; 32 ] > {
@@ -192,7 +225,7 @@ async fn enroll(State(s): State<AppState>, body: Bytes) -> Response {
192225 } ;
193226 let t = EnrollTranscript {
194227 v : r. v ,
195- audience : & s . gateway_urls . enroll_audience ,
228+ audience : ENROLL_AUDIENCE ,
196229 challenge_id : r. challenge_id ,
197230 challenge : & r. challenge ,
198231 key_id : & r. key_id ,
@@ -366,7 +399,7 @@ async fn delegate(State(s): State<AppState>, body: Bytes) -> Response {
366399 }
367400 let t = DelegateTranscript {
368401 v : r. v ,
369- audience : & s . gateway_urls . delegate_audience ,
402+ audience : DELEGATE_AUDIENCE ,
370403 challenge_id : r. challenge_id ,
371404 challenge : & r. challenge ,
372405 installation_handle : r. installation_handle ,
@@ -463,7 +496,7 @@ async fn rotate_endpoint(State(s): State<AppState>, body: Bytes) -> Response {
463496 } ;
464497 let t = RotateTranscript {
465498 v : r. v ,
466- audience : & s . gateway_urls . rotate_endpoint_audience ,
499+ audience : ROTATE_ENDPOINT_AUDIENCE ,
467500 challenge_id : r. challenge_id ,
468501 challenge : & r. challenge ,
469502 installation_handle : r. installation_handle ,
@@ -526,7 +559,7 @@ async fn revoke_delegation(State(s): State<AppState>, body: Bytes) -> Response {
526559 }
527560 let t = RevokeDelegationTranscript {
528561 v : r. v ,
529- audience : & s . gateway_urls . revoke_delegation_audience ,
562+ audience : REVOKE_DELEGATION_AUDIENCE ,
530563 challenge_id : r. challenge_id ,
531564 challenge : & r. challenge ,
532565 installation_handle : r. installation_handle ,
@@ -581,7 +614,7 @@ async fn revoke_installation(State(s): State<AppState>, body: Bytes) -> Response
581614 }
582615 let t = RevokeInstallationTranscript {
583616 v : r. v ,
584- audience : & s . gateway_urls . revoke_installation_audience ,
617+ audience : REVOKE_INSTALLATION_AUDIENCE ,
585618 challenge_id : r. challenge_id ,
586619 challenge : & r. challenge ,
587620 installation_handle : r. installation_handle ,
@@ -618,7 +651,12 @@ async fn revoke_installation(State(s): State<AppState>, body: Bytes) -> Response
618651 }
619652}
620653
621- async fn deliver ( State ( s) : State < AppState > , headers : HeaderMap , body : Bytes ) -> Response {
654+ async fn deliver (
655+ State ( s) : State < AppState > ,
656+ OriginalUri ( uri) : OriginalUri ,
657+ headers : HeaderMap ,
658+ body : Bytes ,
659+ ) -> Response {
622660 let r: DeliveryRequest = match crate :: strict_json:: from_slice ( & body) {
623661 Ok ( x) => x,
624662 Err ( _) => return error ( StatusCode :: BAD_REQUEST , "invalid_request" ) ,
@@ -633,20 +671,13 @@ async fn deliver(State(s): State<AppState>, headers: HeaderMap, body: Bytes) ->
633671 Some ( x) => x,
634672 None => return error ( StatusCode :: UNAUTHORIZED , "invalid_auth" ) ,
635673 } ;
636- let event_id = match auth_event_id ( auth) {
637- Some ( x) => x,
674+ if uri. query ( ) . is_some ( ) {
675+ return error ( StatusCode :: UNAUTHORIZED , "invalid_auth" ) ;
676+ }
677+ let ( event_id, relay) = match delivery_auth ( auth, & body) {
678+ Some ( auth) => auth,
638679 None => return error ( StatusCode :: UNAUTHORIZED , "invalid_auth" ) ,
639680 } ;
640- let relay = match verify_auth_header (
641- auth,
642- & s. gateway_urls . delivery ,
643- HttpMethod :: POST ,
644- Timestamp :: now ( ) ,
645- Some ( & body) ,
646- ) {
647- Ok ( x) => x. to_hex ( ) ,
648- Err ( _) => return error ( StatusCode :: UNAUTHORIZED , "invalid_auth" ) ,
649- } ;
650681 let grant = match s. grant_keyring . open ( & r. endpoint_grant ) {
651682 Ok ( x) => x,
652683 Err ( _) => return error ( StatusCode :: NOT_FOUND , "invalid_grant" ) ,
@@ -828,7 +859,7 @@ pub fn router_with_metrics(
828859 . route ( "/v1/delegations/revoke" , post ( revoke_delegation) )
829860 . route ( "/v1/installations/endpoint" , post ( rotate_endpoint) )
830861 . route ( "/v1/installations/revoke" , post ( revoke_installation) )
831- . route ( "/v1/deliveries/apns" , post ( deliver) )
862+ . route ( DELIVERY_PATH , post ( deliver) )
832863 . layer ( RequestBodyLimitLayer :: new ( MAX_REQUEST_BYTES ) ) ;
833864 let public = Router :: new ( )
834865 . merge ( enrollment)
@@ -906,9 +937,6 @@ mod request_limit_tests {
906937 app_attest : Arc :: new ( app_attest) ,
907938 transport : Arc :: new ( NeverTransport ) ,
908939 } ) ,
909- gateway_urls : Arc :: new (
910- GatewayUrls :: from_origin ( "https://push.example" . parse ( ) . unwrap ( ) ) . unwrap ( ) ,
911- ) ,
912940 max_grant_lifetime_seconds : 86_400 ,
913941 max_installation_lifetime_seconds : 86_400 ,
914942 endpoint_quota_window_seconds : 60 ,
0 commit comments