Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
46 changes: 31 additions & 15 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -156,24 +156,32 @@ jobs:
# A minimal, fully populated mainnet configuration that must pass
# --strict. Keep this fixture in sync with the strict rules in
# scripts/check-env.js and docs/deployment.md.
cat > strict.env <<'EOF'
NETWORK=mainnet
RPC_URL=https://rpc.mainnet.example.com
WS_URL=wss://ws.mainnet.example.com
API_URL=https://api.mainnet.example.com
CONTRACT_ID=CDLZFC3SYJYDZT7K67VZ75HPJVIEUVNIXF47ZG2FB2RMQQVU2HHGCYSC
JWT_SECRET=strict-fixture-jwt-secret-value
DATABASE_URL=postgres://user:pass@db.internal:5432/synapsvault
read -r public_key secret_key < <(node --input-type=module -e 'import {Keypair} from "@stellar/stellar-sdk"; const k=Keypair.random(); console.log(k.publicKey(), k.secret())')
cat > strict.env <<EOF
STELLAR_NETWORK=mainnet
POSTGRES_USER=fixture_user
POSTGRES_PASSWORD=fixture-password
POSTGRES_DB=fixture_db
SOROBAN_RPC_URL=https://rpc.mainnet.example.com
SUPABASE_URL=https://fixture.supabase.co
SUPABASE_ANON_KEY=fixture-anon-key
SUPABASE_SERVICE_KEY=fixture-service-key
ADMIN_API_KEY=fixture-admin-key
OPENROUTER_API_KEY=fixture-openrouter-key
STELLAR_PUBLIC_KEY=${public_key}
STELLAR_SECRET_KEY=${secret_key}
AGENT_SECRET_KEY=${secret_key}
REGISTRY_SECRET_KEY=${secret_key}
EOF
node scripts/check-env.js --target compose --env-file strict.env --strict
# Strict mode must reject testnet configuration.
sed 's/^NETWORK=mainnet$/NETWORK=testnet/' strict.env > strict-testnet.env
sed 's/^STELLAR_NETWORK=mainnet$/STELLAR_NETWORK=testnet/' strict.env > strict-testnet.env
if node scripts/check-env.js --target compose --env-file strict-testnet.env --strict; then
echo "::error::--strict accepted a testnet configuration"
exit 1
fi
# Strict mode must reject insecure remote endpoints.
sed 's#^RPC_URL=.*#RPC_URL=http://rpc.mainnet.example.com#' strict.env > strict-insecure.env
sed 's#^SOROBAN_RPC_URL=.*#SOROBAN_RPC_URL=http://rpc.mainnet.example.com#' strict.env > strict-insecure.env
if node scripts/check-env.js --target compose --env-file strict-insecure.env --strict; then
echo "::error::--strict accepted an insecure remote endpoint"
exit 1
Expand Down Expand Up @@ -206,9 +214,7 @@ jobs:
trap 'rm -rf "${work_dir}"' EXIT

# Generate a valid Stellar keypair for the positive cases.
keypair="$(node scripts/01-generate-keypairs.js)"
public_key="$(printf '%s\n' "${keypair}" | sed -n 's/^Public [Kk]ey: *//p' | head -n1)"
secret_key="$(printf '%s\n' "${keypair}" | sed -n 's/^Secret [Kk]ey: *//p' | head -n1)"
read -r public_key secret_key < <(node --input-type=module -e 'import {Keypair} from "@stellar/stellar-sdk"; const k=Keypair.random(); console.log(k.publicKey(), k.secret())')
if [ -z "${public_key}" ] || [ -z "${secret_key}" ]; then
echo "::error::Could not parse generated keypair"
exit 1
Expand All @@ -220,8 +226,19 @@ jobs:
base_env() {
cat <<EOF
STELLAR_NETWORK=testnet
POSTGRES_USER=fixture_user
POSTGRES_PASSWORD=fixture-password
POSTGRES_DB=fixture_db
SOROBAN_RPC_URL=https://rpc.mainnet.example.com
SUPABASE_URL=https://fixture.supabase.co
SUPABASE_ANON_KEY=fixture-anon-key
SUPABASE_SERVICE_KEY=fixture-service-key
ADMIN_API_KEY=fixture-admin-key
OPENROUTER_API_KEY=fixture-openrouter-key
STELLAR_PUBLIC_KEY=${public_key}
STELLAR_SECRET_KEY=${secret_key}
AGENT_SECRET_KEY=${secret_key}
REGISTRY_SECRET_KEY=${secret_key}
STELLAR_CONTRACT_ID=${contract_id}
EOF
}
Expand Down Expand Up @@ -273,8 +290,7 @@ jobs:
expect_fail "bad contract checksum" "${work_dir}/bad-contract.env" 'STELLAR_CONTRACT_ID'

