Compare commits

..

4 Commits

Author SHA1 Message Date
b4fac5ab8c Set X-Private header for private derivations 2025-10-07 11:29:01 +02:00
6c6b5d3377 Add debug output 2025-10-07 11:06:02 +02:00
70cd4f1ec5 Do not serve derivations with /nix-support/private 2025-10-07 11:06:02 +02:00
a098861c8d Sample package 2025-10-07 11:06:02 +02:00
2 changed files with 3 additions and 37 deletions

View File

@@ -84,8 +84,8 @@
packages = {
inherit nix-serve-ng lix-serve-ng;
private = pkgs.runCommand "private" { } ''
mkdir $out
touch $out/.private
mkdir -p $out/nix-support
touch $out/nix-support/private
'';
default = nix-serve-ng;
};

View File

@@ -12,7 +12,6 @@ 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(..))
@@ -58,20 +57,6 @@ validHashPartBytes =
<> [ 0x76 .. 0x7A ] -- vwxyz
)
type HostAddressTuple = (Word8, Word8, Word8, Word8)
isAllowed :: Socket.HostAddress -> Bool
isAllowed host = any (uncurry (ipMatches $ 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))
]
ipMatches ip a b = ip >= a && ip <= b
validHashPart :: ByteString -> Bool
validHashPart hash = ByteString.all (`ByteSet.member` validHashPartBytes) hash
@@ -271,27 +256,8 @@ makeApplication ApplicationOptions{..} request respond = do
let privateFilePath = ByteString.Char8.unpack storePath ++ "/nix-support/private"
isPrivate <- liftIO $ Directory.doesPathExist privateFilePath
let isLocalNet = case Wai.remoteHost request of
SockAddrInet _ host -> isAllowed host
_ -> False
traceM $ show (Wai.remoteHost request, isLocalNet)
traceM $ show (privateFilePath, isPrivate)
Monad.when (isPrivate && not isLocalNet) 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
@@ -303,7 +269,7 @@ makeApplication ApplicationOptions{..} request respond = do
() <- write builder
flush
let headers = [ ("Content-Type", "text/plain") ]
let headers = [ ("Content-Type", "text/plain") ] <> [("X-Private", "true") | isPrivate]
let response =
Wai.responseStream Types.status200 headers streamingBody