Skip to content
Closed
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
10 changes: 10 additions & 0 deletions .jules/sentinel.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,13 @@
**Vulnerability:** CSP ํ•ด์‹œ ๋ถˆ์ผ์น˜๋กœ ์ธํ•œ ์ธ๋ผ์ธ ์Šคํƒ€์ผ ์ฐจ๋‹จ
**Learning:** ๋ธŒ๋ผ์šฐ์ €๋Š” ์ธ๋ผ์ธ ์Šคํฌ๋ฆฝํŠธ์™€ ์Šคํƒ€์ผ์˜ ๋‚ด๋ถ€ ํ…์ŠคํŠธ(๊ณต๋ฐฑ๊ณผ ์ค„๋ฐ”๊ฟˆ ํฌํ•จ)๋ฅผ ์ •ํ™•ํ•˜๊ฒŒ ํ•ด์‹ฑํ•˜์—ฌ Content-Security-Policy(CSP) ํ•ด์‹œ์™€ ๋น„๊ตํ•ฉ๋‹ˆ๋‹ค. Kotlin์˜ ๋ฉ€ํ‹ฐ๋ผ์ธ ๋ฌธ์ž์—ด(`"""`)์„ ์‚ฌ์šฉํ•˜์—ฌ ํ…œํ”Œ๋ฆฟ์— ์ฝ˜ํ…์ธ ๋ฅผ ์ฃผ์ž…ํ•  ๋•Œ ์•”๋ฌต์ ์ธ ์—ฌ๋ฐฑ์ด๋‚˜ ์ค„๋ฐ”๊ฟˆ์ด ์ถ”๊ฐ€๋˜๋ฉด ์ตœ์ข… HTML ๋ฌธ์ž์—ด์ด ๋ณ€๊ฒฝ๋˜์–ด CSP ํ•ด์‹œ๊ฐ€ ๋ฌดํšจํ™”๋ฉ๋‹ˆ๋‹ค.
**Prevention:** ์ฝ˜ํ…์ธ ๋ฅผ ํ•ด์‹ฑํ•˜๊ธฐ ์ „์— `.trimIndent()`๋ฅผ ์ ์šฉํ•˜์—ฌ ์›๋ณธ ๋ฌธ์ž์—ด์„ ์ •๊ทœํ™”ํ•˜๊ณ , HTML ํ…œํ”Œ๋ฆฟ์— ์ฃผ์ž…ํ•  ๋•Œ `<style>${exactContent}</style>`์™€ ๊ฐ™์ด ๊ณต๋ฐฑ ์—†์ด ์ฃผ์ž…ํ•˜์—ฌ ํ•ด์‹œ๊ฐ€ ์™„๋ฒฝํ•˜๊ฒŒ ์ผ์น˜ํ•˜๋„๋ก ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

## 2024-08-10 - [html4tree] ์›์ž์  ํŒŒ์ผ ์“ฐ๊ธฐ(Atomic File Move)๋ฅผ ํ†ตํ•œ TOCTOU ๋ฐฉ์ง€
**Vulnerability:** ํŒŒ์ผ ์“ฐ๊ธฐ ์ค‘๊ฐ„์— ์ถฉ๋Œ์ด ๋ฐœ์ƒํ•˜๊ฑฐ๋‚˜ ํŒŒ์ผ ๊ต์ฒด ์ค‘ ๋ถˆ์™„์ „ํ•œ ์ƒํƒœ๊ฐ€ ๋…ธ์ถœ๋˜๋Š” ์ทจ์•ฝ์  ๋ฐ TOCTOU(Time-of-Check to Time-of-Use) ์ทจ์•ฝ์ 
**Learning:** ํŒŒ์ผ์„ ์“ธ ๋•Œ ์ž„์‹œ ํŒŒ์ผ์— ๋จผ์ € ์“ฐ๊ณ  `Files.move`๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ `StandardCopyOption.ATOMIC_MOVE`๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด, ํŒŒ์ผ ๊ต์ฒด ์ค‘๊ฐ„ ์ƒํƒœ๊ฐ€ ๋…ธ์ถœ๋˜๊ฑฐ๋‚˜ ๋‹ค๋ฅธ ํ”„๋กœ์„ธ์Šค๊ฐ€ ํ•ด๋‹น ํŒŒ์ผ์„ ์ฐธ์กฐํ•  ๋•Œ ๋ถˆ์™„์ „ํ•œ ์ƒํƒœ์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ™˜๊ฒฝ์— ๋”ฐ๋ผ(์˜ˆ: Docker์˜ overlayfs ๋“ฑ) Atomic Move๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, `AtomicMoveNotSupportedException` ๋ฐœ์ƒ ์‹œ ๊ธฐ๋ณธ ๊ต์ฒด๋กœ ํด๋ฐฑ(fallback)ํ•˜๋Š” ๋ฐฉ์–ด๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.
**Prevention:** ํŒŒ์ผ ์—…๋ฐ์ดํŠธ ์‹œ ์ž„์‹œ ํŒŒ์ผ์— ๋‚ด์šฉ์„ ์™„์ „ํžˆ ์“ด ํ›„, `StandardCopyOption.ATOMIC_MOVE` ์˜ต์…˜๊ณผ ํ•จ๊ป˜ `Files.move`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋Œ€์ƒ ๊ฒฝ๋กœ๋กœ ์›์ž์ ์œผ๋กœ ์ด๋™์‹œํ‚ค๊ณ  ์ง€์›ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๋ฅผ ๋Œ€๋น„ํ•œ `try-catch` ํด๋ฐฑ์„ ๊ตฌํ˜„ํ•˜์‹ญ์‹œ์˜ค.