# Negative: public key does not match the secret key.
other_keypair="$(node scripts/01-generate-keypairs.js)"
other_public="$(printf '%s\n' "${other_keypair}" | sed -n 's/^Public [Kk]ey: *//p' | head -n1)"
other_public="$(node --input-type=module -e 'import {Keypair} from "@stellar/stellar-sdk"; console.log(Keypair.random().publicKey())')"
base_env | sed "s/^STELLAR_PUBLIC_KEY=.*/STELLAR_PUBLIC_KEY=${other_public}/" > "${work_dir}/mismatch.env"
expect_fail "keypair mismatch" "${work_dir}/mismatch.env" 'STELLAR_(PUBLIC|SECRET)_KEY'

Expand Down
6 changes: 3 additions & 3 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# runtime (default) API server: node dist/index.js on $PORT (3000)
# migrate One-shot Drizzle migrations (used as a k8s initContainer)

ARG NODE_VERSION=22
ARG NODE_VERSION=24
# Override when upstream peer dependency conflicts are resolved.
ARG NPM_FLAGS=--legacy-peer-deps

Expand All @@ -23,7 +23,7 @@ ARG NPM_FLAGS
WORKDIR /app

# Build tooling for any native modules
RUN apk add --no-cache python3=3.14.10-r1 make=4.4.1-r4 g++=15.2.0-r5
RUN apk add --no-cache python3 make g++

COPY package.json package-lock.json* ./
# Skip lifecycle scripts: the postinstall symlinks packages/registry-client,
Expand All @@ -36,7 +36,7 @@ FROM node:${NODE_VERSION}-alpine AS prod-deps
ARG NPM_FLAGS
WORKDIR /app

RUN apk add --no-cache python3=3.14.10-r1 make=4.4.1-r4 g++=15.2.0-r5
RUN apk add --no-cache python3 make g++

