Skip to content

Commit e6d772f

Browse files
committed
add a new param allow customize includeSubDomain in hsts
1 parent 6a91b7a commit e6d772f

File tree

5 files changed

+31
-3
lines changed

5 files changed

+31
-3
lines changed

custom/conf/app.ini.sample

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,3 +787,13 @@ IS_INPUT_FILE = false
787787
ENABLED = false
788788
; If you want to add authorization, specify a token here
789789
TOKEN =
790+
791+
[hsts]
792+
; Enables hsts. True or false, default is false.
793+
ENABLED = false
794+
; Max age of the time, default is 365 days.
795+
MAX_AGE = 8760h
796+
; require sub domains use hsts
797+
INCLUDE_SUB_DOMAINS = false
798+
; send preload
799+
SEND_PRELOAD_DIRECTIVE = false

docs/content/doc/advanced/config-cheat-sheet.en-us.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,13 @@ Two special environment variables are passed to the render command:
501501
- `GITEA_PREFIX_SRC`, which contains the current URL prefix in the `src` path tree. To be used as prefix for links.
502502
- `GITEA_PREFIX_RAW`, which contains the current URL prefix in the `raw` path tree. To be used as prefix for image paths.
503503

504+
## HSTS (`hsts`)
505+
506+
- ENABLED: **false** Enables hsts. True or false, default is false.
507+
- MAX_AGE: **8760h** Max age of the time, default is 365 days.
508+
- INCLUDE_SUB_DOMAINS: **false** require sub domains use hsts, default is false.
509+
- SEND_PRELOAD_DIRECTIVE: **false** send preload, default is false.
510+
504511
## Other (`other`)
505512

506513
- `SHOW_FOOTER_BRANDING`: **false**: Show Gitea branding in the footer.

docs/content/doc/advanced/config-cheat-sheet.zh-cn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,12 @@ IS_INPUT_FILE = false
237237
- RENDER_COMMAND: 工具的命令行命令及参数。
238238
- IS_INPUT_FILE: 输入方式是最后一个参数为文件路径还是从标准输入读取。
239239

240+
## HSTS (`hsts`)
240241

242+
- ENABLED: **false** 是否启用 HSTS。默认为否。
243+
- MAX_AGE: **8760h** 最大时间,默认是 365 天。
244+
- INCLUDE_SUB_DOMAINS: **false** 是否包含子域名,默认为否。
245+
- SEND_PRELOAD_DIRECTIVE: **false** 是否预加载,默认为否。
241246

242247
## Other (`other`)
243248

modules/setting/hsts.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ const (
1414
var HSTS = struct {
1515
Enabled bool
1616
MaxAge time.Duration
17+
IncludeSubDomains bool
1718
SendPreloadDirective bool
1819
}{
1920
Enabled: false,
2021
MaxAge: defaultMaxAge,
22+
IncludeSubDomains: false,
2123
SendPreloadDirective: false,
2224
}
2325

@@ -29,5 +31,6 @@ func configHSTS() {
2931

3032
HSTS.Enabled = true
3133
HSTS.MaxAge = sec.Key("MAX_AGE").MustDuration(defaultMaxAge)
34+
HSTS.IncludeSubDomains = sec.Key("INCLUDE_SUB_DOMAINS").MustBool()
3235
HSTS.SendPreloadDirective = sec.Key("SEND_PRELOAD_DIRECTIVE").MustBool()
3336
}

routers/routes/routes.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,12 @@ func RouterHandler(level log.Level) func(ctx *macaron.Context) {
103103
}
104104
}
105105

106-
func createHeaderValueNew(maxAge time.Duration, sendPreloadDirective bool) string {
106+
func createHeaderValueNew(maxAge time.Duration, includeSubDomains, sendPreloadDirective bool) string {
107107
buf := bytes.NewBufferString("max-age=")
108108
buf.WriteString(strconv.Itoa(int(maxAge.Seconds())))
109-
buf.WriteString("; includeSubDomains")
109+
if includeSubDomains {
110+
buf.WriteString("; includeSubDomains")
111+
}
110112
if sendPreloadDirective {
111113
buf.WriteString("; preload")
112114
}
@@ -145,7 +147,8 @@ func NewMacaron() *macaron.Macaron {
145147
if setting.HSTS.Enabled {
146148
m.Use(func() macaron.Handler {
147149
return func(ctx *macaron.Context) {
148-
ctx.Resp.Header().Set("Strict-Transport-Security", createHeaderValueNew(setting.HSTS.MaxAge, setting.HSTS.SendPreloadDirective))
150+
ctx.Resp.Header().Set("Strict-Transport-Security",
151+
createHeaderValueNew(setting.HSTS.MaxAge, setting.HSTS.IncludeSubDomains, setting.HSTS.SendPreloadDirective))
149152
}
150153
})
151154
}

0 commit comments

Comments
 (0)