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] ์›์ž์  ํŒŒ์ผ ์กฐ์ž‘(Atomic File Operations)์„ ํ†ตํ•œ TOCTOU ์™„ํ™”
**Vulnerability:** index.html ์ƒ์„ฑ ์‹œ ์ž„์‹œ ํŒŒ์ผ ๋ฎ์–ด์“ฐ๊ธฐ ๋„์ค‘ ๊ฒฝํ•ฉ ์กฐ๊ฑด(Race Condition)์œผ๋กœ ์ธํ•œ ๋ถˆ์™„์ „ํ•œ ์ƒํƒœ ๋…ธ์ถœ.
**Learning:** ํŒŒ์ผ ๊ต์ฒด ์‹œ ๋‹จ์ˆœ `REPLACE_EXISTING`์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ฎ์–ด์“ฐ๋Š” ์ฐฐ๋‚˜์— ํŒŒ์ผ์„ ์ฝ๋Š” ํ”„๋กœ์„ธ์Šค๊ฐ€ ๋ถˆ์™„์ „ํ•˜๊ฑฐ๋‚˜ ์†์ƒ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋Š” TOCTOU(Time-of-Check to Time-of-Use) ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŠนํžˆ ์ •์  ํŒŒ์ผ ์ƒ์„ฑ๊ธฐ์—์„œ๋Š” ์ผ๊ด€์„ฑ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค.
**Prevention:** `Files.move` ์‹œ `StandardCopyOption.ATOMIC_MOVE`๋ฅผ ์‹œ๋„ํ•˜์—ฌ ๊ต์ฒด๊ฐ€ ์›์ž์ ์œผ๋กœ ์ด๋ฃจ์–ด์ง€๋„๋ก ๋ณด์žฅํ•˜๊ณ , ํŒŒ์ผ ์‹œ์Šคํ…œ ์ง€์›์ด ์—†๋Š” ๊ฒฝ์šฐ์—๋งŒ(`AtomicMoveNotSupportedException`) ์ผ๋ฐ˜ ๊ต์ฒด๋กœ ์šฐ์•„ํ•˜๊ฒŒ ๋Œ€์ฒด(Fallback)ํ•˜์‹ญ์‹œ์˜ค.
15 changes: 13 additions & 2 deletions src/main/kotlin/html4tree/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import java.nio.file.Files
import java.nio.file.LinkOption
import java.nio.file.StandardCopyOption
import java.nio.file.attribute.BasicFileAttributes
import java.nio.file.AtomicMoveNotSupportedException
import java.nio.file.CopyOption
import java.nio.file.Path
import java.util.Base64
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.parameters.options.option
Expand Down Expand Up @@ -229,12 +232,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: (Path, Path, Array<out CopyOption>) -> 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.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE))
} catch (e: AtomicMoveNotSupportedException) {
moveFile(tempPath, indexPath, arrayOf(StandardCopyOption.REPLACE_EXISTING))
}
} finally {
Files.deleteIfExists(tempPath)
}
Expand Down
14 changes: 14 additions & 0 deletions src/test/kotlin/html4tree/MainTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -705,4 +705,18 @@ class MainTest {
assertFalse(processed, "fileKey mismatch should skip directory processing")
assertFalse(listed, "fileKey mismatch should skip child listing")
}

@Test
fun testWriteIndexFileAtomicMoveFallback() {
var fallbackCalled = false
write_index_file(tempDir, "content") { src, dest, options ->
if (options.contains(java.nio.file.StandardCopyOption.ATOMIC_MOVE)) {
throw java.nio.file.AtomicMoveNotSupportedException(src.toString(), dest.toString(), "Mocked exception")
}
fallbackCalled = true
java.nio.file.Files.move(src, dest, *options)
}
assertTrue(fallbackCalled, "Fallback to regular move should be called on AtomicMoveNotSupportedException")
assertTrue(File(tempDir, "index.html").exists())
}
}
Loading