Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker update broke everything #1

Open
immanuelfodor opened this issue Apr 18, 2020 · 41 comments
Open

Docker update broke everything #1

immanuelfodor opened this issue Apr 18, 2020 · 41 comments

Comments

@immanuelfodor
Copy link

Hi, I've been using the master branch with delight since we talked at mozilla/fxa#3652 but I did a docker-compose pull to update the images to get any security etc fixes, and it broke everything, now the stack is in a restart loop. I think the main problem is that the mysql container wouldn't come up, and then all dependent containers fail, too.

fxa-auth-db-mysql_1        | 
fxa-auth-db-mysql_1        | > [email protected] prestart /app
fxa-auth-db-mysql_1        | > ../../_scripts/check-mysql.sh && node ./bin/db_patcher.js >/dev/null
fxa-auth-db-mysql_1        | 
fxa-auth-db-mysql_1        | sh: ../../_scripts/check-mysql.sh: not found
fxa-auth-db-mysql_1        | npm ERR! code ELIFECYCLE
fxa-auth-db-mysql_1        | npm ERR! syscall spawn
fxa-auth-db-mysql_1        | npm ERR! file sh
fxa-auth-db-mysql_1        | npm ERR! errno ENOENT
fxa-auth-db-mysql_1        | npm ERR! [email protected] prestart: `../../_scripts/check-mysql.sh && node ./bin/db_patcher.js >/dev/null`
fxa-auth-db-mysql_1        | npm ERR! spawn ENOENT
fxa-auth-db-mysql_1        | npm ERR! 
fxa-auth-db-mysql_1        | npm ERR! Failed at the [email protected] prestart script.
fxa-auth-db-mysql_1        | npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
fxa-auth-db-mysql_1        | 
fxa-auth-db-mysql_1        | npm ERR! A complete log of this run can be found in:
fxa-auth-db-mysql_1        | npm ERR!     /app/.npm/_logs/2020-04-18T08_12_44_744Z-debug.log

I'd gladly debug the script but there is no script in the first place :D Any idea how to get the stack working again?

@jackyzy823
Copy link
Owner

Fixed in 0bb65ae.

Complain below. TL;DR

FxA is under heavy development. It is quite unstable.Config files are not constent and startup methods of docker and pm2 quite differs . And they do not care about self hosting at all (I think).

@immanuelfodor
Copy link
Author

Thanks for the quick fix, it's fine again now.

It's sad that they don't really care to maintain it in a compatible fashion. What do you recommend, how frequently should the stack be updated?

@jackyzy823
Copy link
Owner

I'm not very sure. If there're feature or functional or security updates , it might be fine. However if the updates are about configs or startup scripts, I have no better idea than opening an issue here when failed to start up. :-(

@immanuelfodor
Copy link
Author

Okay, thank you so much for the maintenance, I hope the repo will take off as the only self-hostable FxA stack! :) Maybe it's a good idea to post it on https://www.reddit.com/r/selfhosted/ then later on HackerNews (Show HN) when you think it is the time to get more attention or contributors. Also, the numerous topics we've talked about in the original issue thread, maybe could be here as separate issues along with the todo items in the README. I'd gladly create them, I just don't want you to feel I'm giving you orders :D I'm so honored you took all this together!

@jackyzy823
Copy link
Owner

I think it's not the time to spread it widely. I'm current working on ytt branch. When it done (may be a loooooong time 😅 ), i'd try to promote this project.

@immanuelfodor
Copy link
Author

Wow, so many updates on the master branch, it seems you made quite a progress! My instance was not updated since we've talked here, I'll look into what has changed and preferably do the update this afternoon. Anything to watch out specifically besides the obvious migration to the YAML config?

@immanuelfodor
Copy link
Author

I got some unexpected output after setting up the config yaml and running the init:

  • In the about:config section: "webextensions.storage.sync.serverURL": "https://$KINTO_SUB.$DOMAIN_NAME/v1" - because the whole echo is in single quotes, the variables are not replaced
  • Before renew certs line: ./init.sh: line 164: test: ==: unary operator expected - missing quotes from the left-hand side

Here is the diff of the fix with some missing whitespaces added as well:

diff --git a/init.sh b/init.sh
index 0981c20..b15b801 100755
--- a/init.sh
+++ b/init.sh
@@ -140,7 +140,7 @@ HERE
 
 # TODO: yq r only once
 if test $(yq r config.yml option.webext_storagesync.enable) == "true" ; then
-       echo '"webextensions.storage.sync.serverURL": "https://$KINTO_SUB.$DOMAIN_NAME/v1"'
+       echo "  \"webextensions.storage.sync.serverURL\": \"https://$KINTO_SUB.$DOMAIN_NAME/v1\""
 fi
 
 echo -e "\e[0m" #reset
