diff --git a/app/common/gristUrls.ts b/app/common/gristUrls.ts index 8779d23a..048e3504 100644 --- a/app/common/gristUrls.ts +++ b/app/common/gristUrls.ts @@ -185,12 +185,20 @@ export interface OrgUrlInfo { orgInPath?: string; // If /o/{orgInPath} should be used to access the requested org. } -function isDocInternalUrl(host: string) { - if (!process.env.APP_DOC_INTERNAL_URL) { return false; } - const internalUrl = new URL('/', process.env.APP_DOC_INTERNAL_URL); +function isInternalUrl(host: string, envValue?: string) { + if (!envValue) { return false; } + const internalUrl = new URL('/', envValue); return internalUrl.host === host; } +function isDocInternalUrl(host: string) { + return isInternalUrl(host, process.env.APP_DOC_INTERNAL_URL); +} + +function isHomeInternalUrl(host: string) { + return isInternalUrl(host, process.env.APP_HOME_INTERNAL_URL); +} + /** * Given host (optionally with port), baseDomain, and pluginUrl, determine whether to interpret host * as a custom domain, a native domain, or a plugin domain. @@ -208,7 +216,12 @@ export function getHostType(host: string, options: { const hostname = host.split(":")[0]; if (!options.baseDomain) { return 'native'; } - if (hostname === 'localhost' || isDocInternalUrl(host) || hostname.endsWith(options.baseDomain)) { + if ( + hostname === 'localhost' || + isDocInternalUrl(host) || + hostname.endsWith(options.baseDomain) || + isHomeInternalUrl(host) + ) { return 'native'; } return 'custom'; diff --git a/app/server/lib/BootProbes.ts b/app/server/lib/BootProbes.ts index 7cddb99f..e0ed47fe 100644 --- a/app/server/lib/BootProbes.ts +++ b/app/server/lib/BootProbes.ts @@ -77,7 +77,7 @@ const _homeUrlReachableProbe: Probe = { id: 'reachable', name: 'Grist is reachable', apply: async (server, req) => { - const url = server.getHomeUrl(req); + const url = server.getHomeInternalUrl(req); try { const resp = await fetch(url); if (resp.status !== 200) { @@ -102,7 +102,7 @@ const _statusCheckProbe: Probe = { id: 'health-check', name: 'Built-in Health check', apply: async (server, req) => { - const baseUrl = server.getHomeUrl(req); + const baseUrl = server.getHomeInternalUrl(req); const url = new URL(baseUrl); url.pathname = removeTrailingSlash(url.pathname) + '/status'; try { diff --git a/app/server/lib/DocApi.ts b/app/server/lib/DocApi.ts index 95d54540..5eb391f2 100644 --- a/app/server/lib/DocApi.ts +++ b/app/server/lib/DocApi.ts @@ -1098,7 +1098,8 @@ export class DocWorkerApi { if (req.body.sourceDocId) { options.sourceDocId = await this._confirmDocIdForRead(req, String(req.body.sourceDocId)); // Make sure that if we wanted to download the full source, we would be allowed. - const result = await fetch(this._grist.getHomeUrl(req, `/api/docs/${options.sourceDocId}/download?dryrun=1`), { + const homeUrl = this._grist.getHomeInternalUrl(req, `/api/docs/${options.sourceDocId}/download?dryrun=1`); + const result = await fetch(homeUrl, { method: 'GET', headers: { ...getTransitiveHeaders(req), @@ -1111,7 +1112,7 @@ export class DocWorkerApi { } // We should make sure the source document has flushed recently. // It may not be served by the same worker, so work through the api. - await fetch(this._grist.getHomeUrl(req, `/api/docs/${options.sourceDocId}/flush`), { + await fetch(this._grist.getHomeInternalUrl(req, `/api/docs/${options.sourceDocId}/flush`), { method: 'POST', headers: { ...getTransitiveHeaders(req), @@ -1170,7 +1171,7 @@ export class DocWorkerApi { const showDetails = isAffirmative(req.query.detail); const docSession = docSessionFromRequest(req); const {states} = await this._getStates(docSession, activeDoc); - const ref = await fetch(this._grist.getHomeUrl(req, `/api/docs/${req.params.docId2}/states`), { + const ref = await fetch(this._grist.getHomeInternalUrl(req, `/api/docs/${req.params.docId2}/states`), { headers: { ...getTransitiveHeaders(req), 'Content-Type': 'application/json', @@ -1199,7 +1200,7 @@ export class DocWorkerApi { // Calculate changes from the (common) parent to the current version of the other document. const url = `/api/docs/${req.params.docId2}/compare?left=${parent.h}`; - const rightChangesReq = await fetch(this._grist.getHomeUrl(req, url), { + const rightChangesReq = await fetch(this._grist.getHomeInternalUrl(req, url), { headers: { ...getTransitiveHeaders(req), 'Content-Type': 'application/json', diff --git a/app/server/lib/FlexServer.ts b/app/server/lib/FlexServer.ts index ec623e1b..2896eb47 100644 --- a/app/server/lib/FlexServer.ts +++ b/app/server/lib/FlexServer.ts @@ -330,7 +330,7 @@ export class FlexServer implements GristServer { * based on domain). */ public async getHomeUrlByDocId(docId: string, relPath: string = ''): Promise { - return new URL(relPath, this.getDefaultHomeUrl()).href; + return new URL(relPath, this.getDefaultHomeInternalUrl()).href; } // Get the port number the server listens on. This may be different from the port @@ -1448,7 +1448,7 @@ export class FlexServer implements GristServer { const permitStore = this.getPermitStore(); const notifier = this.getNotifier(); const loginSystem = await this.resolveLoginSystem(); - const homeUrl = this.getHomeUrl(req).replace(/\/$/, ''); + const homeUrl = this.getHomeInternalUrl(req).replace(/\/$/, ''); return new Doom(dbManager, permitStore, notifier, loginSystem, homeUrl); }; @@ -2432,7 +2432,7 @@ export class FlexServer implements GristServer { const workspace = workspaces.find(w => w.name === 'Home'); if (!workspace) { throw new Error('Home workspace not found'); } - const copyDocUrl = this.getHomeUrl(req, '/api/docs'); + const copyDocUrl = this.getHomeInternalUrl(req, '/api/docs'); const response = await fetch(copyDocUrl, { headers: { ...getTransitiveHeaders(req), diff --git a/app/server/lib/uploads.ts b/app/server/lib/uploads.ts index f4170da2..ce89238a 100644 --- a/app/server/lib/uploads.ts +++ b/app/server/lib/uploads.ts @@ -417,7 +417,7 @@ export async function fetchDoc(server: GristServer, docId: string, req: Request, // Find the doc worker responsible for the document we wish to copy. // The backend needs to be well configured for this to work. - const homeUrl = server.getHomeUrl(req); + const homeUrl = server.getHomeInternalUrl(req); const fetchUrl = new URL(`/api/worker/${docId}`, homeUrl); const response: FetchResponse = await Deps.fetch(fetchUrl.href, {headers}); await _checkForError(response);