Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

17.1 KB
Raw Blame
import { inspect } from 'util'
import { isNil, tryCatchSync } from '@activepieces/core-utils'
import { ApEdition, ApEnvironment, AUDIT_LOG_RETENTION_MAX_DAYS, AUDIT_LOG_RETENTION_MIN_DAYS, DefaultProjectRole, ExecutionMode, FileLocation, maxBarrierSignalsBounds, NetworkMode, PieceSyncMode } from '@activepieces/shared'
import { FastifyBaseLogger } from 'fastify'
import { DatabaseType } from '../database/database-type'
import { RedisType } from '../database/redis/types'
import { s3Helper } from '../file/s3-helper'
import { encryptUtils } from './encryption'
import { jwtUtils } from './jwt-utils'
import { networkUtils } from './network-utils'
import { auditLogRetentionCeiling } from './retention/audit-log-retention-ceiling'
import { system } from './system/system'
import { AppSystemProp, ContainerType, SystemProp } from './system/system-props'


function enumValidator<T extends string>(enumValues: T[]) {
    return (value: string) => {
        const isValid = enumValues.includes(value as T)
        return isValid ? true : `Value must be one of: ${enumValues.join(', ')}`
    }
}

function booleanValidator(value: string | undefined) {
    const isValid = value === 'true' || value === 'false'
    return isValid ? true : 'Value must be either "true" or "false"'
}

function numberValidator(value: string | undefined) {
    const isValid = !isNil(value) && !Number.isNaN(Number(value))
    return isValid ? true : 'Value must be a valid number'
}

function positiveIntegerValidator(value: string | undefined) {
    const parsed = Number(value)
    return Number.isInteger(parsed) && parsed > 0 ? true : 'Value must be a positive integer'
}

function auditLogRetentionDaysValidator(value: string | undefined) {
    return isNil(auditLogRetentionCeiling.parse(value)) ? `Value must be a whole number of days from ${AUDIT_LOG_RETENTION_MIN_DAYS} to ${AUDIT_LOG_RETENTION_MAX_DAYS}` : true
}

function positiveFiniteNumberValidator(value: string | undefined) {
    const parsed = isNil(value) ? Number.NaN : Number(value)
    const isValid = Number.isFinite(parsed) && parsed > 0
    return isValid ? true : 'Value must be a finite number greater than zero'
}

function boundedNumberValidator({ min, max }: { min: number, max: number }) {
    return (value: string | undefined) => {
        const parsed = Number(value)
        const isValid = !isNil(value) && Number.isInteger(parsed) && parsed >= min && parsed <= max
        return isValid ? true : `Value must be a whole number between ${min} and ${max}`
    }
}

function stringValidator(value: string) {
    const isValid = typeof value === 'string' && value.length > 0
    return isValid ? true : 'Value must be a non-empty string'
}

function urlValidator(value: string) {
    try {
        const { protocol } = new URL(value)
        if (protocol !== 'http:' && protocol !== 'https:') {
            return 'Value must be an http or https URL'
        }
        return true
    }
    catch {
        return 'Value must be a valid URL'
    }
}

function assertMcpUrlHasDistinctHost(): void {
    const mcpUrl = system.get(AppSystemProp.MCP_URL)
    const frontendUrl = system.get(AppSystemProp.FRONTEND_URL)
    if (isNil(mcpUrl) || isNil(frontendUrl)) {
        return
    }
    const parsedMcpUrl = tryCatchSync(() => new URL(mcpUrl))
    const parsedFrontendUrl = tryCatchSync(() => new URL(frontendUrl))
    if (parsedMcpUrl.error || parsedFrontendUrl.error) {
        return
    }
    const sameHost = parsedMcpUrl.data.host.toLowerCase() === parsedFrontendUrl.data.host.toLowerCase()
    const sameBaseUrl = networkUtils.cleanTrailingSlash(mcpUrl) === networkUtils.cleanTrailingSlash(frontendUrl)
    if (sameHost && !sameBaseUrl) {
        throw new Error(JSON.stringify({
            message: 'AP_MCP_URL and AP_FRONTEND_URL share a hostname but are not the same URL, so the server cannot tell requests for one from the other. Give AP_MCP_URL its own hostname, or leave it unset.',
            docUrl: 'https://www.activepieces.com/docs/install/configuration/environment-variables',
        }))
    }
}

