From d4ae9588adc93dc3cbb256baf7a7c55e382b6285 Mon Sep 17 00:00:00 2001 From: hyphen-o Date: Thu, 3 Oct 2024 11:39:38 +0900 Subject: [PATCH] =?UTF-8?q?=E3=82=A8=E3=83=A9=E3=83=BC=E3=83=8F=E3=83=B3?= =?UTF-8?q?=E3=83=89=E3=83=AA=E3=83=B3=E3=82=B0=E3=81=AE=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/commands/add.ts | 10 ++++++++++ src/functions/is-valid-path.ts | 23 +++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 src/functions/is-valid-path.ts diff --git a/src/commands/add.ts b/src/commands/add.ts index eadcac0..609527e 100644 --- a/src/commands/add.ts +++ b/src/commands/add.ts @@ -3,6 +3,7 @@ import { readFile } from "node:fs/promises"; import { GIT_INDEX } from "../constants.js"; import { coloredLog } from "../functions/colored-log.js"; import { exists } from "../functions/exists.js"; +import { isValidPath } from "../functions/is-valid-path.js"; import { BlobObject } from "../models/blob-object.js"; import { GitIndex } from "../models/git-index.js"; @@ -19,6 +20,15 @@ export const add = async (options: Array): Promise => { return; } + //ファイル名が条件を満たしていない場合の処理 + if (!isValidPath(filePath)) { + coloredLog({ + text: `fatal: invalid path '${filePath}'`, + color: "red", + }); + return; + } + //ファイルが存在しなかった場合の処理 if (!(await exists(filePath))) { coloredLog({ diff --git a/src/functions/is-valid-path.ts b/src/functions/is-valid-path.ts new file mode 100644 index 0000000..5bd0f1c --- /dev/null +++ b/src/functions/is-valid-path.ts @@ -0,0 +1,23 @@ +// パス名がgitの定める条件に従っているかをチェック +// https://github.com/git/git/blob/v2.12.0/Documentation/technical/index-format.txt +export const isValidPath = (filePath: string): boolean => { + // トップレベルディレクトリからの相対パスであることを確認(先頭のスラッシュがないこと) + if (filePath.startsWith("/")) { + return false; + } + + // 末尾のスラッシュを禁止 + if (filePath.endsWith("/")) { + return false; + } + + // パスコンポーネントをチェック + const components = filePath.split("/"); + for (const component of components) { + if (component === "." || component === ".." || component === ".git") { + return false; + } + } + + return true; +};