Fix #2370: Add allowed_path_traversal_elements option to file_path()#2397
Conversation
|
Hi @maintainers, just checking in on this PR whenever you get a chance to review. All checks have passed and there are no conflicts with the base branch. Happy to make any changes if needed — let me know if you'd like the approach adjusted or have any feedback on the implementation. Thanks for maintaining this great library! |
fcurella
left a comment
There was a problem hiding this comment.
I think this is a good feature to have, but we'll also need some unit tests
| extension: Optional[Union[str, Sequence[str]]] = None, | ||
| absolute: Optional[bool] = True, | ||
| file_system_rule: Literal["linux", "windows"] = "linux", | ||
| allowed_path_traversal_elements: Optional[Sequence[str]] = None, |
There was a problem hiding this comment.
what is the use case for having this as strings, rather than a single boolean? What other path traversal elements are you thinking of, other than . and ..?
|
Hi @fcurella, thanks for the review! Regarding the use case for Sequence[str] over boolean:
I'll add unit tests now and push an update shortly. |
|
Hi @fcurella, thanks for the review! Regarding Sequence[str] over boolean: a boolean would only allow I've now added unit tests covering: default behavior, single |
What does this change
Adds a new optional parameter
allowed_path_traversal_elementstofile_path()that allows path-traversal segments (e.g. '.' and '..') to be randomly included as generated directory components.What was wrong
file_path()had no way to generate paths containing path-traversal elements like '.' or '..', which are useful for testing directory traversal attack detection and input sanitization.How this fixes it
Added a new
allowed_path_traversal_elementsparameter (defaultNone, preserving existing behavior). When provided as a sequence (e.g.['.', '..']), each directory segment in the generated path has a chance of being randomly replaced with one of these traversal elements instead of a regular word.Fixes #2370
AI Assistance Disclosure (REQUIRED)
I used Claude (Anthropic) to help draft the implementation and docstring. I reviewed, understood, and verified the logic and tested it manually before submitting.
Checklist
make lint