Skip to content

Commit 998cea5

Browse files
authored
Use secure cookie for HTTPS sites (#26999)
If the AppURL(ROOT_URL) is an HTTPS URL, then the COOKIE_SECURE's default value should be true. And, if a user visits an "http" site with "https" AppURL, they won't be able to login, and they should have been warned. The only problem is that the "language" can't be set either in such case, while I think it is not a serious problem, and it could be fixed easily if needed. ![image](https://github.com/go-gitea/gitea/assets/2114189/7bc9a859-dcc1-467d-bc7c-1dd6a10389e3)
1 parent 6d96f0b commit 998cea5

File tree

4 files changed

+5
-5
lines changed

4 files changed

+5
-5
lines changed

custom/conf/app.example.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1746,8 +1746,8 @@ LEVEL = Info
17461746
;; Session cookie name
17471747
;COOKIE_NAME = i_like_gitea
17481748
;;
1749-
;; If you use session in https only, default is false
1750-
;COOKIE_SECURE = false
1749+
;; If you use session in https only: true or false. If not set, it defaults to `true` if the ROOT_URL is an HTTPS URL.
1750+
;COOKIE_SECURE =
17511751
;;
17521752
;; Session GC time interval in seconds, default is 86400 (1 day)
17531753
;GC_INTERVAL_TIME = 86400

docs/content/administration/config-cheat-sheet.en-us.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,7 @@ and
777777

778778
- `PROVIDER`: **memory**: Session engine provider \[memory, file, redis, redis-cluster, db, mysql, couchbase, memcache, postgres\]. Setting `db` will reuse the configuration in `[database]`
779779
- `PROVIDER_CONFIG`: **data/sessions**: For file, the root path; for db, empty (database config will be used); for others, the connection string. Relative paths will be made absolute against _`AppWorkPath`_.
780-
- `COOKIE_SECURE`: **false**: Enable this to force using HTTPS for all session access.
780+
- `COOKIE_SECURE`:**_empty_**: `true` or `false`. Enable this to force using HTTPS for all session access. If not set, it defaults to `true` if the ROOT_URL is an HTTPS URL.
781781
- `COOKIE_NAME`: **i\_like\_gitea**: The name of the cookie used for the session ID.
782782
- `GC_INTERVAL_TIME`: **86400**: GC interval in seconds.
783783
- `SESSION_LIFE_TIME`: **86400**: Session life time in seconds, default is 86400 (1 day)

docs/content/administration/config-cheat-sheet.zh-cn.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ Gitea 创建以下非唯一队列:
742742

743743
- `PROVIDER`: **memory**:会话存储引擎 \[memory, file, redis, redis-cluster, db, mysql, couchbase, memcache, postgres\]。设置为 `db` 将会重用 `[database]` 的配置信息。
744744
- `PROVIDER_CONFIG`: **data/sessions**:对于文件,为根路径;对于 db,为空(将使用数据库配置);对于其他引擎,为连接字符串。相对路径将根据 _`AppWorkPath`_ 绝对化。
745-
- `COOKIE_SECURE`: **false**:启用此选项以强制在所有会话访问中使用 HTTPS。
745+
- `COOKIE_SECURE`: **_empty_**`true``false`启用此选项以强制在所有会话访问中使用 HTTPS。如果没有设置,当 ROOT_URL 是 https 链接的时候默认设置为 true
746746
- `COOKIE_NAME`: **i\_like\_gitea**:用于会话 ID 的 cookie 名称。
747747
- `GC_INTERVAL_TIME`: **86400**:GC 间隔时间,以秒为单位。
748748
- `SESSION_LIFE_TIME`: **86400**:会话生命周期,以秒为单位,默认为 86400(1 天)。

modules/setting/session.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func loadSessionFrom(rootCfg ConfigProvider) {
5050
}
5151
SessionConfig.CookieName = sec.Key("COOKIE_NAME").MustString("i_like_gitea")
5252
SessionConfig.CookiePath = AppSubURL + "/" // there was a bug, old code only set CookePath=AppSubURL, no trailing slash
53-
SessionConfig.Secure = sec.Key("COOKIE_SECURE").MustBool(false)
53+
SessionConfig.Secure = sec.Key("COOKIE_SECURE").MustBool(strings.HasPrefix(strings.ToLower(AppURL), "https://"))
5454
SessionConfig.Gclifetime = sec.Key("GC_INTERVAL_TIME").MustInt64(86400)
5555
SessionConfig.Maxlifetime = sec.Key("SESSION_LIFE_TIME").MustInt64(86400)
5656
SessionConfig.Domain = sec.Key("DOMAIN").String()

0 commit comments

Comments
 (0)