COPY package.json package-lock.json* ./
RUN npm ci --omit=dev --ignore-scripts ${NPM_FLAGS}
Expand Down
42 changes: 0 additions & 42 deletions k8s/frontend-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -140,45 +140,3 @@ spec:
port: 80
targetPort: http
protocol: TCP
---
# Egress policy for frontend pods (issue #171).
#
# The frontend is a static-serving nginx container. At runtime it only needs
# to answer inbound HTTP traffic and resolve DNS; it does not call the backend
# directly (the browser does, via APP_API_URL=/api) and does not fetch remote
# assets at runtime. Permitted egress destinations:
# - kube-dns (UDP/TCP 53) for name resolution
# - the backend Service (TCP 80) so /api proxying keeps working if enabled
# Everything else is denied.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: frontend-egress
labels:
app: frontend
spec:
podSelector:
matchLabels:
app: frontend
policyTypes:
- Egress
egress:
- to:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: kube-system
podSelector:
matchLabels:
k8s-app: kube-dns
ports:
- protocol: UDP
port: 53
- protocol: TCP
port: 53
- to:
- podSelector:
matchLabels:
app: backend
ports:
- protocol: TCP
port: 80
4 changes: 4 additions & 0 deletions k8s/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ replacements:
name: synapsvault
fieldPaths:
- metadata.labels.ingress-controller-namespace
options:
create: true
# Propagate the configured DNS resolver address onto the synapsvault
# Namespace as a label so the backend and frontend egress NetworkPolicies
# can select it without hardcoding the kube-dns ClusterIP. Override
Expand All @@ -129,3 +131,5 @@ replacements:
name: synapsvault
fieldPaths:
- metadata.labels.dns-resolver-ip
options:
create: true
11 changes: 4 additions & 7 deletions scripts/backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -237,15 +237,11 @@ cleanup() {
if [[ "${UPLOAD_STATUS}" == "success" ]]; then
err_msg="backup failed after successful upload (exit ${exit_code}); retention=${RETENTION_STATUS}"
fi
emit_metrics "${overall_success}" "$(( end_ts - START_TS ))" \
"${UPLOADED_SIZE_BYTES}" "$(previous_last_success)" "${err_msg}" || true
local reason_msg="backup failed with exit code ${exit_code}"
fi
if [[ -n "${FAILURE_REASON}" ]]; then
reason_msg="${reason_msg} (reason=${FAILURE_REASON})"
err_msg="${err_msg} (reason=${FAILURE_REASON})"
fi
emit_metrics 0 "$(( end_ts - START_TS ))" 0 "$(previous_last_success)" \
"${reason_msg}" || true
emit_metrics "${overall_success}" "$(( end_ts - START_TS ))" \
"${UPLOADED_SIZE_BYTES}" "$(previous_last_success)" "${err_msg}" || true
error "Backup failed with exit code ${exit_code}"
fi
if [[ -n "${LOCK_FD}" ]]; then
Expand Down Expand Up @@ -546,6 +542,7 @@ emit_metrics() {
local ret_escaped
ret_escaped="$(printf '%s' "${RETENTION_ERROR}" | sed -e 's/\\/\\\\/g' -e 's/"/\\"/g' -e 's/\n/ /g')"
printf 'soroban_backup_retention_error_info{message="%s"} 1\n' "${ret_escaped}"
fi
if [[ -n "${FAILURE_REASON}" ]]; then
printf '# HELP soroban_backup_failure_reason Whether the last run failed for a specific reason (1=yes).\n'
printf '# TYPE soroban_backup_failure_reason gauge\n'
Expand Down
69 changes: 23 additions & 46 deletions scripts/check-env.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,42 +398,27 @@ export function validateEnv(specs, fileEnv, target, options = {}) {
resolved[spec.name] = value;
if (isPlaceholder(value)) {
placeholder.push(spec.name);
} else {
const allowedProtocols = URL_PROTOCOLS[target][spec.name];
if (allowedProtocols) {
try {
const url = new URL(value);
if (!allowedProtocols.includes(url.protocol) || !url.hostname) {
invalidUrl.push(spec.name);
} else if (strict && url.protocol === 'http:' && !isLocalHost(url.hostname)) {
insecureUrl.push(spec.name);
}
} catch {
continue;
}

const allowedProtocols = URL_PROTOCOLS[target][spec.name];
if (allowedProtocols) {
try {
const url = new URL(value);
if (!allowedProtocols.includes(url.protocol) || !url.hostname) {
invalidUrl.push(spec.name);
} else if (strict && url.protocol === 'http:' && !isLocalHost(url.hostname)) {
insecureUrl.push(spec.name);
}
} catch {
invalidUrl.push(spec.name);
}
}

if (spec.name === 'STELLAR_NETWORK') {
if (!SUPPORTED_NETWORKS.includes(value.trim().toLowerCase())) {
invalidNetwork.push(spec.name);
}
if (!SUPPORTED_NETWORKS.includes(value.trim().toLowerCase())) invalidNetwork.push(spec.name);
if (strict && /testnet/i.test(value)) testnet.push(spec.name);
}

const strKeyKind = STRKEY_FIELDS[spec.name];
if (strKeyKind && !isValidStrKey(value, strKeyKind)) {
invalidKey.push(spec.name);
}
if (strKeyKind && !isValidStrKey(value, strKeyKind)) invalidKey.push(spec.name);
}

// Cross-field check: derive the public key from the secret and compare.
Expand All @@ -457,33 +442,12 @@ export function validateEnv(specs, fileEnv, target, options = {}) {
if (!invalidKey.includes('STELLAR_SECRET_KEY')) {
invalidKey.push('STELLAR_SECRET_KEY');
}
if (strict && spec.name === 'STELLAR_NETWORK' && /testnet/i.test(value)) {
testnet.push(spec.name);
}
}
}

return {
ok:
missing.length === 0 &&
placeholder.length === 0 &&
invalidUrl.length === 0 &&
testnet.length === 0 &&
insecureUrl.length === 0,
missing,
placeholder,
invalidUrl,
testnet,
insecureUrl,
invalidNetwork.length === 0 &&
invalidKey.length === 0 &&
keyMismatch.length === 0,
missing,
placeholder,
invalidUrl,
invalidNetwork,
invalidKey,
keyMismatch,
ok: [missing, placeholder, invalidUrl, testnet, insecureUrl, invalidNetwork, invalidKey, keyMismatch].every(items => items.length === 0),
missing, placeholder, invalidUrl, testnet, insecureUrl, invalidNetwork, invalidKey, keyMismatch,
};
}

Expand Down Expand Up @@ -603,6 +567,13 @@ export function buildJsonResult({ target, strict, ok, duplicates, result }) {
if (result.insecureUrl.length > 0) {
errors.push({ code: 'INSECURE_ENDPOINT', variables: result.insecureUrl });
}
for (const [code, variables] of [
['INVALID_NETWORK', result.invalidNetwork],
['INVALID_KEY', result.invalidKey],
['KEY_MISMATCH', result.keyMismatch],
]) {
if (variables.length) errors.push({ code, variables });
}
return {
target,
strict,
Expand Down Expand Up @@ -660,9 +631,7 @@ export function run(argv = process.argv.slice(2), cwd = process.cwd()) {
const specs = TARGETS[target];

const result = validateEnv(specs, fileEnv, target, { strict });
const { ok, missing, placeholder, invalidUrl, testnet, insecureUrl } = result;
const { ok, missing, placeholder, invalidUrl, invalidNetwork, invalidKey, keyMismatch } =
validateEnv(specs, fileEnv, target);
const { ok, missing, placeholder, invalidUrl, testnet, insecureUrl, invalidNetwork, invalidKey, keyMismatch } = result;

const targetLabel = target.toUpperCase();

Expand Down Expand Up @@ -726,6 +695,10 @@ export function run(argv = process.argv.slice(2), cwd = process.cwd()) {
console.error(`Testnet configuration rejected in strict mainnet mode:`);
for (const name of testnet) {
console.error(` • ${name} must not identify a testnet`);
}
console.error('');
}

if (invalidNetwork.length > 0) {
console.error(`Invalid STELLAR_NETWORK values (unsupported network):`);
for (const name of invalidNetwork) {
Expand All @@ -741,6 +714,10 @@ export function run(argv = process.argv.slice(2), cwd = process.cwd()) {
console.error(`Insecure remote endpoints rejected in strict mainnet mode:`);
for (const name of insecureUrl) {
console.error(` • ${name} must use https:// for non-local hosts`);
}
console.error('');
}

if (invalidKey.length > 0) {
console.error(`Invalid Stellar key/contract values (bad encoding or checksum):`);
for (const name of invalidKey) {
Expand Down
7 changes: 3 additions & 4 deletions scripts/restore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,9 @@ esac
# duplicate normalized member names, and archives exceeding the configured
# member-count / uncompressed-size budgets.
python3 "$(dirname "${BASH_SOURCE[0]}")/validate-archive.py" "$ARCHIVE_PATH" \
|| die 3 "archive contains unsafe paths or file types"
--max-members "${RESTORE_MAX_MEMBERS:-10000}" \
--max-uncompressed-bytes "${RESTORE_MAX_UNCOMPRESSED_BYTES:-10737418240}" \
|| die 3 "archive rejected: unsafe paths or file types, duplicate member paths, or exceeds size/member budget"

# Check the archive's top-level components against the restore allowlist before
# extracting or touching any database. Unknown required components, unexpected
Expand All @@ -688,9 +690,6 @@ log "validating archive contents against the restore component allowlist"
python3 "$(dirname "${BASH_SOURCE[0]}")/validate-archive.py" --allowlist "$ARCHIVE_PATH" \
|| die 3 "archive contents failed allowlist validation (unexpected or missing components)"

--max-members "${RESTORE_MAX_MEMBERS:-10000}" \
--max-uncompressed-bytes "${RESTORE_MAX_UNCOMPRESSED_BYTES:-10737418240}" \
|| die 3 "archive rejected: unsafe paths or file types, duplicate member paths, or exceeds size/member budget"
EXTRACT_DIR="$(mktemp -d "$WORK_DIR/extracted.XXXXXXXX")"
tar -xzf "$ARCHIVE_PATH" -C "$EXTRACT_DIR" || die 3 "archive is not a valid tar.gz"

Expand Down
Loading