-
-
Notifications
You must be signed in to change notification settings - Fork 188
Mask password in URL.__repr__ for safer debugging output #1629 #1630
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Mask the password component in ``yarl.URL.__repr__`` to prevent accidental credential exposure in logs and debug | ||
| output -- by :user:`mbaas038` and :user:`jhbuhrman`. |
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -491,24 +491,34 @@ def build( | |||||||||||||
| def __init_subclass__(cls) -> NoReturn: | ||||||||||||||
| raise TypeError(f"Inheriting a class {cls!r} from URL is forbidden") | ||||||||||||||
|
|
||||||||||||||
| def __str__(self) -> str: | ||||||||||||||
| def _make_string(self, mask_password: bool = False) -> str: | ||||||||||||||
| if not self._path and self._netloc and (self._query or self._fragment): | ||||||||||||||
| path = "/" | ||||||||||||||
| else: | ||||||||||||||
| path = self._path | ||||||||||||||
| if (port := self.explicit_port) is not None and port == DEFAULT_PORTS.get( | ||||||||||||||
| self._scheme | ||||||||||||||
| ): | ||||||||||||||
| if ( | ||||||||||||||
| (port := self.explicit_port) is not None | ||||||||||||||
| and port == DEFAULT_PORTS.get(self._scheme) | ||||||||||||||
| ) or mask_password: | ||||||||||||||
| # port normalization - using None for default ports to remove from rendering | ||||||||||||||
| # https://datatracker.ietf.org/doc/html/rfc3986.html#section-6.2.3 | ||||||||||||||
| host = self.host_subcomponent | ||||||||||||||
| netloc = make_netloc(self.raw_user, self.raw_password, host, None) | ||||||||||||||
| netloc = make_netloc( | ||||||||||||||
| self.raw_user, | ||||||||||||||
| self.raw_password, | ||||||||||||||
| host, | ||||||||||||||
| None, | ||||||||||||||
| mask_password=mask_password, | ||||||||||||||
| ) | ||||||||||||||
| else: | ||||||||||||||
| netloc = self._netloc | ||||||||||||||
| return unsplit_result(self._scheme, netloc, path, self._query, self._fragment) | ||||||||||||||
|
|
||||||||||||||
| def __str__(self) -> str: | ||||||||||||||
| return self._make_string(mask_password=False) | ||||||||||||||
|
|
||||||||||||||
| def __repr__(self) -> str: | ||||||||||||||
| return f"{self.__class__.__name__}('{str(self)}')" | ||||||||||||||
| return f"{self.__class__.__name__}('{self._make_string(mask_password=True)}')" | ||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An additional consideration: since Suggestion:
Suggested change
This also requires changing the tests. A more general remark: the explicit insertion of single quotes is tricky anyway for certain edge cases, better rely on |
||||||||||||||
|
|
||||||||||||||
| def __bytes__(self) -> bytes: | ||||||||||||||
| return str(self).encode("ascii") | ||||||||||||||
|
|
||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.