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
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-17 - [html4tree] ์›์ž์  ํŒŒ์ผ ์ด๋™์„ ํ†ตํ•œ ๋ณด์•ˆ ๋ฐ ์•ˆ์ •์„ฑ ๊ฐ•ํ™”
**Vulnerability:** ๊ธฐ์กด `write_index_file`์€ ์ž„์‹œ ํŒŒ์ผ์„ `REPLACE_EXISTING`๋งŒ ์‚ฌ์šฉํ•˜์—ฌ ๋Œ€์ƒ ์œ„์น˜๋กœ ์ด๋™์‹œ์ผฐ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ํŒŒ์ผ ๊ต์ฒด ์ค‘ ๋ถˆ์™„์ „ํ•œ ์ƒํƒœ(๋ถ€๋ถ„ ์ฝ๊ธฐ)๊ฐ€ ๋…ธ์ถœ๋˜๊ฑฐ๋‚˜, ๊ต์ฒด ์ง์ „ ๋Œ€์ƒ ๊ฒฝ๋กœ๊ฐ€ ์•…์˜์ ์ธ ์‹ฌ๋ณผ๋ฆญ ๋งํฌ๋กœ ๋ณ€๊ฒฝ๋˜๋Š” TOCTOU(Time-of-Check to Time-of-Use) ๊ณต๊ฒฉ์— ์ทจ์•ฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
**Learning:** ํŒŒ์ผ ๊ต์ฒด ์ž‘์—…์—์„œ ์›์ž์„ฑ(Atomicity)์„ ๋ณด์žฅํ•˜๋Š” ๊ฒƒ์€ ๋ฐ์ดํ„ฐ ์†์ƒ ๋ฐฉ์ง€ ๋ฐ ๋™์‹œ์„ฑ ๊ด€๋ จ ๋ณด์•ˆ ๋ฌธ์ œ๋ฅผ ์˜ˆ๋ฐฉํ•˜๋Š” ํ•ต์‹ฌ ์š”์†Œ์ž…๋‹ˆ๋‹ค. Java/Kotlin์—์„œ ํŒŒ์ผ ์‹œ์Šคํ…œ์ด ์ง€์›ํ•˜๋Š” ๊ฒฝ์šฐ `StandardCopyOption.ATOMIC_MOVE`๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ด๋Ÿฌํ•œ ์œ„ํ—˜์„ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
**Prevention:** ์ •์  ํŒŒ์ผ์ด๋‚˜ ๊ตฌ์„ฑ ํŒŒ์ผ์„ ์ž‘์„ฑํ•  ๋•Œ๋Š” ์ž„์‹œ ํŒŒ์ผ์„ ๋จผ์ € ์ž‘์„ฑํ•œ ํ›„, `StandardCopyOption.ATOMIC_MOVE`์™€ `REPLACE_EXISTING`์„ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜์—ฌ ๋Œ€์ƒ ํŒŒ์ผ๋กœ ์ด๋™์‹œํ‚ต๋‹ˆ๋‹ค. ๋Œ€์ƒ ํŒŒ์ผ ์‹œ์Šคํ…œ์—์„œ ์›์ž์  ์ด๋™์„ ์ง€์›ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ(`AtomicMoveNotSupportedException` ๋ฐœ์ƒ)์— ๋Œ€๋น„ํ•ด `REPLACE_EXISTING`๋งŒ ์‚ฌ์šฉํ•˜๋Š” ์•ˆ์ „ํ•œ ํด๋ฐฑ(fallback) ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ๊ตฌํ˜„ํ•˜์‹ญ์‹œ์˜ค.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ repositories {

dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
testCompile 'junit:junit:4.11'
testCompile 'junit:junit:4.13.2'
testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
compile "com.github.ajalt:clikt:2.7.1"
}
Expand Down
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,
filesMove: (java.nio.file.Path, java.nio.file.Path, Array<out java.nio.file.CopyOption>) -> java.nio.file.Path = Files::move
) {
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 {
filesMove(tempPath, indexPath, arrayOf(StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING))
} catch (e: java.nio.file.AtomicMoveNotSupportedException) {
filesMove(tempPath, indexPath, arrayOf(StandardCopyOption.REPLACE_EXISTING))
}
} finally {
Files.deleteIfExists(tempPath)
}
Expand Down
27 changes: 27 additions & 0 deletions src/test/kotlin/html4tree/MainTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,33 @@ class MainTest {
assertFalse(File(gitDir, "index.html").exists())
}

@Test
fun testWriteIndexFileAtomicMoveFallback() {
val content = "atomic fallback content"

var atomicMoveAttempted = false
var fallbackExecuted = false

val mockFilesMove: (java.nio.file.Path, java.nio.file.Path, Array<out java.nio.file.CopyOption>) -> java.nio.file.Path = { source, target, options ->
if (options.contains(java.nio.file.StandardCopyOption.ATOMIC_MOVE)) {
atomicMoveAttempted = true
throw java.nio.file.AtomicMoveNotSupportedException(source.toString(), target.toString(), "Mocked ATOMIC_MOVE not supported")
} else {
fallbackExecuted = true
java.nio.file.Files.move(source, target, *options)
}
}

write_index_file(tempDir, content, mockFilesMove)

assertTrue(atomicMoveAttempted, "ATOMIC_MOVE should have been attempted")
assertTrue(fallbackExecuted, "Fallback to normal move should have been executed")

val indexFile = File(tempDir, "index.html")
assertTrue(indexFile.exists())
assertEquals(content, indexFile.readText())
}

@Test
fun testProcessDirHandlesNonDirectoryWithoutThrowing() {
val notADirectory = File(tempDir, "not-a-directory")
Expand Down
Loading