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` ์†์„ฑ์„ ์ œ๊ฑฐํ•˜์—ฌ ๋ธŒ๋ผ์šฐ์ €์˜ ๋ฌด๊ฒฐ์„ฑ ๊ฒ€์ฆ ๊ธฐ๋Šฅ์„ ์ ๊ทน ํ™œ์šฉํ•˜์‹ญ์‹œ์˜ค.

## 2024-07-25 - [html4tree] ์›์ž์  ํŒŒ์ผ ๊ต์ฒด๋ฅผ ํ†ตํ•œ ๊ฒฝํ•ฉ ์กฐ๊ฑด(Race Condition) ๋ฐฉ์ง€
**Vulnerability:** ํŒŒ์ผ ๊ต์ฒด ์‹œ `REPLACE_EXISTING`๋งŒ ์‚ฌ์šฉํ•˜๋ฉด ๊ต์ฒด ์ˆœ๊ฐ„์— ๋ถˆ์™„์ „ํ•œ ํŒŒ์ผ ์ƒํƒœ๊ฐ€ ๋…ธ์ถœ๋˜๊ฑฐ๋‚˜ TOCTOU ์ทจ์•ฝ์ ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
**Learning:** ํŒŒ์ผ ๊ต์ฒด ์ž‘์—…์€ ์›์ž์ (Atomic)์œผ๋กœ ์ˆ˜ํ–‰๋˜์–ด์•ผ ํ•˜๋ฉฐ, ์‹œ์Šคํ…œ์—์„œ ์ง€์›ํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ์—๋งŒ ์ผ๋ฐ˜ ๋ณต์‚ฌ/์ด๋™์œผ๋กœ ํด๋ฐฑํ•ด์•ผ ์•ˆ์ „ํ•œ ์ƒํƒœ ์ „ํ™˜์„ ๋ณด์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
**Prevention:** ํŒŒ์ผ ์—…๋ฐ์ดํŠธ ์‹œ `StandardCopyOption.ATOMIC_MOVE`๋ฅผ ์šฐ์„ ์ ์œผ๋กœ ์‹œ๋„ํ•˜๊ณ , ์‹คํŒจ(`AtomicMoveNotSupportedException`)ํ•  ๊ฒฝ์šฐ์—๋งŒ `REPLACE_EXISTING`์œผ๋กœ ํด๋ฐฑํ•˜๋„๋ก ๊ตฌํ˜„ํ•˜์‹ญ์‹œ์˜ค.
Comment on lines +88 to +90
17 changes: 15 additions & 2 deletions src/main/kotlin/html4tree/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,25 @@ 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,
moveAtomic: (java.nio.file.Path, java.nio.file.Path) -> Unit = { src, dest ->
Files.move(src, dest, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE)
},
moveFallback: (java.nio.file.Path, java.nio.file.Path) -> Unit = { src, dest ->
Files.move(src, dest, StandardCopyOption.REPLACE_EXISTING)
}
) {
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 {
moveAtomic(tempPath, indexPath)
} catch (e: java.nio.file.AtomicMoveNotSupportedException) {
moveFallback(tempPath, indexPath)
}
} finally {
Files.deleteIfExists(tempPath)
}
Expand Down
54 changes: 54 additions & 0 deletions src/test/kotlin/html4tree/MainTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,60 @@ class MainTest {
assertTrue(htmlContent.contains("margin: 0 auto;"))
}

@Test
fun testWriteIndexFileAtomicMoveFallback() {
var atomicMoveAttempted = false
var fallbackMoveAttempted = false

write_index_file(
curr_dir = tempDir,
content = "content",
moveAtomic = { src, dest ->
atomicMoveAttempted = true
throw java.nio.file.AtomicMoveNotSupportedException(src.toString(), dest.toString(), "Mocked")
},
moveFallback = { src, dest ->
fallbackMoveAttempted = true
Files.move(src, dest, java.nio.file.StandardCopyOption.REPLACE_EXISTING)
}
)

assertTrue(atomicMoveAttempted)
assertTrue(fallbackMoveAttempted)
assertTrue(File(tempDir, "index.html").exists())
}

@Test
fun testWriteIndexFileDefaultLambdas() {
val testDir = File(tempDir, "testWriteIndexFileDefault")
testDir.mkdir()
val expectedContent = "default-lambda-content"
write_index_file(testDir, expectedContent)
val indexFile = File(testDir, "index.html")
assertTrue(indexFile.exists())
assertEquals(expectedContent, indexFile.readText())
}

@Test
fun testWriteIndexFileDefaultFallbackLambda() {
var atomicMoveAttempted = false
val testDir = File(tempDir, "testWriteIndexFileDefaultFallback")
testDir.mkdir()

write_index_file(
curr_dir = testDir,
content = "fallback-content",
moveAtomic = { src, dest ->
atomicMoveAttempted = true
throw java.nio.file.AtomicMoveNotSupportedException(src.toString(), dest.toString(), "Mocked")
}
)
assertTrue(atomicMoveAttempted)
val indexFile = File(testDir, "index.html")
assertTrue(indexFile.exists())
assertEquals("fallback-content", indexFile.readText())
}

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