Skip to content
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

KTOR-6759 Darwin: Throw specific exception for SSL Pinning failure #4408

Draft
wants to merge 2 commits into
base: 3.1.0-eap
Choose a base branch
from

Conversation

osipxd
Copy link
Member

@osipxd osipxd commented Oct 17, 2024

Subsystem
Client, Darwin

Motivation
KTOR-6759 Darwin: Ambiguous DarwinHttpRequestException for SSL Pinning failure

The problem is that we call completionHandler with NSURLSessionAuthChallengePerformDefaultHandling on SSL pinning failure and then third-party API throws an exception. There are no mechanisms to attach additional data to this exception (or at least I don't know any).
The place where the actual error occurs and the place where DarwinHttpRequestException is constructed are two completely different places.

Solution
Save actual failure thrown during challenge handling to aDarwinTaskHandler and throw it later instead of creating a generic one. Throw specific exception CertificatePinner to make it possible to catch pinning failures specifically:

try {
    client.get("/some-api-with-pinning")
} catch(cause: CertificatePinnerException) {
    println("SSL pinning failure detected.")
}

Naming, comments, and other stylistic things aren’t final yet, but I need this draft to be reviewed.

@osipxd osipxd requested a review from e5l October 17, 2024 14:08
@osipxd osipxd self-assigned this Oct 17, 2024
@osipxd osipxd marked this pull request as draft October 17, 2024 14:09
@osipxd osipxd force-pushed the osipxd/darwin-ambiguous-ssl-pinning-failure branch from 286dd71 to cc70f73 Compare October 17, 2024 14:17
@osipxd osipxd force-pushed the osipxd/darwin-ambiguous-ssl-pinning-failure branch from cc70f73 to 13901e5 Compare October 17, 2024 14:30
@@ -436,3 +430,5 @@ public data class CertificatePinner(
)
}
}

public class CertificatePinnerException(message: String) : IOException(message)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have concerns regarding this exception:

  • Previously DarwinHttpRequestException was thrown for such cases, so it is potentially breaking change
  • This exception is used for any error thrown by the pinner. Should we give a possibility to distinct between these errors?
  • Should we provide a multiplatform exception to be able to handle SSL pinning problems in a unified manner?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

  • I would still consider this as a bug fix
  • We can make this exception open, and wait if there are use cases for that.
  • We could introduce this exception in ktor-network common code.

Let's target to 3.1.0 and do it

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@osipxd Thanks for this PR! I have very little context on the Ktor code, so I don't have much feedback to share. I can see that the exception contains the output message which is exactly what I was hoping to be able to access. I also appreciate how you included some tests. Thanks for your help and @e5l thank you for your review as well! 🙏

@@ -436,3 +430,5 @@ public data class CertificatePinner(
)
}
}

public class CertificatePinnerException(message: String) : IOException(message)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

  • I would still consider this as a bug fix
  • We can make this exception open, and wait if there are use cases for that.
  • We could introduce this exception in ktor-network common code.

Let's target to 3.1.0 and do it

@e5l e5l changed the base branch from main to 3.1.0-eap October 18, 2024 08:38
@@ -436,3 +430,5 @@ public data class CertificatePinner(
)
}
}

public class CertificatePinnerException(message: String) : IOException(message)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@osipxd Thanks for this PR! I have very little context on the Ktor code, so I don't have much feedback to share. I can see that the exception contains the output message which is exactly what I was hoping to be able to access. I also appreciate how you included some tests. Thanks for your help and @e5l thank you for your review as well! 🙏

@e5l e5l force-pushed the 3.1.0-eap branch 2 times, most recently from ce83e36 to ccb920f Compare November 14, 2024 10:25
@e5l e5l force-pushed the 3.1.0-eap branch 5 times, most recently from 1d19b77 to e1f483e Compare November 29, 2024 08:46
@e5l e5l force-pushed the 3.1.0-eap branch 2 times, most recently from 95e95b0 to 1971db7 Compare December 3, 2024 14:07
@osipxd osipxd force-pushed the 3.1.0-eap branch 5 times, most recently from ecb56d2 to d50a66e Compare December 12, 2024 10:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants