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

Add regexes to check keys and values #2017

Closed
wants to merge 11 commits into from

Conversation

ocelotl
Copy link
Contributor

@ocelotl ocelotl commented Aug 3, 2021

Fixes #2010

@ocelotl ocelotl added the bug Something isn't working label Aug 3, 2021
@ocelotl ocelotl requested review from a team, codeboten and srikanthccv and removed request for a team August 3, 2021 22:52
@ocelotl ocelotl self-assigned this Aug 3, 2021
@ocelotl ocelotl added the Skip Changelog PRs that do not require a CHANGELOG.md entry label Aug 3, 2021
@ocelotl ocelotl force-pushed the issue_2010 branch 2 times, most recently from 82ea199 to caabc5f Compare August 3, 2021 22:58

for key, value in baggage_entries.items():
# type: ignore
if _key_regex.match(key) is None or _value_regex.match(value) is None:
Copy link
Member

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.

Copy link
Contributor Author

@ocelotl ocelotl Aug 5, 2021

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

Copy link
Member

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.

Copy link
Contributor

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.

@ocelotl ocelotl force-pushed the issue_2010 branch 2 times, most recently from 1a1ebc6 to 323b627 Compare August 5, 2021 14:52
@ocelotl ocelotl removed the Skip Changelog PRs that do not require a CHANGELOG.md entry label Aug 5, 2021
@ocelotl ocelotl force-pushed the issue_2010 branch 2 times, most recently from d7d924f to 0b86104 Compare August 17, 2021 08:02
@owais owais self-requested a review August 19, 2021 15:37

from opentelemetry import baggage
from opentelemetry.context import get_current
from opentelemetry.context.context import Context
from opentelemetry.propagators import textmap

_logger = getLogger(__name__)
Copy link
Contributor

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?

Copy link
Member

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)))
Copy link
Member

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

@ocelotl ocelotl added the Approve Public API check This label shows that the public symbols added or changed in a PR are strictly necessary label Aug 27, 2021
- 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))
Copy link
Contributor

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
Copy link
Contributor

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_logger = getLogger(__name__)

# The following regular expressions are taken from
# https://github.com/open-telemetry/opentelemetry-go/blob/4bf6150fa94e18bdf01c96ed78ee6d1c76f8e308/baggage/baggage.go#L36-L55
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# 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"[!#-+.-:<-\[\]-~-]*")
Copy link
Member

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)
Copy link
Member

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.

@srikanthccv
Copy link
Member

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 :)

@ocelotl
Copy link
Contributor Author

ocelotl commented Sep 30, 2021

Ah, thanks @lonewolf3739 I appreciate it, assigning to you 👍👍

@srikanthccv
Copy link
Member

Superseded by #2167. Please review that PR.

@srikanthccv srikanthccv closed this Oct 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Approve Public API check This label shows that the public symbols added or changed in a PR are strictly necessary bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Baggage value is not parsed correctly in downstream server
5 participants