Compare commits

..

3 Commits

Author SHA1 Message Date
9c05664130
Sample package 2025-10-06 17:37:13 +02:00
92b407ca39
Add debug output 2025-10-06 17:37:07 +02:00
36d67ff5f3
Do not serve derivations with /nix-support/private 2025-10-06 17:36:52 +02:00

View File

@ -12,6 +12,7 @@ import Control.Monad.IO.Class (liftIO)
import Data.ByteString (ByteString)
import Data.CharSet.ByteSet (ByteSet(..))
import Data.Function ((&))
import Data.Word (Word8)
import Network.Socket (SockAddr(..))
import Network.Wai (Application)
import Nix (NoSuchPath(..), PathInfo(..))
@ -57,6 +58,20 @@ validHashPartBytes =
<> [ 0x76 .. 0x7A ] -- vwxyz
)
type HostAddressTuple = (Word8, Word8, Word8, Word8)
isInWhitelist :: Socket.HostAddress -> Bool
isInWhitelist host = any (uncurry (inRange $ Socket.hostAddressToTuple host)) allowedIPs
where
allowedIPs :: [(HostAddressTuple, HostAddressTuple)]
allowedIPs = [
((127,0,0,1), (127,0,0,1)),
((10,0,0,1), (10,255,255,254)),
((192,168,72,1), (192,168,79,254))
]
inRange ip a b = ip >= a && ip <= b
validHashPart :: ByteString -> Bool
validHashPart hash = ByteString.all (`ByteSet.member` validHashPartBytes) hash
@ -256,8 +271,27 @@ makeApplication ApplicationOptions{..} request respond = do
let privateFilePath = ByteString.Char8.unpack storePath ++ "/nix-support/private"
isPrivate <- liftIO $ Directory.doesPathExist privateFilePath
let isLocal = case Wai.remoteHost request of
SockAddrInet _ host -> isInWhitelist host
_ -> False
traceM $ show (Wai.remoteHost request, isLocal)
traceM $ show (privateFilePath, isPrivate)
Monad.when (isPrivate && not isLocal) do
let headers = [ ("Content-Type", "text/plain") ]
let builder = "Forbidden.\n"
let response =
Wai.responseBuilder
Types.status403
headers
builder
done response
let streamingBody write flush = do
result <- Nix.dumpPath hashPart callback
@ -269,7 +303,7 @@ makeApplication ApplicationOptions{..} request respond = do
() <- write builder
flush
let headers = [ ("Content-Type", "text/plain") ] <> [("X-Private", "true") | isPrivate]
let headers = [ ("Content-Type", "text/plain") ]
let response =
Wai.responseStream Types.status200 headers streamingBody