gristlabs / grist-core

Grist is the evolution of spreadsheets.
https://www.getgrist.com
Apache License 2.0
7.28k stars 322 forks source link

The frontend tries to access API through local IP address instead of APP_HOME_URL/APP_DOC_URL #228

Open ccll opened 2 years ago

ccll commented 2 years ago

I'm deploying grist in a k8s cluster.

According to various comments I found in related Github issues, the following env vars were set:

export DEBUG=1
export GRIST_SINGLE_ORG=docs
export APP_DOC_URL=https://my.domain.tld
export APP_HOME_URL=https://my.domain.tld

The logs of backend show that the backend is listening on the right k8s internal IP address. image

But when the page is loaded in the browser, the frontend is also accessing the API endpoints throught the same k8s internal IP address, instead of https://my.domain.tld. image

Full logs:

$ yarn start
yarn run v1.22.19
$ sandbox/watch.sh
+ PROJECT=
+ export GRIST_EXT=stubs
+ GRIST_EXT=stubs
+ [[ -e ext/app ]]
+ '[' '!' -e _build ']'
+ tsc --build -w --preserveWatchOutput
+ webpack --config buildtools/webpack.config.js --mode development --watch
+ wait
+ NODE_PATH=_build:_build/stubs:_build/ext
+ nodemon --delay 1 -w _build/app/server -w _build/app/common _build/stubs/app/server/server.js
+ catw app/client/app.css app/client/logo.css app/client/components/AceEditor.css app/client/components/ActionLog.css app/client/components/ChartView.css app/client/components/Clipboard.css app/client/components/CodeEditorPanel.css app/client/components/ColumnFilters.css app/client/components/commands.css app/client/components/CustomView.css app/client/components/DetailView.css app/client/components/EmbedForm.css app/client/components/FieldConfigTab.css app/client/components/GridView.css app/client/components/GristDoc.css app/client/components/Layout.css app/client/components/LayoutEditor.css app/client/components/LayoutPreview.css app/client/components/Login.css app/client/components/Preferences.css app/client/components/Printing.css app/client/components/RecordLayout.css app/client/components/SearchBar.css app/client/components/ValidationPanel.css app/client/components/viewCommon.css app/client/components/ViewConfigTab.css app/client/components/ViewLayout.css app/client/components/ViewLinker.css app/client/lib/koDomScrolly.css app/client/lib/koForm.css app/client/lib/multiselect.css app/client/lib/SafeBrowserProcess.css app/client/ui/App.css app/client/widgets/AttachmentsWidget.css app/client/widgets/CheckBox.css app/client/widgets/DateTimeEditor.css app/client/widgets/FieldBuilder.css app/client/widgets/Reference.css app/client/widgets/Spinner.css app/client/widgets/Switch.css app/client/widgets/TextBox.css app/client/widgets/TextEditor.css -o static/bundle.css -v
[nodemon] 2.0.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): _build/app/server/**/* _build/app/common/**/*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node _build/stubs/app/server/server.js`
75918 bytes written to static/bundle.css
[3:32:00 AM] Starting compilation in watch mode...

[3:32:00 AM] Found 0 errors. Watching for file changes.

Welcome to Grist.
2022-07-27 03:32:02.319 - info: == Grist version is 0.1.1 (commit unknown)
2022-07-27 03:32:02.324 - info: Loading empty config because /workspace/grist-core/config.json missing
2022-07-27 03:32:02.343 - warn: did not find an appropriately named example workspace in deployment
2022-07-27 03:32:02.347 - info: No plugins directory: ENOENT: no such file or directory, scandir '/workspace/grist-core/.grist/plugins'
2022-07-27 03:32:02.358 - info: Found 1 valid plugins on the system
2022-07-27 03:32:02.358 - debug: PLUGIN builtIn/core -- /workspace/grist-core/plugins/core
2022-07-27 03:32:02.368 - info: Server timeouts: keepAliveTimeout 305000 headersTimeout 306000
2022-07-27 03:32:02.373 - info: server(home,docs,static) available at 0.0.0.0:8484
2022-07-27 03:32:02.403 - info: activity docCount=0, orgCount=1, orgInGoodStandingCount=1, userCount=6, userWithLoginCount=6
2022-07-27 03:32:02.408 - warn: Failed to create GoogleAuth endpoint: GOOGLE_CLIENT_SECRET is not defined
2022-07-27 03:32:02.416 - info: == appRoot: /workspace/grist-core
2022-07-27 03:32:02.416 - info: == docsRoot: /workspace/grist-core/docs
2022-07-27 03:32:02.416 - info: == defaultBaseDomain: <my.domain.tld>
2022-07-27 03:32:02.417 - info: == pluginUrl: undefined
2022-07-27 03:32:02.417 - info: == instanceRoot: /workspace/grist-core
2022-07-27 03:32:02.417 - info: == tag: unknown
2022-07-27 03:32:02.417 - info: == database: sqlite:///workspace/grist-core/landing.db
2022-07-27 03:32:02.417 - info: == userRoot: /workspace/grist-core/.grist
2022-07-27 03:32:02.417 - info: == loginMiddlewareComment: no-logins
2022-07-27 03:32:02.417 - info: == docWorkerId: testDocWorkerId_8484
2022-07-27 03:32:02.418 - info: == grist.login.system.forwardAuth.header: - [GRIST_FORWARD_AUTH_HEADER]
2022-07-27 03:32:02.418 - info: == grist.login.skipSession: - [GRIST_IGNORE_SESSION]
2022-07-27 03:32:02.418 - info: == grist.login.forced: - [GRIST_FORCE_LOGIN]
2022-07-27 03:32:02.418 - info: == grist.externalStorage.disable: - [GRIST_DISABLE_S3]
2022-07-27 03:32:02.419 - info: == grist.externalStorage.active: false
2022-07-27 03:32:03.588 - debug: Auth[GET]: 10.42.132.154:8484 / customHostSession=, method=GET, host=10.42.132.154:8484, path=/, org=docs, email=admin@grist.local, userId=6, altSessionId=35ovWtJQAo9jNCb5soes8d
2022-07-27 03:32:03.606 10.42.132.154:8484 GET / 200 19.078 ms - 3469
2022-07-27 03:32:03.690 10.42.132.154:8484 GET /v/unknown/jqueryui/themes/smoothness/jquery-ui.css 304 5.725 ms - -
2022-07-27 03:32:03.700 10.42.132.154:8484 GET /v/unknown/bootstrap/dist/css/bootstrap.min.css 304 1.537 ms - -
2022-07-27 03:32:03.710 10.42.132.154:8484 GET /v/unknown/bundle.css 200 6.226 ms - 75918
2022-07-27 03:32:03.716 10.42.132.154:8484 GET /v/unknown/jqueryui/jquery-ui.min.js 304 8.682 ms - -
2022-07-27 03:32:03.727 10.42.132.154:8484 GET /v/unknown/hljs.default.css 304 9.467 ms - -
2022-07-27 03:32:03.728 10.42.132.154:8484 GET /v/unknown/main.bundle.js 304 8.491 ms - -
2022-07-27 03:32:03.729 10.42.132.154:8484 GET /v/unknown/browser-check.js 304 8.144 ms - -
2022-07-27 03:32:03.731 10.42.132.154:8484 GET /v/unknown/icons/icons.css 304 6.719 ms - -
2022-07-27 03:32:03.732 10.42.132.154:8484 GET /v/unknown/bootstrap/dist/js/bootstrap.min.js 304 13.699 ms - -
2022-07-27 03:32:03.734 10.42.132.154:8484 GET /v/unknown/jquery/dist/jquery.min.js 304 10.808 ms - -
2022-07-27 03:32:03.735 10.42.132.154:8484 GET /v/unknown/bootstrap-datepicker/dist/css/bootstrap-datepicker3.min.css 304 9.767 ms - -
2022-07-27 03:32:03.736 10.42.132.154:8484 GET /v/unknown/bootstrap-datepicker/dist/js/bootstrap-datepicker.min.js 304 9.825 ms - -
2022-07-27 03:32:03.783 10.42.132.154:8484 GET /v/unknown/img/gplaypattern.png 304 1.010 ms - -
assets by chunk 4.55 MiB (id hint: vendors)
  asset vendors-node_modules_plotly_js-basic-dist_plotly-basic_js.bundle.js 2.34 MiB [compared for emit] (id hint: vendors) 1 related asset
  asset vendors-node_modules_gristlabs_moment-guess_dist_bundle_js-node_modules_popperjs_core_lib_pop-f82b79.bundle.js 1.83 MiB [compared for emit] (id hint: vendors) 1 related asset
  asset vendors-node_modules_moment-timezone_index_js.bundle.js 190 KiB [compared for emit] (id hint: vendors) 1 related asset
  asset vendors-node_modules_moment_moment_js.bundle.js 144 KiB [compared for emit] (id hint: vendors) 1 related asset
  asset vendors-node_modules_highlight_js_lib_highlight_js-node_modules_highlight_js_lib_languages_py-5c6e70.bundle.js 49.9 KiB [compared for emit] (id hint: vendors) 1 related asset
+ 11 assets
runtime modules 25.2 KiB 39 modules
modules by path ./node_modules/ 5.12 MiB
  javascript modules 4.82 MiB 588 modules
  json modules 312 KiB
    ./node_modules/moment-timezone/data/packed/latest.json 174 KiB [built] [code generated]
    ./node_modules/mime-db/db.json 137 KiB [built] [code generated]
modules by path ./app/ 1.67 MiB
  modules by path ./app/client/ 1.48 MiB 293 modules
  modules by path ./app/common/ 178 KiB 57 modules
  modules by path ./app/plugin/*.ts 13 KiB 14 modules
modules by path ./stubs/app/ 502 bytes
  modules by path ./stubs/app/client/ui/*.ts 274 bytes 3 modules
  ./stubs/app/common/version.ts 99 bytes [built] [code generated]
  ./stubs/app/client/components/Banners.ts 129 bytes [built] [code generated]
webpack 5.73.0 compiled successfully in 9732 ms
2022-07-27 03:32:13.295 - debug: Auth[GET]: 10.42.132.154:8484 / customHostSession=, method=GET, host=10.42.132.154:8484, path=/, org=docs, email=admin@grist.local, userId=6, altSessionId=35ovWtJQAo9jNCb5soes8d
2022-07-27 03:32:13.305 10.42.132.154:8484 GET / 200 6.823 ms - 3469
2022-07-27 03:32:13.357 10.42.132.154:8484 GET /v/unknown/hljs.default.css 304 8.047 ms - -
2022-07-27 03:32:13.359 10.42.132.154:8484 GET /v/unknown/bundle.css 304 5.340 ms - -
2022-07-27 03:32:13.360 10.42.132.154:8484 GET /v/unknown/icons/icons.css 304 5.761 ms - -
2022-07-27 03:32:13.368 10.42.132.154:8484 GET /v/unknown/jqueryui/themes/smoothness/jquery-ui.css 304 17.486 ms - -
2022-07-27 03:32:13.369 10.42.132.154:8484 GET /v/unknown/bootstrap/dist/css/bootstrap.min.css 304 17.458 ms - -
2022-07-27 03:32:13.370 10.42.132.154:8484 GET /v/unknown/bootstrap-datepicker/dist/css/bootstrap-datepicker3.min.css 304 17.012 ms - -
2022-07-27 03:32:13.371 10.42.132.154:8484 GET /v/unknown/jquery/dist/jquery.min.js 304 15.715 ms - -
2022-07-27 03:32:13.372 10.42.132.154:8484 GET /v/unknown/bootstrap/dist/js/bootstrap.min.js 304 15.802 ms - -
2022-07-27 03:32:13.374 10.42.132.154:8484 GET /v/unknown/jqueryui/jquery-ui.min.js 304 7.407 ms - -
2022-07-27 03:32:13.380 10.42.132.154:8484 GET /v/unknown/main.bundle.js 304 1.610 ms - -
2022-07-27 03:32:13.380 10.42.132.154:8484 GET /v/unknown/browser-check.js 304 1.664 ms - -
2022-07-27 03:32:13.381 10.42.132.154:8484 GET /v/unknown/bootstrap-datepicker/dist/js/bootstrap-datepicker.min.js 304 5.613 ms - -
2022-07-27 03:32:13.642 10.42.132.154:8484 GET /v/unknown/icons/favicon.png 304 0.461 ms - -
2022-07-27 03:32:15.468 10.42.132.154:8484 GET /v/unknown/hljs.default.css 304 1.158 ms - -
2022-07-27 03:32:15.469 10.42.132.154:8484 GET /v/unknown/bundle.css 304 1.300 ms - -
2022-07-27 03:32:15.470 10.42.132.154:8484 GET /v/unknown/jqueryui/themes/smoothness/jquery-ui.css 304 4.156 ms - -
2022-07-27 03:32:15.470 10.42.132.154:8484 GET /v/unknown/bootstrap/dist/css/bootstrap.min.css 304 4.037 ms - -
2022-07-27 03:32:15.470 10.42.132.154:8484 GET /v/unknown/bootstrap-datepicker/dist/css/bootstrap-datepicker3.min.css 304 4.044 ms - -
2022-07-27 03:32:15.471 10.42.132.154:8484 GET /v/unknown/icons/icons.css 304 1.642 ms - -
2022-07-27 03:32:15.534 10.42.132.154:8484 GET /v/unknown/main.bundle.js.map 200 1.464 ms - 3622593
2022-07-27 03:32:22.619 - debug: Auth[GET]: 10.42.132.154:8484 / customHostSession=, method=GET, host=10.42.132.154:8484, path=/, org=docs, email=admin@grist.local, userId=6, altSessionId=35ovWtJQAo9jNCb5soes8d

Am I missing something here?

paulfitz commented 2 years ago

Hmm, can you try with GRIST_SERVE_SAME_ORIGIN=false?

edward-shen commented 1 year ago

Closing the loop here, I think I've figured it out. When the server first sends over the GristConfig to the client for caching, makeGristConfig calls adaptServerUrl. This function has the pesky property of overriding the hostname to whatever the hostname of the request if GRIST_SERVE_SAME_ORIGIN is set.

This environment variable is set by default by the Dockerfile.

If a reverse proxy is set up, you must configure the proxy to pass the Host header. Otherwise, the request is extracted to be localhost (if the reverse proxy is on the same host) or whereever the request originates from.

You can verify this by adding just this line in nginx, for example:

  location / {
    # ...
    proxy_set_header Host $host;
  }

reloading nginx, and most importantly, clearing your cookies as this is cached as a window element.

Attempting to access the instance will return different results based on whether or not this header is passed in.

This requirement is undocumented as far as I can tell. I was only able to figure this out by chance and by reading the source code.