Skip to content

Commit f367785

Browse files
committed
Service provider fixes to work as expected.
- Fixed script hash to remove JS console errors when redirecting - Fixed samlsp.New method to not overwrite m.ServiceProvider.IDPMetadata
1 parent 446329e commit f367785

File tree

5 files changed

+19
-17
lines changed

5 files changed

+19
-17
lines changed

samlsp/middleware.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,10 @@ func (m *Middleware) RequireAccount(handler http.Handler) http.Handler {
160160
return
161161
}
162162
if binding == saml.HTTPPostBinding {
163-
w.Header().Set("Content-Security-Policy", ""+
163+
w.Header().Add("Content-Security-Policy", ""+
164164
"default-src; "+
165-
"script-src 'sha256-D8xB+y+rJ90RmLdP72xBqEEc0NUatn7yuCND0orkrgk='; "+
166-
"reflected-xss block; "+
167-
"referrer no-referrer;")
165+
"script-src 'sha256-AjPdJSbZmeWHnEc5ykvJFay8FTWeTeRbs9dutfZ0HqE='; "+
166+
"reflected-xss block; referrer no-referrer;")
168167
w.Header().Add("Content-type", "text/html")
169168
w.Write([]byte(`<!DOCTYPE html><html><body>`))
170169
w.Write(req.Post(relayState))
@@ -243,7 +242,7 @@ func (m *Middleware) Authorize(w http.ResponseWriter, r *http.Request, assertion
243242

244243
// delete the cookie
245244
stateCookie.Value = ""
246-
stateCookie.Expires = time.Unix(1,0) // past time as close to epoch as possible, but not zero time.Time{}
245+
stateCookie.Expires = time.Unix(1, 0) // past time as close to epoch as possible, but not zero time.Time{}
247246
http.SetCookie(w, stateCookie)
248247
}
249248

samlsp/middleware_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,13 @@ func (test *MiddlewareTest) TestRequireAccountNoCredsPostBinding(c *C) {
176176
"<input type=\"hidden\" name=\"RelayState\" value=\"KCosLjAyNDY4Ojw-QEJERkhKTE5QUlRWWFpcXmBiZGZoamxucHJ0dnh6\" />"+
177177
"<input id=\"SAMLSubmitButton\" type=\"submit\" value=\"Submit\" />"+
178178
"</form>"+
179-
"<script>document.getElementById('SAMLSubmitButton').style.visibility=\"hidden\";</script>"+
180-
"<script>document.getElementById('SAMLRequestForm').submit();</script>"+
179+
"<script>document.getElementById('SAMLSubmitButton').style.visibility=\"hidden\";"+
180+
"document.getElementById('SAMLRequestForm').submit();</script>"+
181181
"</body>"+
182182
"</html>")
183183

184184
// check that the CSP script hash is set correctly
185-
scriptContent := "document.getElementById('SAMLRequestForm').submit();"
185+
scriptContent := "document.getElementById('SAMLSubmitButton').style.visibility=\"hidden\";document.getElementById('SAMLRequestForm').submit();"
186186
scriptSum := sha256.Sum256([]byte(scriptContent))
187187
scriptHash := base64.StdEncoding.EncodeToString(scriptSum[:])
188188
c.Assert(resp.Header().Get("Content-Security-Policy"), Equals,

samlsp/samlsp.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,29 +90,32 @@ func New(opts Options) (*Middleware, error) {
9090
continue
9191
}
9292

93-
entity := &saml.EntityDescriptor{}
93+
entity := new(saml.EntityDescriptor)
9494
err = xml.Unmarshal(data, entity)
9595

9696
// this comparison is ugly, but it is how the error is generated in encoding/xml
9797
if err != nil && err.Error() == "expected element type <EntityDescriptor> but have <EntitiesDescriptor>" {
98-
entities := &saml.EntitiesDescriptor{}
99-
if err := xml.Unmarshal(data, entities); err != nil {
98+
entities := new(saml.EntitiesDescriptor)
99+
if err = xml.Unmarshal(data, entities); err != nil {
100100
return nil, err
101101
}
102102

103103
err = fmt.Errorf("no entity found with IDPSSODescriptor")
104-
for _, e := range entities.EntityDescriptors {
104+
for j := range entities.EntityDescriptors {
105+
e := entities.EntityDescriptors[j]
105106
if len(e.IDPSSODescriptors) > 0 {
106107
entity = &e
107108
err = nil
108109
}
109110
}
110111
}
112+
113+
m.ServiceProvider.IDPMetadata = entity
114+
111115
if err != nil {
112116
return nil, err
113117
}
114118

115-
m.ServiceProvider.IDPMetadata = entity
116119
return m, nil
117120
}
118121

service_provider.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,8 @@ func (req *AuthnRequest) Post(relayState string) []byte {
304304
`<input type="hidden" name="RelayState" value="{{.RelayState}}" />` +
305305
`<input id="SAMLSubmitButton" type="submit" value="Submit" />` +
306306
`</form>` +
307-
`<script>document.getElementById('SAMLSubmitButton').style.visibility="hidden";</script>` +
308-
`<script>document.getElementById('SAMLRequestForm').submit();</script>`))
307+
`<script>document.getElementById('SAMLSubmitButton').style.visibility="hidden";` +
308+
`document.getElementById('SAMLRequestForm').submit();</script>`))
309309
data := struct {
310310
URL string
311311
SAMLRequest string

service_provider_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ func (test *ServiceProviderTest) TestCanProducePostRequest(c *C) {
188188
`<input type="hidden" name="SAMLRequest" value="PHNhbWxwOkF1dGhuUmVxdWVzdCB4bWxuczpzYW1sPSJ1cm46b2FzaXM6bmFtZXM6dGM6U0FNTDoyLjA6YXNzZXJ0aW9uIiB4bWxuczpzYW1scD0idXJuOm9hc2lzOm5hbWVzOnRjOlNBTUw6Mi4wOnByb3RvY29sIiBJRD0iaWQtMDAwMjA0MDYwODBhMGMwZTEwMTIxNDE2MTgxYTFjMWUyMDIyMjQyNiIgVmVyc2lvbj0iMi4wIiBJc3N1ZUluc3RhbnQ9IjIwMTUtMTItMDFUMDE6MzE6MjFaIiBEZXN0aW5hdGlvbj0iaHR0cHM6Ly9pZHAudGVzdHNoaWIub3JnL2lkcC9wcm9maWxlL1NBTUwyL1BPU1QvU1NPIiBBc3NlcnRpb25Db25zdW1lclNlcnZpY2VVUkw9Imh0dHBzOi8vMTU2NjE0NDQubmdyb2suaW8vc2FtbDIvYWNzIiBQcm90b2NvbEJpbmRpbmc9InVybjpvYXNpczpuYW1lczp0YzpTQU1MOjIuMDpiaW5kaW5nczpIVFRQLVBPU1QiPjxzYW1sOklzc3VlciBGb3JtYXQ9InVybjpvYXNpczpuYW1lczp0YzpTQU1MOjIuMDpuYW1laWQtZm9ybWF0OmVudGl0eSI&#43;aHR0cHM6Ly8xNTY2MTQ0NC5uZ3Jvay5pby9zYW1sMi9tZXRhZGF0YTwvc2FtbDpJc3N1ZXI&#43;PHNhbWxwOk5hbWVJRFBvbGljeSBGb3JtYXQ9InVybjpvYXNpczpuYW1lczp0YzpTQU1MOjIuMDpuYW1laWQtZm9ybWF0OnRyYW5zaWVudCIgQWxsb3dDcmVhdGU9InRydWUiLz48L3NhbWxwOkF1dGhuUmVxdWVzdD4=" />`+
189189
`<input type="hidden" name="RelayState" value="relayState" />`+
190190
`<input id="SAMLSubmitButton" type="submit" value="Submit" /></form>`+
191-
`<script>document.getElementById('SAMLSubmitButton').style.visibility="hidden";</script>`+
192-
`<script>document.getElementById('SAMLRequestForm').submit();</script>`)
191+
`<script>document.getElementById('SAMLSubmitButton').style.visibility="hidden";`+
192+
`document.getElementById('SAMLRequestForm').submit();</script>`)
193193
}
194194

195195
func (test *ServiceProviderTest) TestCanHandleOneloginResponse(c *C) {

0 commit comments

Comments
 (0)