Title: Clarify the documentation of pathlib.Path.is_relative_to() · Issue #99334 · python/cpython · GitHub
Open Graph Title: Clarify the documentation of pathlib.Path.is_relative_to() · Issue #99334 · python/cpython
X Title: Clarify the documentation of pathlib.Path.is_relative_to() · Issue #99334 · python/cpython
Description: Hi, Currently (python 3.10.6 & 3.11.0): from pathlib import Path p = Path('/var/log/../../opt') p.is_relative_to('/var/log') >>> True p = p.resolve() p.is_relative_to('/var/log') >>> False Once you know is_relative_to uses relative_to, t...
Open Graph Description: Hi, Currently (python 3.10.6 & 3.11.0): from pathlib import Path p = Path('/var/log/../../opt') p.is_relative_to('/var/log') >>> True p = p.resolve() p.is_relative_to('/var/log') >>> False Once you...
X Description: Hi, Currently (python 3.10.6 & 3.11.0): from pathlib import Path p = Path('/var/log/../../opt') p.is_relative_to('/var/log') >>> True p = p.resolve() p.is_relative_to(&...
Opengraph URL: https://github.com/python/cpython/issues/99334
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Clarify the documentation of pathlib.Path.is_relative_to()","articleBody":"Hi,\r\n\r\nCurrently (python 3.10.6 \u0026 3.11.0):\r\n```python\r\nfrom pathlib import Path\r\n\r\np = Path('/var/log/../../opt')\r\np.is_relative_to('/var/log')\r\n\u003e\u003e\u003e True\r\n\r\np = p.resolve()\r\np.is_relative_to('/var/log')\r\n\u003e\u003e\u003e False\r\n```\r\nOnce you know `is_relative_to` uses `relative_to`, this makes more sense but it's not obvious from the documentation and the examples given. Also it can easily lead to code that looks secure but isn't. Case in point, I was tasked with reviewing this code today (simplified for illustration purposes):\r\n\r\n```python\r\npath = Path(ROOT_PATH, user_input_rel_path)\r\nif path.is_relative_to(ROOT_PATH):\r\n path.unlink()\r\nelse:\r\n raise PermissionError('Nope!')\r\n```\r\n\r\nI was unsure if I should open a bug or not because one could easily argue it isn't a bug. I do believe however that a warning in the documentation could save a few devs from making a mistake.\r\n\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-114031\n* gh-114460\n* gh-114461\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/thomas-mckay","@type":"Person","name":"thomas-mckay"},"datePublished":"2022-11-10T14:58:53.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":4},"url":"https://github.com/99334/cpython/issues/99334"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:3ff6fc3f-9340-01d1-fb7f-63f23bfc4d27 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DEEE:31CE75:1849DA:1FADA9:696976A3 |
| html-safe-nonce | 6c6ac7f59b448b7e2f0ba7ebab733d481c3818b0022ee2ba850230cc51edec49 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERUVFOjMxQ0U3NToxODQ5REE6MUZBREE5OjY5Njk3NkEzIiwidmlzaXRvcl9pZCI6IjMyOTI4NTE5ODYyMzM5MTUwNDMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 13a67f81ac815aab5b969088913c568de964bd0b764d8be1edb95a16876dfbb4 |
| hovercard-subject-tag | issue:1444059294 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/99334/issue_layout |
| twitter:image | https://opengraph.githubassets.com/630d97bf9215fba571c5add466ee733ad9d2d89a5d412ea7cca09855648ce3b9/python/cpython/issues/99334 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/630d97bf9215fba571c5add466ee733ad9d2d89a5d412ea7cca09855648ce3b9/python/cpython/issues/99334 |
| og:image:alt | Hi, Currently (python 3.10.6 & 3.11.0): from pathlib import Path p = Path('/var/log/../../opt') p.is_relative_to('/var/log') >>> True p = p.resolve() p.is_relative_to('/var/log') >>> False Once you... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | thomas-mckay |
| hostname | github.com |
| expected-hostname | github.com |
| None | c6f193beb8ff08443adc07685d75302ab8aaf0a135f6e251c3ff3112c8deb881 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 212e3e3d3298bf5b313830edfd2399e869f7ea76 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width