continue-custom/core/tools/implementations/createNewFile.ts
Nate Sesti faca87bbc8
fix: add security check to createNewFile tool (CWE-22) (#11238)
* fix: add security check to createNewFile tool (CWE-22)

The createNewFile tool was missing the throwIfFileIsSecurityConcern()
check that readFile already has, allowing path traversal to write
sensitive file types (e.g. .env, SSH keys) via prompt injection.

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

* fix: upgrade tar-fs to patch CVE-2024-12905 and CVE-2025-59343

core/vendor had tar-fs@2.1.2 (via prebuild-install) and tar-fs@3.0.8,
both in vulnerable ranges. npm audit fix upgraded them.

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

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-09 12:27:47 -07:00

49 lines
1.6 KiB
TypeScript

import { inferResolvedUriFromRelativePath } from "../../util/ideUtils";
import { ToolImpl } from ".";
import { throwIfFileIsSecurityConcern } from "../../indexing/ignore";
import { getCleanUriPath, getUriPathBasename } from "../../util/uri";
import { getStringArg } from "../parseArgs";
import { ContinueError, ContinueErrorReason } from "../../util/errors";
export const createNewFileImpl: ToolImpl = async (args, extras) => {
const filepath = getStringArg(args, "filepath");
const contents = getStringArg(args, "contents", true);
const resolvedFileUri = await inferResolvedUriFromRelativePath(
filepath,
extras.ide,
);
if (resolvedFileUri) {
throwIfFileIsSecurityConcern(getCleanUriPath(resolvedFileUri));
const exists = await extras.ide.fileExists(resolvedFileUri);
if (exists) {
throw new ContinueError(
ContinueErrorReason.FileAlreadyExists,
`File ${filepath} already exists. Use the edit tool to edit this file`,
);
}
await extras.ide.writeFile(resolvedFileUri, contents);
await extras.ide.openFile(resolvedFileUri);
await extras.ide.saveFile(resolvedFileUri);
if (extras.codeBaseIndexer) {
void extras.codeBaseIndexer?.refreshCodebaseIndexFiles([resolvedFileUri]);
}
return [
{
name: getUriPathBasename(resolvedFileUri),
description: getCleanUriPath(resolvedFileUri),
content: "File created successfuly",
uri: {
type: "file",
value: resolvedFileUri,
},
},
];
} else {
throw new ContinueError(
ContinueErrorReason.PathResolutionFailed,
"Failed to resolve path",
);
}
};