feat: add common package registries domains to allowed-domains list (#414)
feat: add common package registries domains to allowed-domains list
This commit is contained in:
@@ -2,6 +2,26 @@
|
||||
set -euo pipefail # Exit on error, undefined vars, and pipeline failures
|
||||
IFS=$'\n\t' # Stricter word splitting
|
||||
|
||||
# Read allowed domains from file
|
||||
ALLOWED_DOMAINS_FILE="/etc/codex/allowed_domains.txt"
|
||||
if [ -f "$ALLOWED_DOMAINS_FILE" ]; then
|
||||
ALLOWED_DOMAINS=()
|
||||
while IFS= read -r domain; do
|
||||
ALLOWED_DOMAINS+=("$domain")
|
||||
done < "$ALLOWED_DOMAINS_FILE"
|
||||
echo "Using domains from file: ${ALLOWED_DOMAINS[*]}"
|
||||
else
|
||||
# Fallback to default domains
|
||||
ALLOWED_DOMAINS=("api.openai.com")
|
||||
echo "Domains file not found, using default: ${ALLOWED_DOMAINS[*]}"
|
||||
fi
|
||||
|
||||
# Ensure we have at least one domain
|
||||
if [ ${#ALLOWED_DOMAINS[@]} -eq 0 ]; then
|
||||
echo "ERROR: No allowed domains specified"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Flush existing rules and delete existing ipsets
|
||||
iptables -F
|
||||
iptables -X
|
||||
@@ -24,8 +44,7 @@ iptables -A OUTPUT -o lo -j ACCEPT
|
||||
ipset create allowed-domains hash:net
|
||||
|
||||
# Resolve and add other allowed domains
|
||||
for domain in \
|
||||
"api.openai.com"; do
|
||||
for domain in "${ALLOWED_DOMAINS[@]}"; do
|
||||
echo "Resolving $domain..."
|
||||
ips=$(dig +short A "$domain")
|
||||
if [ -z "$ips" ]; then
|
||||
@@ -87,7 +106,7 @@ else
|
||||
echo "Firewall verification passed - unable to reach https://example.com as expected"
|
||||
fi
|
||||
|
||||
# Verify OpenAI API access
|
||||
# Always verify OpenAI API access is working
|
||||
if ! curl --connect-timeout 5 https://api.openai.com >/dev/null 2>&1; then
|
||||
echo "ERROR: Firewall verification failed - unable to reach https://api.openai.com"
|
||||
exit 1
|
||||
|
||||
@@ -10,6 +10,8 @@ set -e
|
||||
|
||||
# Default the work directory to WORKSPACE_ROOT_DIR if not provided.
|
||||
WORK_DIR="${WORKSPACE_ROOT_DIR:-$(pwd)}"
|
||||
# Default allowed domains - can be overridden with OPENAI_ALLOWED_DOMAINS env var
|
||||
OPENAI_ALLOWED_DOMAINS="${OPENAI_ALLOWED_DOMAINS:-api.openai.com}"
|
||||
|
||||
# Parse optional flag.
|
||||
if [ "$1" = "--work_dir" ]; then
|
||||
@@ -45,6 +47,12 @@ if [ -z "$WORK_DIR" ]; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Verify that OPENAI_ALLOWED_DOMAINS is not empty
|
||||
if [ -z "$OPENAI_ALLOWED_DOMAINS" ]; then
|
||||
echo "Error: OPENAI_ALLOWED_DOMAINS is empty."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Kill any existing container for the working directory using cleanup(), centralizing removal logic.
|
||||
cleanup
|
||||
|
||||
@@ -57,8 +65,25 @@ docker run --name "$CONTAINER_NAME" -d \
|
||||
codex \
|
||||
sleep infinity
|
||||
|
||||
# Initialize the firewall inside the container with root privileges.
|
||||
docker exec --user root "$CONTAINER_NAME" /usr/local/bin/init_firewall.sh
|
||||
# Write the allowed domains to a file in the container
|
||||
docker exec --user root "$CONTAINER_NAME" bash -c "mkdir -p /etc/codex"
|
||||
for domain in $OPENAI_ALLOWED_DOMAINS; do
|
||||
# Validate domain format to prevent injection
|
||||
if [[ ! "$domain" =~ ^[a-zA-Z0-9][a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]]; then
|
||||
echo "Error: Invalid domain format: $domain"
|
||||
exit 1
|
||||
fi
|
||||
echo "$domain" | docker exec --user root -i "$CONTAINER_NAME" bash -c "cat >> /etc/codex/allowed_domains.txt"
|
||||
done
|
||||
|
||||
# Set proper permissions on the domains file
|
||||
docker exec --user root "$CONTAINER_NAME" bash -c "chmod 444 /etc/codex/allowed_domains.txt && chown root:root /etc/codex/allowed_domains.txt"
|
||||
|
||||
# Initialize the firewall inside the container as root user
|
||||
docker exec --user root "$CONTAINER_NAME" bash -c "/usr/local/bin/init_firewall.sh"
|
||||
|
||||
# Remove the firewall script after running it
|
||||
docker exec --user root "$CONTAINER_NAME" bash -c "rm -f /usr/local/bin/init_firewall.sh"
|
||||
|
||||
# Execute the provided command in the container, ensuring it runs in the work directory.
|
||||
# We use a parameterized bash command to safely handle the command and directory.
|
||||
|
||||
Reference in New Issue
Block a user