Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
190 changes: 142 additions & 48 deletions scripts/nemoclaw-start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,14 @@ if [ "${1:-}" = "env" ]; then
_self_wrapper_index=""
for ((i = 1; i < ${#_raw_args[@]}; i += 1)); do
case "${_raw_args[$i]}" in
*=*) ;;
nemoclaw-start | /usr/local/bin/nemoclaw-start)
_self_wrapper_index="$i"
break
;;
*)
break
;;
*=*) ;;
nemoclaw-start | /usr/local/bin/nemoclaw-start)
_self_wrapper_index="$i"
break
;;
*)
break
;;
esac
done
if [ -n "$_self_wrapper_index" ]; then
Expand All @@ -152,7 +152,7 @@ fi
# receiving our own name as $1 would otherwise recurse via the NEMOCLAW_CMD
# exec path. Only strip from $1 — later args with this name are legitimate.
case "${1:-}" in
nemoclaw-start | /usr/local/bin/nemoclaw-start) shift ;;
nemoclaw-start | /usr/local/bin/nemoclaw-start) shift ;;
esac
NEMOCLAW_CMD=("$@")
# Validate NEMOCLAW_DASHBOARD_PORT if set (same behavior as ports.js: fail fast).
Expand All @@ -162,10 +162,10 @@ if [ -z "$_DASHBOARD_PORT_RAW" ]; then
else
_DASHBOARD_PORT="$(printf '%s' "$_DASHBOARD_PORT_RAW" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')"
case "$_DASHBOARD_PORT" in
*[!0-9]* | '')
echo "[SECURITY] Invalid NEMOCLAW_DASHBOARD_PORT='${NEMOCLAW_DASHBOARD_PORT}' — must be an integer between 1024 and 65535" >&2
exit 1
;;
*[!0-9]* | '')
echo "[SECURITY] Invalid NEMOCLAW_DASHBOARD_PORT='${NEMOCLAW_DASHBOARD_PORT}' — must be an integer between 1024 and 65535" >&2
exit 1
;;
esac
if [ "$_DASHBOARD_PORT" -lt 1024 ] || [ "$_DASHBOARD_PORT" -gt 65535 ]; then
echo "[SECURITY] Invalid NEMOCLAW_DASHBOARD_PORT='${NEMOCLAW_DASHBOARD_PORT}' — must be an integer between 1024 and 65535" >&2
Expand Down Expand Up @@ -218,12 +218,12 @@ verify_config_integrity() {

apply_model_override() {
# Any of these env vars trigger a config patch
[ -n "${NEMOCLAW_MODEL_OVERRIDE:-}" ] \
|| [ -n "${NEMOCLAW_INFERENCE_API_OVERRIDE:-}" ] \
|| [ -n "${NEMOCLAW_CONTEXT_WINDOW:-}" ] \
|| [ -n "${NEMOCLAW_MAX_TOKENS:-}" ] \
|| [ -n "${NEMOCLAW_REASONING:-}" ] \
|| return 0
[ -n "${NEMOCLAW_MODEL_OVERRIDE:-}" ] ||
[ -n "${NEMOCLAW_INFERENCE_API_OVERRIDE:-}" ] ||
[ -n "${NEMOCLAW_CONTEXT_WINDOW:-}" ] ||
[ -n "${NEMOCLAW_MAX_TOKENS:-}" ] ||
[ -n "${NEMOCLAW_REASONING:-}" ] ||
return 0

# SECURITY: Only root can write to /sandbox/.openclaw (root:root 444).
# In non-root mode the sandbox user cannot modify the config.
Expand All @@ -242,7 +242,7 @@ apply_model_override() {
return 1
fi

local model_override="$NEMOCLAW_MODEL_OVERRIDE"
local model_override="${NEMOCLAW_MODEL_OVERRIDE:-}"
local api_override="${NEMOCLAW_INFERENCE_API_OVERRIDE:-}"

# SECURITY: Validate inputs — reject control characters and enforce length limit.
Expand All @@ -258,11 +258,11 @@ apply_model_override() {
# SECURITY: Allowlist inference API types to prevent unexpected routing.
if [ -n "$api_override" ]; then
case "$api_override" in
openai-completions | anthropic-messages) ;;
*)
printf '[SECURITY] NEMOCLAW_INFERENCE_API_OVERRIDE must be "openai-completions" or "anthropic-messages", got "%s"\n' "$api_override" >&2
return 1
;;
openai-completions | anthropic-messages) ;;
*)
printf '[SECURITY] NEMOCLAW_INFERENCE_API_OVERRIDE must be "openai-completions" or "anthropic-messages", got "%s"\n' "$api_override" >&2
return 1
;;
esac
fi

