Skip to content

Use general utility methods for path safety and file name validation. - #3297

Open
drgrice1 wants to merge 2 commits into
openwebwork:developfrom
drgrice1:path-handling-rework
Open

drgrice1 wants to merge 2 commits into
openwebwork:developfrom
drgrice1:path-handling-rework

Conversation

@drgrice1

Copy link
Copy Markdown
Member

Instead of each module deciding what a safe path or a valid file name is, frequently in slightly different ways, use the new pathIsSafe, fileNameIsValid, and filePathIsValid methods and the existing pathIsSubdir method (renamed from the path_is_subdir method to match camel casing used by other methods in the file) in WeBWorK::Utils::Files. isAchievementAssetSafe was removed from the WeBWorK::Utils::Achievements package.

The names of new files and directories are now validated the same way everywhere. Names may contain letters, digits, spaces, and the characters "-", "_", ".", "+", ",", "(", ")", and "~", and may not start with a period. Paths to existing files are only checked to not contain ".." directories. Files whose names start with a period are no longer extracted from archives in the file manager.

SendMail also now checks the message and merge file names before saving them as settings, and only reports that a message was saved if the file was actually written.

@somiaj

somiaj commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

I notice that the error messages use the same list in lots of different locations:

can only contain letters, digits, spaces, and the characters '"-", "_", ".", "+", ",", "(", ")", and "~~", and cannot start with a period.

I was wondering if it would be worth centralizing that list in case that list were to ever change. Though due to translations (the start of the message isn't always the same), that might just cause other problems. Just a thought.

@drgrice1

Copy link
Copy Markdown
Member Author

Those messages are now generated by utility methods in WeBWorK::Utils::Files. Since they are with the methods that determine what characters are valid in a file name, they can be easily kept in sync.

drgrice1 and others added 2 commits November 22, 2026 12:53
Instead of each module deciding what a safe path or a valid file name
is, frequently in slightly different ways, use the new `pathIsSafe`,
`fileNameIsValid`, and `filePathIsValid` methods and the existing
`pathIsSubdir` method (renamed from the `path_is_subdir` method to match
camel casing used by other methods in the file) in `WeBWorK::Utils::Files`.
`isAchievementAssetSafe` was removed from the `WeBWorK::Utils::Achievements`
package.

The names of new files and directories are now validated the same way
everywhere. Names may contain letters, digits, spaces, and the characters
"-", "_", ".", "+", ",", "(", ")", and "~", and may not start with a
period. Paths to existing files are only checked to not contain ".."
directories. Files whose names start with a period are no longer
extracted from archives in the file manager.

SendMail also now checks the message and merge file names before saving
them as settings, and only reports that a message was saved if the file
was actually written.
Add `fileNameRequirementsMessage` and `filePathRequirementsMessage`
methods to `WeBWorK::Utils::Files` so that the description of what a
valid name or path is lives next to `fileNameIsValid` and
`filePathIsValid`, instead of being duplicated everywhere it is reported.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@drgrice1
drgrice1 force-pushed the path-handling-rework branch from b5e4bc1 to 637fa6d Compare November 22, 2026 18:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants