Skip to content

Commit e45b016

Browse files
Potential fix for code scanning alert no. 2: Uncontrolled data used in path expression (#96)
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
1 parent b5e83e4 commit e45b016

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/content/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ export const getAllDocsCategories = () => {
5252
};
5353

5454
export const getMdxBySlug = async (basePath: string, slug: string) => {
55-
const mdxPath = path.join(DATA_PATH, basePath, `${slug}.mdx`);
55+
// Construct and resolve the path to prevent path traversal
56+
const mdxPath = path.resolve(DATA_PATH, basePath, `${slug}.mdx`);
57+
// Ensure the resolved path is within DATA_PATH
58+
if (!mdxPath.startsWith(DATA_PATH + path.sep)) return;
5659
if (!fs.existsSync(mdxPath)) return;
5760

58-
const source = fs.readFileSync(
59-
path.join(DATA_PATH, basePath, `${slug}.mdx`),
60-
"utf8",
61-
);
61+
const source = fs.readFileSync(mdxPath, "utf8");
6262

6363
const { frontmatter, code } = await bundleMDX({ source });
6464

0 commit comments

Comments
 (0)