From 1fa9f13e1565665a8aa082d647a7e2c3d53d84e2 Mon Sep 17 00:00:00 2001 From: Florent FAYOLLE Date: Tue, 31 Oct 2023 11:03:09 +0100 Subject: [PATCH] getHostType: consider APP_DOC_INTERNAL_URL as native --- README.md | 1 + app/common/gristUrls.ts | 13 +++++++++++-- test/common/gristUrls.ts | 42 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 8dead7d679..30a1cd6866 100644 --- a/README.md +++ b/README.md @@ -250,6 +250,7 @@ Variable | Purpose -------- | ------- ALLOWED_WEBHOOK_DOMAINS | comma-separated list of permitted domains to use in webhooks (e.g. webhook.site,zapier.com). You can set this to `*` to allow all domains, but if doing so, we recommend using a carefully locked-down proxy (see `GRIST_HTTPS_PROXY`) if you do not entirely trust users. Otherwise services on your internal network may become vulnerable to manipulation. APP_DOC_URL | doc worker url, set when starting an individual doc worker (other servers will find doc worker urls via redis) +APP_DOC_INTERNAL_URL | like `APP_DOC_URL` but used by the home server to join the server using an internal domain name resolution (like in a docker environment). Defaults to APP_DOC_URL APP_HOME_URL | url prefix for home api (home and doc servers need this) APP_STATIC_URL | url prefix for static resources APP_STATIC_INCLUDE_CUSTOM_CSS | set to "true" to include custom.css (from APP_STATIC_URL) in static pages diff --git a/app/common/gristUrls.ts b/app/common/gristUrls.ts index 73cc8fdda5..c3a080f870 100644 --- a/app/common/gristUrls.ts +++ b/app/common/gristUrls.ts @@ -163,6 +163,13 @@ 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); + const internalHostnameAndMaybePort = internalUrl.hostname + (internalUrl.port ? `:${internalUrl.port}` : ''); + return internalHostnameAndMaybePort === host; +} + /** * Given host (optionally with port), baseDomain, and pluginUrl, determine whether to interpret host * as a custom domain, a native domain, or a plugin domain. @@ -180,8 +187,10 @@ export function getHostType(host: string, options: { const hostname = host.split(":")[0]; if (!options.baseDomain) { return 'native'; } - if (hostname !== 'localhost' && !hostname.endsWith(options.baseDomain)) { return 'custom'; } - return 'native'; + if (hostname === 'localhost' || isDocInternalUrl(host) || hostname.endsWith(options.baseDomain)) { + return 'native'; + } + return 'custom'; } export function getOrgUrlInfo(newOrg: string, currentHost: string, options: OrgUrlOptions): OrgUrlInfo { diff --git a/test/common/gristUrls.ts b/test/common/gristUrls.ts index 54e09b00b7..26a44b6917 100644 --- a/test/common/gristUrls.ts +++ b/test/common/gristUrls.ts @@ -1,4 +1,4 @@ -import {decodeUrl, IGristUrlState, parseFirstUrlPart} from 'app/common/gristUrls'; +import {decodeUrl, getHostType, IGristUrlState, parseFirstUrlPart} from 'app/common/gristUrls'; import {assert} from 'chai'; describe('gristUrls', function() { @@ -64,4 +64,44 @@ describe('gristUrls', function() { assert.deepEqual(parseFirstUrlPart('o', ''), {path: ''}); }); }); + + describe('getHostType', function() { + const defaultOptions = { + baseDomain: 'getgrist.com', + pluginUrl: 'https://plugin.getgrist.com/path', + }; + afterEach(function () { + delete process.env.APP_DOC_INTERNAL_URL; + }); + it('should interpret localhost as "native"', function() { + assert.equal(getHostType('localhost', defaultOptions), 'native'); + assert.equal(getHostType('localhost:8080', defaultOptions), 'native'); + }); + it('should interpret base domain as "native"', function() { + assert.equal(getHostType('getgrist.com', defaultOptions), 'native'); + assert.equal(getHostType('www.getgrist.com', defaultOptions), 'native'); + assert.equal(getHostType('foo.getgrist.com', defaultOptions), 'native'); + assert.equal(getHostType('foo.getgrist.com:8080', defaultOptions), 'native'); + }); + it('should interpret plugin domain as "plugin"', function() { + assert.equal(getHostType('plugin.getgrist.com', defaultOptions), 'plugin'); + assert.equal(getHostType('PLUGIN.getgrist.com', { pluginUrl: 'https://pLuGin.getgrist.com' }), 'plugin'); + }); + it('should interpret other domains as "custom"', function() { + assert.equal(getHostType('foo.com', defaultOptions), 'custom'); + assert.equal(getHostType('foo.bar.com', defaultOptions), 'custom'); + }); + it('should interpret doc internal url as "native"', function() { + process.env.APP_DOC_INTERNAL_URL = 'https://doc-worker-123.internal/path'; + assert.equal(getHostType('doc-worker-123.internal', defaultOptions), 'native'); + assert.equal(getHostType('doc-worker-123.internal:8080', defaultOptions), 'custom'); + assert.equal(getHostType('doc-worker-124.internal', defaultOptions), 'custom'); + + process.env.APP_DOC_INTERNAL_URL = 'https://doc-worker-123.internal:8080/path'; + assert.equal(getHostType('doc-worker-123.internal:8080', defaultOptions), 'native'); + assert.equal(getHostType('doc-worker-123.internal', defaultOptions), 'custom'); + assert.equal(getHostType('doc-worker-124.internal:8080', defaultOptions), 'custom'); + assert.equal(getHostType('doc-worker-123.internal:8079', defaultOptions), 'custom'); + }); + }); });