Invidious is an alternative front-end to YouTube
Add support for alternative domains for Invidious cookies (#5647)
Fijxu98f4f118b2e4
diff --git a/config/config.example.yml b/config/config.example.ymlindex 08005a12..5d76acc1 100644--- a/config/config.example.yml+++ b/config/config.example.yml@@ -151,6 +151,26 @@ db:##domain:+##+## List of alternative domains where the invidious instance is being served.+## This needs to be set in order to be able to login and update user preferences+## when using a domain that is not the same as the `domain` configuration,+## like a .`onion` address, `.i2p` address, `.b32.i2p` address, etc.+##+## It will detect the alternative domain trough the `X-Forwarded-Host` header.+## https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/X-Forwarded-Host+##+## Accepted values: a list of fully qualified domain names (FQDN)+## Default: <none>+##+## Example:+## alternative_domains:+## - invidious.example.com+## - inv.example.com+## - videos.example.com+##+alternative_domains:+#### Tell Invidious that it is behind a proxy that provides only## HTTPS, so all links must use the https:// scheme. Thisdiff --git a/src/invidious/config.cr b/src/invidious/config.crindex 914d828c..af76bb8e 100644--- a/src/invidious/config.cr+++ b/src/invidious/config.cr@@ -121,6 +121,8 @@ class Configproperty hmac_key : String = ""# Domain to be used for links to resources on the site where an absolute URL is requiredproperty domain : String?+ # Additional domain list that is going to be used for cookie domain validation+ property alternative_domains : Array(String) = [] of String# Subscribe to channels using PubSubHubbub (requires domain, hmac_key)property use_pubsub_feeds : Bool | Int32 = falseproperty popular_enabled : Bool = truediff --git a/src/invidious/routes/before_all.cr b/src/invidious/routes/before_all.crindex 45b8c91c..b75574b6 100644--- a/src/invidious/routes/before_all.cr+++ b/src/invidious/routes/before_all.cr@@ -32,6 +32,8 @@ module Invidious::Routes::BeforeAllenv.response.headers["X-XSS-Protection"] = "1; mode=block"env.response.headers["X-Content-Type-Options"] = "nosniff"+ env.set "header_x-forwarded-host", env.request.headers["X-Forwarded-Host"]?+# Only allow the pages at /embed/* to be embeddedif env.request.resource.starts_with?("/embed")frame_ancestors = "'self' file: http: https:"diff --git a/src/invidious/routes/login.cr b/src/invidious/routes/login.crindex 01a199d3..7d7da487 100644--- a/src/invidious/routes/login.cr+++ b/src/invidious/routes/login.cr@@ -26,6 +26,7 @@ module Invidious::Routes::Logindef self.login(env)locale = env.get("preferences").as(Preferences).locale+ host = env.get("header_x-forwarded-host")referer = get_referer(env, "/feed/subscriptions")@@ -57,7 +58,11 @@ module Invidious::Routes::Loginsid = Base64.urlsafe_encode(Random::Secure.random_bytes(32))Invidious::Database::SessionIDs.insert(sid, email)- env.response.cookies["SID"] = Invidious::User::Cookies.sid(CONFIG.domain, sid)+ if alt = CONFIG.alternative_domains.index(host)+ env.response.cookies["SID"] = Invidious::User::Cookies.sid(CONFIG.alternative_domains[alt], sid)+ else+ env.response.cookies["SID"] = Invidious::User::Cookies.sid(CONFIG.domain, sid)+ endelsereturn error_template(401, "Wrong username or password")end@@ -123,7 +128,11 @@ module Invidious::Routes::Loginview_name = "subscriptions_#{sha256(user.email)}"PG_DB.exec("CREATE MATERIALIZED VIEW #{view_name} AS #{MATERIALIZED_VIEW_SQL.call(user.email)}")- env.response.cookies["SID"] = Invidious::User::Cookies.sid(CONFIG.domain, sid)+ if alt = CONFIG.alternative_domains.index(host)+ env.response.cookies["SID"] = Invidious::User::Cookies.sid(CONFIG.alternative_domains[alt], sid)+ else+ env.response.cookies["SID"] = Invidious::User::Cookies.sid(CONFIG.domain, sid)+ endif env.request.cookies["PREFS"]?user.preferences = env.get("preferences").as(Preferences)diff --git a/src/invidious/routes/preferences.cr b/src/invidious/routes/preferences.crindex a2108b9b..77ea02e9 100644--- a/src/invidious/routes/preferences.cr+++ b/src/invidious/routes/preferences.cr@@ -231,7 +231,12 @@ module Invidious::Routes::PreferencesRouteFile.write("config/config.yml", CONFIG.to_yaml)endelse- env.response.cookies["PREFS"] = Invidious::User::Cookies.prefs(CONFIG.domain, preferences)+ host = env.get("header_x-forwarded-host")+ if alt = CONFIG.alternative_domains.index(host)+ env.response.cookies["PREFS"] = Invidious::User::Cookies.prefs(CONFIG.alternative_domains[alt], preferences)+ else+ env.response.cookies["PREFS"] = Invidious::User::Cookies.prefs(CONFIG.domain, preferences)+ endendenv.redirect referer@@ -266,7 +271,12 @@ module Invidious::Routes::PreferencesRoutepreferences.dark_mode = "dark"end- env.response.cookies["PREFS"] = Invidious::User::Cookies.prefs(CONFIG.domain, preferences)+ host = env.get("header_x-forwarded-host")+ if alt = CONFIG.alternative_domains.index(host)+ env.response.cookies["PREFS"] = Invidious::User::Cookies.prefs(CONFIG.alternative_domains[alt], preferences)+ else+ env.response.cookies["PREFS"] = Invidious::User::Cookies.prefs(CONFIG.domain, preferences)+ endendif redirectdiff --git a/src/invidious/user/cookies.cr b/src/invidious/user/cookies.crindex 654efc15..ed7665fd 100644--- a/src/invidious/user/cookies.cr+++ b/src/invidious/user/cookies.cr@@ -6,17 +6,24 @@ struct Invidious::User# Note: we use ternary operator because the two variables# used in here are not booleans.- SECURE = (Kemal.config.ssl || CONFIG.https_only) ? true : false+ @@secure = (Kemal.config.ssl || CONFIG.https_only) ? true : false# Session ID (SID) cookie# Parameter "domain" comes from the global configdef sid(domain : String?, sid) : HTTP::Cookie+ # Not secure if it's being accessed from I2P+ # Browsers expect the domain to include https. On I2P there is no HTTPS+ # Tor browser works fine with secure being true+ if (domain.try &.split(".").last == "i2p") && @@secure+ @@secure = false+ end+return HTTP::Cookie.new(name: "SID",domain: domain,value: sid,expires: Time.utc + 2.years,- secure: SECURE,+ secure: @@secure,http_only: true,samesite: HTTP::Cookie::SameSite::Lax)@@ -25,12 +32,19 @@ struct Invidious::User# Preferences (PREFS) cookie# Parameter "domain" comes from the global configdef prefs(domain : String?, preferences : Preferences) : HTTP::Cookie+ # Not secure if it's being accessed from I2P+ # Browsers expect the domain to include https. On I2P there is no HTTPS+ # Tor browser works fine with secure being true+ if (domain.try &.split(".").last == "i2p") && @@secure+ @@secure = false+ end+return HTTP::Cookie.new(name: "PREFS",domain: domain,value: URI.encode_www_form(preferences.to_json),expires: Time.utc + 2.years,- secure: SECURE,+ secure: @@secure,http_only: false,samesite: HTTP::Cookie::SameSite::Lax)