-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
fix(path_util): guard path_Mapping against IndexError on short relative targets #8920
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,19 @@ | ||
| from astrbot.core.utils.path_util import path_Mapping | ||
|
|
||
|
|
||
| def test_path_mapping_target_single_dot_does_not_crash(): | ||
| # A mapping whose target is "." reduces srcPath to "." (one character). | ||
| # The relative-path branch indexed srcPath[1] unconditionally and raised | ||
| # IndexError. path_Mapping is reachable from the respond stage, so a user | ||
| # configuring such a rule could crash message handling. | ||
| assert path_Mapping(["somepath:."], "somepath") == "." | ||
|
|
||
|
|
||
| def test_path_mapping_target_double_dot_does_not_crash(): | ||
| # ".." is two characters, so the inner srcPath[2] access also overran. | ||
| assert path_Mapping(["somepath:.."], "somepath") == ".." | ||
|
|
||
|
|
||
| def test_path_mapping_relative_target_still_normalized(): | ||
| # Regression: a normal relative target keeps its existing behaviour. | ||
| assert path_Mapping(["somepath:./sub"], "somepath") == "./sub" | ||
|
Comment on lines
+17
to
+19
Contributor
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. suggestion (testing): Add coverage for short relative targets like "./" and "../" to fully lock in the new index-guard logic. Since the new logic guards |
||
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.
In Python, string slicing is safe from
IndexErrorand returns an empty string if the index is out of bounds. We can leverage this to simplify the code and avoid verbose length checks by usingsrcPath[1:2]andsrcPath[2:3].