-
Notifications
You must be signed in to change notification settings - Fork 868
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factor in X-Forwarded-Host / Forwarded when capturing server.address …
…and server.port
- Loading branch information
Mateusz Rzeszutek
committed
Oct 19, 2023
1 parent
e1efa2d
commit 2732c3b
Showing
9 changed files
with
328 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
...telemetry/instrumentation/api/instrumenter/http/ForwardedHostAddressAndPortExtractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.api.instrumenter.http; | ||
|
||
import static io.opentelemetry.instrumentation.api.instrumenter.http.HeaderParsingHelper.notFound; | ||
import static io.opentelemetry.instrumentation.api.instrumenter.http.HeaderParsingHelper.setPort; | ||
|
||
import io.opentelemetry.instrumentation.api.instrumenter.network.internal.AddressAndPortExtractor; | ||
import java.util.Locale; | ||
|
||
final class ForwardedHostAddressAndPortExtractor<REQUEST> | ||
implements AddressAndPortExtractor<REQUEST> { | ||
|
||
private final HttpCommonAttributesGetter<REQUEST, ?> getter; | ||
|
||
ForwardedHostAddressAndPortExtractor(HttpCommonAttributesGetter<REQUEST, ?> getter) { | ||
this.getter = getter; | ||
} | ||
|
||
@Override | ||
public void extract(AddressPortSink sink, REQUEST request) { | ||
// try Forwarded | ||
for (String forwarded : getter.getHttpRequestHeader(request, "forwarded")) { | ||
if (extractFromForwardedHeader(sink, forwarded)) { | ||
return; | ||
} | ||
} | ||
|
||
// try X-Forwarded-Host | ||
for (String forwardedHost : getter.getHttpRequestHeader(request, "x-forwarded-host")) { | ||
if (extractHost(sink, forwardedHost, 0, forwardedHost.length())) { | ||
return; | ||
} | ||
} | ||
|
||
// try Host | ||
for (String host : getter.getHttpRequestHeader(request, "host")) { | ||
if (extractHost(sink, host, 0, host.length())) { | ||
return; | ||
} | ||
} | ||
} | ||
|
||
private static boolean extractFromForwardedHeader(AddressPortSink sink, String forwarded) { | ||
int start = forwarded.toLowerCase(Locale.ROOT).indexOf("host="); | ||
if (start < 0) { | ||
return false; | ||
} | ||
start += "host=".length(); // start is now the index after host= | ||
if (start >= forwarded.length() - 1) { // the value after host= must not be empty | ||
return false; | ||
} | ||
// find the end of the `host=<address>` section | ||
int end = forwarded.indexOf(';', start); | ||
if (end < 0) { | ||
end = forwarded.length(); | ||
} | ||
return extractHost(sink, forwarded, start, end); | ||
} | ||
|
||
private static boolean extractHost(AddressPortSink sink, String host, int start, int end) { | ||
if (start >= end) { | ||
return false; | ||
} | ||
|
||
// skip quotes | ||
if (host.charAt(start) == '"') { | ||
// try to find the end of the quote | ||
int quoteEnd = host.indexOf('"', start + 1); | ||
if (notFound(quoteEnd, end)) { | ||
// malformed header value | ||
return false; | ||
} | ||
return extractHost(sink, host, start + 1, quoteEnd); | ||
} | ||
|
||
int hostHeaderSeparator = host.indexOf(':', start); | ||
if (notFound(hostHeaderSeparator, end)) { | ||
sink.setAddress(host.substring(start, end)); | ||
} else { | ||
sink.setAddress(host.substring(start, hostHeaderSeparator)); | ||
setPort(sink, host, hostHeaderSeparator + 1, end); | ||
} | ||
|
||
return true; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HeaderParsingHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.api.instrumenter.http; | ||
|
||
import io.opentelemetry.instrumentation.api.instrumenter.network.internal.AddressAndPortExtractor.AddressPortSink; | ||
|
||
final class HeaderParsingHelper { | ||
|
||
static boolean notFound(int pos, int end) { | ||
return pos < 0 || pos >= end; | ||
} | ||
|
||
static void setPort(AddressPortSink sink, String header, int start, int end) { | ||
if (start == end) { | ||
return; | ||
} | ||
try { | ||
sink.setPort(Integer.parseInt(header.substring(start, end))); | ||
} catch (NumberFormatException ignored) { | ||
// malformed port, ignoring | ||
} | ||
} | ||
|
||
private HeaderParsingHelper() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.