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:** ํŒŒ์ผ ๊ต์ฒด ์‹œ ์›์ž์  ์ด๋™(ATOMIC_MOVE) ๋ˆ„๋ฝ์œผ๋กœ ์ธํ•œ ๊ฒฝ์Ÿ ์กฐ๊ฑด.
**Learning:** `Files.move`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž„์‹œ ํŒŒ์ผ์„ ์ตœ์ข… ๋ชฉ์ ์ง€(์˜ˆ: `index.html`)๋กœ ์ด๋™ํ•  ๋•Œ ์›์ž์  ์ด๋™ ์˜ต์…˜(`StandardCopyOption.ATOMIC_MOVE`)์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด, ์ด๋™ ๊ณผ์ •์—์„œ ํŒŒ์ผ์ด ๋ถˆ์™„์ „ํ•˜๊ฒŒ ๋ณต์‚ฌ๋œ ์ƒํƒœ๋กœ ๋‹ค๋ฅธ ํ”„๋กœ์„ธ์Šค๋‚˜ ์Šค๋ ˆ๋“œ์— ์˜ํ•ด ์ฝํž ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์Ÿ ์กฐ๊ฑด ์ทจ์•ฝ์ ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
**Prevention:** ํŒŒ์ผ ๊ต์ฒด ์‹œ ํ•ญ์ƒ `StandardCopyOption.ATOMIC_MOVE` ์˜ต์…˜์„ ๋จผ์ € ์‹œ๋„ํ•˜๊ณ , ํŒŒ์ผ ์‹œ์Šคํ…œ์—์„œ ์ง€์›ํ•˜์ง€ ์•Š์•„ `AtomicMoveNotSupportedException`์ด ๋ฐœ์ƒํ•  ๊ฒฝ์šฐ์—๋งŒ `StandardCopyOption.REPLACE_EXISTING`์œผ๋กœ ํด๋ฐฑ(Fallback)ํ•˜๋„๋ก ๊ตฌํ˜„ํ•˜์‹ญ์‹œ์˜ค.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ buildscript {
ext.kotlin_version = '1.3.72'

repositories {
google()
maven { url "https://plugins.gradle.org/m2/" }
mavenCentral()
}
dependencies {
Expand Down
16 changes: 14 additions & 2 deletions src/main/kotlin/html4tree/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import java.security.MessageDigest
import java.nio.file.Files
import java.nio.file.LinkOption
import java.nio.file.StandardCopyOption
import java.nio.file.AtomicMoveNotSupportedException
import java.nio.file.CopyOption
import java.nio.file.attribute.BasicFileAttributes
import java.util.Base64
import com.github.ajalt.clikt.core.CliktCommand
Expand Down Expand Up @@ -229,12 +231,22 @@ 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<out 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)
// ๋ณด์•ˆ ํ–ฅ์ƒ: ํŒŒ์ผ ๊ต์ฒด ์‹œ ์›์ž์  ์ด๋™(ATOMIC_MOVE)์„ ์‹œ๋„ํ•˜์—ฌ
// ๋ถˆ์™„์ „ํ•œ ํŒŒ์ผ์ด ์ฝํžˆ๊ฑฐ๋‚˜ ๊ต์ฒด ๊ณผ์ •์—์„œ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์Ÿ ์กฐ๊ฑด(Race Condition)์„ ๋ฐฉ์ง€ํ•ฉ๋‹ˆ๋‹ค.
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
36 changes: 36 additions & 0 deletions src/test/kotlin/html4tree/AtomicMoveTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package html4tree

import org.junit.Test
import java.io.File
import kotlin.test.assertTrue
import java.nio.file.Files
import java.nio.file.StandardCopyOption
import java.nio.file.AtomicMoveNotSupportedException
import java.nio.file.Path
import java.nio.file.CopyOption

class AtomicMoveTest {
@Test
fun testWriteIndexFileAtomicMoveFallback() {
val tempDir = Files.createTempDirectory("atomic_test").toFile()
try {
var moveCalled = 0
val moveMock: (Path, Path, Array<out CopyOption>) -> Path = { src, dest, options ->
moveCalled++
if (options.contains(StandardCopyOption.ATOMIC_MOVE)) {
throw AtomicMoveNotSupportedException(src.toString(), dest.toString(), "Mocked exception")
} else {
Files.move(src, dest, *options)
}
}

write_index_file(tempDir, "test content", moveMock)

val indexHtml = File(tempDir, "index.html")
assertTrue(indexHtml.exists())
assertTrue(moveCalled >= 2, "Fallback should be called")
Comment on lines +30 to +31
} finally {
tempDir.deleteRecursively()
}
}
}
Loading