parseRedirectBindingQuery parses rawQuery as an HTTP-Redirect binding message whose protocol message is carried in messageParam (SAMLRequest or SAMLResponse). The query is parsed exactly once and every value used later (the message that gets decoded and the content the signature is checked over) co
(rawQuery, messageParam string)
| 79 | // query that carries both SAMLRequest and SAMLResponse, is rejected so that |
| 80 | // the signed message and the processed message cannot diverge. |
| 81 | func parseRedirectBindingQuery(rawQuery, messageParam string) (*redirectBindingMessage, error) { |
| 82 | rawValues := map[string]string{} |
| 83 | for _, pair := range strings.Split(rawQuery, "&") { |
| 84 | if pair == "" { |
| 85 | continue |
| 86 | } |
| 87 | rawKey, rawValue, _ := strings.Cut(pair, "=") |
| 88 | key, err := url.QueryUnescape(rawKey) |
| 89 | if err != nil { |
| 90 | return nil, fmt.Errorf("cannot parse query parameter name %q: %w", rawKey, err) |
| 91 | } |
| 92 | switch key { |
| 93 | case redirectParamSAMLRequest, redirectParamSAMLResponse, redirectParamRelayState, |
| 94 | redirectParamSigAlg, redirectParamSignature: |
| 95 | default: |
| 96 | continue |
| 97 | } |
| 98 | if _, seen := rawValues[key]; seen { |
| 99 | return nil, fmt.Errorf("query parameter %s appears more than once", key) |
| 100 | } |
| 101 | rawValues[key] = rawValue |
| 102 | } |
| 103 | |
| 104 | otherParam := redirectParamSAMLRequest |
| 105 | if messageParam == redirectParamSAMLRequest { |
| 106 | otherParam = redirectParamSAMLResponse |
| 107 | } |
| 108 | if _, ok := rawValues[otherParam]; ok { |
| 109 | return nil, fmt.Errorf("query contains both %s and %s", redirectParamSAMLRequest, redirectParamSAMLResponse) |
| 110 | } |
| 111 | |
| 112 | rawMessage, ok := rawValues[messageParam] |
| 113 | if !ok { |
| 114 | return nil, fmt.Errorf("query does not contain %s", messageParam) |
| 115 | } |
| 116 | |
| 117 | decoded := map[string]string{} |
| 118 | for key, rawValue := range rawValues { |
| 119 | value, err := url.QueryUnescape(rawValue) |
| 120 | if err != nil { |
| 121 | return nil, fmt.Errorf("cannot parse query parameter %s: %w", key, err) |
| 122 | } |
| 123 | decoded[key] = value |
| 124 | } |
| 125 | |
| 126 | rv := &redirectBindingMessage{ |
| 127 | Message: decoded[messageParam], |
| 128 | SigAlg: decoded[redirectParamSigAlg], |
| 129 | Signature: decoded[redirectParamSignature], |
| 130 | } |
| 131 | rv.RelayState, rv.HasRelayState = decoded[redirectParamRelayState] |
| 132 | |
| 133 | _, hasSigAlg := rawValues[redirectParamSigAlg] |
| 134 | _, hasSignature := rawValues[redirectParamSignature] |
| 135 | if hasSigAlg || hasSignature { |
| 136 | if rv.SigAlg == "" || rv.Signature == "" { |
| 137 | return nil, fmt.Errorf("query must contain both %s and %s, or neither", redirectParamSigAlg, redirectParamSignature) |
| 138 | } |
no outgoing calls