-
Notifications
You must be signed in to change notification settings - Fork 610
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
Add regexes to check keys and values #2017
Conversation
82ea199
to
caabc5f
Compare
|
||
for key, value in baggage_entries.items(): | ||
# type: ignore | ||
if _key_regex.match(key) is None or _value_regex.match(value) is None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be done in set_baggage
https://github.com/open-telemetry/opentelemetry-python/blob/main/opentelemetry-api/src/opentelemetry/baggage/__init__.py#L58? A error/warning log would be nice when key/value are invalid. Also, I think we need to add a CHANGELOG entry for this fix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be done in
set_baggage
https://github.com/open-telemetry/opentelemetry-python/blob/main/opentelemetry-api/src/opentelemetry/baggage/__init__.py#L58?
Not sure, these regexes are defined for the W3C Baggage, I would expect them to be in the W3C propagator 🤷
A error/warning log would be nice when key/value are invalid.
Added warnings.
Also, I think we need to add a CHANGELOG entry for this fix.
Added an entry
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think set_baggage
is the correct place to validate. If we do that we can safely assume that baggage contents are valid and serialize them. The extract
also uses the set_baggage
and we would just return the same context when key/value are invalid. This requires no change in the propagator if handled in set_baggage
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also set_baggage
could be called independently of extract
if user wants to manually set baggage themselves in context.
1a1ebc6
to
323b627
Compare
d7d924f
to
0b86104
Compare
|
||
from opentelemetry import baggage | ||
from opentelemetry.context import get_current | ||
from opentelemetry.context.context import Context | ||
from opentelemetry.propagators import textmap | ||
|
||
_logger = getLogger(__name__) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we document a link to the spec doc that specifies these rules?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1, and did you find this regex verbatim somewhere, or should we review it for correctness 😅
) | ||
continue | ||
|
||
key_values.append(key + "=" + urllib.parse.quote(str(value))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Going off the issue, this quote()
/ quote_plus()
thing is a separate issue? Would be good to document that in the PR/changelog
- Fix documentation on well known exporters and variable OTEL_TRACES_EXPORTER which were misnamed | ||
([#2023](https://github.com/open-telemetry/opentelemetry-python/pull/2023)) | ||
- `opentelemetry-sdk` `get_aggregated_resource()` returns default resource and service name | ||
whenever called | ||
([#2013](https://github.com/open-telemetry/opentelemetry-python/pull/2013)) | ||
- Add regexes to check W3C baggage keys and values | ||
([#2017](https://github.com/open-telemetry/opentelemetry-python/pull/2017)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
duplicate changelog entries?
_logger = getLogger(__name__) | ||
|
||
# The following regular expressions are taken from | ||
# https://github.com/open-telemetry/opentelemetry-go/blob/4bf6150fa94e18bdf01c96ed78ee6d1c76f8e308/baggage/baggage.go#L36-L55 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel linking to the spec would be better for docs instead of another implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can link to this spec https://w3c.github.io/baggage/#definition
aebac5c
to
24badf3
Compare
_logger = getLogger(__name__) | ||
|
||
# The following regular expressions are taken from | ||
# https://github.com/open-telemetry/opentelemetry-go/blob/4bf6150fa94e18bdf01c96ed78ee6d1c76f8e308/baggage/baggage.go#L36-L55 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can link to this spec https://w3c.github.io/baggage/#definition
# The following regexes are taken from | ||
# https://github.com/open-telemetry/opentelemetry-go/blob/4bf6150fa94e18bdf01c96ed78ee6d1c76f8e308/baggage/baggage.go#L36-L55 | ||
_key_regex = compile_(r"[!#-'*+-.0-9A-Z^-z|~]+") | ||
_value_regex = compile_(r"[!#-+.-:<-\[\]-~-]*") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can avoid re-declaration here?
continue | ||
try: | ||
name, value = entry.split("=", 1) | ||
except Exception: # pylint: disable=broad-except | ||
_logger.warning("Entry %s has been discarded", entry) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we dropping extra members, invalid member? Shouldn't we return the original context if we find baggage string corrupted. We do this in trace context trace state parse.
Hey @ocelotl , This is in open state for a while and good to have it fixed on main. I know you are actively working on metrics prototype. Let me know if you are overwhelmed with lot of stuff. I can take this up and continue :) |
Ah, thanks @lonewolf3739 I appreciate it, assigning to you 👍👍 |
Superseded by #2167. Please review that PR. |
Fixes #2010