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:
@@ -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