Skip to content

Commit

Permalink
Fixed script hash to remove JS console errors when redirecting (crewj…
Browse files Browse the repository at this point in the history
  • Loading branch information
crewjam authored Sep 25, 2017
1 parent 56801ab commit 50777a1
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 11 deletions.
7 changes: 3 additions & 4 deletions samlsp/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,10 @@ func (m *Middleware) RequireAccount(handler http.Handler) http.Handler {
return
}
if binding == saml.HTTPPostBinding {
w.Header().Set("Content-Security-Policy", ""+
w.Header().Add("Content-Security-Policy", ""+
"default-src; "+
"script-src 'sha256-D8xB+y+rJ90RmLdP72xBqEEc0NUatn7yuCND0orkrgk='; "+
"reflected-xss block; "+
"referrer no-referrer;")
"script-src 'sha256-AjPdJSbZmeWHnEc5ykvJFay8FTWeTeRbs9dutfZ0HqE='; "+
"reflected-xss block; referrer no-referrer;")
w.Header().Add("Content-type", "text/html")
w.Write([]byte(`<!DOCTYPE html><html><body>`))
w.Write(req.Post(relayState))
Expand Down
6 changes: 3 additions & 3 deletions samlsp/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,13 @@ func (test *MiddlewareTest) TestRequireAccountNoCredsPostBinding(c *C) {
"<input type=\"hidden\" name=\"RelayState\" value=\"KCosLjAyNDY4Ojw-QEJERkhKTE5QUlRWWFpcXmBiZGZoamxucHJ0dnh6\" />"+
"<input id=\"SAMLSubmitButton\" type=\"submit\" value=\"Submit\" />"+
"</form>"+
"<script>document.getElementById('SAMLSubmitButton').style.visibility=\"hidden\";</script>"+
"<script>document.getElementById('SAMLRequestForm').submit();</script>"+
"<script>document.getElementById('SAMLSubmitButton').style.visibility=\"hidden\";"+
"document.getElementById('SAMLRequestForm').submit();</script>"+
"</body>"+
"</html>")

// check that the CSP script hash is set correctly
scriptContent := "document.getElementById('SAMLRequestForm').submit();"
scriptContent := "document.getElementById('SAMLSubmitButton').style.visibility=\"hidden\";document.getElementById('SAMLRequestForm').submit();"
scriptSum := sha256.Sum256([]byte(scriptContent))
scriptHash := base64.StdEncoding.EncodeToString(scriptSum[:])
c.Assert(resp.Header().Get("Content-Security-Policy"), Equals,
Expand Down
4 changes: 2 additions & 2 deletions service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,8 @@ func (req *AuthnRequest) Post(relayState string) []byte {
`<input type="hidden" name="RelayState" value="{{.RelayState}}" />` +
`<input id="SAMLSubmitButton" type="submit" value="Submit" />` +
`</form>` +
`<script>document.getElementById('SAMLSubmitButton').style.visibility="hidden";</script>` +
`<script>document.getElementById('SAMLRequestForm').submit();</script>`))
`<script>document.getElementById('SAMLSubmitButton').style.visibility="hidden";` +
`document.getElementById('SAMLRequestForm').submit();</script>`))
data := struct {
URL string
SAMLRequest string
Expand Down
4 changes: 2 additions & 2 deletions service_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ func (test *ServiceProviderTest) TestCanProducePostRequest(c *C) {
`<input type="hidden" name="SAMLRequest" value="PHNhbWxwOkF1dGhuUmVxdWVzdCB4bWxuczpzYW1sPSJ1cm46b2FzaXM6bmFtZXM6dGM6U0FNTDoyLjA6YXNzZXJ0aW9uIiB4bWxuczpzYW1scD0idXJuOm9hc2lzOm5hbWVzOnRjOlNBTUw6Mi4wOnByb3RvY29sIiBJRD0iaWQtMDAwMjA0MDYwODBhMGMwZTEwMTIxNDE2MTgxYTFjMWUyMDIyMjQyNiIgVmVyc2lvbj0iMi4wIiBJc3N1ZUluc3RhbnQ9IjIwMTUtMTItMDFUMDE6MzE6MjFaIiBEZXN0aW5hdGlvbj0iaHR0cHM6Ly9pZHAudGVzdHNoaWIub3JnL2lkcC9wcm9maWxlL1NBTUwyL1BPU1QvU1NPIiBBc3NlcnRpb25Db25zdW1lclNlcnZpY2VVUkw9Imh0dHBzOi8vMTU2NjE0NDQubmdyb2suaW8vc2FtbDIvYWNzIiBQcm90b2NvbEJpbmRpbmc9InVybjpvYXNpczpuYW1lczp0YzpTQU1MOjIuMDpiaW5kaW5nczpIVFRQLVBPU1QiPjxzYW1sOklzc3VlciBGb3JtYXQ9InVybjpvYXNpczpuYW1lczp0YzpTQU1MOjIuMDpuYW1laWQtZm9ybWF0OmVudGl0eSI&#43;aHR0cHM6Ly8xNTY2MTQ0NC5uZ3Jvay5pby9zYW1sMi9tZXRhZGF0YTwvc2FtbDpJc3N1ZXI&#43;PHNhbWxwOk5hbWVJRFBvbGljeSBGb3JtYXQ9InVybjpvYXNpczpuYW1lczp0YzpTQU1MOjIuMDpuYW1laWQtZm9ybWF0OnRyYW5zaWVudCIgQWxsb3dDcmVhdGU9InRydWUiLz48L3NhbWxwOkF1dGhuUmVxdWVzdD4=" />`+
`<input type="hidden" name="RelayState" value="relayState" />`+
`<input id="SAMLSubmitButton" type="submit" value="Submit" /></form>`+
`<script>document.getElementById('SAMLSubmitButton').style.visibility="hidden";</script>`+
`<script>document.getElementById('SAMLRequestForm').submit();</script>`)
`<script>document.getElementById('SAMLSubmitButton').style.visibility="hidden";`+
`document.getElementById('SAMLRequestForm').submit();</script>`)
}

func (test *ServiceProviderTest) TestCanHandleOneloginResponse(c *C) {
Expand Down

0 comments on commit 50777a1

Please sign in to comment.