Expand All @@ -282,11 +282,11 @@ apply_model_override() {
# Validate reasoning is true/false
if [ -n "$reasoning" ]; then
case "$reasoning" in
true | false) ;;
*)
printf '[SECURITY] NEMOCLAW_REASONING must be "true" or "false", got "%s"\n' "$reasoning" >&2
return 1
;;
true | false) ;;
*)
printf '[SECURITY] NEMOCLAW_REASONING must be "true" or "false", got "%s"\n' "$reasoning" >&2
return 1
;;
esac
fi

Expand Down Expand Up @@ -401,6 +401,84 @@ PYCORS
printf '[config] Config hash recomputed after CORS override\n' >&2
}

# ── Slack token placeholder resolution ────────────────────────────
# Resolves openshell:resolve:env:SLACK_* placeholders in openclaw.json at
# container startup, before chattr +i locks the file. This ensures Bolt's
# in-process token validation (appToken must start with xapp-) succeeds even
# before the L7 proxy can intercept HTTP calls.
# Same trust model as apply_model_override: host-set env vars, root-only,
# applied before Landlock/chattr +i, hash recomputed. Tokens are unset from
# the process env after patching so they are not visible inside the sandbox.
# Ref: https://github.com/NVIDIA/NemoClaw/issues/2085

apply_slack_token_override() {
[ -n "${SLACK_BOT_TOKEN:-}" ] || return 0

# SECURITY: Only root can write to /sandbox/.openclaw (root:root 444).
if [ "$(id -u)" -ne 0 ]; then
printf '[SECURITY] Slack token override ignored — requires root (non-root mode cannot write to config)\n' >&2
return 0
fi

local config_file="/sandbox/.openclaw/openclaw.json"
local hash_file="/sandbox/.openclaw/.config-hash"

# SECURITY: Refuse to write through symlinks to prevent symlink-following attacks.
if [ -L "$config_file" ] || [ -L "$hash_file" ]; then
printf '[SECURITY] Refusing Slack token override — config or hash path is a symlink\n' >&2
return 1
fi

# SECURITY: Validate token prefixes — reject anything that doesn't look like a real Slack token.
case "${SLACK_BOT_TOKEN}" in
xoxb-*) ;;
*)
printf '[channels] SLACK_BOT_TOKEN does not start with xoxb- — skipping Slack placeholder resolution\n' >&2
return 0
;;
esac

if [ -n "${SLACK_APP_TOKEN:-}" ]; then
case "$SLACK_APP_TOKEN" in
xapp-*) ;;
*)
printf '[channels] SLACK_APP_TOKEN does not start with xapp- — skipping Slack placeholder resolution\n' >&2
return 0
;;
esac
fi

printf '[channels] Resolving Slack token placeholders in openclaw.json\n' >&2

SLACK_BOT_TOKEN="$SLACK_BOT_TOKEN" \
SLACK_APP_TOKEN="${SLACK_APP_TOKEN:-}" \
python3 - "$config_file" <<'PYSLACK'
import json, os, sys

config_file = sys.argv[1]
bot_token = os.environ["SLACK_BOT_TOKEN"]
app_token = os.environ.get("SLACK_APP_TOKEN", "")
placeholder_prefix = "openshell:resolve:env:"

with open(config_file) as f:
cfg = json.load(f)

slack = cfg.get("channels", {}).get("slack", {})
default_acct = slack.get("accounts", {}).get("default", {})

if default_acct.get("botToken", "").startswith(placeholder_prefix):
default_acct["botToken"] = bot_token
if app_token and default_acct.get("appToken", "").startswith(placeholder_prefix):
default_acct["appToken"] = app_token

with open(config_file, "w") as f:
json.dump(cfg, f, indent=2)
PYSLACK

(cd /sandbox/.openclaw && sha256sum openclaw.json >"$hash_file")
printf '[channels] Config hash recomputed after Slack token override\n' >&2
}

