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

fix(instrumentation-http): skip malformed forwarded headers #5099

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ All notable changes to experimental packages in this project will be documented

### :bug: (Bug Fix)

* fix(instrumentation-http): skip malformed forwarded headers. [#5095](https://github.com/open-telemetry/opentelemetry-js/issues/5095) @pmlanger

### :books: (Refine Doc)

### :house: (Internal)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ function getServerAddress(
): { host: string; port?: string } | null {
const forwardedHeader = request.headers['forwarded'];
if (forwardedHeader) {
for (const entry of forwardedParse(forwardedHeader)) {
for (const entry of parseForwardedHeader(forwardedHeader)) {
if (entry.host) {
return parseHostHeader(entry.host, entry.proto);
}
Expand Down Expand Up @@ -635,7 +635,7 @@ export function getRemoteClientAddress(
): string | null {
const forwardedHeader = request.headers['forwarded'];
if (forwardedHeader) {
for (const entry of forwardedParse(forwardedHeader)) {
for (const entry of parseForwardedHeader(forwardedHeader)) {
if (entry.for) {
return entry.for;
}
Expand Down Expand Up @@ -916,3 +916,11 @@ function normalizeMethod(method?: string | null) {

return '_OTHER';
}

function parseForwardedHeader(header: string): Record<string, string>[] {
try {
return forwardedParse(header);
} catch {
return [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,20 @@ describe('HttpInstrumentation Integration tests', () => {
assert.ok(result.reqHeaders[DummyPropagation.SPAN_CONTEXT_KEY]);
});

it('should succeed even with malformed Forwarded header', async () => {
const spans = memoryExporter.getFinishedSpans();
assert.strictEqual(spans.length, 0);

const headers = { 'x-foo': 'foo', forwarded: 'malformed' };
const result = await httpRequest.get(
new url.URL(`${protocol}://localhost:${mockServerPort}/?query=test`),
{ headers }
);

assert.ok(result.reqHeaders[DummyPropagation.TRACE_CONTEXT_KEY]);
assert.ok(result.reqHeaders[DummyPropagation.SPAN_CONTEXT_KEY]);
});

it('should create a span for GET requests and add propagation headers with Expect headers', async () => {
let spans = memoryExporter.getFinishedSpans();
assert.strictEqual(spans.length, 0);
Expand Down