Repository navigation
Reject "." and ".." as a file name and as a file path's last segment [patch] - #555
Open
matt-edmondson wants to merge 1 commit into
Open
matt-edmondson wants to merge 1 commit into
matt-edmondson wants to merge 1 commit into
Conversation
…[patch]
"." and ".." name a directory on every platform, but IsFileName checked only
for invalid characters, so FileName.Create("..") passed. dir / FileName then
built a "file" path naming dir's parent, outside dir, and RemoveExtension()
read the second dot as an extension separator and moved it to another
directory. The file path types accepted the same final segments.
IsFileName now rejects both. A new internal HasFileNameSegment attribute on
SemanticFilePath rejects them as the last segment of AbsoluteFilePath,
RelativeFilePath and FilePath, so a valid file path always has a valid
FileName. DirectoryName and the directory path types are unchanged (#503).
Fixes #554
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW3W1DsZmnUsfmWXu2k
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes #554
What was wrong
IsFileNameAttributerejected onlyPath.GetInvalidFileNameChars(). That meantFileName.Create("..")andFileName.Create(".")were accepted. As a result:dir / FileName.Create("..")built a "file" path that namesdir's parent, which is outsidedir.RemoveExtension()then read the second dot as an extension separator and moved the path to another directory.The file path types also accepted a final
.or..segment, such as/home/user/..,a/..or..Change
IsFileNameAttributenow rejects.and...HasFileNameSegmentAttributeis applied toSemanticFilePath<TDerived>. It rejects a final.or..segment, splitting on/or\asSemanticFilePathalready does.AbsoluteFilePath,RelativeFilePathandFilePathinherit it, so a valid file path always has a validFileName. The attribute is internal, so the public API is unchanged.DirectoryNameand the directory path types are unchanged, because a directory can legitimately be named..(Paths:dir.Parent / dir.Namenames a different directory when the path ends in..or., breaking the documented round trip #503).docs/validation-reference.mddescribes the new rule.Open question from the issue:
home / FileName.Create("")still returns the directory itself, typed as a file. Under the framework's convention, empty input passes attribute validation, so whether this should throw is a design choice. I left it unchanged rather than settle it here.Tests
Semantics.Test/Paths/DotFileNameTests.cscovers:FileName.CreatethrowsArgumentExceptionfor.and.., andTryCreatereturns false../..as the final segment: absolute, relative, a bare segment, and with/and\separators....,.bashrc,..hidden,file.. Leading and inner..segments in file paths are still accepted.DirectoryName,RelativeDirectoryPathandAbsoluteDirectoryPathstill accept./...I checked the tests against the code without the fix: with the two library changes reverted, all 7 rejection tests fail.
Full suite on Linux / .NET 10: 2069 passed, 0 failed, 14 skipped. One run error is from this environment only: the
Semantics.Cpp.Testnet9.0 leg needs a .NET 9 runtime, which isn't installed here. Its net10.0 leg passed.Generated by Claude Code