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
5 changes: 5 additions & 0 deletions .jules/sentinel.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,8 @@
**Vulnerability:** ์ •์  HTML ์ƒ์„ฑ ๋„๊ตฌ์—์„œ ๋งค๋ฒˆ ๋‹ค๋ฅธ Nonce๋ฅผ ๋™์ ์œผ๋กœ ์ƒ์„ฑํ•˜์—ฌ CSP์— ์ ์šฉํ•˜๋Š” ๊ฒƒ์€, ์บ์‹ฑ ํšจ์œจ์„ ์ €ํ•˜์‹œํ‚ฌ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์ •์  ๋ฐฐํฌ ํ™˜๊ฒฝ(์˜ˆ: GitHub Pages ๋“ฑ)์—์„œ ์˜ฌ๋ฐ”๋ฅธ ๋ณด์•ˆ ์ •์ฑ… ์ˆ˜๋ฆฝ์„ ๋ฐฉํ•ดํ•  ์ˆ˜ ์žˆ๋Š” ์•ˆํ‹ฐ ํŒจํ„ด์ž…๋‹ˆ๋‹ค.
**Learning:** ์ •์ ์œผ๋กœ ๊ณ ์ •๋œ ์ธ๋ผ์ธ ์Šคํƒ€์ผ์ด๋‚˜ ์Šคํฌ๋ฆฝํŠธ์—๋Š” ๋‚œ์ˆ˜ํ™”๋œ Nonce๋ณด๋‹ค ์ฝ˜ํ…์ธ  ์ž์ฒด์˜ ํ•ด์‹œ(SHA-256 ๋“ฑ)๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์•ˆ์ „ํ•˜๊ณ  ์ผ๊ด€๋œ ๋ฐฉ์‹์ž„์„ ๋ฐฐ์› ์Šต๋‹ˆ๋‹ค.
**Prevention:** ์ž๋™ ์ƒ์„ฑ๋˜๋Š” ์ •์  HTML์˜ ์ฝ˜ํ…์ธ  ๋ณด์•ˆ ์ •์ฑ…(CSP)์—๋Š” `style-src 'sha256-<HASH>'` ๋ฐฉ์‹์„ ์ ์šฉํ•˜๊ณ , `<style>` ํƒœ๊ทธ์—์„œ ๋ถˆํ•„์š”ํ•œ `nonce` ์†์„ฑ์„ ์ œ๊ฑฐํ•˜์—ฌ ๋ธŒ๋ผ์šฐ์ €์˜ ๋ฌด๊ฒฐ์„ฑ ๊ฒ€์ฆ ๊ธฐ๋Šฅ์„ ์ ๊ทน ํ™œ์šฉํ•˜์‹ญ์‹œ์˜ค.

## 2025-01-15 - [html4tree] ์›์ž์  ํŒŒ์ผ ์“ฐ๊ธฐ๋ฅผ ํ†ตํ•œ TOCTOU ์ทจ์•ฝ์  ์™„ํ™”
**Vulnerability:** ํŒŒ์ผ ๋ฎ์–ด์“ฐ๊ธฐ ์ค‘ ์‹œ๊ฐ„์ฐจ ๊ณต๊ฒฉ(TOCTOU) ๋ฐ ๋ฐ์ดํ„ฐ ๋ฌด๊ฒฐ์„ฑ ์†์‹ค ๊ฐ€๋Šฅ์„ฑ.
**Learning:** `Files.move`๋ฅผ `REPLACE_EXISTING`์œผ๋กœ๋งŒ ์‚ฌ์šฉํ•˜๋ฉด ํŒŒ์ผ์ด ๊ต์ฒด๋˜๋Š” ๋™์•ˆ ๋‹ค๋ฅธ ํ”„๋กœ์„ธ์Šค๋‚˜ ์Šค๋ ˆ๋“œ๊ฐ€ ๋ถˆ์™„์ „ํ•œ ํŒŒ์ผ์„ ์ฝ๊ฑฐ๋‚˜ ์˜ˆ๊ธฐ์น˜ ์•Š๊ฒŒ ๋ฎ์–ด์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์‹œ์Šคํ…œ ์ค‘๋‹จ ์‹œ ์›์ž์„ฑ์ด ๋ณด์žฅ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
**Prevention:** ํŒŒ์ผ ์“ฐ๊ธฐ ์ž‘์—… ํ›„ ๋Œ€์ƒ์„ ๋ฎ์–ด์“ธ ๋•Œ `StandardCopyOption.ATOMIC_MOVE`๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ์ด ์˜ต์…˜์ด ํŒŒ์ผ ์‹œ์Šคํ…œ์—์„œ ์ง€์›๋˜์ง€ ์•Š์„ ๊ฒฝ์šฐ์— ๋Œ€๋น„ํ•˜์—ฌ `AtomicMoveNotSupportedException`์„ ํฌ์ฐฉํ•œ ๋’ค `REPLACE_EXISTING`์œผ๋กœ ํด๋ฐฑ(Fallback)ํ•˜๋„๋ก ํ•˜์‹ญ์‹œ์˜ค.
12 changes: 10 additions & 2 deletions src/main/kotlin/html4tree/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,20 @@ 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 {
moveFile(tempPath, indexPath, arrayOf(StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING))
} catch (e: java.nio.file.AtomicMoveNotSupportedException) {
moveFile(tempPath, indexPath, arrayOf(StandardCopyOption.REPLACE_EXISTING))
}
} finally {
Files.deleteIfExists(tempPath)
}
Expand Down
20 changes: 20 additions & 0 deletions src/test/kotlin/html4tree/MainTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,26 @@ class MainTest {
assertTrue(htmlContent.contains("margin: 0 auto;"))
}

@Test
fun testWriteIndexFileAtomicMoveFallback() {
var atomicAttempted = false
var fallbackAttempted = false

write_index_file(tempDir, "atomic fallback content") { src, dest, options ->
if (options.contains(java.nio.file.StandardCopyOption.ATOMIC_MOVE)) {
atomicAttempted = true
throw java.nio.file.AtomicMoveNotSupportedException(src.toString(), dest.toString(), "Atomic move not supported in test")
} else {
fallbackAttempted = true
java.nio.file.Files.move(src, dest, *options)
}
}

assertTrue(atomicAttempted, "Should have attempted ATOMIC_MOVE")
assertTrue(fallbackAttempted, "Should have attempted fallback after ATOMIC_MOVE failed")
assertEquals("atomic fallback content", File(tempDir, "index.html").readText())
}

@Test
fun testWriteIndexFileCleansUpTempFileOnFailure() {
// Files.move cannot replace a non-empty directory, so this drives the
Expand Down
Loading