const systemPropValidators: {
    [key in SystemProp]: (value: string) => true | string
} = {
    // AppSystemProp
    [AppSystemProp.ALLOW_OPEN_SIGN_UP]: booleanValidator,
    [AppSystemProp.EXECUTION_MODE]: enumValidator(Object.values(ExecutionMode)),
    [AppSystemProp.SKIP_PREWARM_CODES]: booleanValidator,
    [AppSystemProp.SKIP_PROJECT_LIMITS_CHECK]: booleanValidator,
    [AppSystemProp.LOG_LEVEL]: enumValidator(['error', 'warn', 'info', 'debug', 'trace']),
    [AppSystemProp.LOG_PRETTY]: booleanValidator,
    [AppSystemProp.LOG_FILE]: booleanValidator,
    [AppSystemProp.ENVIRONMENT]: enumValidator(Object.values(ApEnvironment)),
    [AppSystemProp.TRIGGER_TIMEOUT_SECONDS]: numberValidator,
    [AppSystemProp.TRIGGER_HOOKS_TIMEOUT_SECONDS]: numberValidator,
    [AppSystemProp.FLOW_TIMEOUT_SECONDS]: numberValidator,
    [AppSystemProp.EVENT_DESTINATION_TIMEOUT_SECONDS]: numberValidator,
    [AppSystemProp.PAUSED_FLOW_TIMEOUT_DAYS]: numberValidator,
    [AppSystemProp.APP_WEBHOOK_SECRETS]: stringValidator,
    [AppSystemProp.MAX_BARRIER_SIGNALS]: boundedNumberValidator(maxBarrierSignalsBounds),
    [AppSystemProp.MAX_FILE_SIZE_MB]: numberValidator,
    [AppSystemProp.AI_CREDIT_USD_VALUE]: positiveFiniteNumberValidator,
    [AppSystemProp.MAX_FLOW_RUN_LOG_SIZE_MB]: numberValidator,
    [AppSystemProp.FLOW_RUN_LOG_INPUT_TRUNCATE_THRESHOLD_KB]: positiveIntegerValidator,
    [AppSystemProp.FLOW_RUN_LOG_SLICE_THRESHOLD_KB]: positiveIntegerValidator,
    [AppSystemProp.SANDBOX_MEMORY_LIMIT]: numberValidator,
    [AppSystemProp.SANDBOX_PROPAGATED_ENV_VARS]: stringValidator,
    [AppSystemProp.SENTRY_DSN]: urlValidator,
    [AppSystemProp.FRONTEND_SENTRY_DSN]: urlValidator,
    [AppSystemProp.RUNS_METADATA_UPDATE_CONCURRENCY]: numberValidator,
    [AppSystemProp.LOKI_PASSWORD]: stringValidator,
    [AppSystemProp.LOKI_URL]: urlValidator,
    [AppSystemProp.LOKI_USERNAME]: stringValidator,

    [AppSystemProp.BETTERSTACK_TOKEN]: stringValidator,
    [AppSystemProp.TURNSTILE_SECRET_KEY]: stringValidator,
    [AppSystemProp.TURNSTILE_SITE_KEY]: stringValidator,
    [AppSystemProp.BETTERSTACK_HOST]: stringValidator,
    [AppSystemProp.OTEL_ENABLED]: booleanValidator,
    [AppSystemProp.OTEL_QUEUE_METRICS_ENABLED]: booleanValidator,
    [AppSystemProp.HYPERDX_TOKEN]: stringValidator,
    [AppSystemProp.AXIOM_TOKEN]: stringValidator,
    [AppSystemProp.AXIOM_DATASET]: stringValidator,
    [AppSystemProp.FRONTEND_URL]: urlValidator,
    [AppSystemProp.MCP_URL]: urlValidator,
    [AppSystemProp.CONTAINER_TYPE]: enumValidator(Object.values(ContainerType)),
    [AppSystemProp.PORT]: numberValidator,
    [AppSystemProp.CONSOLE_API_SECRET_KEY]: stringValidator,
    // AppSystemProp
    [AppSystemProp.API_KEY]: stringValidator,
    [AppSystemProp.TEMPLATES_API_KEY]: stringValidator,
    [AppSystemProp.TEMPLATE_MANAGER_API_KEY]: stringValidator,
    [AppSystemProp.API_RATE_LIMIT_AUTHN_ENABLED]: booleanValidator,
    [AppSystemProp.API_RATE_LIMIT_AUTHN_MAX]: numberValidator,
    [AppSystemProp.API_RATE_LIMIT_AUTHN_WINDOW]: stringValidator,
    [AppSystemProp.API_RATE_LIMIT_EMAIL_CODE_MAX]: numberValidator,
    [AppSystemProp.CLIENT_REAL_IP_HEADER]: stringValidator,
    [AppSystemProp.CLOUD_AUTH_ENABLED]: booleanValidator,
    [AppSystemProp.CONFIG_PATH]: stringValidator,
    [AppSystemProp.DB_TYPE]: enumValidator(Object.values(DatabaseType)),
    [AppSystemProp.DEV_PIECES]: stringValidator,
    [AppSystemProp.ENCRYPTION_KEY]: stringValidator,
    [AppSystemProp.EXECUTION_DATA_RETENTION_DAYS]: numberValidator,
    [AppSystemProp.AUDIT_LOG_RETENTION_DAYS]: auditLogRetentionDaysValidator,
    [AppSystemProp.AUDIT_LOG_RETENTION_PAUSED]: booleanValidator,
    [AppSystemProp.JWT_SECRET]: stringValidator,
    [AppSystemProp.DEFAULT_CONCURRENT_JOBS_LIMIT]: numberValidator,
    [AppSystemProp.PIECES_SYNC_MODE]: enumValidator(Object.values(PieceSyncMode)),
    [AppSystemProp.POSTGRES_DATABASE]: stringValidator,
    [AppSystemProp.POSTGRES_HOST]: stringValidator,
    [AppSystemProp.POSTGRES_PASSWORD]: stringValidator,
    [AppSystemProp.POSTGRES_PORT]: numberValidator,
    [AppSystemProp.POSTGRES_SSL_CA]: stringValidator,
    [AppSystemProp.POSTGRES_URL]: stringValidator,
    [AppSystemProp.POSTGRES_MIGRATION_URL]: stringValidator,
    [AppSystemProp.POSTGRES_USERNAME]: stringValidator,
    [AppSystemProp.POSTGRES_USE_SSL]: booleanValidator,
    [AppSystemProp.POSTGRES_POOL_SIZE]: numberValidator,
    [AppSystemProp.POSTGRES_IDLE_TIMEOUT_MS]: numberValidator,
    [AppSystemProp.PROJECT_RATE_LIMITER_ENABLED]: booleanValidator,
    [AppSystemProp.QUEUE_UI_ENABLED]: booleanValidator,
    [AppSystemProp.QUEUE_UI_PASSWORD]: stringValidator,
    [AppSystemProp.QUEUE_UI_USERNAME]: stringValidator,
    [AppSystemProp.REDIS_TYPE]: enumValidator(Object.values(RedisType)),
    [AppSystemProp.REDIS_FAILED_JOB_RETENTION_DAYS]: numberValidator,
    [AppSystemProp.REDIS_FAILED_JOB_RETENTION_MAX_COUNT]: numberValidator,
    [AppSystemProp.REDIS_SSL_CA_FILE]: stringValidator,
    [AppSystemProp.REDIS_DB]: numberValidator,
    [AppSystemProp.REDIS_HOST]: stringValidator,
    [AppSystemProp.REDIS_PASSWORD]: stringValidator,
    [AppSystemProp.REDIS_PORT]: numberValidator,
    [AppSystemProp.REDIS_URL]: stringValidator,
    [AppSystemProp.REDIS_USER]: stringValidator,
    [AppSystemProp.REDIS_USE_SSL]: booleanValidator,
    [AppSystemProp.REDIS_SENTINEL_ROLE]: stringValidator,
    [AppSystemProp.REDIS_SENTINEL_HOSTS]: stringValidator,
    [AppSystemProp.REDIS_SENTINEL_NAME]: stringValidator,
    [AppSystemProp.USE_CDN_FOR_BUNDLES]: booleanValidator,
    [AppSystemProp.S3_ACCESS_KEY_ID]: stringValidator,
    [AppSystemProp.S3_BUCKET]: stringValidator,
    [AppSystemProp.S3_ENDPOINT]: stringValidator,
    [AppSystemProp.S3_REGION]: stringValidator,
    [AppSystemProp.S3_SECRET_ACCESS_KEY]: stringValidator,
    [AppSystemProp.S3_USE_SIGNED_URLS]: booleanValidator,
    [AppSystemProp.S3_USE_IRSA]: booleanValidator,
    [AppSystemProp.SMTP_HOST]: stringValidator,
    [AppSystemProp.SMTP_PASSWORD]: stringValidator,
    [AppSystemProp.SMTP_PORT]: numberValidator,
    [AppSystemProp.SMTP_SENDER_EMAIL]: (value: string) => value.includes('@') ? true : 'Value must be a valid email address',
    [AppSystemProp.SMTP_SENDER_NAME]: stringValidator,
    [AppSystemProp.SMTP_TLS_REJECT_UNAUTHORIZED]: booleanValidator,
    [AppSystemProp.SMTP_USERNAME]: stringValidator,
    [AppSystemProp.TELEMETRY_ENABLED]: booleanValidator,
    [AppSystemProp.TOOL_SEARCH_ENABLED]: booleanValidator,
    [AppSystemProp.TRIGGER_DEFAULT_POLL_INTERVAL]: numberValidator,
    [AppSystemProp.WEBHOOK_TIMEOUT_SECONDS]: numberValidator,
    [AppSystemProp.ZEROBOUNCE_API_KEY]: stringValidator,
    [AppSystemProp.LOAD_TRANSLATIONS_FOR_DEV_PIECES]: booleanValidator,
    [AppSystemProp.APPSUMO_TOKEN]: stringValidator,
    [AppSystemProp.APOLLO_API_KEY]: stringValidator,
    [AppSystemProp.AUTUMN_CONSOLE_URL]: urlValidator,
    [AppSystemProp.FILE_STORAGE_LOCATION]: enumValidator(Object.values(FileLocation)),
    [AppSystemProp.FIREBASE_ADMIN_CREDENTIALS]: stringValidator,
    [AppSystemProp.FIREBASE_HASH_PARAMETERS]: stringValidator,
    [AppSystemProp.INTERNAL_URL]: stringValidator,
    [AppSystemProp.WORKERS]: numberValidator,
    [AppSystemProp.EDITION]: enumValidator(Object.values(ApEdition)),
    [AppSystemProp.FEATUREBASE_API_KEY]: stringValidator,
    [AppSystemProp.OPENROUTER_PROVISION_KEY]: stringValidator,
    [AppSystemProp.OPENAI_API_KEY]: stringValidator,
    [AppSystemProp.SCIM_DEFAULT_PROJECT_ROLE]: enumValidator(Object.values(DefaultProjectRole)),

    // AppSystemProp
    // Cloud
    [AppSystemProp.GOOGLE_CLIENT_ID]: stringValidator,
    [AppSystemProp.GOOGLE_CLIENT_SECRET]: stringValidator,

    // Cloudflare
    [AppSystemProp.CLOUDFLARE_API_TOKEN]: stringValidator,
    [AppSystemProp.CLOUDFLARE_API_BASE]: stringValidator,
    [AppSystemProp.CLOUDFLARE_SAAS_FALLBACK_ORIGIN]: stringValidator,
    [AppSystemProp.CLOUDFLARE_ZONE_ID]: stringValidator,

    // Tables
    [AppSystemProp.MAX_RECORDS_PER_TABLE]: numberValidator,
    [AppSystemProp.MAX_FIELDS_PER_TABLE]: numberValidator,

    [AppSystemProp.ENABLE_FLOW_ON_PUBLISH]: booleanValidator,
    [AppSystemProp.ENFORCE_CONNECTION_PIECE_BINDING]: booleanValidator,
    [AppSystemProp.ISSUE_ARCHIVE_DAYS]: (value: string) => {
        const days = parseInt(value)
        if (isNaN(days) || days < 0) {
            return 'Value must be a non-negative number'
        }
        return true
    },

    // Webhook payload limits
    [AppSystemProp.MAX_WEBHOOK_PAYLOAD_SIZE_MB]: numberValidator,
    [AppSystemProp.WEBHOOK_PAYLOAD_INLINE_THRESHOLD_KB]: numberValidator,

    // Canary
    [AppSystemProp.CANARY_APP_URL]: urlValidator,
    [AppSystemProp.IS_CANARY_APP]: booleanValidator,
    // SSRF protection
    [AppSystemProp.SSRF_ALLOW_LIST]: stringValidator,

    // Embed
    [AppSystemProp.ALLOWED_EMBED_ORIGINS]: stringValidator,
    [AppSystemProp.NETWORK_MODE]: enumValidator(Object.values(NetworkMode)),

    // On-call
    [AppSystemProp.PAGE_ONCALL_WEBHOOK]: urlValidator,

    // evlog sampling / slow-event threshold
    [AppSystemProp.LOG_SAMPLE_RATE_INFO]: (value: string) => {
        const n = parseInt(value, 10)
        return !isNaN(n) && n >= 0 && n <= 100 ? true : 'Value must be a number between 0 and 100'
    },
    [AppSystemProp.LOG_KEEP_SLOW_MS]: numberValidator,
}



