Compare commits
3 Commits
b4fac5ab8c
...
9c05664130
Author | SHA1 | Date | |
---|---|---|---|
9c05664130 | |||
92b407ca39 | |||
36d67ff5f3 |
@ -83,6 +83,10 @@
|
|||||||
rec {
|
rec {
|
||||||
packages = {
|
packages = {
|
||||||
inherit nix-serve-ng lix-serve-ng;
|
inherit nix-serve-ng lix-serve-ng;
|
||||||
|
private = pkgs.runCommand "private" { } ''
|
||||||
|
mkdir -p $out/nix-support
|
||||||
|
touch $out/nix-support/private
|
||||||
|
'';
|
||||||
default = nix-serve-ng;
|
default = nix-serve-ng;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -48,6 +48,7 @@ executable nix-serve
|
|||||||
, base32
|
, base32
|
||||||
, bytestring
|
, bytestring
|
||||||
, charset
|
, charset
|
||||||
|
, directory
|
||||||
, http-types
|
, http-types
|
||||||
, managed
|
, managed
|
||||||
, megaparsec
|
, megaparsec
|
||||||
|
42
src/Main.hs
42
src/Main.hs
@ -6,10 +6,13 @@
|
|||||||
|
|
||||||
module Main where
|
module Main where
|
||||||
|
|
||||||
|
import Debug.Trace
|
||||||
|
|
||||||
import Control.Monad.IO.Class (liftIO)
|
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(..))
|
||||||
@ -35,6 +38,7 @@ import qualified Network.Wai.Middleware.RequestLogger as RequestLogger
|
|||||||
import qualified Nix
|
import qualified Nix
|
||||||
import qualified Options
|
import qualified Options
|
||||||
import qualified Options.Applicative as Options
|
import qualified Options.Applicative as Options
|
||||||
|
import qualified System.Directory as Directory
|
||||||
import qualified System.Environment as Environment
|
import qualified System.Environment as Environment
|
||||||
|
|
||||||
data ApplicationOptions = ApplicationOptions
|
data ApplicationOptions = ApplicationOptions
|
||||||
@ -54,6 +58,20 @@ validHashPartBytes =
|
|||||||
<> [ 0x76 .. 0x7A ] -- vwxyz
|
<> [ 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 :: ByteString -> Bool
|
||||||
validHashPart hash = ByteString.all (`ByteSet.member` validHashPartBytes) hash
|
validHashPart hash = ByteString.all (`ByteSet.member` validHashPartBytes) hash
|
||||||
|
|
||||||
@ -250,6 +268,30 @@ makeApplication ApplicationOptions{..} request respond = do
|
|||||||
|
|
||||||
done response
|
done response
|
||||||
|
|
||||||
|
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
|
let streamingBody write flush = do
|
||||||
result <- Nix.dumpPath hashPart callback
|
result <- Nix.dumpPath hashPart callback
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user