## 2026-08-10 - [html4tree] ํŒŒ์ผ ๋ฆฌ์ŠคํŒ… ์ค‘ ๋ฐœ์ƒํ•˜๋Š” ์‹ฌ๋ณผ๋ฆญ ๋งํฌ ์Šค์™‘ ์ทจ์•ฝ์ (TOCTOU) ์ถ”๊ฐ€ ๋Œ€์‘
**Vulnerability:** ๋””๋ ‰ํ† ๋ฆฌ ์†์„ฑ ๊ฒ€์‚ฌ ์งํ›„๋ถ€ํ„ฐ ํŒŒ์ผ ๋ฆฌ์ŠคํŒ… ์‹œ์  ์‚ฌ์ด์— ์•…์˜์ ์ธ ์‹ฌ๋ณผ๋ฆญ ๋งํฌ๋กœ ๊ต์ฒด๋  ์ˆ˜ ์žˆ๋Š” TOCTOU ์ทจ์•ฝ์ .
**Learning:** ํฌ๋กค๋Ÿฌ๊ฐ€ ํŒŒ์ผ์„ ํ์—์„œ ๊บผ๋‚ด์–ด ๊ฒ€์‚ฌํ•˜๋Š” ๊ณผ์ •๊ณผ ์‹ค์ œ ๋ฆฌ์ŠคํŒ…ํ•˜๋Š” ๊ณผ์ • ์‚ฌ์ด์— ์‹œ๊ฐ„์ฐจ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด, ๊ณต๊ฒฉ์ž๊ฐ€ ์‹ฌ๋ณผ๋ฆญ ๋งํฌ๋ฅผ ํ†ตํ•ด ์˜๋„๋˜์ง€ ์•Š์€ ๋””๋ ‰ํ† ๋ฆฌ์˜ ํŒŒ์ผ์„ ๋…ธ์ถœ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
**Prevention:** ํ์—์„œ ๊บผ๋‚ด์–ด ๊ฒ€์‚ฌํ•œ ์งํ›„ ๋ฆฌ์ŠคํŒ…(`listFiles`)์„ ์ˆ˜ํ–‰ํ•œ ๋‹ค์Œ, ๋‹ค์‹œ ํ•œ๋ฒˆ ๋””๋ ‰ํ† ๋ฆฌ์˜ ์†์„ฑ(`BasicFileAttributes.fileKey()`)์„ ๊ฒ€์‚ฌํ•˜์—ฌ ์†์„ฑ์ด ๋ณ€๊ฒฝ๋˜์—ˆ๊ฑฐ๋‚˜ ์ ‘๊ทผ์ด ๋ถˆ๊ฐ€ํ•ด์ง„ ๊ฒฝ์šฐ๋ฅผ ๊ฐ์ง€ํ•˜๊ณ  ์ž‘์—…์„ ์ทจ์†Œํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
21 changes: 19 additions & 2 deletions src/main/kotlin/html4tree/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,14 @@ internal fun crawl_directories(

// โšก Bolt Performance Optimization: ๋””๋ ‰ํ† ๋ฆฌ ๋ชฉ๋ก์„ ์บ์‹ฑํ•˜์—ฌ ์ค‘๋ณต๋œ I/O ์‹œ์Šคํ…œ ํ˜ธ์ถœ์„ ์ค„์ž„
val dirFiles = listFiles(lle.file)

// ๋ณด์•ˆ ํ–ฅ์ƒ: ๋””๋ ‰ํ† ๋ฆฌ ๋ชฉ๋ก์„ ๊ฐ€์ ธ์˜จ ํ›„, ์•…์˜์ ์ธ ์‹ฌ๋ณผ๋ฆญ ๋งํฌ๋กœ ๊ต์ฒด๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜์—ฌ TOCTOU ์ทจ์•ฝ์ ์„ ๋ฐฉ์ง€ํ•ฉ๋‹ˆ๋‹ค.
val postIdentity = readIdentity(lle.file)
if (!postIdentity.readable || currentIdentity.key != postIdentity.key) {
lle = ll.pull()
continue
}

val dirFilesNames = dirFiles?.map { it.name }?.toTypedArray()
val exclude = processIgnoreFile(lle.file, dirFilesNames)

Expand Down Expand Up @@ -327,12 +335,21 @@ fun process_ignore_file(curr_dir: File, dirFilesNames: Array<String>? = null): S
return files_to_exclude
}

