Skip to content

Commit

Permalink
fix disabled user-agent (#6459)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackkav committed Sep 7, 2023
1 parent 2e5b963 commit cc9f759
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/insomnia/src/common/render.ts
Expand Up @@ -36,6 +36,7 @@ export type RenderedRequest = Request & {
disabled?: boolean;
}[];
cookieJar: CookieJar;
suppressUserAgent: boolean;
};

export type RenderedGrpcRequest = GrpcRequest;
Expand Down Expand Up @@ -495,6 +496,7 @@ export async function getRenderedRequestAndContext(
const renderedRequest = renderResult._request;
const renderedCookieJar = renderResult._cookieJar;
renderedRequest.description = await render(description, renderContext, null, KEEP_ON_ERROR);
const suppressUserAgent = request.headers.some(h => h.name.toLowerCase() === 'user-agent' && h.disabled === true);
// Remove disabled params
renderedRequest.parameters = renderedRequest.parameters.filter(p => !p.disabled);
// Remove disabled headers
Expand All @@ -515,6 +517,7 @@ export async function getRenderedRequestAndContext(
return {
context: renderContext,
request: {
suppressUserAgent,
cookieJar: renderedCookieJar,
cookies: [],
isPrivate: false,
Expand Down
3 changes: 2 additions & 1 deletion packages/insomnia/src/main/network/curl.ts
Expand Up @@ -90,6 +90,7 @@ interface OpenCurlRequestOptions {
authentication: RequestAuthentication;
cookieJar: CookieJar;
initialPayload?: string;
suppressUserAgent: boolean;
}
const openCurlConnection = async (
_event: Electron.IpcMainInvokeEvent,
Expand Down Expand Up @@ -137,7 +138,7 @@ const openCurlConnection = async (
const clientCertificates = await models.clientCertificate.findByParentId(options.workspaceId);
const filteredClientCertificates = clientCertificates.filter(c => !c.disabled && urlMatchesCertHost(setDefaultProtocol(c.host, 'https:'), options.url));
const { curl, debugTimeline } = createConfiguredCurlInstance({
req: { ...request, cookieJar: options.cookieJar, cookies: [] },
req: { ...request, cookieJar: options.cookieJar, cookies: [], suppressUserAgent: options.suppressUserAgent },
finalUrl: options.url,
settings,
caCert: caCertificate,
Expand Down
4 changes: 4 additions & 0 deletions packages/insomnia/src/main/network/libcurl-promise.ts
Expand Up @@ -41,6 +41,7 @@ interface RequestUsedHere {
url: string;
cookieJar: any;
cookies: { name: string; value: string }[];
suppressUserAgent: boolean;
}
interface SettingsUsedHere {
preferredHttpVersion: string;
Expand Down Expand Up @@ -376,6 +377,9 @@ export const createConfiguredCurlInstance = ({
const userAgent: RequestHeader | null = headers.find((h: any) => h.name.toLowerCase() === 'user-agent') || null;
const userAgentOrFallback = typeof userAgent?.value === 'string' ? userAgent?.value : 'insomnia/' + version;
curl.setOpt(Curl.option.USERAGENT, userAgentOrFallback);
if (req.suppressUserAgent) {
curl.setOpt(Curl.option.USERAGENT, '');
}

const { username, password, disabled } = authentication;
const isDigest = authentication.type === AUTH_DIGEST;
Expand Down
1 change: 1 addition & 0 deletions packages/insomnia/src/ui/components/request-url-bar.tsx
Expand Up @@ -140,6 +140,7 @@ export const RequestUrlBar = forwardRef<RequestUrlBarHandle, Props>(({
headers: rendered.headers,
authentication: rendered.authentication,
cookieJar: rendered.workspaceCookieJar,
suppressUserAgent: rendered.suppressUserAgent,
});
};
startListening();
Expand Down
Expand Up @@ -106,6 +106,7 @@ export const WebSocketActionBar: FC<ActionBarProps> = ({ request, environmentId,
headers: rendered.headers,
authentication: rendered.authentication,
cookieJar: rendered.workspaceCookieJar,
suppressUserAgent: rendered.suppressUserAgent,
});

};
Expand Down
2 changes: 2 additions & 0 deletions packages/insomnia/src/ui/routes/request.tsx
Expand Up @@ -248,6 +248,7 @@ export interface ConnectActionParams {
headers: RequestHeader[];
authentication: RequestAuthentication;
cookieJar: CookieJar;
suppressUserAgent: boolean;
}
export const connectAction: ActionFunction = async ({ request, params }) => {
const { requestId, workspaceId } = params;
Expand All @@ -274,6 +275,7 @@ export const connectAction: ActionFunction = async ({ request, params }) => {
headers: rendered.headers,
authentication: rendered.authentication,
cookieJar: rendered.cookieJar,
suppressUserAgent: rendered.suppressUserAgent,
});
}
// HACK: even more elaborate hack to get the request to update
Expand Down

0 comments on commit cc9f759

Please sign in to comment.