@@ -155,13 +155,13 @@ cat <<HERE
   "identity.fxaccounts.remote.webchannel.uri":"https://$CONTENT_SUB.$DOMAIN_NAME/",
   "identity.sync.tokenserver.uri": "https://$SYNC_SUB.$DOMAIN_NAME/token/1.0/sync/1.5",
 
-APPEND/PREPEND https://$CONTENT_SUB.$DOMAIN_NAME to "webchannel.allowObject.urlWhitelist"
+  APPEND/PREPEND https://$CONTENT_SUB.$DOMAIN_NAME to "webchannel.allowObject.urlWhitelist"
 
 HERE
 
 echo -e "\e[0m" #reset
 
-if test $(yq r config.yml mail.type.enable) == "localhelper" ; then
+if test "$(yq r config.yml mail.type.enable)" == "localhelper" ; then
        echo -e "\e[32m Check sigincode \e[0m"
        echo -e "\e[33m" 
        cat  <<HERE

@immanuelfodor
Copy link
Author

After starting the stack with docker-compose with all services enabled, 3 containers are in restart loop:

CONTAINER ID        IMAGE                               COMMAND                  CREATED             STATUS                          PORTS                                NAMES
27011dbf2916        nginx                               "/docker-entrypoint.…"   3 minutes ago       Restarting (1) 30 seconds ago                                        fxa-selfhosting_nginx_1
b640a61b103c        mozilla/fxa-content-server:latest   "docker-entrypoint.s…"   3 minutes ago       Restarting (0) 35 seconds ago                                        fxa-selfhosting_fxa-content-server_1
1e157e702450        mozilla/fxa-auth-server:latest      "docker-entrypoint.s…"   3 minutes ago       Restarting (8) 25 seconds ago                                        fxa-selfhosting_fxa-auth-server_1

The auth server log is all about this output:

fxa-auth-server_1             | --------------------------------------------------------
fxa-auth-server_1             |  docker-compose-wait 2.7.3
fxa-auth-server_1             | ---------------------------
fxa-auth-server_1             | Starting with configuration:
fxa-auth-server_1             |  - Hosts to be waiting for: [redis:6379,fxa-auth-db-mysql:8000]
fxa-auth-server_1             |  - Timeout before failure: 120 seconds 
fxa-auth-server_1             |  - TCP connection timeout before retry: 5 seconds 
fxa-auth-server_1             |  - Sleeping time before checking for hosts availability: 0 seconds
fxa-auth-server_1             |  - Sleeping time once all hosts are available: 0 seconds
fxa-auth-server_1             |  - Sleeping time between retries: 1 seconds
fxa-auth-server_1             | --------------------------------------------------------
fxa-auth-server_1             | Checking availability of redis:6379
fxa-auth-server_1             | Host redis:6379 is now available!
fxa-auth-server_1             | --------------------------------------------------------
fxa-auth-server_1             | Checking availability of fxa-auth-db-mysql:8000
fxa-auth-server_1             | Host fxa-auth-db-mysql:8000 is now available!
fxa-auth-server_1             | --------------------------------------------------------
fxa-auth-server_1             | docker-compose-wait - Everything's fine, the application can now start!
fxa-auth-server_1             | --------------------------------------------------------
fxa-auth-server_1             | AssertionError [ERR_ASSERTION]: oauthServer.openid.key is missing; bailing out in a cowardly fashion...
fxa-auth-server_1             |     at Object.<anonymous> (/fxa/packages/fxa-auth-server/lib/oauth/keys.js:46:12)
fxa-auth-server_1             |     at Module._compile (internal/modules/cjs/loader.js:1138:30)
fxa-auth-server_1             |     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
fxa-auth-server_1             |     at Module.load (internal/modules/cjs/loader.js:986:32)
fxa-auth-server_1             |     at Function.Module._load (internal/modules/cjs/loader.js:879:14)
fxa-auth-server_1             |     at Module.require (internal/modules/cjs/loader.js:1026:19)
fxa-auth-server_1             |     at require (internal/modules/cjs/helpers.js:72:18)
fxa-auth-server_1             |     at Object.<anonymous> (/fxa/packages/fxa-auth-server/lib/oauth/jwt.js:7:62)
fxa-auth-server_1             |     at Module._compile (internal/modules/cjs/loader.js:1138:30)
fxa-auth-server_1             |     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
fxa-auth-server_1             |     at Module.load (internal/modules/cjs/loader.js:986:32)
fxa-auth-server_1             |     at Function.Module._load (internal/modules/cjs/loader.js:879:14)
fxa-auth-server_1             |     at Module.require (internal/modules/cjs/loader.js:1026:19)
fxa-auth-server_1             |     at require (internal/modules/cjs/helpers.js:72:18)
fxa-auth-server_1             |     at Object.<anonymous> (/fxa/packages/fxa-auth-server/lib/oauth/jwt_access_token.js:7:13)
fxa-auth-server_1             |     at Module._compile (internal/modules/cjs/loader.js:1138:30)
fxa-auth-server_1             |     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
fxa-auth-server_1             |     at Module.load (internal/modules/cjs/loader.js:986:32)
fxa-auth-server_1             |     at Function.Module._load (internal/modules/cjs/loader.js:879:14)
fxa-auth-server_1             |     at Module.require (internal/modules/cjs/loader.js:1026:19)
fxa-auth-server_1             |     at require (internal/modules/cjs/helpers.js:72:18)
fxa-auth-server_1             |     at Object.<anonymous> (/fxa/packages/fxa-auth-server/lib/oauth/grant.js:12:24)
fxa-auth-server_1             |     at Module._compile (internal/modules/cjs/loader.js:1138:30)
fxa-auth-server_1             |     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
fxa-auth-server_1             |     at Module.load (internal/modules/cjs/loader.js:986:32)
fxa-auth-server_1             |     at Function.Module._load (internal/modules/cjs/loader.js:879:14)
fxa-auth-server_1             |     at Module.require (internal/modules/cjs/loader.js:1026:19)
fxa-auth-server_1             |     at require (internal/modules/cjs/helpers.js:72:18) {
fxa-auth-server_1             |   generatedMessage: false,
fxa-auth-server_1             |   code: 'ERR_ASSERTION',
fxa-auth-server_1             |   actual: undefined,
fxa-auth-server_1             |   expected: undefined,
fxa-auth-server_1             |   operator: 'fail'
fxa-auth-server_1             | }

