|
| 1 | +// Copyright 2025 Russell Haering et al. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package fuzz |
| 16 | + |
| 17 | +import ( |
| 18 | + "encoding/base64" |
| 19 | + "encoding/binary" |
| 20 | + "testing" |
| 21 | + |
| 22 | + saml2 "github.com/russellhaering/gosaml2" |
| 23 | +) |
| 24 | + |
| 25 | +func FuzzDecodeResponse(f *testing.F) { |
| 26 | + f.Fuzz(func(t *testing.T, data []byte) { |
| 27 | + encodedResponse := base64.StdEncoding.EncodeToString(data) |
| 28 | + |
| 29 | + _, err := saml2.DecodeUnverifiedBaseResponse(encodedResponse) |
| 30 | + if err != nil { |
| 31 | + return |
| 32 | + } |
| 33 | + |
| 34 | + sp := &saml2.SAMLServiceProvider{} |
| 35 | + _, _ = sp.ValidateEncodedResponse(encodedResponse) |
| 36 | + }) |
| 37 | +} |
| 38 | + |
| 39 | +func FuzzLogoutResponse(f *testing.F) { |
| 40 | + f.Fuzz(func(t *testing.T, data []byte) { |
| 41 | + encodedResponse := base64.StdEncoding.EncodeToString(data) |
| 42 | + |
| 43 | + _, err := saml2.DecodeUnverifiedLogoutResponse(encodedResponse) |
| 44 | + if err != nil { |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + sp := &saml2.SAMLServiceProvider{} |
| 49 | + _, _ = sp.ValidateEncodedLogoutResponsePOST(encodedResponse) |
| 50 | + }) |
| 51 | +} |
| 52 | + |
| 53 | +func FuzzBuildRequest(f *testing.F) { |
| 54 | + f.Fuzz(func(t *testing.T, data []byte) { |
| 55 | + if len(data) < 8 { |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + idValue := binary.LittleEndian.Uint64(data[:8]) |
| 60 | + relayState := string(data[8:]) |
| 61 | + |
| 62 | + if len(relayState) == 0 { |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + sp := &saml2.SAMLServiceProvider{ |
| 67 | + IdentityProviderSSOURL: "https://idp.example.com/sso", |
| 68 | + IdentityProviderIssuer: "https://idp.example.com/", |
| 69 | + AssertionConsumerServiceURL: "https://sp.example.com/acs", |
| 70 | + AudienceURI: "https://sp.example.com/audience", |
| 71 | + SignAuthnRequests: idValue%2 == 0, |
| 72 | + ForceAuthn: idValue%3 == 0, |
| 73 | + IsPassive: idValue%5 == 0, |
| 74 | + } |
| 75 | + |
| 76 | + _, _ = sp.BuildAuthURL(relayState) |
| 77 | + _, _ = sp.BuildAuthRequest() |
| 78 | + }) |
| 79 | +} |
0 commit comments