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

fix(templates): Explore and Automations load on self-hosted instances without internet access (#16480)

Bartosz Majewskif5fd5d7496bd
diff --git a/brain/knowledge/flows-execution/templates.md b/brain/knowledge/flows-execution/templates.md
index 93e7fb74e31..f313334cb61 100644
--- a/brain/knowledge/flows-execution/templates.md
+++ b/brain/knowledge/flows-execution/templates.md
@@ -24,6 +24,7 @@ Templates are a library of reusable flow (and table) blueprints users can browse
- Flow version migration (`migrateFlowVersionTemplateList`) runs as a `preValidation` hook on create/update to handle schema evolution in stored flows.
- An update only replaces a template's workflow when it carries a non-empty `flows`. The update `preValidation` turns an absent `flows` into `[]`, so `templateService.update` treats an empty list as "keep what's stored": it validates and prepares flows (against the template's own platform) only when some are sent, then hands those prepared `flows` and `pieces` to `platformTemplateService.update` for CUSTOM. Before GIT-1834 the CUSTOM branch got the raw request instead, so a details-only edit wrote `flows: []` over the export, skipped validation, and took `pieces` from the first flow only.
- `pieces` and `categories` are denormalized + indexed for fast filtering.
+- **On CE/EE the official templates are optional data fetched live from `cloud.activepieces.com` on every request.** Air-gapped and egress-restricted installs are supported, so every `communityTemplates` call goes through `fetchFromCloud` (`safeHttp.axios`, 10 s timeout) and degrades to "none" on any failure: categories `null`, list `[]`, detail 404. A call that throws turns Explore and Automations into a 500 and hides the platform's custom templates too (ENG-829). If DNS sends the host to a private IP (an internal egress proxy), the SSRF filter blocks it until the IP is added to `AP_SSRF_ALLOW_LIST`.
- **A flow export (`GET /v1/flows/:id/template`) keeps its connection refs.** `removeConnectionsFromInput` in `flow-version.service.ts` strips only the old `{{connections.x}}` dot syntax, and the builder has written `{{connections['x']}}` since 2023. Import keeps the ref, so a flow re-imported where a connection with that External ID exists just works, and users rely on it. Fixing the regex is a functional breaking change, not a cleanup (GIT-1957).
- **`POST /v1/templates-telemetry/event` serves two callers, and only one of them is the relay — do not gate it on edition or skip the gate.** It is `securityAccess.public()`, and the local browser posts to it directly for VIEW, INSTALL and EXPLORE_VIEW (`packages/web/src/features/templates/api/templates-telemetry-api.ts`, four call sites); a self-hosted instance *also* posts to Cloud's copy of the same route via `sendToCloud`. Those three event types travel **only** this path, so treating the route as "the relay hop, already consented upstream" silently un-gates them for every opted-out self-hoster. The controller therefore resolves `platformUtils.getPlatformIdForRequest` and the service gates on that platform's row when one resolves; a null platform means the genuine relay case (a Cloud request with no principal, whose `projectId`/`templateId` belong to the sending deployment's database) and is forwarded. ACTIVATE/DEACTIVATE come from `trigger-source-service` with a real `projectId` and are gated through the project. Also note `sendToCloud`/`sendToInternal` use raw `fetch`, not `safeHttp`/`apAxios` — pre-existing, and only sound because both URLs are hardcoded.
diff --git a/packages/server/api/src/app/template/community-templates.service.ts b/packages/server/api/src/app/template/community-templates.service.ts
index 15cd2aef2ba..95150af158d 100644
--- a/packages/server/api/src/app/template/community-templates.service.ts
+++ b/packages/server/api/src/app/template/community-templates.service.ts
@@ -1,55 +1,28 @@
-import { ActivepiecesError, ErrorCode, isNil, SeekPage } from '@activepieces/core-utils'
-import { ListTemplatesRequestQuery, Template } from '@activepieces/shared'
+import { isNil, SeekPage, tryCatch } from '@activepieces/core-utils'
+import { safeHttp } from '@activepieces/server-utils'
+import { Flag, ListTemplatesRequestQuery, Template } from '@activepieces/shared'
+import { FastifyBaseLogger } from 'fastify'
const TEMPLATES_SOURCE_URL = 'https://cloud.activepieces.com/api/v1/templates'
+const TEMPLATES_SOURCE_TIMEOUT_MS = 10_000
-export const communityTemplates = {
- getOrThrow: async (id: string): Promise<Template> => {
- const url = `${TEMPLATES_SOURCE_URL}/${id}`
- const response = await fetch(url, {
- method: 'GET',
- headers: {
- 'Content-Type': 'application/json',
- },
- })
- if (!response.ok) {
- throw new ActivepiecesError({
- code: ErrorCode.ENTITY_NOT_FOUND,
- params: {
- entityType: 'template',
- entityId: id,
- message: `Template ${id} not found`,
- },
- })
- }
- const template = await response.json()
- return template
- },
- getCategories: async (): Promise<string[]> => {
- const url = `${TEMPLATES_SOURCE_URL}/categories`
- const response = await fetch(url, {
- method: 'GET',
- headers: {
- 'Content-Type': 'application/json',
- },
- })
- const categories = await response.json()
- return categories
+export const communityTemplates = (log: FastifyBaseLogger) => ({
+ getOne: (id: string): Promise<Template | null> => fetchFromCloud({ url: `${TEMPLATES_SOURCE_URL}/${encodeURIComponent(id)}`, log }),
+ getCategories: (): Promise<Flag | null> => fetchFromCloud({ url: `${TEMPLATES_SOURCE_URL}/categories`, log }),
+ list: async (request: ListTemplatesRequestQuery): Promise<Template[]> => {
+ const templates = await fetchFromCloud<SeekPage<Template>>({ url: `${TEMPLATES_SOURCE_URL}?${convertToQueryString(request)}`, log })
+ return templates?.data ?? []
},
- list: async (request: ListTemplatesRequestQuery): Promise<SeekPage<Template>> => {
- const queryString = convertToQueryString(request)
- const url = `${TEMPLATES_SOURCE_URL}?${queryString}`
- const response = await fetch(url, {
- method: 'GET',
- headers: {
- 'Content-Type': 'application/json',
- },
- })
- const templates = await response.json()
- return templates
- },
-}
+})
+async function fetchFromCloud<T>({ url, log }: { url: string, log: FastifyBaseLogger }): Promise<T | null> {
+ const { data: response, error } = await tryCatch(() => safeHttp.axios.get<T>(url, { timeout: TEMPLATES_SOURCE_TIMEOUT_MS }))
+ if (error) {
+ log.warn({ error, url }, 'Failed to fetch templates from cloud')
+ return null
+ }
+ return response.data
+}
function convertToQueryString(params: ListTemplatesRequestQuery): string {
const searchParams = new URLSearchParams()
diff --git a/packages/server/api/src/app/template/template.controller.ts b/packages/server/api/src/app/template/template.controller.ts
index 2c87e5f79ff..ecb19ab3be1 100644
--- a/packages/server/api/src/app/template/template.controller.ts
+++ b/packages/server/api/src/app/template/template.controller.ts
@@ -21,8 +21,9 @@ export const templateController: FastifyPluginAsyncZod = async (app) => {
if (!isNil(template)) {
return template
}
- if (edition !== ApEdition.CLOUD) {
- return communityTemplates.getOrThrow(request.params.id)
+ const communityTemplate = edition === ApEdition.CLOUD ? null : await communityTemplates(request.log).getOne(request.params.id)
+ if (!isNil(communityTemplate)) {
+ return communityTemplate
}
throw new ActivepiecesError({
code: ErrorCode.ENTITY_NOT_FOUND,
@@ -38,7 +39,7 @@ export const templateController: FastifyPluginAsyncZod = async (app) => {
if (edition === ApEdition.CLOUD) {
return flagService(request.log).getOne(ApFlagId.TEMPLATES_CATEGORIES)
}
- return communityTemplates.getCategories()
+ return communityTemplates(request.log).getCategories()
})
app.get('/', ListTemplatesParams, async (request) => {
@@ -244,8 +245,7 @@ async function loadOfficialTemplatesOrReturnEmpty(
})
return officialTemplatesFromCloud.data
}
- const loadTemplatesFromCloud = await communityTemplates.list({ ...query, type: TemplateType.OFFICIAL })
- return loadTemplatesFromCloud.data
+ return communityTemplates(log).list({ ...query, type: TemplateType.OFFICIAL })
}
async function loadCustomTemplatesOrReturnEmpty(
diff --git a/packages/server/api/test/integration/ee/template/templates-offline.test.ts b/packages/server/api/test/integration/ee/template/templates-offline.test.ts
new file mode 100644
index 00000000000..7a1eaa517d6
--- /dev/null
+++ b/packages/server/api/test/integration/ee/template/templates-offline.test.ts
@@ -0,0 +1,80 @@
+import { safeHttp } from '@activepieces/server-utils'
+import { TemplateType } from '@activepieces/shared'
+import { AxiosError } from 'axios'
+import { FastifyInstance } from 'fastify'
+import { StatusCodes } from 'http-status-codes'
+import { db } from '../../../helpers/db'
+import { createMockTemplate } from '../../../helpers/mocks'
+import { createTestContext } from '../../../helpers/test-context'
+import { setupTestEnvironment, teardownTestEnvironment } from '../../../helpers/test-setup'
+
+let app: FastifyInstance
+
+beforeAll(async () => {
+ app = await setupTestEnvironment()
+})
+
+afterAll(async () => {
+ await teardownTestEnvironment()
+})
+
+afterEach(() => {
+ vi.restoreAllMocks()
+})
+
+describe('Templates when cloud.activepieces.com is unreachable', () => {
+ beforeEach(() => {
+ vi.spyOn(safeHttp.axios, 'get').mockRejectedValue(new AxiosError('getaddrinfo ENOTFOUND cloud.activepieces.com', 'ENOTFOUND'))
+ })
+
+ it('GET /v1/templates/categories answers 200', async () => {
+ const ctx = await createTestContext(app)
+
+ const response = await ctx.get('/v1/templates/categories')
+
+ expect(response.statusCode).toBe(StatusCodes.OK)
+ })
+
+ it('GET /v1/templates still lists the platform\'s own templates', async () => {
+ const ctx = await createTestContext(app, { plan: { manageTemplatesEnabled: true } })
+ const customTemplate = createMockTemplate({ platformId: ctx.platform.id, type: TemplateType.CUSTOM })
+ await db.save('template', customTemplate)
+
+ const response = await ctx.get('/v1/templates')
+
+ expect(response.statusCode).toBe(StatusCodes.OK)
+ expect(response.json().data.map((template: { id: string }) => template.id)).toEqual([customTemplate.id])
+ })
+
+ it('GET /v1/templates/:id answers 404', async () => {
+ const ctx = await createTestContext(app)
+
+ const response = await ctx.get('/v1/templates/unknown-template')
+
+ expect(response.statusCode).toBe(StatusCodes.NOT_FOUND)
+ })
+})
+
+describe('Templates when cloud.activepieces.com is reachable', () => {
+ it('GET /v1/templates merges the official templates with the platform\'s own', async () => {
+ const ctx = await createTestContext(app, { plan: { manageTemplatesEnabled: true } })
+ const customTemplate = createMockTemplate({ platformId: ctx.platform.id, type: TemplateType.CUSTOM })
+ await db.save('template', customTemplate)
+ const officialTemplate = createMockTemplate({ type: TemplateType.OFFICIAL })
+ vi.spyOn(safeHttp.axios, 'get').mockResolvedValue({ data: { data: [officialTemplate], next: null, previous: null } })
+
+ const response = await ctx.get('/v1/templates')
+
+ expect(response.statusCode).toBe(StatusCodes.OK)
+ expect(response.json().data.map((template: { id: string }) => template.id)).toEqual([officialTemplate.id, customTemplate.id])
+ })
+
+ it('GET /v1/templates/:id calls the cloud with a timeout and keeps the id inside the templates path', async () => {
+ const ctx = await createTestContext(app)
+ const get = vi.spyOn(safeHttp.axios, 'get').mockRejectedValue(new AxiosError('Request failed with status code 404', 'ERR_BAD_REQUEST'))
+
+ await ctx.get('/v1/templates/..%2F..%2Fflags')
+
+ expect(get).toHaveBeenCalledWith('https://cloud.activepieces.com/api/v1/templates/..%2F..%2Fflags', { timeout: expect.any(Number) })
+ })
+})