Skip to content

Validate API-supplied paths to prevent directory traversal#398

Merged
Shivansps merged 2 commits into
KnossosNET:mainfrom
Goober5000:fix/h6h7
May 9, 2026
Merged

Validate API-supplied paths to prevent directory traversal#398
Shivansps merged 2 commits into
KnossosNET:mainfrom
Goober5000:fix/h6h7

Conversation

@Goober5000
Copy link
Copy Markdown
Contributor

file.dest and file.filename values from the Nebula API were used directly in filesystem operations (directory creation, download target paths, decompression destinations) with no validation, allowing a compromised server or MITM to write files outside the mod library.

Adds IsSubPath() to KnUtils using Path.GetFullPath containment to reject absolute paths and "../" traversal sequences. InstallMod and InstallBuild now validate both fields before use and cancel the task if either escapes the mod directory.

Also adds belt-and-suspenders entry path validation to the SharpCompress extraction loops and fixes the WriteSymbolicLink lambda to reject symlink targets that resolve outside the destination directory.

file.dest and file.filename values from the Nebula API were used
directly in filesystem operations (directory creation, download
target paths, decompression destinations) with no validation, allowing
a compromised server or MITM to write files outside the mod library.

Adds IsSubPath() to KnUtils using Path.GetFullPath containment to
reject absolute paths and "../" traversal sequences. InstallMod and
InstallBuild now validate both fields before use and cancel the task
if either escapes the mod directory.

Also adds belt-and-suspenders entry path validation to the SharpCompress
extraction loops and fixes the WriteSymbolicLink lambda to reject
symlink targets that resolve outside the destination directory.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
}

Info = "Tasks: " + ProgressCurrent + "/" + ProgressBarMax;
if (!KnUtils.IsSubPath(modPath, file.filename))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning: file is not null here, possible null reference against arg for parameter candiatePath

{
file.dest = string.Empty;
}
if (!KnUtils.IsSubPath(modPath, file.filename))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning: file is not null here, possible null reference against arg for parameter candiatePath

{
Log.Add(Log.LogSeverity.Error, "TaskItemViewModel.InstallBuild()", $"Unsafe dest path in build '{build.id}': {file.dest}");
CancelTaskCommand();
return false;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This return line needs to be removed, otherwise it does not build

@wookieejedi
Copy link
Copy Markdown
Contributor

I've pushed changs to fix the above issues, and my tests work as expected

@Shivansps Shivansps merged commit 2d8e844 into KnossosNET:main May 9, 2026
@Goober5000 Goober5000 deleted the fix/h6h7 branch May 9, 2026 18:47
@Goober5000 Goober5000 mentioned this pull request May 10, 2026
Shivansps pushed a commit that referenced this pull request May 10, 2026
Removing `return false` in the second commit to #398 was the wrong fix; it should have been changed to `return null`.  But this had an unexpected benefit as it turns out several other places in the code need proper handling of task cancellation.
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.

3 participants