Content server has no output, and nginx doesn't honor the debug logs option in the config, need to manually edit the compose file.

Anyways, it says it misses the content server, probably because it's in a restart loop:

nginx_1                       | 2020/06/07 12:08:09 [emerg] 9#9: host not found in upstream "fxa-content-server" in /etc/nginx/conf.d/fxa.conf:29
nginx_1                       | nginx: [emerg] host not found in upstream "fxa-content-server" in /etc/nginx/conf.d/fxa.conf:29

@immanuelfodor
Copy link
Author

immanuelfodor commented Jun 7, 2020

I've found some keys and comments in this old PR, maybe it helps: https://github.com/mozilla/fxa-oauth-server/pull/368/files

@jackyzy823
Copy link
Owner

Sorry for the inconvince.
I'm working on fxa v1.173+ , because they changed docker base images and have some problems which related to fxa-auth-server and fxa-content-server and syncserver.
If you want to use v1.172 , you could update master branch .
if you want to use v1.173+, you could use br-v.174.0 branch or v1.174.0 tag.

@jackyzy823
Copy link
Owner

jackyzy823 commented Jun 7, 2020

They use a docker image DO NOT contains pre-generated publickey.json key.json and other keys in fxa-auth-server. AND also the docker image does not contain openssl which makes impossible to generate these key json files . see https://github.com/mozilla/fxa/blob/23c71720960e0366e24a5c7290feff077c3e2281/packages/fxa-auth-server/scripts/gen_keys.js#L69

So i need to make a breaking change for fxa v1.173+ , i made a branch br-v1.174.0 . to install openssl and generate missing keys.

And they do not care about self hosting at all (I think).

Again

@jackyzy823
Copy link
Owner

Thanks for your review on init.sh . I think i'm quite poor on shell scripting :(

and i decide not to set fxa_version to latest finally. I can't always keep trace with mozilla's development spped.

nginx doesn't honor the debug logs option in the config

deps_logs not implemented yet. I'm lazy. :(

@jackyzy823
Copy link
Owner

jackyzy823 commented Jun 7, 2020

I'll look into what has changed and preferably do the update this afternoon. Anything to watch out specifically besides the obvious migration to the YAML config?

what a coincidence. I'm struggling with the latest fxa too at the quite same time.

Wow, so many updates on the master branch, it seems you made quite a progress!

Basically. I implement all features discuesed in mozilla/fxa#3652. Reverse proxies, subdomain name ,certs , firefox send, webextension storage.sync , firefox notes.

Thanks for giving a try. :) and also questions are welcomed.

@immanuelfodor
Copy link
Author

Again

That's so sad. However, until it is open-source and can be debugged how it works and reproducible locally, we can keep up with their changes! :) And maybe through this repo, self-hosting can get intentional support from the core team. I can imagine they could even learn from these efforts to create a better hosting alternative for at least dev envs or maybe for production, too.

