fix: hardening
This commit is contained in:
168 files changed
+9166
-1644
No files matched your search
@@ -21,9 +21,17 @@ let
|
||||
fetchSubscriptionPy
|
||||
routingMarkJq
|
||||
;
|
||||
inherit (constants) stateDir runtimeSubscriptionsDir;
|
||||
inherit (constants) stateDir runtimeSubscriptionsDir runtimeOutboundsDir;
|
||||
# Outbounds and groups the start script adds after the subscriptions, which a subscription
|
||||
# entry must not take the tag of.
|
||||
laterTagsJson = builtins.toJSON (ctx.effectiveOutboundTags ++ ctx.groupTags);
|
||||
subscriptionBackend = if hybridEnabled then "hybrid" else mainBackend;
|
||||
subscriptionBackendArg = "--backend ${subscriptionBackend}";
|
||||
# Each entry also through its backend's own config check: one it refuses would fail the
|
||||
# whole start, every other outbound with it.
|
||||
subscriptionBackendArg =
|
||||
"--backend ${subscriptionBackend}"
|
||||
+ lib.optionalString (subscriptionBackend != "xray") " --check-sing-box ${ctx.singBox}"
|
||||
+ lib.optionalString (subscriptionBackend != "sing-box") " --check-xray ${ctx.xray}";
|
||||
subscriptionCacheDir = "${stateDir}/subscriptions/${backend}";
|
||||
|
||||
mkSubscriptionUrlSource =
|
||||
@@ -35,16 +43,53 @@ let
|
||||
readSourceFunction = constants.readSourceFunction pkgs;
|
||||
cacheDir = subscriptionCacheDir;
|
||||
inherit runtimeSubscriptionsDir jq;
|
||||
declaredTags = lib.escapeShellArgs (map (sub: sub.tag) proxyCfg.subscriptions);
|
||||
validCacheFilter = lib.escapeShellArg (
|
||||
if hybridEnabled then
|
||||
''type == "object" and (.singBox | type == "array") and (.xray | type == "array")''
|
||||
else
|
||||
''type == "array"''
|
||||
);
|
||||
diffutils = pkgs.diffutils;
|
||||
inherit (pkgs) diffutils findutils;
|
||||
# The directory alone, not what is in it (constants.grantDirAcl): the caches are 0644, and
|
||||
# a recursive pass would trip over another start script's temporary file.
|
||||
cacheDirAccess =
|
||||
let
|
||||
secrets = ctx.userControlAllows "secrets";
|
||||
extraGroups = ctx.userControlExtraGroupsFor "secrets";
|
||||
in
|
||||
lib.optionalString secrets ''
|
||||
${pkgs.coreutils}/bin/chgrp ${lib.escapeShellArg ctx.userControlCfg.group} "$SUB_CACHE_DIR"
|
||||
''
|
||||
+ ''
|
||||
chmod ${if secrets then "0750" else "0700"} "$SUB_CACHE_DIR"
|
||||
''
|
||||
+ constants.ifPrivileged (
|
||||
if extraGroups == [ ] then
|
||||
''${pkgs.acl}/bin/setfacl -b -- "$SUB_CACHE_DIR"''
|
||||
else
|
||||
''${pkgs.acl}/bin/setfacl --set ${
|
||||
lib.concatStringsSep "," (
|
||||
[
|
||||
"u::rwx"
|
||||
"g::${if secrets then "r-x" else "---"}"
|
||||
"o::---"
|
||||
]
|
||||
++ map (g: "g:${g}:r-x") extraGroups
|
||||
)
|
||||
} -- "$SUB_CACHE_DIR"''
|
||||
);
|
||||
pythonPath = parserScriptsPythonPath;
|
||||
inherit python3 fetchSubscriptionPy;
|
||||
backendArg = subscriptionBackendArg;
|
||||
killSwitch = lib.boolToString ctx.killSwitchDirectFallbacks;
|
||||
# Its own user, not exempt from the tunnel or the kill switch: the HTTP client and the
|
||||
# parser of what any subscription server sends do not run as root.
|
||||
tunnelFetch = constants.ifPrivileged "${pkgs.util-linux}/bin/setpriv --reuid=${ctx.subscriptionFetchUser} --regid=${ctx.subscriptionFetchUser} --clear-groups --inh-caps=-all --ambient-caps=-all --bounding-set=-all --no-new-privs --";
|
||||
# Past the kill switch as the service user, resolving names itself (constants.ownLookups).
|
||||
directFetch = lib.optionalString ctx.killSwitchDirectFallbacks "${pkgs.util-linux}/bin/unshare --mount ${constants.ownLookups pkgs} ${
|
||||
constants.runAsServiceUser pkgs [ ]
|
||||
}";
|
||||
};
|
||||
|
||||
# Merging a cache into OUTBOUNDS_JSON needs the routing mark, which differs per
|
||||
@@ -62,25 +107,54 @@ let
|
||||
mergeBlock =
|
||||
if hybridEnabled then
|
||||
''
|
||||
SUB_SING_BOX_JSON=$(${jq} -c '.singBox' "$cache")
|
||||
SUB_SING_BOX_JSON=$(_proxy_suite_free_tags "$tag" < <(${jq} -c '.singBox' "$cache"))
|
||||
${markBlock}OUTBOUNDS_JSON=$(${jq} --slurpfile sub <(printf '%s' "$SUB_SING_BOX_JSON") '. + $sub[0]' <<< "$OUTBOUNDS_JSON")
|
||||
_proxy_suite_add_xray_sidecar_obs "$(${jq} -c '.xray' "$cache")"
|
||||
_proxy_suite_add_xray_sidecar_obs "$(_proxy_suite_free_tags "$tag" < <(${jq} -c '.xray' "$cache"))"
|
||||
''
|
||||
else
|
||||
''
|
||||
SUB_JSON=$(cat "$cache")
|
||||
SUB_JSON=$(_proxy_suite_free_tags "$tag" < "$cache")
|
||||
${markBlock}OUTBOUNDS_JSON=$(${jq} --slurpfile sub <(printf '%s' "$SUB_JSON") '. + $sub[0]' <<< "$OUTBOUNDS_JSON")
|
||||
'';
|
||||
in
|
||||
''
|
||||
# $1 tag, $2 file holding the subscription URL. Fetches on a cold cache and
|
||||
# merges whatever is cached; a subscription that cannot be fetched is a
|
||||
# stdin: a subscription's outbounds; prints those whose tag is free. A taken one (by any
|
||||
# other outbound, a group or the suite's own) would fail the backend's start.
|
||||
_proxy_suite_free_tags() {
|
||||
local entries taken f name
|
||||
entries=$(cat)
|
||||
taken=$(
|
||||
{
|
||||
${jq} -r 'map(.tag) | .[]' <<< "$OUTBOUNDS_JSON"
|
||||
if [ -d ${lib.escapeShellArg runtimeOutboundsDir} ]; then
|
||||
for f in ${lib.escapeShellArg runtimeOutboundsDir}/*.url ${lib.escapeShellArg runtimeOutboundsDir}/*.json \
|
||||
${lib.escapeShellArg runtimeOutboundsDir}/*.awg ${lib.escapeShellArg runtimeOutboundsDir}/*.group; do
|
||||
[ -e "$f" ] || continue
|
||||
name="''${f##*/}"
|
||||
printf '%s\n' "''${name%.*}"
|
||||
done
|
||||
fi
|
||||
} | ${jq} -R . | ${jq} -cs --argjson fixed ${lib.escapeShellArg laterTagsJson} '. + $fixed + ["proxy", "direct", "block"] | unique'
|
||||
)
|
||||
${jq} -r --arg sub "$1" --slurpfile taken <(printf '%s' "$taken") '
|
||||
$taken[0] as $taken
|
||||
| .[] | .tag | select(. as $t | ($taken | index([$t])) != null or startswith("proxy-suite-"))
|
||||
| "proxy-suite: warning: subscription \($sub) entry \(.) left out: the tag is taken"
|
||||
' <<< "$entries" >&2
|
||||
${jq} -c --slurpfile taken <(printf '%s' "$taken") '
|
||||
$taken[0] as $taken
|
||||
| map(select(.tag as $t | ($taken | index([$t])) == null and ($t | startswith("proxy-suite-") | not)))
|
||||
' <<< "$entries"
|
||||
}
|
||||
|
||||
# $1 tag, $2 file holding the subscription URL, then the fetcher's flags. Fetches on a
|
||||
# cold cache and merges whatever is cached; a subscription that cannot be fetched is a
|
||||
# warning, never a failed start.
|
||||
_proxy_suite_load_subscription() {
|
||||
local tag="$1" src="$2" cache="$SUB_CACHE_DIR/$1.json" before after
|
||||
_proxy_suite_drop_invalid_subscription_cache "$cache" "$tag"
|
||||
if [ ! -f "$cache" ]; then
|
||||
_proxy_suite_fetch_subscription "$tag" "$src" \
|
||||
_proxy_suite_fetch_subscription "$tag" "$src" "''${@:3}" \
|
||||
|| echo "proxy-suite: warning: could not fetch subscription '$tag'" >&2
|
||||
fi
|
||||
[ -f "$cache" ] || return 0
|
||||
@@ -92,21 +166,31 @@ let
|
||||
}
|
||||
'';
|
||||
|
||||
# A subscription's fetcher flags. Runtime ones take proxy.runtimeSubscriptions', https only
|
||||
# unless allowHttp: over http anyone on the way could add entries.
|
||||
mkSubscriptionFetchArgs =
|
||||
sub:
|
||||
lib.optionalString sub.allowPrivateServers " --allow-private-servers"
|
||||
+ lib.optionalString sub.allowInsecure " --allow-insecure";
|
||||
runtimeCfg = proxyCfg.runtimeSubscriptions;
|
||||
runtimeFetchArgs =
|
||||
mkSubscriptionFetchArgs runtimeCfg + lib.optionalString (!runtimeCfg.allowHttp) " --https-only";
|
||||
|
||||
mkSubscriptionBlock = sub: _routingMark: ''
|
||||
# subscription: ${sub.tag}
|
||||
_proxy_suite_load_subscription ${lib.escapeShellArg sub.tag} ${lib.escapeShellArg (mkSubscriptionUrlSource sub)}
|
||||
_proxy_suite_load_subscription ${lib.escapeShellArg sub.tag} ${lib.escapeShellArg (mkSubscriptionUrlSource sub)}${mkSubscriptionFetchArgs sub}
|
||||
'';
|
||||
|
||||
runtimeSubscriptionsBlock = ''
|
||||
# subscriptions added at runtime
|
||||
while IFS=$'\t' read -r RUNTIME_SUB_TAG RUNTIME_SUB_SRC; do
|
||||
[ -n "$RUNTIME_SUB_TAG" ] || continue
|
||||
_proxy_suite_load_subscription "$RUNTIME_SUB_TAG" "$RUNTIME_SUB_SRC"
|
||||
_proxy_suite_load_subscription "$RUNTIME_SUB_TAG" "$RUNTIME_SUB_SRC"${runtimeFetchArgs}
|
||||
done < <(_proxy_suite_runtime_subscriptions)
|
||||
'';
|
||||
|
||||
mkSubscriptionFetchBlock = sub: ''
|
||||
if _proxy_suite_fetch_subscription ${lib.escapeShellArg sub.tag} ${lib.escapeShellArg (mkSubscriptionUrlSource sub)}; then
|
||||
if _proxy_suite_fetch_subscription ${lib.escapeShellArg sub.tag} ${lib.escapeShellArg (mkSubscriptionUrlSource sub)}${mkSubscriptionFetchArgs sub}; then
|
||||
echo "Updated subscription: ${sub.tag}"
|
||||
else
|
||||
FAILED=1
|
||||
@@ -116,7 +200,7 @@ let
|
||||
runtimeSubscriptionsFetchBlock = ''
|
||||
while IFS=$'\t' read -r RUNTIME_SUB_TAG RUNTIME_SUB_SRC; do
|
||||
[ -n "$RUNTIME_SUB_TAG" ] || continue
|
||||
if _proxy_suite_fetch_subscription "$RUNTIME_SUB_TAG" "$RUNTIME_SUB_SRC"; then
|
||||
if _proxy_suite_fetch_subscription "$RUNTIME_SUB_TAG" "$RUNTIME_SUB_SRC"${runtimeFetchArgs}; then
|
||||
echo "Updated subscription: $RUNTIME_SUB_TAG"
|
||||
else
|
||||
FAILED=1
|
||||
|
||||
Reference in new issue
Block a user