Skip to content

Commit 6b4f886

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 6b4f886

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

samlsp/middleware.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,9 @@ 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='; ")
168166
w.Header().Add("Content-type", "text/html")
169167
w.Write([]byte(`<!DOCTYPE html><html><body>`))
170168
w.Write(req.Post(relayState))
@@ -243,7 +241,7 @@ func (m *Middleware) Authorize(w http.ResponseWriter, r *http.Request, assertion
243241

244242
// delete the cookie
245243
stateCookie.Value = ""
246-
stateCookie.Expires = time.Unix(1,0) // past time as close to epoch as possible, but not zero time.Time{}
244+
stateCookie.Expires = time.Unix(1, 0) // past time as close to epoch as possible, but not zero time.Time{}
247245
http.SetCookie(w, stateCookie)
248246
}
249247

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

0 commit comments

Comments
 (0)