I can't always keep trace with mozilla's development spped.

That's okay and completely normal, they are more than one dev :) This is also why I didn't update my FXA for ~1.5 months, I didn't want to complain about something going south with some rouge updates :D Today was the day that I finally found suitable to look into it what's new around here.

Basically. I implement all features discuesed in mozilla/fxa#3652

And it's wonderful! I could immediately update the config file with the custom subdomains, etc. really good job! By replacing the custom stuff with sed, I always had an dirty git status with modifications to many files:

        modified:   .env.sample
        modified:   README
        modified:   _init/nginx/fxa.conf.tmpl
        modified:   docker-compose.yml
        modified:   init.sh

Now, it's none! :) (With one temporary exception, see below)

I switched to the master branch, and as I use SSL, I've found a bug that keeps nginx from starting:

nginx_1                       | 2020/06/07 14:04:28 [emerg] 9#9: cannot load certificate key "/certs/kinto.cer": PEM_read_bio_PrivateKey() failed (SSL: error:0909006C:PEM routines:get_name:no start line:Expecting: ANY PRIVATE KEY)
nginx_1                       | nginx: [emerg] cannot load certificate key "/certs/kinto.cer": PEM_read_bio_PrivateKey() failed (SSL: error:0909006C:PEM routines:get_name:no start line:Expecting: ANY PRIVATE KEY)

The fix is simple:

