Skip to content

Commit 501d2bd

Browse files
onurtemizkanAbhiPrasad
authored andcommitted
fix(remix): Don't inject trace/baggage to redirect and catch responses (#8467)
Skips `trace` and `baggage` injections to `redirect` and `catch` responses. For `redirect` responses: It was breaking the behaviour of redirection. Internal redirection targets should already have their `trace` and `baggage`, so I assume this should not break the connection between services at the end. `catch` responses do not have bodies, and they are thrown by Remix, so skipping injection as well not to potentially break the internal catch behaviour of Remix.
1 parent cc71d36 commit 501d2bd

File tree

4 files changed

+58
-12
lines changed

4 files changed

+58
-12
lines changed

packages/remix/src/utils/instrumentServer.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -236,18 +236,26 @@ function makeWrappedRootLoader(origLoader: DataFunction): DataFunction {
236236
};
237237
}
238238

239-
// Note: `redirect` and `catch` responses do not have bodies to extract
240-
if (isResponse(res) && !isRedirectResponse(res) && !isCatchResponse(res)) {
241-
const data = await extractData(res);
242-
243-
if (typeof data === 'object') {
244-
return json(
245-
{ ...data, ...traceAndBaggage },
246-
{ headers: res.headers, statusText: res.statusText, status: res.status },
247-
);
248-
} else {
249-
__DEBUG_BUILD__ && logger.warn('Skipping injection of trace and baggage as the response body is not an object');
239+
if (isResponse(res)) {
240+
// Note: `redirect` and `catch` responses do not have bodies to extract.
241+
// We skip injection of trace and baggage in those cases.
242+
// For `redirect`, a valid internal redirection target will have the trace and baggage injected.
243+
if (isRedirectResponse(res) || isCatchResponse(res)) {
244+
__DEBUG_BUILD__ && logger.warn('Skipping injection of trace and baggage as the response does not have a body');
250245
return res;
246+
} else {
247+
const data = await extractData(res);
248+
249+
if (typeof data === 'object') {
250+
return json(
251+
{ ...data, ...traceAndBaggage },
252+
{ headers: res.headers, statusText: res.statusText, status: res.status },
253+
);
254+
} else {
255+
__DEBUG_BUILD__ &&
256+
logger.warn('Skipping injection of trace and baggage as the response body is not an object');
257+
return res;
258+
}
251259
}
252260
}
253261

packages/remix/test/integration/app_v1/root.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ export const loader: LoaderFunction = async ({ request }) => {
3434
throw redirect('/?type=plain');
3535
case 'returnRedirect':
3636
return redirect('/?type=plain');
37+
case 'throwRedirectToExternal':
38+
throw redirect('https://example.com');
39+
case 'returnRedirectToExternal':
40+
return redirect('https://example.com');
3741
default: {
3842
return {};
3943
}

packages/remix/test/integration/app_v2/root.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ export const loader: LoaderFunction = async ({ request }) => {
3434
throw redirect('/?type=plain');
3535
case 'returnRedirect':
3636
return redirect('/?type=plain');
37+
case 'throwRedirectToExternal':
38+
throw redirect('https://example.com');
39+
case 'returnRedirectToExternal':
40+
return redirect('https://example.com');
3741
default: {
3842
return {};
3943
}

packages/remix/test/integration/test/client/root-loader.test.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ test('should inject `sentry-trace` and `baggage` into root loader throwing a red
125125
}) => {
126126
await page.goto('/?type=throwRedirect');
127127

128+
// We should be successfully redirected to the path.
129+
expect(page.url()).toEqual(expect.stringContaining('/?type=plain'));
130+
128131
const { sentryTrace, sentryBaggage } = await extractTraceAndBaggageFromMeta(page);
129132

130133
expect(sentryTrace).toEqual(expect.any(String));
@@ -138,11 +141,14 @@ test('should inject `sentry-trace` and `baggage` into root loader throwing a red
138141
});
139142
});
140143

141-
test('should inject `sentry-trace` and `baggage` into root loader returning a redirection to a plain object', async ({
144+
test('should inject `sentry-trace` and `baggage` into root loader returning a redirection to valid path.', async ({
142145
page,
143146
}) => {
144147
await page.goto('/?type=returnRedirect');
145148

149+
// We should be successfully redirected to the path.
150+
expect(page.url()).toEqual(expect.stringContaining('/?type=plain'));
151+
146152
const { sentryTrace, sentryBaggage } = await extractTraceAndBaggageFromMeta(page);
147153

148154
expect(sentryTrace).toEqual(expect.any(String));
@@ -155,3 +161,27 @@ test('should inject `sentry-trace` and `baggage` into root loader returning a re
155161
sentryBaggage: sentryBaggage,
156162
});
157163
});
164+
165+
test('should return redirect to an external path with no baggage and trace injected.', async ({ page }) => {
166+
await page.goto('/?type=returnRedirectToExternal');
167+
168+
// We should be successfully redirected to the external path.
169+
expect(page.url()).toEqual(expect.stringContaining('https://example.com'));
170+
171+
const { sentryTrace, sentryBaggage } = await extractTraceAndBaggageFromMeta(page);
172+
173+
expect(sentryTrace).toBeUndefined();
174+
expect(sentryBaggage).toBeUndefined();
175+
});
176+
177+
test('should throw redirect to an external path with no baggage and trace injected.', async ({ page }) => {
178+
await page.goto('/?type=throwRedirectToExternal');
179+
180+
// We should be successfully redirected to the external path.
181+
expect(page.url()).toEqual(expect.stringContaining('https://example.com'));
182+
183+
const { sentryTrace, sentryBaggage } = await extractTraceAndBaggageFromMeta(page);
184+
185+
expect(sentryTrace).toBeUndefined();
186+
expect(sentryBaggage).toBeUndefined();
187+
});

0 commit comments

Comments
 (0)