Compare commits
5 Commits
main
...
a050a87129
| Author | SHA1 | Date | |
|---|---|---|---|
|
a050a87129
|
|||
|
b4a36218e5
|
|||
|
70ea76007c
|
|||
|
2ef34fe396
|
|||
|
9126d8d979
|
@@ -84,8 +84,8 @@
|
|||||||
packages = {
|
packages = {
|
||||||
inherit nix-serve-ng lix-serve-ng;
|
inherit nix-serve-ng lix-serve-ng;
|
||||||
private = pkgs.runCommand "private" { } ''
|
private = pkgs.runCommand "private" { } ''
|
||||||
mkdir -p $out/nix-support
|
mkdir $out
|
||||||
touch $out/nix-support/private
|
touch $out/.private
|
||||||
'';
|
'';
|
||||||
default = nix-serve-ng;
|
default = nix-serve-ng;
|
||||||
};
|
};
|
||||||
|
|||||||
36
src/Main.hs
36
src/Main.hs
@@ -12,6 +12,7 @@ import Control.Monad.IO.Class (liftIO)
|
|||||||
import Data.ByteString (ByteString)
|
import Data.ByteString (ByteString)
|
||||||
import Data.CharSet.ByteSet (ByteSet(..))
|
import Data.CharSet.ByteSet (ByteSet(..))
|
||||||
import Data.Function ((&))
|
import Data.Function ((&))
|
||||||
|
import Data.Word (Word8)
|
||||||
import Network.Socket (SockAddr(..))
|
import Network.Socket (SockAddr(..))
|
||||||
import Network.Wai (Application)
|
import Network.Wai (Application)
|
||||||
import Nix (NoSuchPath(..), PathInfo(..))
|
import Nix (NoSuchPath(..), PathInfo(..))
|
||||||
@@ -57,6 +58,20 @@ validHashPartBytes =
|
|||||||
<> [ 0x76 .. 0x7A ] -- vwxyz
|
<> [ 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 :: ByteString -> Bool
|
||||||
validHashPart hash = ByteString.all (`ByteSet.member` validHashPartBytes) hash
|
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"
|
let privateFilePath = ByteString.Char8.unpack storePath ++ "/nix-support/private"
|
||||||
isPrivate <- liftIO $ Directory.doesPathExist privateFilePath
|
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)
|
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
|
let streamingBody write flush = do
|
||||||
result <- Nix.dumpPath hashPart callback
|
result <- Nix.dumpPath hashPart callback
|
||||||
|
|
||||||
@@ -269,7 +303,7 @@ makeApplication ApplicationOptions{..} request respond = do
|
|||||||
() <- write builder
|
() <- write builder
|
||||||
flush
|
flush
|
||||||
|
|
||||||
let headers = [ ("Content-Type", "text/plain") ] <> [("X-Private", "true") | isPrivate]
|
let headers = [ ("Content-Type", "text/plain") ]
|
||||||
|
|
||||||
let response =
|
let response =
|
||||||
Wai.responseStream Types.status200 headers streamingBody
|
Wai.responseStream Types.status200 headers streamingBody
|
||||||
|
|||||||
Reference in New Issue
Block a user