这是indexloc提供的服务,不要输入任何密码
Skip to content

Fix Uncontrolled data used in path expression #3821

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions server/utils/files/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,17 +177,23 @@ async function storeVectorResult(vectorData = [], filename = null) {
// Purges a file from the documents/ folder.
async function purgeSourceDocument(filename = null) {
if (!filename) return;
const filePath = path.resolve(documentsPath, normalizePath(filename));
try {
const sanitizedFilename = normalizePath(filename);
const filePath = path.resolve(documentsPath, sanitizedFilename);

if (
!fs.existsSync(filePath) ||
!isWithin(documentsPath, filePath) ||
!fs.lstatSync(filePath).isFile()
)
return;
if (
!filePath.startsWith(documentsPath) || // Ensure the path is within documentsPath
!fs.existsSync(filePath) ||
!fs.lstatSync(filePath).isFile()
) {
throw new Error(`Invalid or unsafe file path: ${filename}`);
}

console.log(`Purging source document of ${filename}.`);
fs.rmSync(filePath);
console.log(`Purging source document of ${filename}.`);
fs.rmSync(filePath);
} catch (error) {
console.error(`Failed to purge source document: ${error.message}`);
}
Comment on lines +180 to +196
Copy link
Member

Choose a reason for hiding this comment

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

const sanitizedFilename = normalizePath(filename);
const filePath = path.resolve(documentsPath, sanitizedFilename);

is the same as

const filePath = path.resolve(documentsPath, normalizePath(filename));

and this block

 !filePath.startsWith(documentsPath) || // Ensure the path is within documentsPath
!fs.existsSync(filePath) ||
 !fs.lstatSync(filePath).isFile()

filePath.startsWith(documentsPath) does a similar check of what isWithin does already, no? So functionally, the logic has not changed here. We could have both or check within before checking existence, maybe?

return;
}

Expand Down Expand Up @@ -251,9 +257,12 @@ function isWithin(outer, inner) {
function normalizePath(filepath = "") {
const result = path
.normalize(filepath.trim())
.replace(/^(\.\.(\/|\\|$))+/, "")
.replace(/^(\.\.(\/|\\|$))+/, "") // Remove leading `../` sequences
.replace(/(\.\.(\/|\\|$))+/g, "") // Remove any remaining `../` segments
.trim();
if (["..", ".", "/"].includes(result)) throw new Error("Invalid path.");
if (!result || result.startsWith("/") || result.includes("..")) {
throw new Error("Invalid or unsafe path.");
}
return result;
Comment on lines +260 to 266
Copy link
Member

Choose a reason for hiding this comment

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

This is a good improvement to apply 👍

}

Expand Down