Skip to content

Commit 2ec2ac7

Browse files
author
Daniel Rogers
committed
Add support for sanitizing HTTP header values.
First step of #1184
1 parent 18e056b commit 2ec2ac7

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

Diff for: CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3535
([#1197](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1197))
3636
- Add metric instumentation for flask
3737
([#1186](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1186))
38+
- `opentelemetry-util-http` Add support for sanitizing HTTP header values.
39+
([#1253](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1253))
3840

3941
## [1.12.0rc2-0.32b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.12.0rc2-0.32b0) - 2022-07-01
4042

Diff for: util/opentelemetry-util-http/src/opentelemetry/util/http/__init__.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@
1515
from os import environ
1616
from re import compile as re_compile
1717
from re import search
18+
from re import IGNORECASE as RE_IGNORECASE
1819
from typing import Iterable, List
1920
from urllib.parse import urlparse, urlunparse
2021

2122
from opentelemetry.semconv.trace import SpanAttributes
2223

24+
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SANITIZE_FIELDS = (
25+
"OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SANITIZE_FIELDS"
26+
)
2327
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST = (
2428
"OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST"
2529
)
@@ -60,6 +64,22 @@ def url_disabled(self, url: str) -> bool:
6064
return bool(self._excluded_urls and search(self._regex, url))
6165

6266

67+
class SanitizeValue:
68+
"""Class to sanitize (remove sensitive data from) certain headers (given as a list of regexes)"""
69+
70+
def __init__(self, sanitized_fields: Iterable[str]):
71+
self._sanitized_fields = sanitized_fields
72+
if self._sanitized_fields:
73+
self._regex = re_compile("|".join(sanitized_fields), RE_IGNORECASE)
74+
75+
def sanitize_header_value(self, header: str, value: str) -> str:
76+
return (
77+
"[REDACTED]"
78+
if (self._sanitized_fields and search(self._regex, header))
79+
else value
80+
)
81+
82+
6383
_root = r"OTEL_PYTHON_{}"
6484

6585

@@ -90,7 +110,7 @@ def get_excluded_urls(instrumentation: str) -> ExcludeList:
90110

91111
def parse_excluded_urls(excluded_urls: str) -> ExcludeList:
92112
"""
93-
Small helper to put an arbitrary url list inside of ExcludeList
113+
Small helper to put an arbitrary url list inside an ExcludeList
94114
"""
95115
if excluded_urls:
96116
excluded_url_list = [

Diff for: util/opentelemetry-util-http/tests/test_capture_custom_headers.py

+16
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from unittest.mock import patch
1717

1818
from opentelemetry.util.http import (
19+
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SANITIZE_FIELDS,
1920
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST,
2021
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE,
2122
get_custom_headers,
@@ -58,6 +59,21 @@ def test_get_custom_response_header(self):
5859
],
5960
)
6061

62+
@patch.dict(
63+
"os.environ",
64+
{
65+
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SANITIZE_FIELDS: "My-Secret-Header,My-Secret-Header-2"
66+
},
67+
)
68+
def test_get_custom_sanitize_header(self):
69+
custom_headers_to_capture = get_custom_headers(
70+
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SANITIZE_FIELDS
71+
)
72+
self.assertEqual(
73+
custom_headers_to_capture,
74+
["My-Secret-Header", "My-Secret-Header-2"],
75+
)
76+
6177
def test_normalise_request_header_name(self):
6278
key = normalise_request_header_name("Test-Header")
6379
self.assertEqual(key, "http.request.header.test_header")

0 commit comments

Comments
 (0)