const validateSystemPropTypes = () => {
    const systemProperties: SystemProp[] = [...Object.values(AppSystemProp), ...Object.values(AppSystemProp)]
    const errors: {
        [key in SystemProp]?: string
    } = {}

    for (const prop of systemProperties) {
        const value = system.get(prop)
        const onlyValidateIfValueIsSet = !isNil(value)
        if (onlyValidateIfValueIsSet) {
            const validationResult = systemPropValidators[prop](value)
            if (validationResult !== true) {
                errors[prop] = `Current value: ${value}. Expected: ${validationResult}`
            }
        }
    }
    return errors
}

export const validateEnvPropsOnStartup = async (log: FastifyBaseLogger): Promise<void> => {

    const environment = system.get(AppSystemProp.ENVIRONMENT)
    const fileStorageLocation = process.env.AP_FILE_STORAGE_LOCATION
    
    if (environment !== ApEnvironment.TESTING && fileStorageLocation === FileLocation.S3) {
        try {
            await s3Helper(log).validateS3Configuration()
        }
        catch (error: unknown) {
            throw new Error(JSON.stringify({
                error: inspect(error),
                message: 'S3 validation failed. Check your configuration and credentials.',
                docUrl: 'https://www.activepieces.com/docs/install/configuration/overview#configure-s3-optional',
            }))
        }
    }

    const errors = validateSystemPropTypes()
    if (Object.keys(errors).length > 0) {
        log.warn({
            errors,
        }, '[validateEnvPropsOnStartup]')
    }

    const codeSandboxType = process.env.AP_CODE_SANDBOX_TYPE
    if (!isNil(codeSandboxType)) {
        throw new Error(JSON.stringify({
            message: 'AP_CODE_SANDBOX_TYPE is deprecated, please use AP_EXECUTION_MODE instead',
            docUrl: 'https://www.activepieces.com/docs/install/configuration/overview',
        }))
    }
    const encryptionKey = await encryptUtils.getEncryptionKey()
    const isValidHexKey = encryptionKey && /^[A-Za-z0-9]{32}$/.test(encryptionKey)
    if (!isValidHexKey) {
        throw new Error(JSON.stringify({
            message: 'AP_ENCRYPTION_KEY is missing or invalid. It must be a 32-character hexadecimal string (representing 16 bytes). You can generate one using the command: `openssl rand -hex 16`',
            docUrl: 'https://www.activepieces.com/docs/install/configuration/environment-variables',
        }))
    }
    const isApp = system.isApp()
    if (isApp) {
        const rentionPeriod = system.getNumberOrThrow(AppSystemProp.EXECUTION_DATA_RETENTION_DAYS)
        const maximumPausedFlowTimeout = system.getNumberOrThrow(AppSystemProp.PAUSED_FLOW_TIMEOUT_DAYS)
        if (maximumPausedFlowTimeout > rentionPeriod) {
            throw new Error(JSON.stringify({
                message: 'AP_PAUSED_FLOW_TIMEOUT_DAYS can not exceed AP_EXECUTION_DATA_RETENTION_DAYS',
            }))
        }
    }

    const jwtSecret = await jwtUtils.getJwtSecret()
    if (isNil(jwtSecret)) {
        throw new Error(JSON.stringify({
            message: 'AP_JWT_SECRET is undefined, please define it in the environment variables',
            docUrl: 'https://www.activepieces.com/docs/install/configuration/environment-variables',
        }))
    }

    const edition = system.getEdition()
    if ([ApEdition.CLOUD, ApEdition.ENTERPRISE].includes(edition) && environment === ApEnvironment.PRODUCTION) {
        const executionMode = system.getOrThrow<ExecutionMode>(AppSystemProp.EXECUTION_MODE)
        if (![ExecutionMode.SANDBOX_PROCESS, ExecutionMode.SANDBOX_CODE_ONLY, ExecutionMode.SANDBOX_CODE_AND_PROCESS].includes(executionMode)) {
            throw new Error(JSON.stringify({
                message: `Execution mode ${executionMode} is no longer supported in this edition, check the documentation for recent changes`,
                docUrl: 'https://www.activepieces.com/docs/install/configuration/overview',
            }))
        }
    }

    assertMcpUrlHasDistinctHost()
}