Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions .jules/sentinel.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,7 @@
**Vulnerability:** The `.html4ignore` parser still allowed excessively large files and root-directory crawls could generate unbounded filesystem output.
**Learning:** Local CLI configuration inputs and traversal roots need explicit resource ceilings, not only syntactic validation.
**Prevention:** Limit `.html4ignore` file size, parsed line count, compiled pattern count, and regex length; reject filesystem root traversal using `File.parentFile != null`.
## 2024-07-11 - [.html4ignore 파일의 ReDoS 취약점 μˆ˜μ • (Glob νŒ¨ν„΄ 적용)]
**Vulnerability:** [μ‚¬μš©μžκ°€ μ œκ³΅ν•œ `.html4ignore` νŒ¨ν„΄μ„ 직접 μ •κ·œν‘œν˜„μ‹μœΌλ‘œ μ»΄νŒŒμΌν•˜μ—¬ λ°œμƒν•˜λŠ” ReDoS(μ •κ·œν‘œν˜„μ‹ μ„œλΉ„μŠ€ κ±°λΆ€) 취약점 발견.]
**Learning:** [필터링 νŒ¨ν„΄μœΌλ‘œ μ •κ·œν‘œν˜„μ‹μ„ 직접 λ…ΈμΆœν•˜λ©΄ μ•…μ˜μ μœΌλ‘œ μ‘°μž‘λœ κΈ΄ λ¬Έμžμ—΄μ΄λ‚˜ λ³΅μž‘ν•œ νŒ¨ν„΄μ„ 톡해 μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ˜ λ¦¬μ†ŒμŠ€λ₯Ό κ³ κ°ˆμ‹œν‚¬ 수 있음.]
**Prevention:** [μ‚¬μš©μž μž…λ ₯ νŒ¨ν„΄μ€ μ •κ·œν‘œν˜„μ‹μœΌλ‘œ λ³€ν™˜ν•˜κΈ° μ „ `java.nio.file.FileSystems.getDefault().getPathMatcher("glob:$pattern")`와 같은 μ•ˆμ „ν•œ Glob λ§€μΉ­ 방식을 μ‚¬μš©ν•΄μ•Ό 함.]
11 changes: 6 additions & 5 deletions src/main/kotlin/html4tree/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ fun process_ignore_file(curr_dir: File): Set<String> {
// λ³΄μ•ˆ ν–₯상: .html4ignore 파일이 일반 νŒŒμΌμΈμ§€ ν™•μΈν•˜κ³ , 심볼릭 링크인 경우 λ¬΄μ‹œν•˜μ—¬ DoS 및 경둜 μ‘°μž‘μ„ λ°©μ§€ν•©λ‹ˆλ‹€.
// λ³΄μ•ˆ ν–₯상: 파일 크기(1MB μ œν•œ) 및 쀄 수(1000쀄), μ •κ·œμ‹ 길이(100자)λ₯Ό μ œν•œν•˜μ—¬ ReDoS 및 λ©”λͺ¨λ¦¬ 고갈(OOM) λ°©μ§€
if(ignore_file.isFile && !Files.isSymbolicLink(ignore_file.toPath()) && ignore_file.length() <= 1048576){
val ignored_regexes = mutableListOf<Regex>()
val ignored_matchers = mutableListOf<java.nio.file.PathMatcher>()

ignore_file.useLines { lines ->
for ((lineIndex, it) in lines.withIndex()) {
Expand All @@ -131,17 +131,18 @@ fun process_ignore_file(curr_dir: File): Set<String> {
val pattern = it.trim()
if (pattern.isNotEmpty() && pattern.length <= 100) {
try {
ignored_regexes.add(("^"+pattern+"$").toRegex())
} catch (_: IllegalArgumentException) {
ignored_matchers.add(java.nio.file.FileSystems.getDefault().getPathMatcher("glob:$pattern"))
} catch (_: java.util.regex.PatternSyntaxException) {
}
}
}
}

curr_dir.list()?.sorted()?.forEach {
val current = it
ignored_regexes.forEach { regex ->
if(regex.matches(current)){
val pathCurrent = java.nio.file.Paths.get(current)
ignored_matchers.forEach { matcher ->
if(matcher.matches(pathCurrent)){
files_to_exclude.add(current)
}
}
Expand Down
18 changes: 9 additions & 9 deletions src/test/kotlin/html4tree/MainTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class MainTest {
@Test
fun testProcessIgnoreFile() {
val ignoreFile = File(tempDir, ".html4ignore")
ignoreFile.writeText(".*\\.txt\n.*\\.log")
ignoreFile.writeText("*.txt\n*.log")

File(tempDir, "test.txt").createNewFile()
File(tempDir, "test.log").createNewFile()
Expand All @@ -123,7 +123,7 @@ class MainTest {
@Test
fun testProcessIgnoreFileInvalidRegex() {
val ignoreFile = File(tempDir, ".html4ignore")
ignoreFile.writeText("[\n.*\\.log")
ignoreFile.writeText("[\n*.log")

File(tempDir, "test.log").createNewFile()
File(tempDir, "test.txt").createNewFile()
Expand All @@ -140,7 +140,7 @@ class MainTest {
subdir.mkdir()
File(tempDir, "file1.txt").createNewFile()
File(tempDir, "test.ignore").createNewFile()
File(tempDir, ".html4ignore").writeText(".*\\.ignore")
File(tempDir, ".html4ignore").writeText("*.ignore")

process_dir(tempDir)

Expand Down Expand Up @@ -302,7 +302,7 @@ class MainTest {
@Test
fun testProcessIgnoreFileWithIndexHtml() {
val ignoreFile = File(tempDir, ".html4ignore")
ignoreFile.writeText("index\\.html")
ignoreFile.writeText("index.html")
File(tempDir, "index.html").writeText("existing")
val excluded = process_ignore_file(tempDir)
assertTrue(excluded.contains("index.html"))
Expand Down Expand Up @@ -340,7 +340,7 @@ class MainTest {
@Test
fun testProcessIgnoreFileEmptyLine() {
val ignoreFile = File(tempDir, ".html4ignore")
ignoreFile.writeText("\n.*\\.txt\n\n.*\\.log\n")
ignoreFile.writeText("\n*.txt\n\n*.log\n")

File(tempDir, "test.txt").createNewFile()

Expand Down Expand Up @@ -387,7 +387,7 @@ class MainTest {
@Test
fun testIgnoreFileIsSymlink() {
val targetFile = File(tempDir, "target.ignore")
targetFile.writeText(".*\\.txt")
targetFile.writeText("*.txt")
val ignoreFile = File(tempDir, ".html4ignore")
try {
Files.createSymbolicLink(ignoreFile.toPath(), targetFile.toPath())
Expand Down Expand Up @@ -421,8 +421,8 @@ class MainTest {
@Test
fun testProcessIgnoreFileLongRegex() {
val ignoreFile = File(tempDir, ".html4ignore")
val longRegex = ".*".repeat(55) // Length 110
ignoreFile.writeText("$longRegex\n.*\\.log")
val longRegex = "*".repeat(110) // Length 110
ignoreFile.writeText("$longRegex\n*.log")

File(tempDir, "test.log").createNewFile()
File(tempDir, "test.txt").createNewFile()
Expand All @@ -440,7 +440,7 @@ class MainTest {
val ignoreFile = File(tempDir, ".html4ignore")
val content = StringBuilder()
for (i in 1..1005) {
content.append(".*\\.txt$i\n")
content.append("*.txt$i\n")
}
ignoreFile.writeText(content.toString())

Expand Down
Loading