diff --git a/.jules/sentinel.md b/.jules/sentinel.md index cdf88010..6fc3158c 100644 --- a/.jules/sentinel.md +++ b/.jules/sentinel.md @@ -88,3 +88,8 @@ **Vulnerability:** CSP 해시 불일치로 인한 인라인 스타일 차단 **Learning:** 브라우저는 인라인 스크립트와 스타일의 내부 텍스트(공백과 줄바꿈 포함)를 정확하게 해싱하여 Content-Security-Policy(CSP) 해시와 비교합니다. Kotlin의 멀티라인 문자열(`"""`)을 사용하여 템플릿에 콘텐츠를 주입할 때 암묵적인 여백이나 줄바꿈이 추가되면 최종 HTML 문자열이 변경되어 CSP 해시가 무효화됩니다. **Prevention:** 콘텐츠를 해싱하기 전에 `.trimIndent()`를 적용하여 원본 문자열을 정규화하고, HTML 템플릿에 주입할 때 ``와 같이 공백 없이 주입하여 해시가 완벽하게 일치하도록 해야 합니다. + +## 2024-07-15 - [CRITICAL] 숨김 파일 유니코드 우회 취약점 방지 +**Vulnerability:** 파일 이름 검증 시 유니코드 유사 문자를 통한 숨김 파일 우회 (Information Exposure) +**Learning:** `startsWith(".")` 방식만으로는 유니코드의 다른 형태 점 문자(예: `\u3002`, `\uFF0E`, `\uFF61`)로 조작된 악의적인 파일이 필터링을 우회하여 노출될 수 있음을 배웠습니다. +**Prevention:** 파일 필터링 로직에서 유니코드 등가 문자를 함께 처리하는 일관된 유틸리티 함수(`isHiddenFile`)를 사용하여 우회를 방지해야 합니다. diff --git a/src/main/kotlin/html4tree/main.kt b/src/main/kotlin/html4tree/main.kt index e93fbea7..76ba8c82 100644 --- a/src/main/kotlin/html4tree/main.kt +++ b/src/main/kotlin/html4tree/main.kt @@ -108,6 +108,12 @@ internal fun read_file_identity(file: File): FileIdentity { } } +fun String.isHiddenFile(): Boolean { + if (this.isEmpty()) return false + val firstChar = this[0] + return firstChar == '.' || firstChar == '\u3002' || firstChar == '\uFF0E' || firstChar == '\uFF61' +} + fun go(topDir: String, maxLevel: Int) { require(topDir.isNotBlank()) require(!topDir.contains("..")) { "Path traversal sequences are not allowed." } @@ -165,7 +171,7 @@ internal fun crawl_directories( dirFiles?.forEach { // ⚡ Bolt Performance Optimization: Short-circuit OS stat calls (isDirectory/isSymbolicLink) // by checking cheap in-memory string exclusion rules first - if(!it.name.startsWith(".") && it.name !in exclude && isDirectory(it) && !isSymbolicLink(it)) { + if(!it.name.isHiddenFile() && it.name !in exclude && isDirectory(it) && !isSymbolicLink(it)) { val childEntry = LinkedListEntry(it, currentLevel+1, readIdentity(it).key) ll.push(childEntry) } @@ -292,7 +298,7 @@ fun process_ignore_file(curr_dir: File, dirFilesNames: Array? = null): S // 보안 향상: .env, .git 등 민감한 정보가 포함될 수 있는 숨김 파일(.으로 시작하는 모든 항목)을 기본적으로 노출하지 않도록 제외 (정보 노출 방지) (dirFilesNames ?: curr_dir.list())?.forEach { - if (it.startsWith(".")) { + if (it.isHiddenFile()) { files_to_exclude.add(it) } } @@ -346,7 +352,7 @@ fun process_dir(curr_dir: File, excludeSet: Set? = null, dirFiles: Array val fileName = it.getName() // ⚡ Bolt Performance Optimization: Short-circuit string match before expensive OS filesystem calls // 🛡️ Sentinel: Ignore hidden files/directories to prevent sensitive data exposure - if (!fileName.startsWith(".") && fileName !in exclude) { + if (!fileName.isHiddenFile() && fileName !in exclude) { var isLinkedDirectory = false var isSymbolicLink = false try { diff --git a/src/test/kotlin/html4tree/MainTest.kt b/src/test/kotlin/html4tree/MainTest.kt index 83739c9c..c6bae2d3 100644 --- a/src/test/kotlin/html4tree/MainTest.kt +++ b/src/test/kotlin/html4tree/MainTest.kt @@ -706,4 +706,14 @@ class MainTest { assertFalse(processed, "fileKey mismatch should skip directory processing") assertFalse(listed, "fileKey mismatch should skip child listing") } + + @Test + fun testIsHiddenFile() { + assertTrue(".env".isHiddenFile()) + assertTrue("\u3002env".isHiddenFile()) + assertTrue("\uFF0Eenv".isHiddenFile()) + assertTrue("\uFF61env".isHiddenFile()) + assertFalse("env".isHiddenFile()) + assertFalse("".isHiddenFile()) + } }