Skip to content

Commit

Permalink
getHostType: consider APP_DOC_INTERNAL_URL as native
Browse files Browse the repository at this point in the history
  • Loading branch information
Florent FAYOLLE committed Oct 31, 2023
1 parent b63b042 commit 1fa9f13
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 11 additions & 2 deletions app/common/gristUrls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand Down
42 changes: 41 additions & 1 deletion test/common/gristUrls.ts
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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');
});
});
});

0 comments on commit 1fa9f13

Please sign in to comment.