Skip to content
Open
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-06-29 - [html4tree] App Crashing on Unreadable Files/Unwritable Directories
**Vulnerability:** DoS (Denial of Service) via Permissions
**Learning:** Application blindly reading `.html4ignore` or attempting to create `index.html` without verifying `canRead()` or catching `IOException`/`AccessDeniedException` allows a user/process with sufficient privileges to create directories/files with restricted permissions, crashing the entire tree traversal crawler.
**Prevention:** Always verify `canRead()` on configuration files before parsing them, and wrap I/O operations (like file writes) in `try-catch` blocks for `IOException` to ensure the crawler skips restricted areas gracefully rather than terminating.
17 changes: 11 additions & 6 deletions src/main/kotlin/html4tree/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,9 @@ fun process_ignore_file(curr_dir: File): Set<String> {
val files_to_exclude = mutableSetOf<String>()

// λ³΄μ•ˆ ν–₯상: .html4ignore 파일이 일반 νŒŒμΌμΈμ§€ ν™•μΈν•˜κ³ , 심볼릭 링크인 경우 λ¬΄μ‹œν•˜μ—¬ DoS 및 경둜 μ‘°μž‘μ„ λ°©μ§€ν•©λ‹ˆλ‹€.
// λ³΄μ•ˆ ν–₯상: κΆŒν•œ 기반 μ„œλΉ„μŠ€ κ±°λΆ€(DoS) λ°©μ§€λ₯Ό μœ„ν•΄ 읽기 κΆŒν•œμ„ ν™•μΈν•©λ‹ˆλ‹€.
// λ³΄μ•ˆ ν–₯상: 파일 크기(1MB μ œν•œ) 및 쀄 수(1000쀄), μ •κ·œμ‹ 길이(100자)λ₯Ό μ œν•œν•˜μ—¬ ReDoS 및 λ©”λͺ¨λ¦¬ 고갈(OOM) λ°©μ§€
if(ignore_file.isFile && !Files.isSymbolicLink(ignore_file.toPath()) && ignore_file.length() <= 1048576){
if(ignore_file.isFile && !Files.isSymbolicLink(ignore_file.toPath()) && ignore_file.canRead() && ignore_file.length() <= 1048576){
val ignored_regexes = mutableListOf<Regex>()

ignore_file.useLines { lines ->
Expand Down Expand Up @@ -160,12 +161,16 @@ fun process_ignore_file(curr_dir: File): Set<String> {

fun write_index_file(curr_dir: File, content: String) {
val indexPath = curr_dir.toPath().resolve("index.html")
val tempPath = Files.createTempFile(curr_dir.toPath(), ".index-", ".html")
try {
Files.write(tempPath, content.toByteArray(Charsets.UTF_8))
Files.move(tempPath, indexPath, StandardCopyOption.REPLACE_EXISTING)
} finally {
Files.deleteIfExists(tempPath)
val tempPath = Files.createTempFile(curr_dir.toPath(), ".index-", ".html")
try {
Files.write(tempPath, content.toByteArray(Charsets.UTF_8))
Files.move(tempPath, indexPath, StandardCopyOption.REPLACE_EXISTING)
} finally {
Files.deleteIfExists(tempPath)
}
} catch (e: java.io.IOException) {
// λ³΄μ•ˆ ν–₯상: μ“°κΈ° κΆŒν•œμ΄ μ—†λŠ” λ””λ ‰ν† λ¦¬μ—μ„œ ν¬λž˜μ‹œ(κ°€μš©μ„± μ €ν•˜)κ°€ λ°œμƒν•˜μ§€ μ•Šλ„λ‘ μ˜ˆμ™Έλ₯Ό μž‘μ•„μ„œ λ¬΄μ‹œν•©λ‹ˆλ‹€.
}
}

Expand Down
41 changes: 38 additions & 3 deletions src/test/kotlin/html4tree/MainTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ class MainTest {
indexDir.mkdir()
File(indexDir, "occupant.txt").writeText("keep")

assertFailsWith<Exception> {
write_index_file(tempDir, "content")
}
// Since we now catch IOException and AccessDeniedException in write_index_file,
// it should no longer throw an exception.
write_index_file(tempDir, "content")

assertTrue(indexDir.isDirectory)
assertEquals("keep", File(indexDir, "occupant.txt").readText())
Expand Down Expand Up @@ -453,4 +453,39 @@ class MainTest {
// Line 1001 should be ignored due to line limit
assertFalse(excluded.contains("test.txt1001"))
}

@Test
fun testProcessIgnoreFileUnreadable() {
val ignoreFile = File(tempDir, ".html4ignore")
ignoreFile.writeText(".*\\.txt\n")
File(tempDir, "test.txt").createNewFile()

try {
Assume.assumeTrue(ignoreFile.setReadable(false, false))
val excluded = process_ignore_file(tempDir)
// unreadable ignore file is ignored, so the file is not excluded
assertFalse(excluded.contains("test.txt"))
assertTrue(excluded.contains("index.html"))
} finally {
ignoreFile.setReadable(true, false)
}
}

@Test
fun testProcessDirUnwritable() {
val unwritableDir = File(tempDir, "unwritable")
unwritableDir.mkdir()
File(unwritableDir, "test.txt").createNewFile()

try {
Assume.assumeTrue(unwritableDir.setWritable(false, false))
// This should not throw an exception
process_dir(unwritableDir)

// The index.html should not have been created
assertFalse(File(unwritableDir, "index.html").exists())
} finally {
unwritableDir.setWritable(true, false)
}
}
}
Loading