Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/conjure-client/changelog/@unreleased/pr-186.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: feature
feature:
description: Conjure User-Agents supports arbitrary comment data
links:
- https://github.com/palantir/conjure-typescript-runtime/pull/186
1 change: 1 addition & 0 deletions packages/conjure-client/src/__integTest__/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const bridge = new FetchBridge({
userAgent: {
productName: "conjure-typescript-runtime",
productVersion: "0.0.0",
comments: ["some-comment"],
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe("FetchBridgeImplServer", () => {
const host = "localhost";
const port = 9000;
const baseUrl = `http://${host}:${port}`;
const userAgent: IUserAgent = { productName: "foo", productVersion: "1.2.3" };
const userAgent: IUserAgent = { productName: "foo", productVersion: "1.2.3", comments: ["some-comment"] };
bridge = new FetchBridge({ baseUrl, token, fetch: nodeFetchStreamAdapter(nodeFetch), userAgent });

app = express();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,32 @@ describe("FetchBridgeImpl", () => {
verifyFetchUserAgent(`foo/1.2.3 bar/1.2.3 baz/1.2.3 conjure-typescript-runtime/${IMPLEMENTATION_VERSION}`);
});

it("for a user agent with comments", async () => {
const fetchBridge = new FetchBridge({
baseUrl,
token,
fetch: undefined,
userAgent: { productName: "foo", productVersion: "1.2.3", comments: ["comment1", "comment2"] },
});

await fetchBridge.callEndpoint(request);

verifyFetchUserAgent(`foo/1.2.3 (comment1; comment2) conjure-typescript-runtime/${IMPLEMENTATION_VERSION}`);
});

it("for a user agent with empty coments", async () => {
const fetchBridge = new FetchBridge({
baseUrl,
token,
fetch: undefined,
userAgent: { productName: "foo", productVersion: "1.2.3", comments: [] },
});

await fetchBridge.callEndpoint(request);

verifyFetchUserAgent(`foo/1.2.3 conjure-typescript-runtime/${IMPLEMENTATION_VERSION}`);
});

it("for changing user agent", async () => {
const userAgentSupplier = jest
.fn()
Expand Down
20 changes: 18 additions & 2 deletions packages/conjure-client/src/userAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@
export interface IUserAgent {
productName: string;
productVersion: string;
/**
* [rfc7231 section-5.5.3](https://datatracker.ietf.org/doc/html/rfc7231#section-5.5.3) comment
* metadata (as described by
* [rfc7230 section-3.2.6](https://datatracker.ietf.org/doc/html/rfc7230#section-3.2.6"))
* for additional diagnostic information.
*
* Note that this library provides a much stricter set of allowed
* characters within comments than the linked RFCs to reduce complexity.
*
* Allowed characters: "a-zA-Z0-9.-:_/ "

Choose a reason for hiding this comment

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

@PhoebeSzmucer Where does this get enforced, and what happens if a comment is provided that contains a character outside of this set?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's enforced on the conjure-java side palantir/conjure-java-runtime-api#1286

I felt like we don't want to validate here again, because it wouldn't be much safer. But I still wanted to mention this requirement, since the BE validates this.

Do you have another idea? I'm happy to open a FLUP PR to do something else.

Choose a reason for hiding this comment

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

I worry a bit that the "comments" terminology implies that it allows arbitrary text, including potentially user-entered text, which could lead applications to completely fail to load under particular conditions (e.g. when the text contains a comma). If we added % to this list, we could consider explicitly URL-encoding each comment?

In practice, we should never be including user-entered content in user agents due to log safety, so I think this is fine for our planned use-cases. Just feels like a bit more of a footgun than we typically leave around.

Choose a reason for hiding this comment

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

It looks like we'd also need to add !~*'( ) to this list to fully support URL-encoding.

Let's keep this as it is for now given it matches the BE behavior and works for how we intend to use it.

*/
comments?: string[];
}

/**
Expand All @@ -40,6 +52,10 @@ export class UserAgent {
}

function formatUserAgent(userAgent: IUserAgent): string {
const { productName, productVersion } = userAgent;
return `${productName}/${productVersion}`;
const { productName, productVersion, comments } = userAgent;
let formattedUserAgent = `${productName}/${productVersion}`;
if (comments != null && comments.length > 0) {
formattedUserAgent += " (" + comments.join("; ") + ")";
}
return formattedUserAgent;
}