Skip to content

fix: pre-request script variable hostname certificate resolution [INS-4733]#8249

Merged
ryan-willis merged 4 commits into
Kong:developfrom
ryanexus:fix/client-certificate-usage-in-requests
Jan 3, 2025
Merged

fix: pre-request script variable hostname certificate resolution [INS-4733]#8249
ryan-willis merged 4 commits into
Kong:developfrom
ryanexus:fix/client-certificate-usage-in-requests

Conversation

@ryan-willis
Copy link
Copy Markdown
Contributor

This fixes two distinct, but closely related issues:

When using a variable reference in the request URL for the hostname and a pre-request script with non-empty contents, client certs would be filtered using the un-rendered template text before sending the request and yield an error (part 1 of the new smoke test).

When gathering context to send a request that has a pre-request script with non-empty contents, the first cert in the list was always selected, disregarding the disabled flag and the hostname on the cert (part 2 of the new smoke test).

@ryan-willis ryan-willis force-pushed the fix/client-certificate-usage-in-requests branch from e5a8312 to 67e40d0 Compare December 12, 2024 21:19
marckong
marckong previously approved these changes Dec 12, 2024
Copy link
Copy Markdown
Contributor

@marckong marckong left a comment

Choose a reason for hiding this comment

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

lgtm!

marckong
marckong previously approved these changes Dec 13, 2024
Copy link
Copy Markdown
Contributor

@ihexxa ihexxa left a comment

Choose a reason for hiding this comment

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

Added some comments and feel free to let me know your thoughts, also thanks for looking into this complicated issue.


const existClientCert = rawObj.clientCertificates != null && rawObj.clientCertificates.length > 0;
const certificate = existClientCert && rawObj.clientCertificates[0] ?
const reqUrl = toUrlObject(rawObj.request.url);
Copy link
Copy Markdown
Contributor

@ihexxa ihexxa Dec 16, 2024

Choose a reason for hiding this comment

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

This makes sense to me to move this part from below, pls notice that the url parsing is not reliable for all cases as there could be templates in it (there's one card for it). such as representing both host and path with tag {{_.srvr}}{{ _.path }}, then this url will not be rendered as expected.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point. I tried to avoid running the url through a full render but it looks like we might need to do that to cover this case

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Now that I'm looking at this again, I could perform the template render hack prior to calling getHost, what do you think?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We also have a plan to improve the replaceIn method, let's optimize it later.

const certificate = existingClientCert ?
{
disabled: rawObj.clientCertificates[0].disabled,
disabled: existingClientCert.disabled,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You might notice that there seems no perfect way to transform it between 2 sides, I'm thinking if we could just leave the certificate undefined at the beginning, if user specified the cert in the script, we prepend to the cert list, or return the original certs, pls let me know what you think.

Copy link
Copy Markdown
Contributor Author

@ryan-willis ryan-willis Jan 2, 2025

Choose a reason for hiding this comment

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

I'd much rather return original certs, but I'm thinking we revamp this later

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes we can revamp it later and the prepend operation is for those cases who would like to update the cert through script.

Comment thread packages/insomnia-sdk/src/objects/insomnia.ts Outdated
Comment thread packages/insomnia-smoke-test/tests/smoke/mtls.test.ts
@ryan-willis ryan-willis force-pushed the fix/client-certificate-usage-in-requests branch from 1db77f7 to 9ba2476 Compare January 2, 2025 19:26
@ryan-willis ryan-willis merged commit 90bd25c into Kong:develop Jan 3, 2025
@ryan-willis ryan-willis deleted the fix/client-certificate-usage-in-requests branch February 17, 2025 14:27
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