diff --git a/_init/nginx/kinto.conf.tmpl b/_init/nginx/kinto.conf.tmpl
index 1507383..0ab1301 100644
--- a/_init/nginx/kinto.conf.tmpl
+++ b/_init/nginx/kinto.conf.tmpl
@@ -3,7 +3,7 @@ server {
     listen 443 ssl;
     listen [::]:443 ssl;
     ssl_certificate /certs/kinto.cer;
-    ssl_certificate_key /certs/kinto.cer;
+    ssl_certificate_key /certs/kinto.key;
 
 
     location / {

@immanuelfodor
Copy link
Author

Question: the Notes app is although enabled in the config, it is not yet compiled into the docker-compose?

Suggestion: maybe when Send is enabled, the identity.fxaccounts.service.sendLoginUrl could be in the init's output for configuration of the service.

@immanuelfodor
Copy link
Author

Hmm, the Send login doesn't work with a final redirect to /error "Something went wrong". I've set anon_max_file_size: "0", so you always need to login to use Send, and that's effectively an unsolvable lock-out :D

The JS console says: Content Security Policy: The page’s settings blocked the loading of a resource at https://oauth.local/v1/token (“connect-src”). And the network tab has one request blocked through the login flow:

image

I did not tamper with any Firefox CSP settings in about:config, all default. Maybe an Nginx custom CSP header could solve it.

@immanuelfodor
Copy link
Author

Send loads with this CSP header, and as it doesn't contain the custom self-hosted URL, it blocks the login:

default-src 'self'; connect-src 'self' wss://*.dev.lcip.org wss://*.send.nonprod.cloudops.mozgcp.net wss://send.firefox.com https://*.dev.lcip.org https://accounts.firefox.com https://*.accounts.firefox.com https://sentry.prod.mozaws.net; img-src 'self' https://*.dev.lcip.org https://firefoxusercontent.com https://secure.gravatar.com; script-src 'self' 'nonce-0e102589cae7aa79ce7586fa507ce66f'; form-action 'none'; frame-ancestors 'none'; object-src 'none'; report-uri /__cspreport__

I hid it as a quick fix to prove it is the source, and now the login works. A bit drastic solution, and effectively defeats the purpose of CSP headers in a preferably secure E2EE app :(

diff --git a/_init/nginx/send.conf.tmpl b/_init/nginx/send.conf.tmpl
index fe8ecdf..b590a8e 100644
--- a/_init/nginx/send.conf.tmpl
+++ b/_init/nginx/send.conf.tmpl
@@ -8,6 +8,8 @@ server {
     location / {
         proxy_http_version 1.1;
         proxy_pass http://send:1443;
+
+        proxy_hide_header Content-Security-Policy;
     }
     location /api/ws {
         proxy_http_version 1.1;

@jackyzy823
Copy link
Owner

jackyzy823 commented Jun 7, 2020

Question: the Notes app is although enabled in the config, it is not yet compiled into the docker-compose?

do you mean note webextension/android client ?

Hmm, the Send login doesn't work with a final redirect to /error "Something went wrong". I've set anon_max_file_size: "0", so you always need to login to use Send, and that's effectively an unsolvable lock-out :D

blame on these lines

#! else url protocol of file depends on NODE_ENV to be http/https
- NODE_ENV=production

however if you ** do ** use prod env , csp are always wrong.
see mozilla/send#1434 for more info.

so your solution seem the best. or we can set correct CSP via nginx too.

Suggestion: maybe when Send is enabled, the identity.fxaccounts.service.sendLoginUrl could be in the init's output for configuration of the service.

good idea!

@immanuelfodor
Copy link
Author

do you mean note webextension/android client ?

I don't know, I meant no running containers for https://github.com/mozilla/notes/ I thought this is why there is a note block in the config. It might be completely unrelated though.

or we can set correct CSP via nginx too.

After some tinkering, I found a reasonably good solution. Hiding the original header and providing a new one. I could not calculate the script nonce value, so I needed to use unsafe-inline to let the page work, but this is still better than having no meaningful CSP: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src

diff --git a/_init/nginx/send.conf.tmpl b/_init/nginx/send.conf.tmpl
index fe8ecdf..d5bbfb2 100644
--- a/_init/nginx/send.conf.tmpl
+++ b/_init/nginx/send.conf.tmpl
@@ -8,6 +8,9 @@ server {
     location / {
         proxy_http_version 1.1;
         proxy_pass http://send:1443;
+
+        proxy_hide_header Content-Security-Policy;
+        add_header Content-Security-Policy "default-src 'self'; connect-src 'self' https://${SEND}.${NGINX_DOMAIN_NAME} https://${OAUTH}.${NGINX_DOMAIN_NAME} https://${PROFILE}.${NGINX_DOMAIN_NAME}; img-src 'self' https://firefoxusercontent.com https://secure.gravatar.com; script-src 'self' 'unsafe-inline'; form-action 'none'; frame-ancestors 'none'; object-src 'none'; report-uri /__cspreport__";
     }
     location /api/ws {
         proxy_http_version 1.1;
diff --git a/docker-compose.tmpl.yml b/docker-compose.tmpl.yml
index d9de2b9..ae63bb1 100644
--- a/docker-compose.tmpl.yml
+++ b/docker-compose.tmpl.yml
@@ -590,7 +590,7 @@ services:
     #@ command = []
     #@ command.append('/bin/sh -c ' + '"'  + "envsubst '$$NGINX_DOMAIN_NAME $$CONTENT $$AUTH $$OAUTH $$PROFILE $$SYNC' < /etc/nginx/conf.d/fxa.conf.tmpl > /etc/nginx/conf.d/fxa.conf ") 
     #@ if data.values.option.send.enable == True:
-    #@ command.append("envsubst '$$NGINX_DOMAIN_NAME $$SEND' < /etc/nginx/conf.d/send.conf.tmpl > /etc/nginx/conf.d/send.conf ")
+    #@ command.append("envsubst '$$NGINX_DOMAIN_NAME $$SEND $$OAUTH $$PROFILE' < /etc/nginx/conf.d/send.conf.tmpl > /etc/nginx/conf.d/send.conf ")
     #@ end
     #@ if data.values.option.notes.enable == True or data.values.option.webext_storagesync.enable == True:
     #@ command.append("envsubst '$$NGINX_DOMAIN_NAME $$KINTO' < /etc/nginx/conf.d/kinto.conf.tmpl > /etc/nginx/conf.d/kinto.conf ")

Maybe with some Lua code, the new domains could be injected into the original header instead of forging a new one from scratch. This way the nonce could be calculated by the Send backend as usual, and no need for the unsafe-inline directive. It would be just as safe as the original.

A couple of links if you want to look into it, I somehow feel it would worth the effort to create ~5-6 lines of Lua code inside the Send nginx config to solve this:

@immanuelfodor
Copy link
Author

Oh, and one thing, the postgres_data folder is not on gitignore :D

@jackyzy823
Copy link
Owner

jackyzy823 commented Jun 7, 2020

I don't know, I meant no running containers for https://github.com/mozilla/notes/ I thought this is why there is a note block in the config. It might be completely unrelated though.

notes app use kinto to store data. so there's no notes container.

Hiding the original header and providing a new one.

can we prepend or append our to the origin one ? i'm not quite familar with nginx , maybe possbile?

@immanuelfodor
Copy link
Author

immanuelfodor commented Jun 8, 2020

Yes, through the Lua module, we can modify headers on the fly. However, the Lua module is not included in the original nginx image, so I needed to add it in the command section of the compose. Since it installs a new base config, the gzip on directive is redundant, and it needs to be removed otherwise the container fails to start. I also commented out the logging driver none from the compose template as it made me go crazy to not see the logs while debugging nginx and regenerating the config many times. Here is the final solution:

diff --git a/_init/nginx/fxa.conf.tmpl b/_init/nginx/fxa.conf.tmpl
index 85cf2cb..945dcd7 100644
--- a/_init/nginx/fxa.conf.tmpl
+++ b/_init/nginx/fxa.conf.tmpl
@@ -1,4 +1,3 @@
-gzip on;
 gzip_types
        text/css
        text/javascript
diff --git a/_init/nginx/send.conf.tmpl b/_init/nginx/send.conf.tmpl
index fe8ecdf..f338275 100644
--- a/_init/nginx/send.conf.tmpl
+++ b/_init/nginx/send.conf.tmpl
@@ -8,6 +8,30 @@ server {
     location / {
         proxy_http_version 1.1;
         proxy_pass http://send:1443;
+
+        # replace the original Mozilla domains to our custom domains in the original CSP header directives to make the login work
+        # @see: https://github.com/openresty/lua-nginx-module/blob/master/README.markdown#ngxheaderheader
+        # @see: https://github.com/openresty/lua-nginx-module#ngxresub
+        header_filter_by_lua_block {
+            local newstr, n, err = ngx.re.sub(ngx.header["Content-Security-Policy"], "(connect-src 'self').*?;", "$1 https://${SEND}.${NGINX_DOMAIN_NAME} https://${OAUTH}.${NGINX_DOMAIN_NAME} https://${PROFILE}.${NGINX_DOMAIN_NAME} https://${CONTENT}.${NGINX_DOMAIN_NAME} wss://${SEND}.${NGINX_DOMAIN_NAME};")
+            if newstr then
+                ngx.header["Content-Security-Policy"] = newstr
+                ngx.header["X-Content-Security-Policy"] = newstr
+                ngx.header["X-WebKit-CSP"] = newstr
+            else
+                ngx.log(ngx.ERR, "error: ", err)
+                return
+            end
+            local newstr, n, err = ngx.re.sub(ngx.header["Content-Security-Policy"], "(img-src 'self').*?;", "$1 https://${SEND}.${NGINX_DOMAIN_NAME} https://${PROFILE}.${NGINX_DOMAIN_NAME} https://secure.gravatar.com;")
+            if newstr then
+                ngx.header["Content-Security-Policy"] = newstr
+                ngx.header["X-Content-Security-Policy"] = newstr
+                ngx.header["X-WebKit-CSP"] = newstr
+            else
+                ngx.log(ngx.ERR, "error: ", err)
+                return
+            end
+        }
     }
     location /api/ws {
         proxy_http_version 1.1;
diff --git a/docker-compose.tmpl.yml b/docker-compose.tmpl.yml
index d9de2b9..7e13fa1 100644
--- a/docker-compose.tmpl.yml
+++ b/docker-compose.tmpl.yml
@@ -583,18 +583,21 @@ services:
       
 
 
-    logging:
-      driver: "none"
+    #! logging:
+    #!   driver: "none"
     #! use envsubst only replace domain name https://github.com/docker-library/docs/issues/496#! issuecomment-186149231
     #! build command!
     #@ command = []
     #@ command.append('/bin/sh -c ' + '"'  + "envsubst '$$NGINX_DOMAIN_NAME $$CONTENT $$AUTH $$OAUTH $$PROFILE $$SYNC' < /etc/nginx/conf.d/fxa.conf.tmpl > /etc/nginx/conf.d/fxa.conf ") 
     #@ if data.values.option.send.enable == True:
-    #@ command.append("envsubst '$$NGINX_DOMAIN_NAME $$SEND' < /etc/nginx/conf.d/send.conf.tmpl > /etc/nginx/conf.d/send.conf ")
+    #@ command.append("envsubst '$$NGINX_DOMAIN_NAME $$SEND $$OAUTH $$PROFILE $$CONTENT' < /etc/nginx/conf.d/send.conf.tmpl > /etc/nginx/conf.d/send.conf ")
     #@ end
     #@ if data.values.option.notes.enable == True or data.values.option.webext_storagesync.enable == True:
     #@ command.append("envsubst '$$NGINX_DOMAIN_NAME $$KINTO' < /etc/nginx/conf.d/kinto.conf.tmpl > /etc/nginx/conf.d/kinto.conf ")
     #@ end
+    #@ command.append("apt update")
+    #@ command.append("apt autoremove -y")
+    #@ command.append("apt install -y nginx-common nginx-full libnginx-mod-http-lua libnginx-mod-http-headers-more-filter")
     #@ command.append("nginx -g 'daemon off;'" + '"')
     command:  #@ "&& ".join(command)
     restart: unless-stopped

You can test the old and the new headers with the following command, Lua rocks! :)

curl -I https://send.local

@immanuelfodor
Copy link
Author

Updated the previous diff with another Lua block to replace the img-src as well, otherwise, the profile picture would not show up after login.

@immanuelfodor
Copy link
Author

immanuelfodor commented Jun 8, 2020

Updated the previous diff with adding the CONTENT url to the connect-src, otherwise, some metrics-flow requests to the www subdomain would be blocked.

I think I'm done now :D

@jackyzy823
Copy link
Owner

Hello @immanuelfodor

Since they merged my PR, now send don't have csp issue.
and
Since they add openssl in base docker images, fxa version bumps to almost the latest.

Note:

  1. You need to update syncserver image , to use a new introduced ENV arg SYNCSERVER_OAUTH_VERIFIER .
  2. You need to update kinto image if you use and dependency PyFxA in this image is >0.7.3 and <0.7.6.

@immanuelfodor
Copy link
Author

Hi, I've checked all the changes since 78e054a, all looks good, and impressive solution with the Send upstream changes! :) Updated the whole stack, config and compose alike, it works fine with v1.179.0 out of the box. It feels like the project is somewhere around the beta stage 🚀

@immanuelfodor
Copy link
Author

Hi @jackyzy823 , it seems FF 83 has broke the sync :(

image

When I sign in or sign out + sign in, the account seems fine but as soon as I turn on sync, the above message appears.

$ firefox -v
Mozilla Firefox 83.0

I tried updating from v1.179.0 to v1.186.0 with no luck, it's the same. I also tried using latest but many docker containers get into a crash loop. Do you have some time these days to follow-up on the past months' dev advancement in FXA?

@jackyzy823
Copy link
Owner

Hi @immanuelfodor

Have you try to force update synserver's docker image?

From FF80 useOAuthForSyncToken is set to true by default. see https://cat-in-136.github.io/2020/08/diff-between-firefox-800-beta-7-default.html

if your about:config useOAuthForSyncToken is true. Please update syncserver docker image to the latest.

If previous method do not help. Could you provide your about:sync-log 's error log. And i'll try to follow up with FxA and other related project (syncserver etc...)

@immanuelfodor
Copy link
Author

Hmm, my useOAuthForSyncToken is indeed true and I didn't changed this setting previously. I did a docker system prune -a after docker-compose down, so all my images should be now the latest with v1.186.0 set in the config.yml (latest here doesn't work, multiple containers are crashing). With 186 set, these are my images:

$ docker image ls 
REPOSITORY                   TAG                 IMAGE ID            CREATED             SIZE
nginx                        latest              bc9a0695f571        6 days ago          133MB
redis                        latest              74d107221092        13 days ago         104MB
mysql/mysql-server           5.6                 853cb833f211        6 weeks ago         238MB
mozilla/syncserver           latest              cb31d5dbb897        6 weeks ago         447MB
mozilla/send                 latest              17fad6a03bfe        2 months ago        327MB
mozilla/fxa-profile-server   v1.186.0            b271dc1f2e58        3 months ago        1.04GB
mozilla/fxa-content-server   v1.186.0            3d5149bb15f6        3 months ago        1.04GB
mozilla/fxa-auth-server      v1.186.0            483619d8c3fb        3 months ago        1.04GB
mozilla/fxa-auth-db-mysql    v1.186.0            5f775075ab74        3 months ago        1.04GB
mozilla/browserid-verifier   v1.186.0            2ac4b70be132        3 months ago        1.04GB
mozilla/pushbox              latest              d2ff07b96d59        5 months ago        143MB

And here is my sync log after a logout-login pair. There was no error before turning sync on, the file appeared right after enabling sync and then instantly asking for signing in again. Actual domain names are replaced with local, and based on the bad requests, it should be something oauth related, just as you thought.

sync.log

@jackyzy823
Copy link
Owner

Well . that's weird. with the exact same config as yours , i could sync normally.

Could you please use a clean firefox ( all configs set to default) and only change these configs

"identity.fxaccounts.auth.uri" 
"identity.fxaccounts.remote.root"
"identity.fxaccounts.remote.oauth.uri"
"identity.fxaccounts.remote.profile.uri"
"identity.sync.tokenserver.uri"

and try again .

@immanuelfodor
Copy link
Author

immanuelfodor commented Dec 3, 2020

I tried starting a new FF instance with an empty profile, it starts up fine as a brand new FF:

mkdir -p /tmp/mozilla-temp-profile/
firefox --profile /tmp/mozilla-temp-profile/

Set up the above options in about:config but it wouldn't even login, an infinite loader is displayed:

image

Inspecting the network requests of the login page, there is none that would contain my email address, just the ones to load the page and then the anonymized metrics:

image

Restarting with the same temp profile path helps, the account is recognized. Maybe this behavior is normal after entering custom FXA URLs:

image

However, the account menu is instantly yellow:

image

Clicking that menu takes back to the sync page with no option to turn on sync but to reconnect:

image

Reconnect won't help, it is asking for reconnect instantaneously.

@jackyzy823
Copy link
Owner

then i have no idea. 😞

maybe you need to set fxa-auth-server 's log level to debug and see what it complains about.

@immanuelfodor
Copy link
Author

I've set it to debug, and anonymized the output with search and replace to be consistent, maybe you can make something out of it: fxa-auth-server-debug.log

The only thing I see is that I get a flow id but it is somehow getting lost and starts to be missing in later calls.

@jackyzy823
Copy link
Owner

jackyzy823 commented Dec 27, 2020

I'll try to dig into it.

And have you try a totally clean test ?
Step:

  1. remove all docker image,containers.
  2. backup your database to another place (important!)
  3. remove database files
  4. backup your config.yml and maybe changes in _init folder (important!!)
  5. remove this project folder
  6. clone this project
  7. change only config.yml
  8. init.sh
  9. clean firefox with config changed which i mentioned before
  10. test

if this works. then maybe problem in database. else maybe in my scripts.

I would be thankful to you if you could do this test for me.

some problem in re-init:
if you have not docker mikefarah/yq in local
please edit init.sh replace all mikefarah/yq to mikefarah/yq:3

@immanuelfodor
Copy link
Author

Oh my, you're a genius, why I haven't thought of this before I don't know :D Starting over with a clean project clone helped logging in even with the existing desktop profile, now it syncs fine. This means something was indeed bad in the database, I think. The yq:3 addition was also much appreciated to avoid broken init.sh output.

One thing is left, logging in with Firefox Android. I have the Beta app, so when I tap the Firefox logo 5 times on the about page, a few hidden settings are revealed:

image

Bottom of the settings, two new menus but these are either empty or containing nothing useful:

image

image

image

On the other hand, over the top, there are two new URL fields:

image

No help is displayed, just an input box in a popup with the same title as the button.

When I enter the https://www.domain.local (for account) and https://token.domain.local (for sync) URLs, tapping the sign in with email option takes to a page which is infinitely loading. The page is the https://www.domain.local/authorize?.... page with lots of parameters in the query. In desktop mode, it also displays the Mozilla logo at the bottom of the loading page. It looks like as if some resources are not downloaded or JS is not executed on the login page. However, there are no webchannel.allowObject.urlWhitelist or any identity.fxaccounts.*, identity.sync.* options in about:config, just these fields. Do you have any idea how to make the login page work?

@jackyzy823
Copy link
Owner

selfhosting fxa for firefox beta (aka fenix ) is not supported . because they hardcoded urls that could run content_scripts for login.

see : https://github.com/mozilla-mobile/android-components/blob/8bf25ffd5272a063fef237f47411d65a098d1312/components/feature/accounts/src/main/assets/extensions/fxawebchannel/manifest.template.json#L13-L17
and

i try to make this work , but failed.
see: mozilla-mobile/android-components#6225

==========

And about database. if you are interested and want to merge data back if needed . you could try to find out which database caused the issue. (which is time consuming).
i think maybe fxa_oauth database , but not sure.

@immanuelfodor
Copy link
Author

This is so sad, the whole point of sync was desktop<->mobile for me :( Since the Fenix update, I started to use self-chat messages to transfer links between the platforms, and only synced the desktop in hope mobile would be solved once. Although desktop sync broke a month ago as well, we could fix it now, but not having Fenix is still heartbreaking. Yesterday, after the auth debug, I was on the brink of shutting the whole stack down, but now that desktop works, it's saved but still a bit useless. I really don't get why Mozilla is making this hard to self-host the open-source stack and make everything work together. They might be really just after the profits, and don't care for the community wholeheartedly. I really hope the linked issue gets some attention, and they make the Beta app able to whitelist the necessary webchannel URL or don't validate the source.

@jackyzy823
Copy link
Owner

if you insist on desktop <-> mobile sync, you could downgrade fenix to fennec (version 68 lts and more extension can be used , that's also the reason why i don't upgrade to fenix).
However be aware of data loss.

@immanuelfodor
Copy link
Author

What kind of data loss? A one-time loss upon starting to sync, or losing data constantly? And what data is being lost? Like browser history or extension data? Maybe I just need to setup all extensions on mobile manually? It's an interesting option to downgrade, I'd like to know more about this data loss.

@jackyzy823
Copy link
Owner

In android, application downgrade will remove all data under this app. (just like remove the app and install it again)

@immanuelfodor
Copy link
Author

I see, the app data. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants