[gddo] FIX: Missing HSTS-header

391 views
Skip to first unread message

Gerrit Bot (Gerrit)

unread,
Jul 4, 2018, 12:19:12 AM7/4/18
to Ian Lance Taylor, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Gerrit Bot has uploaded this change for review.

View Change

FIX: Missing HSTS-header

ADD HSTS header to godoc.org

Please review my changes

# Related
https://github.com/golang/go/issues/26162

Change-Id: I0680e5be76450c5b3e047b7a22169d0a09935e48
GitHub-Last-Rev: d00fa25781702672c65d1c50860cb325b490c6e3
GitHub-Pull-Request: golang/gddo#562
---
A gddo-server/debug.test
M gddo-server/main.go
M gddo-server/main_test.go
3 files changed, 31 insertions(+), 2 deletions(-)

diff --git a/gddo-server/debug.test b/gddo-server/debug.test
new file mode 100755
index 0000000..333bcc3
--- /dev/null
+++ b/gddo-server/debug.test
Binary files differ
diff --git a/gddo-server/main.go b/gddo-server/main.go
index 2137cb9..55fa664 100644
--- a/gddo-server/main.go
+++ b/gddo-server/main.go
@@ -989,6 +989,18 @@
s.root.ServeHTTP(w, r)
}

+const HSTSHeaderKey = "Strict-Transport-Security"
+
+func middlewareHSTS(next http.Handler) http.Handler {
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ // This enforces the use of HTTPS for 1 year, including present and future subdomains.
+ // Chrome and Mozilla Firefox maintain an HSTS preload list
+ // that automatically informs the browser that the website can only be accessed through HTTPS.
+ w.Header().Set(HSTSHeaderKey, "max-age=31536000; includeSubDomains; preload")
+ next.ServeHTTP(w, r)
+ })
+}
+
func main() {
ctx := context.Background()
v, err := loadConfig(ctx, os.Args)
@@ -1016,8 +1028,9 @@
}
}
}()
- http.Handle("/", s)
- log.Fatal(http.ListenAndServe(s.v.GetString(ConfigBindAddress), s))
+ ss := middlewareHSTS(s)
+ http.Handle("/", ss)
+ log.Fatal(http.ListenAndServe(s.v.GetString(ConfigBindAddress), ss))
}

// removeInternal removes the internal packages from the given package
diff --git a/gddo-server/main_test.go b/gddo-server/main_test.go
index 6d42906..4d2232c 100644
--- a/gddo-server/main_test.go
+++ b/gddo-server/main_test.go
@@ -7,11 +7,15 @@
package main

import (
+ "io"
+ "net/http"
+ "net/http/httptest"
"reflect"
"testing"

"github.com/golang/gddo/database"
"github.com/golang/gddo/doc"
+ "github.com/stretchr/testify/require"
)

var robotTests = []string{
@@ -111,3 +115,15 @@
})
}
}
+
+func TestMiddlewareHSTS(t *testing.T) {
+ require := require.New(t)
+ req := httptest.NewRequest(http.MethodGet, "/", nil)
+ respRecorder := httptest.NewRecorder()
+ handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ io.WriteString(w, "")
+ })
+ handlerWithMiddlewareHSTS := middlewareHSTS(handler)
+ handlerWithMiddlewareHSTS.ServeHTTP(respRecorder, req)
+ require.Equal("max-age=31536000; includeSubDomains; preload", respRecorder.Header().Get(HSTSHeaderKey))
+}

To view, visit change 122195. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: gddo
Gerrit-Branch: master
Gerrit-Change-Id: I0680e5be76450c5b3e047b7a22169d0a09935e48
Gerrit-Change-Number: 122195
Gerrit-PatchSet: 1
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-MessageType: newchange

Gobot Gobot (Gerrit)

unread,
Jul 4, 2018, 12:19:16 AM7/4/18
to Gerrit Bot, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Congratulations on opening your first change. Thank you for your contribution!

Next steps:
Within the next week or so, a maintainer will review your change and provide
feedback. See https://golang.org/doc/contribute.html#review for more info and
tips to get your patch through code review.

Most changes in the Go project go through a few rounds of revision. This can be
surprising to people new to the project. The careful, iterative review process
is our way of helping mentor contributors and ensuring that their contributions
have a lasting impact.

During May-July and Nov-Jan the Go project is in a code freeze, during which
little code gets reviewed or merged. If a reviewer responds with a comment like
R=go1.11, it means that this CL will be reviewed as part of the next development
cycle. See https://golang.org/s/release for more details.

View Change

    To view, visit change 122195. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: gddo
    Gerrit-Branch: master
    Gerrit-Change-Id: I0680e5be76450c5b3e047b7a22169d0a09935e48
    Gerrit-Change-Number: 122195
    Gerrit-PatchSet: 1
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-CC: Gobot Gobot <go...@golang.org>
    Gerrit-Comment-Date: Wed, 04 Jul 2018 04:19:14 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: No
    Gerrit-MessageType: comment

    Gerrit Bot (Gerrit)

    unread,
    Jul 4, 2018, 12:31:25 AM7/4/18
    to goph...@pubsubhelper.golang.org, Gobot Gobot, golang-co...@googlegroups.com

    Gerrit Bot uploaded patch set #2 to this change.

    View Change

    FIX: Missing HSTS-header

    ADD HSTS header to godoc.org

    Please review my changes

    # Related
    https://github.com/golang/go/issues/26162

    Change-Id: I0680e5be76450c5b3e047b7a22169d0a09935e48
    GitHub-Last-Rev: 5dc784b1ce3c6c2191b7823a8f93a6a21a388553

    GitHub-Pull-Request: golang/gddo#562
    ---
    A gddo-server/debug.test
    M gddo-server/main.go
    M gddo-server/main_test.go
    A vendor/github.com/stretchr/testify
    4 files changed, 32 insertions(+), 2 deletions(-)

    To view, visit change 122195. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: gddo
    Gerrit-Branch: master
    Gerrit-Change-Id: I0680e5be76450c5b3e047b7a22169d0a09935e48
    Gerrit-Change-Number: 122195
    Gerrit-PatchSet: 2
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-CC: Gobot Gobot <go...@golang.org>
    Gerrit-MessageType: newpatchset

    Gerrit Bot (Gerrit)

    unread,
    Jul 4, 2018, 12:38:50 AM7/4/18
    to goph...@pubsubhelper.golang.org, Gobot Gobot, golang-co...@googlegroups.com

    Gerrit Bot uploaded patch set #3 to this change.

    View Change

    FIX: Missing HSTS-header

    ADD HSTS header to godoc.org

    Please review my changes

    # Related
    https://github.com/golang/go/issues/26162

    Change-Id: I0680e5be76450c5b3e047b7a22169d0a09935e48
    GitHub-Last-Rev: 4104bd14fce765b5e525dfcc2a8f13663ea02a33

    GitHub-Pull-Request: golang/gddo#562
    ---
    A gddo-server/debug.test
    M gddo-server/main.go
    M gddo-server/main_test.go
    A vendor/github.com/stretchr/testify
    4 files changed, 35 insertions(+), 2 deletions(-)

    To view, visit change 122195. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: gddo
    Gerrit-Branch: master
    Gerrit-Change-Id: I0680e5be76450c5b3e047b7a22169d0a09935e48
    Gerrit-Change-Number: 122195
    Gerrit-PatchSet: 3

    Gerrit Bot (Gerrit)

    unread,
    Jul 4, 2018, 12:45:55 AM7/4/18
    to goph...@pubsubhelper.golang.org, Gobot Gobot, golang-co...@googlegroups.com

    Gerrit Bot uploaded patch set #4 to this change.

    View Change

    FIX: Missing HSTS-header

    ADD HSTS header to godoc.org

    Please review my changes

    # Related
    https://github.com/golang/go/issues/26162

    Change-Id: I0680e5be76450c5b3e047b7a22169d0a09935e48
    GitHub-Last-Rev: 86c197da8cffa2858b4362e33a911cf3d4d96b8b

    GitHub-Pull-Request: golang/gddo#562
    ---
    A gddo-server/debug.test
    M gddo-server/main.go
    M gddo-server/main_test.go
    3 files changed, 33 insertions(+), 2 deletions(-)

    To view, visit change 122195. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: gddo
    Gerrit-Branch: master
    Gerrit-Change-Id: I0680e5be76450c5b3e047b7a22169d0a09935e48
    Gerrit-Change-Number: 122195
    Gerrit-PatchSet: 4

    Akhil Indurti (Gerrit)

    unread,
    Jul 4, 2018, 1:18:23 AM7/4/18
    to Gerrit Bot, goph...@pubsubhelper.golang.org, Gobot Gobot, golang-co...@googlegroups.com

    View Change

    2 comments:

    • File gddo-server/main.go:

      • Patch Set #4, Line 994: func middlewareHSTS(next http.Handler) http.Handler {

        Maybe declare this in gddo/httputil instead:

            func hsts(h http.Handler) http.Handler.
      • Patch Set #4, Line 999: HSTSHeaderKey

        Minor nitpick, but this const header declaration isn't necessary. Follow the convention in the rest of the file:

            w.Header().Add("Strict-Transport-Security", "max-age=31536000; includeSubDomains; preload")

    To view, visit change 122195. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: gddo
    Gerrit-Branch: master
    Gerrit-Change-Id: I0680e5be76450c5b3e047b7a22169d0a09935e48
    Gerrit-Change-Number: 122195
    Gerrit-PatchSet: 4
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-CC: Akhil Indurti <aind...@gmail.com>
    Gerrit-CC: Gobot Gobot <go...@golang.org>
    Gerrit-Comment-Date: Wed, 04 Jul 2018 05:18:20 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    Gerrit-MessageType: comment

    Gerrit Bot (Gerrit)

    unread,
    Jul 4, 2018, 3:58:45 AM7/4/18
    to goph...@pubsubhelper.golang.org, Akhil Indurti, Gobot Gobot, golang-co...@googlegroups.com

    Gerrit Bot uploaded patch set #5 to this change.

    View Change

    FIX: Missing HSTS-header

    ADD HSTS header to godoc.org

    Please review my changes

    # Related
    https://github.com/golang/go/issues/26162

    Change-Id: I0680e5be76450c5b3e047b7a22169d0a09935e48
    GitHub-Last-Rev: f9314757afd2be14ad6ed0793522ec8cb41440a9

    GitHub-Pull-Request: golang/gddo#562
    ---
    A gddo-server/debug.test
    M gddo-server/main.go
    A httputil/middleware.go
    A httputil/middleware_test.go
    4 files changed, 40 insertions(+), 2 deletions(-)

    To view, visit change 122195. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: gddo
    Gerrit-Branch: master
    Gerrit-Change-Id: I0680e5be76450c5b3e047b7a22169d0a09935e48
    Gerrit-Change-Number: 122195
    Gerrit-PatchSet: 5
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-CC: Akhil Indurti <aind...@gmail.com>
    Gerrit-CC: Gobot Gobot <go...@golang.org>
    Gerrit-MessageType: newpatchset

    Gerrit Bot (Gerrit)

    unread,
    Jul 4, 2018, 4:08:18 AM7/4/18
    to goph...@pubsubhelper.golang.org, Akhil Indurti, Alpha WONG, Gobot Gobot, golang-co...@googlegroups.com

    Gerrit Bot uploaded patch set #6 to this change.

    View Change

    FIX: Missing HSTS-header

    ADD HSTS header to godoc.org

    Please review my changes

    # Related
    https://github.com/golang/go/issues/26162

    Change-Id: I0680e5be76450c5b3e047b7a22169d0a09935e48
    GitHub-Last-Rev: 989725ac16f4846c15033cf661c078d9ebf68dfa
    GitHub-Pull-Request: golang/gddo#562
    ---

    M gddo-server/main.go
    A httputil/middleware.go
    A httputil/middleware_test.go
    3 files changed, 40 insertions(+), 2 deletions(-)

    To view, visit change 122195. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: gddo
    Gerrit-Branch: master
    Gerrit-Change-Id: I0680e5be76450c5b3e047b7a22169d0a09935e48
    Gerrit-Change-Number: 122195
    Gerrit-PatchSet: 6
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-CC: Akhil Indurti <aind...@gmail.com>
    Gerrit-CC: Alpha WONG <jhex...@gmail.com>

    Alpha WONG (Gerrit)

    unread,
    Jul 4, 2018, 1:36:36 PM7/4/18
    to Gerrit Bot, goph...@pubsubhelper.golang.org, Akhil Indurti, Gobot Gobot, golang-co...@googlegroups.com

    Patch Set 4:

    (2 comments)

    I change it according to your review

    View Change

      To view, visit change 122195. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-Project: gddo
      Gerrit-Branch: master
      Gerrit-Change-Id: I0680e5be76450c5b3e047b7a22169d0a09935e48
      Gerrit-Change-Number: 122195
      Gerrit-PatchSet: 5
      Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
      Gerrit-CC: Akhil Indurti <aind...@gmail.com>
      Gerrit-CC: Alpha WONG <jhex...@gmail.com>
      Gerrit-CC: Gobot Gobot <go...@golang.org>
      Gerrit-Comment-Date: Wed, 04 Jul 2018 08:00:33 +0000

      Akhil Indurti (Gerrit)

      unread,
      Jul 4, 2018, 2:26:04 PM7/4/18
      to Gerrit Bot, goph...@pubsubhelper.golang.org, Brad Fitzpatrick, Alpha WONG, Gobot Gobot, golang-co...@googlegroups.com

      LGTM, but I haven't reviewed for godoc.org before so I'll ping Brad.

      View Change

      2 comments:

        • // This enforces the use of HTTPS for 1 year, including present and future subdomains.

        • 		// Chrome and Mozilla Firefox maintain an HSTS preload list

        • 		// that automatically informs the browser that the website can only be accessed through HTTPS.

        • This comment should be moved up to be documentation for the exported function.

        • Patch Set #6, Line 10: // issue : https://github.com/golang/go/issues/26162

          This comment is not related to the HSTS function, but the gddo-server's usage of httputil.HSTS. Move it to gddo-server/main.go where httputil.HSTS is called.

          Also if you reference an issue number, try to use golang.org/issue/26162 or #26162 to make the issue agnostic to the tracker (github).

      To view, visit change 122195. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-Project: gddo
      Gerrit-Branch: master
      Gerrit-Change-Id: I0680e5be76450c5b3e047b7a22169d0a09935e48
      Gerrit-Change-Number: 122195
      Gerrit-PatchSet: 6
      Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
      Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
      Gerrit-CC: Akhil Indurti <aind...@gmail.com>
      Gerrit-CC: Alpha WONG <jhex...@gmail.com>
      Gerrit-CC: Gobot Gobot <go...@golang.org>
      Gerrit-Comment-Date: Wed, 04 Jul 2018 18:26:01 +0000

      Alpha WONG (Gerrit)

      unread,
      Jul 4, 2018, 9:24:18 PM7/4/18
      to Gerrit Bot, goph...@pubsubhelper.golang.org, Brad Fitzpatrick, Akhil Indurti, Gobot Gobot, golang-co...@googlegroups.com

      Patch Set 6:

      (2 comments)

      LGTM, but I haven't reviewed for godoc.org before so I'll ping Brad.

      Sure, let me clear it up.
      thx

      View Change

        To view, visit change 122195. To unsubscribe, or for help writing mail filters, visit settings.

        Gerrit-Project: gddo
        Gerrit-Branch: master
        Gerrit-Change-Id: I0680e5be76450c5b3e047b7a22169d0a09935e48
        Gerrit-Change-Number: 122195
        Gerrit-PatchSet: 6
        Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
        Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
        Gerrit-CC: Akhil Indurti <aind...@gmail.com>
        Gerrit-CC: Alpha WONG <jhex...@gmail.com>
        Gerrit-CC: Gobot Gobot <go...@golang.org>
        Gerrit-Comment-Date: Thu, 05 Jul 2018 01:24:13 +0000

        Gerrit Bot (Gerrit)

        unread,
        Jul 4, 2018, 11:04:26 PM7/4/18
        to Brad Fitzpatrick, goph...@pubsubhelper.golang.org, Akhil Indurti, Alpha WONG, Gobot Gobot, golang-co...@googlegroups.com

        Gerrit Bot uploaded patch set #7 to this change.

        View Change

        FIX: Missing HSTS-header

        ADD HSTS header to godoc.org

        Please review my changes

        # Related
        https://github.com/golang/go/issues/26162

        Change-Id: I0680e5be76450c5b3e047b7a22169d0a09935e48
        GitHub-Last-Rev: c80d1b2c865074821f631c1ab38070d0ad6f0a49

        GitHub-Pull-Request: golang/gddo#562
        ---
        M gddo-server/main.go
        A httputil/middleware.go
        A httputil/middleware_test.go
        3 files changed, 40 insertions(+), 2 deletions(-)

        To view, visit change 122195. To unsubscribe, or for help writing mail filters, visit settings.

        Gerrit-Project: gddo
        Gerrit-Branch: master
        Gerrit-Change-Id: I0680e5be76450c5b3e047b7a22169d0a09935e48
        Gerrit-Change-Number: 122195
        Gerrit-PatchSet: 7
        Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
        Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
        Gerrit-CC: Akhil Indurti <aind...@gmail.com>
        Gerrit-CC: Alpha WONG <jhex...@gmail.com>
        Gerrit-CC: Gobot Gobot <go...@golang.org>
        Gerrit-MessageType: newpatchset

        Martin Tournoij (Gerrit)

        unread,
        Aug 23, 2018, 1:14:52 PM8/23/18
        to Gerrit Bot, goph...@pubsubhelper.golang.org, Brad Fitzpatrick, Alpha WONG, Akhil Indurti, Gobot Gobot, golang-co...@googlegroups.com

        View Change

        1 comment:

        • File gddo-server/main.go:

          • Patch Set #7, Line 1019: s

            What if I am running a local gddo instance and don't want HSTS enabled, or enable it for a shorter period?

            One of the downsides of HSTS is that you can't just disable it after you made a mistake, and browsers don't allow users to easily override wrong settings. Enabling it by default can be quite annoying. I think it may be worth considering making an option for this, which should perhaps even default to off.

        To view, visit change 122195. To unsubscribe, or for help writing mail filters, visit settings.

        Gerrit-Project: gddo
        Gerrit-Branch: master
        Gerrit-Change-Id: I0680e5be76450c5b3e047b7a22169d0a09935e48
        Gerrit-Change-Number: 122195
        Gerrit-PatchSet: 7
        Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
        Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
        Gerrit-CC: Akhil Indurti <aind...@gmail.com>
        Gerrit-CC: Alpha WONG <jhex...@gmail.com>
        Gerrit-CC: Gobot Gobot <go...@golang.org>
        Gerrit-CC: Martin Tournoij <mar...@arp242.net>
        Gerrit-Comment-Date: Thu, 23 Aug 2018 17:14:48 +0000
        Reply all
        Reply to author
        Forward
        0 new messages