fun write_index_file(curr_dir: File, content: String) {
fun write_index_file(
curr_dir: File,
content: String,
moveFile: (java.nio.file.Path, java.nio.file.Path, Array<java.nio.file.CopyOption>) -> java.nio.file.Path = { src, dest, options -> Files.move(src, dest, *options) }
) {
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)
try {
// ๋ณด์•ˆ ํ–ฅ์ƒ: TOCTOU(Time-of-Check to Time-of-Use) ๋ฐ ๋ถˆ์™„์ „ํ•œ ํŒŒ์ผ ์“ฐ๊ธฐ๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด Atomic Move๋ฅผ ์‹œ๋„ํ•ฉ๋‹ˆ๋‹ค.
moveFile(tempPath, indexPath, arrayOf<java.nio.file.CopyOption>(StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE))
} catch (e: java.nio.file.AtomicMoveNotSupportedException) {
moveFile(tempPath, indexPath, arrayOf<java.nio.file.CopyOption>(StandardCopyOption.REPLACE_EXISTING))
}
} finally {
Files.deleteIfExists(tempPath)
}
Expand Down
59 changes: 59 additions & 0 deletions src/test/kotlin/html4tree/MainTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import java.io.ByteArrayOutputStream
import java.io.File
import java.io.PrintStream
import java.nio.file.Files
import java.nio.file.StandardCopyOption
import java.nio.file.attribute.BasicFileAttributes
import java.nio.file.attribute.FileTime
import kotlin.test.assertEquals
Expand Down Expand Up @@ -376,6 +377,27 @@ class MainTest {
assertTrue(leftoverTemp.isEmpty(), "temporary index file should be cleaned up on failure")
}

@Test
fun testWriteIndexFileAtomicFallback() {
var calledWithAtomic = false
var calledWithoutAtomic = false
val mockMove: (java.nio.file.Path, java.nio.file.Path, Array<java.nio.file.CopyOption>) -> java.nio.file.Path = { src, dest, options ->
if (options.contains(StandardCopyOption.ATOMIC_MOVE)) {
calledWithAtomic = true
throw java.nio.file.AtomicMoveNotSupportedException(src.toString(), dest.toString(), "Mocked")
} else {
calledWithoutAtomic = true
Files.move(src, dest, *options)
}
}

write_index_file(tempDir, "atomic fallback test", mockMove)

assertTrue(calledWithAtomic, "Should attempt atomic move first")
assertTrue(calledWithoutAtomic, "Should fallback to non-atomic move")
assertEquals("atomic fallback test", File(tempDir, "index.html").readText())
}

@Test
fun testProcessDirReplacesIndexSymlinkWithoutTouchingTarget() {
val targetFile = File(tempDir, "target.txt")
Expand Down Expand Up @@ -717,4 +739,41 @@ class MainTest {
assertFalse(processed, "fileKey mismatch should skip directory processing")
assertFalse(listed, "fileKey mismatch should skip child listing")
}

@Test
fun testToctouSymlinkSwapRejectionPostListing() {
val subdir = File(tempDir, "toctou_test_dir_post")
subdir.mkdir()
val ll = LinkedList()
val entry = LinkedListEntry(subdir, 0)
entry.fileKey = "valid-key"
ll.push(entry)

var processed = false
var listCalled = false
var identityCallCount = 0

crawl_directories(
ll,
-1,
processDirectory = { _, _, _ -> processed = true },
processIgnoreFile = { _, _ -> emptySet() },
listFiles = {
listCalled = true
emptyArray()
},
readAttributes = { file -> createMockAttributes(isDir = true, isSymlink = false) },
readIdentity = {
identityCallCount++
if (identityCallCount == 1) {
FileIdentity("valid-key", true) // First check passes
} else {
FileIdentity("changed-key", true) // Second check fails (swapped)
}
}
)

assertTrue(listCalled, "listFiles should be called since the first check passes")
assertFalse(processed, "Directory processing should be skipped due to TOCTOU mitigation post-listing")
}
}
Loading