Repository navigation
fix(server): reject server-to-client requests on discover sessions #1397
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gocamille
wants to merge
3
commits into
main
Choose a base branch
from
fix/1326-discover-outbound-guard
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -161,7 +161,7 @@ pub trait ServiceRole: std::fmt::Debug + Send + Sync + 'static + Copy + Clone { | |||||||
| /// Rejects outbound requests when the negotiated protocol forbids this role | ||||||||
| /// from sending any. | ||||||||
| #[doc(hidden)] | ||||||||
| fn enforce_outbound_request(_peer_info: Option<&Self::PeerInfo>) -> Result<(), ServiceError> { | ||||||||
| fn enforce_outbound_request(_peer: &Peer<Self>) -> Result<(), ServiceError> { | ||||||||
| Ok(()) | ||||||||
| } | ||||||||
|
|
||||||||
|
|
@@ -226,6 +226,18 @@ pub(crate) fn peer_request_association<Req: crate::model::GetExtensions, V>( | |||||||
|
|
||||||||
| tokio::task_local! { | ||||||||
| pub(crate) static ORIGINATING_REQUEST: RequestId; | ||||||||
| /// The protocol version the originating request names in its `_meta`. | ||||||||
| pub(crate) static ORIGINATING_PROTOCOL_VERSION: Option<ProtocolVersion>; | ||||||||
| } | ||||||||
|
|
||||||||
| /// The protocol version named by the request the current handler is serving, | ||||||||
| /// if any. Like [`ORIGINATING_REQUEST`], it does not cross `tokio::spawn`. | ||||||||
| #[cfg(feature = "server")] | ||||||||
| pub(crate) fn originating_protocol_version() -> Option<ProtocolVersion> { | ||||||||
| ORIGINATING_PROTOCOL_VERSION | ||||||||
| .try_with(Clone::clone) | ||||||||
| .ok() | ||||||||
| .flatten() | ||||||||
| } | ||||||||
|
|
||||||||
| /// Marker in an outbound request's non-serialized [`Extensions`] identifying | ||||||||
|
|
@@ -717,7 +729,7 @@ pub struct Peer<R: ServiceRole> { | |||||||
| progress_timeout_watchers: ProgressTimeoutWatchers, | ||||||||
| info: Arc<std::sync::RwLock<Option<Arc<R::PeerInfo>>>>, | ||||||||
| client_request_metadata: Arc<OnceLock<ClientRequestMetadata>>, | ||||||||
| request_metadata_required: Arc<std::sync::atomic::AtomicBool>, | ||||||||
| inline_protocol_version: Arc<OnceLock<ProtocolVersion>>, | ||||||||
| subscription_channels: Arc<std::sync::RwLock<SubscriptionChannelMap<R::PeerNot>>>, | ||||||||
| #[cfg(feature = "client")] | ||||||||
| response_cache: client::cache::PeerResponseCache<R>, | ||||||||
|
|
@@ -735,7 +747,7 @@ where | |||||||
| progress_timeout_watchers: self.progress_timeout_watchers.clone(), | ||||||||
| info: self.info.clone(), | ||||||||
| client_request_metadata: self.client_request_metadata.clone(), | ||||||||
| request_metadata_required: self.request_metadata_required.clone(), | ||||||||
| inline_protocol_version: self.inline_protocol_version.clone(), | ||||||||
| subscription_channels: self.subscription_channels.clone(), | ||||||||
| #[cfg(feature = "client")] | ||||||||
| response_cache: self.response_cache.clone(), | ||||||||
|
|
@@ -811,7 +823,7 @@ impl<R: ServiceRole> Peer<R> { | |||||||
| progress_timeout_watchers: Default::default(), | ||||||||
| info: Arc::new(std::sync::RwLock::new(peer_info.map(Arc::new))), | ||||||||
| client_request_metadata: Default::default(), | ||||||||
| request_metadata_required: Default::default(), | ||||||||
| inline_protocol_version: Default::default(), | ||||||||
| subscription_channels: Default::default(), | ||||||||
| #[cfg(feature = "client")] | ||||||||
| response_cache: Default::default(), | ||||||||
|
|
@@ -860,7 +872,7 @@ impl<R: ServiceRole> Peer<R> { | |||||||
| options: PeerRequestOptions, | ||||||||
| subscription_sender: Option<SubscriptionChannel<R::PeerNot>>, | ||||||||
| ) -> Result<RequestHandle<R>, ServiceError> { | ||||||||
| R::enforce_outbound_request(self.peer_info().as_deref())?; | ||||||||
| R::enforce_outbound_request(self)?; | ||||||||
| if let Ok(originating) = ORIGINATING_REQUEST.try_with(|id| id.clone()) { | ||||||||
| request | ||||||||
| .extensions_mut() | ||||||||
|
|
@@ -1024,16 +1036,22 @@ impl<R: ServiceRole> Peer<R> { | |||||||
| debug_assert!(result.is_ok(), "client request metadata set more than once"); | ||||||||
| } | ||||||||
|
|
||||||||
| /// Marks an inline lifecycle session that opened on `protocol_version`. | ||||||||
| #[cfg(feature = "server")] | ||||||||
| pub(crate) fn require_request_metadata(&self, protocol_version: ProtocolVersion) { | ||||||||
| let _ = self.inline_protocol_version.set(protocol_version); | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| } | ||||||||
|
|
||||||||
| /// The version an inline lifecycle session opened on. Such a session never | ||||||||
| /// stores peer info. | ||||||||
| #[cfg(feature = "server")] | ||||||||
| pub(crate) fn require_request_metadata(&self) { | ||||||||
| self.request_metadata_required | ||||||||
| .store(true, std::sync::atomic::Ordering::Release); | ||||||||
| pub(crate) fn inline_protocol_version(&self) -> Option<&ProtocolVersion> { | ||||||||
| self.inline_protocol_version.get() | ||||||||
| } | ||||||||
|
|
||||||||
| #[cfg(feature = "server")] | ||||||||
| pub(crate) fn request_metadata_required(&self) -> bool { | ||||||||
| self.request_metadata_required | ||||||||
| .load(std::sync::atomic::Ordering::Acquire) | ||||||||
| self.inline_protocol_version.get().is_some() | ||||||||
| } | ||||||||
|
|
||||||||
| pub fn is_transport_closed(&self) -> bool { | ||||||||
|
|
@@ -1586,9 +1604,16 @@ where | |||||||
| }; | ||||||||
| let current_span = tracing::Span::current(); | ||||||||
| let handler_id = id.clone(); | ||||||||
| let handler_version = context.meta.protocol_version(); | ||||||||
| spawn_service_task(async move { | ||||||||
| let result = ORIGINATING_REQUEST | ||||||||
| .scope(handler_id, service.handle_request(request, context)) | ||||||||
| .scope( | ||||||||
| handler_id, | ||||||||
| ORIGINATING_PROTOCOL_VERSION.scope( | ||||||||
| handler_version, | ||||||||
| service.handle_request(request, context), | ||||||||
| ), | ||||||||
| ) | ||||||||
| .await; | ||||||||
| let response = match result { | ||||||||
| Ok(result) => { | ||||||||
|
|
||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that this records the session's initial version instead of flipping a flag,
require_request_metadatasounds a bit odd next toinline_protocol_version(). What aboutset_inline_protocol_versioninstead?