Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

3.7 KB
Raw Blame
#!/usr/bin/env bash
set -euo pipefail

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
WORK_DIR="$(mktemp -d)"
trap 'rm -rf "$WORK_DIR"' EXIT

ALLOW_PRIVATE_CONF="$WORK_DIR/dify_allow_private.conf"
export ALLOW_PRIVATE_CONF

write_optional_private_allowlist() {
    local env_name="$1"
    local acl_name="$2"
    local acl_type="$3"
    local raw_values="${!env_name:-}"
    local -a tokens=()

    raw_values="${raw_values//,/ }"
    raw_values="${raw_values//$'\n'/ }"
    raw_values="${raw_values//$'\r'/ }"

    if [ -z "${raw_values//[[:space:]]/}" ]; then
        return
    fi

    read -r -a tokens <<< "$raw_values"

    printf 'acl %s %s' "$acl_name" "$acl_type" >> "$ALLOW_PRIVATE_CONF"
    for value in "${tokens[@]}"; do
        [ -z "${value//[[:space:]]/}" ] && continue
        printf ' %s' "$value" >> "$ALLOW_PRIVATE_CONF"
    done
    printf '\nhttp_access allow client_localnet %s\n' "$acl_name" >> "$ALLOW_PRIVATE_CONF"
}

generate_allowlist_config() {
    {
        echo "# Generated by docker-agent-entrypoint.sh."
        echo "# Allows selected private targets before the default private-network deny rule."
    } > "$ALLOW_PRIVATE_CONF"
    write_optional_private_allowlist "SSRF_PROXY_ALLOW_PRIVATE_IPS" "dify_allowed_private_networks" "dst"
    write_optional_private_allowlist "SSRF_PROXY_ALLOW_PRIVATE_DOMAINS" "dify_allowed_private_domains" "dstdomain"
}

assert_contains() {
    local file="$1"
    local expected="$2"
    if ! grep -Fq "$expected" "$file"; then
        echo "Expected generated config to contain: $expected"
        cat "$file" >&2 || true
        exit 1
    fi
}

assert_not_contains() {
    local file="$1"
    local unexpected="$2"
    if grep -Fq "$unexpected" "$file"; then
        echo "Expected generated config NOT to contain: $unexpected"
        cat "$file" >&2 || true
        exit 1
    fi
}

# Empty env vars should produce only the header comments.
unset SSRF_PROXY_ALLOW_PRIVATE_IPS SSRF_PROXY_ALLOW_PRIVATE_DOMAINS
generate_allowlist_config
assert_not_contains "$ALLOW_PRIVATE_CONF" "dify_allowed_private_networks"
assert_not_contains "$ALLOW_PRIVATE_CONF" "dify_allowed_private_domains"

# IP and domain allowlists should match the workflow ssrf_proxy entrypoint format.
export SSRF_PROXY_ALLOW_PRIVATE_IPS="172.21.0.0/16,10.0.0.1"
export SSRF_PROXY_ALLOW_PRIVATE_DOMAINS="internal_api,.corp.local"
generate_allowlist_config
assert_contains "$ALLOW_PRIVATE_CONF" "acl dify_allowed_private_networks dst 172.21.0.0/16 10.0.0.1"
assert_contains "$ALLOW_PRIVATE_CONF" "http_access allow client_localnet dify_allowed_private_networks"
assert_contains "$ALLOW_PRIVATE_CONF" "acl dify_allowed_private_domains dstdomain internal_api .corp.local"
assert_contains "$ALLOW_PRIVATE_CONF" "http_access allow client_localnet dify_allowed_private_domains"

# Glob-like tokens and newline separators must not be expanded or split by the shell.
export SSRF_PROXY_ALLOW_PRIVATE_IPS=$'172.21.0.0/16\n10.0.0.1'
export SSRF_PROXY_ALLOW_PRIVATE_DOMAINS='*.corp.local,?internal.example'
generate_allowlist_config
assert_contains "$ALLOW_PRIVATE_CONF" "acl dify_allowed_private_networks dst 172.21.0.0/16 10.0.0.1"
assert_contains "$ALLOW_PRIVATE_CONF" "acl dify_allowed_private_domains dstdomain *.corp.local ?internal.example"

if ! grep -Fq "include /etc/squid/dify_allow_private.conf" "$ROOT_DIR/squid-agent.conf.template"; then
    echo "squid-agent.conf.template must include dify_allow_private.conf before the private-network deny rule."
    exit 1
fi

if ! grep -A1 "include /etc/squid/dify_allow_private.conf" "$ROOT_DIR/squid-agent.conf.template" | grep -Fq "http_access deny to_private_networks"; then
    echo "dify_allow_private.conf must be included before http_access deny to_private_networks."
    exit 1
fi

echo "Agent SSRF allowlist unit tests passed."