Skip to content

Commit

Permalink
Add protection against self-delegation
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiothinks2 committed Feb 4, 2025
1 parent 2626d7c commit cb05c5f
Showing 1 changed file with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ class URLSessionDelegateProxy: NSObject {

// check if the originalDelegate responds to the selector
if let originalDelegate = originalDelegate,
originalDelegate.responds(to: selector) {
originalDelegate.responds(to: selector) {
// Add protection against self-delegation
guard !(originalDelegate is URLSessionDelegateProxy) else {
return .doesNotRespond
}

if let delegateAsT = originalDelegate as? T {
return .respondsAndConforms(as: delegateAsT)
} else if let object = originalDelegate as? NSObject {
Expand All @@ -90,6 +95,17 @@ class URLSessionDelegateProxy: NSObject {
// if session delegate also responds to selector, we must call it
if let sessionDelegate = session.delegate,
sessionDelegate.responds(to: selector) {
// Guard that we are not the session.delegate to prevent infinite recursion
guard (sessionDelegate as? URLSessionDelegateProxy) != self,
!(sessionDelegate is URLSessionDelegateProxy) else {
return .doesNotRespond
}

// Avoid forwarding if already swizzled
guard self.swizzledDelegate == nil else {
return .doesNotRespond
}

if let sessionDelegateAsT = sessionDelegate as? T {
return .respondsAndConforms(as: sessionDelegateAsT)
} else if let object = sessionDelegate as? NSObject {
Expand Down

0 comments on commit cb05c5f

Please sign in to comment.