_read_gateway_token() {
python3 - <<'PYTOKEN'
import json
Expand Down Expand Up @@ -635,14 +713,16 @@ harden_auth_profiles() {

configure_messaging_channels() {
# Channel entries are baked into openclaw.json at image build time via
# NEMOCLAW_MESSAGING_CHANNELS_B64 (see Dockerfile). Placeholder tokens
# (openshell:resolve:env:*) flow through to API calls where the L7 proxy
# rewrites them with real secrets at egress. Real tokens are never visible
# inside the sandbox.
# NEMOCLAW_MESSAGING_CHANNELS_B64 (see Dockerfile).
#
# Telegram/Discord: placeholder tokens (openshell:resolve:env:*) flow through
# to API calls where the L7 proxy rewrites them with real secrets at egress.
# Real tokens are never visible inside the sandbox for these channels.
#
# Runtime patching of /sandbox/.openclaw/openclaw.json is not possible:
# Landlock enforces read-only on /sandbox/.openclaw/ at the kernel level,
# regardless of DAC (file ownership/chmod). Writes fail with EPERM.
# Slack: apply_slack_token_override (runs before this function) resolves
# SLACK_BOT_TOKEN/SLACK_APP_TOKEN placeholders directly into openclaw.json so
# Bolt's in-process token validation passes. Both env vars are unset before the
# gateway starts (root path) so they do not leak into the sandbox process env.
[ -n "${TELEGRAM_BOT_TOKEN:-}" ] || [ -n "${DISCORD_BOT_TOKEN:-}" ] || [ -n "${SLACK_BOT_TOKEN:-}" ] || return 0

echo "[channels] Messaging channels active (baked at build time):" >&2
Expand Down Expand Up @@ -858,6 +938,14 @@ if [ "$(id -u)" -ne 0 ]; then
fi
apply_model_override
apply_cors_override
apply_slack_token_override
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
# SECURITY: apply_slack_token_override is a no-op when non-root.
# If SLACK_BOT_TOKEN is still set here the placeholder was never resolved —
# Bolt will crash with invalid_auth at startup. Fail fast with a clear message.
if [ -n "${SLACK_BOT_TOKEN:-}" ]; then
printf '[SECURITY] Slack Socket Mode requires a root container — SLACK_BOT_TOKEN is set but token placeholder resolution needs root. Run the container as root or remove SLACK_BOT_TOKEN.\n' >&2
exit 1
fi
export_gateway_token
install_configure_guard
configure_messaging_channels
Expand All @@ -882,25 +970,25 @@ if [ "$(id -u)" -ne 0 ]; then
current="$(readlink -f "$link_path" 2>/dev/null || true)"
expected="$(readlink -f "$target" 2>/dev/null || true)"
[ "$current" != "$expected" ] || return 0
ln -snf "$target" "$link_path" 2>/dev/null \
&& echo "[setup] repaired identity symlink" >&2 \
|| echo "[setup] could not repair identity symlink" >&2
ln -snf "$target" "$link_path" 2>/dev/null &&
echo "[setup] repaired identity symlink" >&2 ||
echo "[setup] could not repair identity symlink" >&2
return 0
fi

# Nothing exists yet — create the symlink.
if [ ! -e "$link_path" ]; then
ln -snf "$target" "$link_path" 2>/dev/null \
&& echo "[setup] created identity symlink" >&2 \
|| echo "[setup] could not create identity symlink" >&2
ln -snf "$target" "$link_path" 2>/dev/null &&
echo "[setup] created identity symlink" >&2 ||
echo "[setup] could not create identity symlink" >&2
return 0
fi

# A non-symlink entry exists — back it up, then replace.
local backup
backup="${link_path}.bak.$(date +%s)"
if mv "$link_path" "$backup" 2>/dev/null \
&& ln -snf "$target" "$link_path" 2>/dev/null; then
if mv "$link_path" "$backup" 2>/dev/null &&
ln -snf "$target" "$link_path" 2>/dev/null; then
echo "[setup] replaced non-symlink identity path (backup: ${backup})" >&2
else
echo "[setup] could not replace ${link_path}; writes may fail" >&2
Expand All @@ -916,9 +1004,9 @@ if [ "$(id -u)" -ne 0 ]; then
mkdir -p "${data_dir}/${sub}" 2>/dev/null || true
done
if find "$data_dir" ! -uid "$(id -u)" -print -quit 2>/dev/null | grep -q .; then
chown -R "$(id -u):$(id -g)" "$data_dir" 2>/dev/null \
&& echo "[setup] fixed ownership on ${data_dir}" >&2 \
|| echo "[setup] could not fix ownership on ${data_dir}; writes may fail" >&2
chown -R "$(id -u):$(id -g)" "$data_dir" 2>/dev/null &&
echo "[setup] fixed ownership on ${data_dir}" >&2 ||
echo "[setup] could not fix ownership on ${data_dir}; writes may fail" >&2
fi
ensure_identity_symlink "$data_dir" "$openclaw_dir"
}
Expand Down Expand Up @@ -957,6 +1045,7 @@ fi
verify_config_integrity
apply_model_override
apply_cors_override
apply_slack_token_override
export_gateway_token
install_configure_guard

Expand All @@ -965,6 +1054,11 @@ install_configure_guard
# BEFORE chattr +i (which locks the config permanently).
configure_messaging_channels

# SECURITY: Slack tokens were resolved into openclaw.json by apply_slack_token_override.
# Unset here — before any gosu sandbox child — so neither the sandbox user nor
# the gateway inherits them from the process environment.
unset SLACK_BOT_TOKEN SLACK_APP_TOKEN

# Write auth profile as sandbox user (needs writable .openclaw-data)
# and recursively re-tighten any auth-profiles.json files under ~/.openclaw.
gosu sandbox bash -c "$(declare -f write_auth_profile harden_auth_profiles); write_auth_profile; harden_auth_profiles"
Expand Down
Loading
Loading