fix(libreoffice): refuse the requests NSS sends while validating signatures

This commit is contained in:
Julien Neuhart
2026-12-01 18:02:29 +01:00
parent 2f725293c6
commit 8295283a25
4 changed files with 161 additions and 1 deletions

View File

@@ -12,7 +12,7 @@ ARG GOLANG_VERSION=1.27.4
FROM debian:13-slim AS lok-binary-stage
# See https://github.com/gotenberg/lok/releases.
ARG LOK_VERSION=v0.0.8
ARG LOK_VERSION=v0.0.9
RUN apt-get update -qq \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y -qq --no-install-recommends ca-certificates curl \

View File

@@ -1425,6 +1425,20 @@ Feature: /forms/libreoffice/convert
Then the response status code should be 200
Then the helper server should NOT have received a canary request
# Validating a document's signature, LibreOffice asks NSS for the issuer
# certificate the signer certificate names. NSS's own HTTP client ignores
# the proxy, so lok refuses every NSS request, whatever the filters allow.
@libreoffice-ssrf
Scenario: POST /forms/libreoffice/convert (Canary in a signed document's certificate never leaves)
Given I have a static server
Given I have a default Gotenberg container
When I make a "POST" request to Gotenberg at the "/forms/libreoffice/convert" endpoint with the following form data and header(s):
| downloadFrom | [{"url":"{helper}/canary-signed/signed.odt"}] | field |
| Gotenberg-Output-Filename | foo | header |
Then the response status code should be 200
Then there should be 1 PDF(s) in the response
Then the helper server should NOT have received a canary request
# The workbook was saved scrolled down to row 37. Without the topLeftCell
# reset, singlePage would start the page there and drop the header rows,
# so the "Meteor" column header only appears when the whole sheet is rendered.

View File

@@ -254,6 +254,18 @@ func newServer(ctx context.Context, workdir string) (*server, error) {
return c.Blob(http.StatusOK, contentType, b)
})
// An ODT signed with a certificate whose issuer certificate, never
// shipped, sits at a canary URL. Generated per request, since the
// certificate holds the canary URL and the helper port changes with every
// scenario. downloadFrom takes the filename from the path.
srv.GET("/canary-signed/*", func(c *echo.Context) error {
b, err := signedODT(fmt.Sprintf("http://%s/canary/issuer.cer", c.Request().Host))
if err != nil {
return c.String(http.StatusInternalServerError, fmt.Sprintf("generate signed document: %s", err))
}
return c.Blob(http.StatusOK, "application/vnd.oasis.opendocument.text", b)
})
return s, nil
}

View File

@@ -0,0 +1,134 @@
package scenario
import (
"archive/zip"
"bytes"
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"crypto/x509/pkix"
"encoding/base64"
"fmt"
"hash/crc32"
"math/big"
"time"
)
// signedODT returns a one-paragraph ODT signed with a certificate whose
// Authority Information Access (caIssuers) URL is issuerURL. The issuer
// certificate is not shipped, so validating the signature looks for it at
// issuerURL.
func signedODT(issuerURL string) ([]byte, error) {
issuerKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return nil, fmt.Errorf("generate issuer key: %w", err)
}
leafKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return nil, fmt.Errorf("generate leaf key: %w", err)
}
issuer := pkix.Name{CommonName: "Gotenberg test issuer"}
template := &x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{CommonName: "Gotenberg test signer"},
Issuer: issuer,
NotBefore: time.Now().Add(-time.Hour),
NotAfter: time.Now().Add(time.Hour),
BasicConstraintsValid: true,
IssuingCertificateURL: []string{issuerURL},
}
certDER, err := x509.CreateCertificate(rand.Reader, template, &x509.Certificate{Subject: issuer}, &leafKey.PublicKey, issuerKey)
if err != nil {
return nil, fmt.Errorf("create certificate: %w", err)
}
content := []byte(`<?xml version="1.0" encoding="UTF-8"?><office:document-content ` +
`xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" ` +
`xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" office:version="1.2">` +
`<office:body><office:text><text:p>signed</text:p></office:text></office:body>` +
`</office:document-content>`)
manifest := []byte(`<?xml version="1.0" encoding="UTF-8"?><manifest:manifest ` +
`xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0" manifest:version="1.2">` +
`<manifest:file-entry manifest:full-path="/" manifest:media-type="application/vnd.oasis.opendocument.text"/>` +
`<manifest:file-entry manifest:full-path="content.xml" manifest:media-type="text/xml"/>` +
`</manifest:manifest>`)
contentDigest := sha256.Sum256(content)
// Written in its canonical (C14N) form, so the bytes signed are the bytes
// LibreOffice canonicalizes.
signedInfo := `<CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"></CanonicalizationMethod>` +
`<SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"></SignatureMethod>` +
`<Reference URI="content.xml"><DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"></DigestMethod>` +
`<DigestValue>` + base64.StdEncoding.EncodeToString(contentDigest[:]) + `</DigestValue></Reference>`
signedInfoDigest := sha256.Sum256([]byte(`<SignedInfo xmlns="http://www.w3.org/2000/09/xmldsig#">` + signedInfo + `</SignedInfo>`))
signatureValue, err := rsa.SignPKCS1v15(rand.Reader, leafKey, crypto.SHA256, signedInfoDigest[:])
if err != nil {
return nil, fmt.Errorf("sign: %w", err)
}
signatures := []byte(`<?xml version="1.0" encoding="UTF-8"?>` +
`<document-signatures xmlns="urn:oasis:names:tc:opendocument:xmlns:digitalsignature:1.0">` +
`<Signature xmlns="http://www.w3.org/2000/09/xmldsig#" Id="sig1"><SignedInfo>` + signedInfo + `</SignedInfo>` +
`<SignatureValue>` + base64.StdEncoding.EncodeToString(signatureValue) + `</SignatureValue>` +
`<KeyInfo><X509Data><X509IssuerSerial><X509IssuerName>CN=Gotenberg test issuer</X509IssuerName>` +
`<X509SerialNumber>1</X509SerialNumber></X509IssuerSerial>` +
`<X509Certificate>` + base64.StdEncoding.EncodeToString(certDER) + `</X509Certificate>` +
`</X509Data></KeyInfo></Signature></document-signatures>`)
var buf bytes.Buffer
zw := zip.NewWriter(&buf)
// ODF requires the mimetype entry first, stored, and without a data
// descriptor, which CreateRaw allows by taking the sizes up front.
mimetype := []byte("application/vnd.oasis.opendocument.text")
w, err := zw.CreateRaw(&zip.FileHeader{
Name: "mimetype",
Method: zip.Store,
CRC32: crc32.ChecksumIEEE(mimetype),
CompressedSize64: uint64(len(mimetype)),
UncompressedSize64: uint64(len(mimetype)),
})
if err != nil {
return nil, fmt.Errorf("create mimetype: %w", err)
}
_, err = w.Write(mimetype)
if err != nil {
return nil, fmt.Errorf("write mimetype: %w", err)
}
entries := []struct {
name string
body []byte
}{
{"content.xml", content},
{"META-INF/manifest.xml", manifest},
{"META-INF/documentsignatures.xml", signatures},
}
for _, e := range entries {
w, err := zw.Create(e.name)
if err != nil {
return nil, fmt.Errorf("create %s: %w", e.name, err)
}
_, err = w.Write(e.body)
if err != nil {
return nil, fmt.Errorf("write %s: %w", e.name, err)
}
}
err = zw.Close()
if err != nil {
return nil, fmt.Errorf("close zip: %w", err)
}
return buf.Bytes(), nil
}